-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEvolutionStrategies.js
143 lines (127 loc) · 4.37 KB
/
EvolutionStrategies.js
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
"use strict";
importScripts('./ContinuousOptimizer.js', './GaussianRNG.js');
/**
* (1+lambda) strategy
* Uncorrelated mutation with one sigma
*/
function EvolutionStrategies(parameters) {
ContinuousOptimizer.call(this, parameters);
this.sigma = parameters.sigma;
this.lambda = parameters.lambda;
this.tau = parameters.tau;
this.rng = new MarsagliaPolar();
}
;
EvolutionStrategies.prototype = Object.create(ContinuousOptimizer.prototype);
EvolutionStrategies.prototype.constructor = ContinuousOptimizer;
EvolutionStrategies.prototype.createParent = function () {
var randomPos = this.createRandomPosition();
return new ESUncorrelatedSolution(randomPos, this.calculateObjValue(randomPos), this.sigma);
};
EvolutionStrategies.prototype.solve = function () {
var numOfFunctionEval = 0;
var i, neighbor;
var bestFitness, bestPosition = [], bestSigma;
this.globalBest = this.createParent();
while (numOfFunctionEval + this.lambda <= this.maxFEs) {
bestFitness = Number.MAX_VALUE;
for (i = 0; i < this.lambda; i++) {
neighbor = this.createNeighbor();
if(neighbor.fitness <= bestFitness) { //store the properties of the best child
bestFitness = neighbor.fitness;
bestPosition = neighbor.position.slice(0);
bestSigma = neighbor.sigma;
}
}
numOfFunctionEval += this.lambda;
if (bestFitness <= this.globalBest.fitness) { //update the globalBest
this.globalBest = new ESUncorrelatedSolution(bestPosition, bestFitness, bestSigma);
postMessage([numOfFunctionEval, this.globalBest.fitness, this.globalBest.position]);
}
// if (numOfFunctionEval % 1000 === 0) {
// postMessage([numOfFunctionEval, this.globalBest.fitness, this.globalBest.position]);
// }
}
postMessage([numOfFunctionEval, this.globalBest.fitness, this.globalBest.position]);
};
EvolutionStrategies.prototype.createNeighbor = function () {
var neighborPos, sigmaPrime, i;
var step;
neighborPos = this.globalBest.position.slice(0);
sigmaPrime = this.globalBest.sigma * Math.exp(this.tau * this.rng.generateRandom(0, 1));
for (i = 0; i < this.dimension; i++) { //mutate each dimension
do {
step = sigmaPrime * this.rng.generateRandom(0, 1);
} while (this.checkBoundary(neighborPos[i] + step, i) === false); //ensure that new pos is in the limits
neighborPos[i] += step;
}
return new ESUncorrelatedSolution(neighborPos, this.calculateObjValue(neighborPos), sigmaPrime);
};
EvolutionStrategies.prototype.checkBoundary = function (num, i) {
if (num > this.upperBound || num < this.lowerBound) {
return false;
}
return true;
};
function ESUncorrelatedSolution(position, fitness, sigma) {
Solution.call(this, position, fitness);
this.sigma = sigma;
}
;
ESUncorrelatedSolution.prototype = Object.create(Solution.prototype);
ESUncorrelatedSolution.prototype.constructor = ESUncorrelatedSolution;
onmessage = function (e) {
var func;
switch (e.data[4]) {
case "sphere":
func = sphere;
break;
case "schwefel2_22":
func = schwefel2_22;
break;
case "schwefel1_2":
func = schwefel1_2;
break;
case "schwefel2_21":
func = schwefel2_21;
break;
case "rosenbrock":
func = rosenbrock;
break;
case "step":
func = step;
break;
case "quarticWithNoise":
func = quarticWithNoise;
break;
case "schwefel2_26":
func = schwefel2_26;
break;
case "rastrigin":
func = rastrigin;
break;
case "ackley":
func = ackley;
break;
case "griewank":
func = griewank;
break;
case "penalized":
func = penalized;
break;
default:
func = null;
}
var parameters = {
"sigma": e.data[0],
"lambda": e.data[1],
"tau": e.data[2],
"maxFEs": e.data[3],
"objFunc": func,
"upperBound": e.data[5],
"lowerBound": e.data[6],
"dimension": e.data[7]
};
var es = new EvolutionStrategies(parameters);
es.solve();
};