-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptgenflow.pde
More file actions
301 lines (250 loc) · 8.58 KB
/
optgenflow.pde
File metadata and controls
301 lines (250 loc) · 8.58 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
String version = "1.0.0";
import controlP5.*;
import gifAnimation.*;
ControlP5 cp5;
boolean startOptimization = false;
GifMaker gif;
int fc;
boolean movie = false;
float[][] population; // population
float[] best; // best individual
float UPP = 0.0; // upper
float LOW = 0.0; // lower
float minX = 0.0; // minimum x
float minY = 0.0; // minimum y
float starX = 0.0; // solution x
float starY = 0.0; // solution y
int generation = 0; // generation
void setup() {
size(800, 800);
PFont font = createFont("Zen Kaku Gothic New", 18);
cp5 = new ControlP5(this);
cp5.setFont(font);
cp5.setColorBackground(color(0, 158, 255));
cp5.addTextfield("How many individuals?")
.setPosition(20, 20)
.setSize(200, 40)
.setAutoClear(false)
.setText(str(N));
cp5.addTextfield("Dimensions")
.setPosition(20, 90)
.setSize(200, 40)
.setAutoClear(false)
.setText(str(dimensions));
cp5.addTextfield("Which benchmark function? \n (SPHERE, ROSENBROCK, RASTRIGIN)")
.setPosition(20, 160)
.setSize(200, 40)
.setAutoClear(false)
.setText(benchmark);
cp5.addTextfield("Which optimization method? \n (GA, DE, ABC)")
.setPosition(20, 250)
.setSize(200, 40)
.setAutoClear(false)
.setText(method);
cp5.addTextfield("The number of maximum generations")
.setPosition(20, 340)
.setSize(200, 40)
.setAutoClear(false)
.setText(str(MAX_GENERATION));
cp5.addTextfield("GA: The rate of elite selection")
.setPosition(20, 410)
.setSize(200, 40)
.setAutoClear(false)
.setText(str(ELITE_RATE));
cp5.addTextfield("GA: The rate of mutation")
.setPosition(20, 480)
.setSize(200, 40)
.setAutoClear(false)
.setText(str(MUTATION_RATE));
cp5.addTextfield("DE: The scaling factor (F)")
.setPosition(20, 550)
.setSize(200, 40)
.setAutoClear(false)
.setText(str(F));
cp5.addTextfield("DE: The crossover rate (CR)")
.setPosition(20, 620)
.setSize(200, 40)
.setAutoClear(false)
.setText(str(CR));
cp5.addButton("start")
.setPosition(20, 690)
.setSize(200, 40)
.setColorBackground(color(30, 220, 80))
.onClick(new CallbackListener() {
public void controlEvent(CallbackEvent event) {
startOptimization = true;
generation = 0;
initializePopulation();
cp5.hide();
}
});
cp5.addCheckBox("recordGif")
.setPosition(620, 760)
.setSize(20, 20)
.setItemsPerRow(1)
.setSpacingColumn(50)
.addItem("Record GIF", 0);
println("OptGenFlow " + version);
println("Configure the settings and press 'start' to begin optimization.");
frameRate(50);
}
void draw() {
if (!startOptimization) { // Wait for the user to press the start button
return;
} else { // Optimization started
generation++;
}
// Record gif
if (generation == 1 && movie == true) {
gif = new GifMaker(this, "./image/" + benchmark + "_" + method + ".gif");
fc = frameCount;
gif.setRepeat(0);
gif.setQuality(10);
gif.setDelay(40);
}
// Draw the benchmark function
background(255);
noStroke();
drawBenchmarkContours(benchmark, LOW, UPP, LOW, UPP, 1000, 1000);
// Draw the generation number
println("Generation: " + generation);
fill(255);
textSize(24);
textAlign(LEFT);
text("Generation: " + generation, 40, height - 20);
// Draw the population
for (int i = 0; i < N; i++) {
float[] position = population[i];
float fitness = evaluationFunction(objectiveFunction(position, benchmark));
float x = map(position[0], LOW, UPP, 0, width);
float y = map(position[1], LOW, UPP, height, 0);
fill(0);
ellipse(x, y, 10, 10);
textSize(14);
textAlign(CENTER);
text(nf(fitness, 0, 4), x, y - 10);
}
// Draw the best individual
float bestX = map(best[0], LOW, UPP, 0, width);
float bestY = map(best[1], LOW, UPP, height, 0);
float bestFitness = evaluationFunction(objectiveFunction(best, benchmark));
fill(255, 0, 0);
ellipse(bestX, bestY, 7, 7);
fill(0);
textSize(14);
textAlign(CENTER);
text(nf(bestFitness, 0, 4), bestX, bestY - 10);
// Draw the solution
fill(255, 215, 0);
textSize(18);
textAlign(CENTER);
text("☆", starX, starY + 5);
textSize(14);
text("x: " + nf(minX, 0, 2) + ", y: " + nf(minY, 0, 2), starX, starY + 20);
if (generation < MAX_GENERATION) { // Continue optimization
if (movie == true) {
gif.addFrame();
}
evolvePopulation();
println("Best individual: [" + nf(best[0], 0, 4) + ", " + nf(best[1], 0, 4) + "]");
println("Best fitness: " + bestFitness);
} else { // Optimization finished
startOptimization = false;
if (movie == true) {
gif.finish();
movie = false;
println("Saved gif");
}
println("\nOptimization finished");
println("Best individual: [" + nf(best[0], 0, 4) + ", " + nf(best[1], 0, 4) + "]");
println("Best fitness: " + evaluationFunction(objectiveFunction(best, benchmark)));
cp5.show();
cp5.addTextfield("Best individual")
.setPosition(580, 20)
.setSize(200, 40)
.setAutoClear(false)
.setText("[" + nf(best[0], 0, 4) + ", " + nf(best[1], 0, 4) + "]")
.setColor(color(0, 0, 0))
.setColorBackground(color(255, 190, 0));
cp5.addTextfield("Best fitness")
.setPosition(580, 100)
.setSize(200, 40)
.setAutoClear(false)
.setText(str(evaluationFunction(objectiveFunction(best, benchmark))))
.setColor(color(0, 0, 0))
.setColorBackground(color(255, 190, 0));
}
delay(20);
}
void mousePressed() {
if (mouseX > 20 && mouseX < 40 && mouseY > 760 && mouseY < 780) {
movie = !movie;
println("Movie recording: " + movie);
}
}
void initializePopulation() {
N = int(cp5.get(Textfield.class, "How many individuals?").getText());
dimensions = int(cp5.get(Textfield.class, "Dimensions").getText());
benchmark = cp5.get(Textfield.class, "Which benchmark function? \n (SPHERE, ROSENBROCK, RASTRIGIN)").getText();
method = cp5.get(Textfield.class, "Which optimization method? \n (GA, DE, ABC)").getText();
MAX_GENERATION = int(cp5.get(Textfield.class, "The number of maximum generations").getText());
ELITE_RATE = float(cp5.get(Textfield.class, "GA: The rate of elite selection").getText());
MUTATION_RATE = float(cp5.get(Textfield.class, "GA: The rate of mutation").getText());
F = float(cp5.get(Textfield.class, "DE: The scaling factor (F)").getText());
CR = float(cp5.get(Textfield.class, "DE: The crossover rate (CR)").getText());
population = new float[N][dimensions];
best = new float[dimensions];
if (benchmark.equals("SPHERE")) {
UPP = 5.0;
LOW = -5.0;
minX = 0;
minY = 0;
} else if (benchmark.equals("ROSENBROCK")) {
UPP = 2.0;
LOW = -2.0;
minX = 1;
minY = 1;
} else if (benchmark.equals("RASTRIGIN")) {
UPP = 5.12;
LOW = -5.12;
minX = 0;
minY = 0;
} else {
println("Unknown benchmark type: " + benchmark);
exit();
}
starX = map(minX, LOW, UPP, 0, width);
starY = map(minY, LOW, UPP, height, 0);
if (method.equals("ABC")) {
trialCounter = new int[N];
for (int i = 0; i < N; i++) {
trialCounter[i] = 0;
}
}
for (int i = 0; i < N; i++) {
for (int d = 0; d < dimensions; d++) {
population[i][d] = random(LOW, UPP);
}
}
best = population[0].clone();
if (cp5.get(CheckBox.class, "recordGif").getState(0)) {
movie = true;
gif = new GifMaker(this, "./image/" + benchmark + "_" + method + ".gif");
gif.setRepeat(0);
gif.setQuality(10);
gif.setDelay(20);
}
println("Start optimization with " + method + " method");
}
void evolvePopulation() {
if (method.equals("GA")) {
geneticAlgorithm();
} else if (method.equals("DE")) {
differentialEvolution();
} else if (method.equals("ABC")) {
artificialBeeColony();
} else {
println("Unknown method: " + method);
exit();
}
}