-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathES.js
More file actions
180 lines (134 loc) · 5.1 KB
/
ES.js
File metadata and controls
180 lines (134 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Evolution Strategies (ES). Javascript - Ing. Manuel Freitas 2025.
"use strict";
class Optimizar_ES {
constructor(Parametros) {
this.Mejor_Global = null;
this.Limites_Inf = Parametros.Limites_Inf;
this.Limites_Sup = Parametros.Limites_Sup;
this.N_Variables = Parametros.N_Variables;
this.N_Iteraciones = Parametros.N_Iteraciones;
this.Funcion_Objetivo = Parametros.Funcion_Objetivo;
this.N_Individuos = Parametros.N_Individuos;
this.sigma = Parametros.sigma;
this.lambda = Parametros.lambda;
this.tau = Parametros.tau;
this.rng = new MarsagliaPolar();
}
Calcular_Func_Obj(Arreglo) {
return this.Funcion_Objetivo(Arreglo);
}
Ajustar_Limites (Arreglo) {
for (var i = 0; i < Arreglo.length; i++) {
if (Arreglo[i] > this.Limites_Sup[i]) {
Arreglo[i] = this.Limites_Sup[i];
} else if (Arreglo[i] < this.Limites_Inf[i]) {
Arreglo[i] = this.Limites_Inf[i];
}
}
return Arreglo;
};
Crear_Posicion_Aleatoria(Limites_Inf, Limites_Sup, N_Variables) {
var Posicion_Aleatoria = [];
var i;
for (i = 0; i < N_Variables; i++) {
Posicion_Aleatoria[i] = Limites_Inf[i] + Math.random() * (Limites_Sup[i] - Limites_Inf[i]);
}
Posicion_Aleatoria = this.Ajustar_Limites(Posicion_Aleatoria);
return Posicion_Aleatoria;
}
Chequear_Limites = function (num, i) {
if (num > this.Limites_Sup[i] || num < this.Limites_Inf[i]) {
return false;
}
return true;
};
Crear_Padre() {
var Posicion_Aleatoria = this.Crear_Posicion_Aleatoria(this.Limites_Inf, this.Limites_Sup, this.N_Variables);
return new ES_Solucion_No_Correlacionada(Posicion_Aleatoria, this.Calcular_Func_Obj(Posicion_Aleatoria), this.sigma);
};
Crear_Vecino() {
var Vecino_Pos, sigmaPrime, i;
var step;
Vecino_Pos = this.Mejor_Global.Posicion.slice(0);
sigmaPrime = this.Mejor_Global.sigma * Math.exp(this.tau * this.rng.generateRandom(0, 1));
for (i = 0; i < this.N_Variables; i++) {
do {
step = sigmaPrime * this.rng.generateRandom(0, 1);
} while (this.Chequear_Limites(Vecino_Pos[i] + step, i) === false);
Vecino_Pos[i] += step;
}
return new ES_Solucion_No_Correlacionada(Vecino_Pos, this.Calcular_Func_Obj(Vecino_Pos), sigmaPrime);
};
Optimizacion() {
var N_Func_Eval = 0;
var i, Vecino;
var Mejor_Valor, Mejor_Pos = [], Mejor_Sigma;
this.Mejor_Global = this.Crear_Padre();
while (N_Func_Eval < this.N_Iteraciones) {
Mejor_Valor = Number.MAX_VALUE;
for (i = 0; i < this.lambda; i++) {
Vecino = this.Crear_Vecino();
if(Vecino.Valor <= Mejor_Valor) {
Mejor_Valor = Vecino.Valor;
Mejor_Pos = Vecino.Posicion.slice(0);
Mejor_Sigma = Vecino.sigma;
}
}
N_Func_Eval ++;
if (Mejor_Valor <= this.Mejor_Global.Valor) {
this.Mejor_Global = new ES_Solucion_No_Correlacionada(Mejor_Pos, Mejor_Valor, Mejor_Sigma);
}
}
console.log({"Iter.": N_Func_Eval, "Func. Obj.": this.Mejor_Global.Valor, "Posicion": this.Mejor_Global.Posicion});
};
}
class ES_Solucion_No_Correlacionada {
constructor(Posicion, Valor, sigma) {
this.Posicion = Posicion.slice(0);
this.Valor = Valor;
this.sigma = sigma;
}
}
class MarsagliaPolar {
constructor() {
this.hasSpare = false;
this.spare;
};
generateRandom (mean, stdDev) {
if (this.hasSpare)
{
this.hasSpare = false;
return mean + stdDev * this.spare;
}
this.hasSpare = true;
var u, v, s;
do {
u = Math.random() * 2.0 - 1.0;
v = Math.random() * 2.0 - 1.0;
s = u * u + v * v;
} while( (s >= 1.0) || (s == 0.0) );
s = Math.sqrt(-2.0 * Math.log(s) / s);
this.spare = v * s;
return mean + stdDev * u * s;
};
};
function Fx_Mishra_Bird(Arreglo) {
var Total = 0.0;
var x1 = Arreglo[0];
var x2 = Arreglo[1];
Total = Math.sin(x2) * Math.exp(1-Math.cos(x1)) ** 2 + Math.cos(x1) * Math.exp(1-Math.sin(x2)) ** 2 + (x1-x2) ** 2;
return Total;
}
var Parametros = {
"N_Individuos": 100,
"sigma": 5,
"lambda": 100,
"tau": 0.18,
"N_Iteraciones": 50000,
"Funcion_Objetivo": Fx_Mishra_Bird,
"Limites_Sup": [0, 0],
"Limites_Inf": [-10, -6.5],
"N_Variables": 2
};
var ES = new Optimizar_ES(Parametros);
ES.Optimizacion();