-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabc.pde
123 lines (93 loc) · 3.6 KB
/
abc.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
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
int[] trialCounter;
int roulleteSelection(float[] cum_probs) {
float r = random(1.0);
int i = 0;
while (i < cum_probs.length - 1 && r > cum_probs[i]) {
i++;
}
return i;
}
float[][] employedBees(float[][] flowers) {
float[][] newFlowers = new float[N][dimensions];
float[] newFlower = new float[dimensions];
int k = 0;
for (int i = 0; i < N; i++) {
newFlower = new float[dimensions];
for (int j = 0; j < dimensions; j++) {
do {
k = int(random(N));
} while (k == i);
newFlower[j] = flowers[i][j] + (flowers[i][j] - flowers[k][j]) * (random(1.0) * 2.0 - 1.0);
if (evaluationFunction(objectiveFunction(newFlower, benchmark)) > evaluationFunction(objectiveFunction(flowers[i], benchmark))) {
newFlowers[i] = newFlower;
trialCounter[i] = 0;
} else {
newFlowers[i] = flowers[i];
trialCounter[i]++;
}
if (best == null || evaluationFunction(objectiveFunction(newFlowers[i], benchmark)) > evaluationFunction(objectiveFunction(best, benchmark))) {
best = newFlowers[i];
}
}
}
return newFlowers;
}
float[][] onlookerBees(float[][] flowers) {
float[][] newFlowers = new float[N][dimensions];
float[] newFlower = new float[dimensions];
float[] cumFlower = new float[dimensions];
float[] cumP = new float[N];
float sumFitness = 0;
int k = 0;
for (int i = 0; i < N; i++) {
sumFitness += evaluationFunction(objectiveFunction(flowers[i], benchmark));
}
for (int i = 0; i < N; i++) {
cumP[i] = evaluationFunction(objectiveFunction(flowers[i], benchmark)) / sumFitness;
if (i > 0) {
cumP[i] += cumP[i - 1];
}
}
for (int i = 0; i < N; i++) {
cumFlower = flowers[roulleteSelection(cumP)];
newFlower = new float[dimensions];
for (int j = 0; j < dimensions; j++) {
do {
k = int(random(N));
} while (k == i);
newFlower[j] = cumFlower[j] + (cumFlower[j] - flowers[k][j]) * (random(1.0) * 2.0 - 1.0);
if (evaluationFunction(objectiveFunction(newFlower, benchmark)) > evaluationFunction(objectiveFunction(cumFlower, benchmark))) {
newFlowers[i] = newFlower;
trialCounter[i] = 0;
} else {
newFlowers[i] = cumFlower;
trialCounter[i]++;
}
if (best == null || evaluationFunction(objectiveFunction(newFlowers[i], benchmark)) > evaluationFunction(objectiveFunction(best, benchmark))) {
best = newFlowers[i];
}
}
}
return newFlowers;
}
float[][] scoutBees(float[][] flowers) {
if (max(trialCounter) > TC_LIMIT) {
for (int i = 0; i < N; i++) {
if (trialCounter[i] > TC_LIMIT) {
flowers[i] = new float[dimensions];
for (int j = 0; j < dimensions; j++) {
flowers[i][j] = random(LOW, UPP);
}
trialCounter[i] = 0;
}
}
}
return flowers;
}
void artificialBeeColony() {
float[][] newPopulation = new float[N][dimensions];
newPopulation = employedBees(population);
newPopulation = onlookerBees(newPopulation);
newPopulation = scoutBees(newPopulation);
population = newPopulation;
}