-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathde.pde
65 lines (53 loc) · 1.9 KB
/
de.pde
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
float[][] bincrossover(float[] parent1, float[] parent2) {
int point1 = int(random(dimensions));
int point2 = int(random(dimensions));
if (point1 > point2) {
int temp = point1;
point1 = point2;
point2 = temp;
}
float[] child1 = new float[dimensions];
float[] child2 = new float[dimensions];
for (int i = 0; i < dimensions; i++) {
if (i >= point1 && i <= point2) {
child1[i] = parent2[i];
child2[i] = parent1[i];
} else {
child1[i] = parent1[i];
child2[i] = parent2[i];
}
}
return new float[][] {child1, child2};
}
void differentialEvolution() {
float[][] newPopulation = new float[N][dimensions];
for (int i = 0; i < N; i++) {
int r1, r2, r3;
float[] mutant = new float[dimensions];
float[] trial = new float[dimensions];
do {
r1 = int(random(N));
r2 = int(random(N));
r3 = int(random(N));
} while (r3 == i || r3 == r1 || r3 == r2);
for (int j = 0; j < dimensions; j++) {
mutant[j] = population[r1][j] + F * (population[r2][j] - population[r3][j]);
}
for (int j = 0; j < dimensions; j++) {
if (random(1.0) < CR || j == int(random(dimensions))) {
trial[j] = mutant[j];
} else {
trial[j] = population[i][j];
}
}
if (evaluationFunction(objectiveFunction(trial, benchmark)) > evaluationFunction(objectiveFunction(population[i], benchmark))) {
newPopulation[i] = trial;
} else {
newPopulation[i] = population[i];
}
if (best == null || evaluationFunction(objectiveFunction(newPopulation[i], benchmark)) > evaluationFunction(objectiveFunction(best, benchmark))) {
best = newPopulation[i];
}
}
population = newPopulation;
}