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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
package jcgp.gui.settings;
import java.io.File;
import java.util.ArrayList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import jcgp.JCGP;
import jcgp.backend.function.FunctionSet;
import jcgp.backend.modules.es.EvolutionaryStrategy;
import jcgp.backend.modules.mutator.Mutator;
import jcgp.backend.modules.problem.Problem;
import jcgp.backend.modules.problem.TestCaseProblem;
import jcgp.backend.parameters.Parameter;
import jcgp.gui.GUI;
import jcgp.gui.settings.parameters.GUIParameter;
import jcgp.gui.settings.testcase.TestCaseTable;
public class SettingsPane extends AnchorPane {
private VBox mainContainer;
private VBox baseParameterPane, eaPane, mutatorPane, problemPane;
private VBox nodeFunctions;
private Button runPause = new Button("Run"), step = new Button("Step"), reset = new Button("Reset");
private Button loadParameters = new Button("Load parameters"), loadChromosome = new Button("Load chromosome"), saveChromosome = new Button("Save chromosome");
private ArrayList<GUIParameter<?>> parameters = new ArrayList<GUIParameter<?>>();
private TestCaseTable testCaseTable;
private GUI gui;
private int currentArity;
public SettingsPane(GUI gui) {
super();
this.gui = gui;
final JCGP jcgp = gui.getExperiment();
mainContainer = new VBox(8);
mainContainer.setPadding(new Insets(5, GUI.RESIZE_MARGIN, 0, 2));
setMinWidth(GUI.SETTINGS_MIN_WIDTH);
setPrefWidth(GUI.SETTINGS_MIN_WIDTH);
initialiseBaseParameters(jcgp);
initialiseEAParameters(jcgp);
initialiseMutatorParameters(jcgp);
initialiseProblemTypeParameters(jcgp, gui);
createControls(gui);
ScrollPane scroll = new ScrollPane();
scroll.setFitToWidth(true);
scroll.setContent(mainContainer);
scroll.setStyle("-fx-background-color: #FFFFFF");
AnchorPane.setTopAnchor(scroll, 0.0);
AnchorPane.setBottomAnchor(scroll, 0.0);
AnchorPane.setRightAnchor(scroll, 0.0);
AnchorPane.setLeftAnchor(scroll, GUI.RESIZE_MARGIN);
getChildren().add(scroll);
}
private void initialiseBaseParameters(JCGP jcgp) {
baseParameterPane = new VBox(2);
Text header = new Text("Base Parameters");
header.setFont(Font.font("Arial", 14));
header.setUnderline(true);
baseParameterPane.getChildren().add(header);
parameters.add(GUIParameter.create(jcgp.getResources().getRowsParameter(), this));
parameters.add(GUIParameter.create(jcgp.getResources().getColumnsParameter(), this));
parameters.add(GUIParameter.create(jcgp.getResources().getInputsParameter(), this));
parameters.add(GUIParameter.create(jcgp.getResources().getOutputsParameter(), this));
parameters.add(GUIParameter.create(jcgp.getResources().getLevelsBackParameter(), this));
GUIParameter<?> gp = GUIParameter.create(jcgp.getResources().getPopulationSizeParameter(), this);
gp.setPadding(new Insets(0, 0, 10, 0));
parameters.add(gp);
parameters.add(GUIParameter.create(jcgp.getResources().getCurrentGenerationParameter(), this));
parameters.add(GUIParameter.create(jcgp.getResources().getGenerationsParameter(), this));
parameters.add(GUIParameter.create(jcgp.getResources().getCurrentRunParameter(), this));
gp = GUIParameter.create(jcgp.getResources().getRunsParameter(), this);
gp.setPadding(new Insets(0, 0, 10, 0));
parameters.add(gp);
parameters.add(GUIParameter.create(jcgp.getResources().getSeedParameter(), this));
parameters.add(GUIParameter.create(jcgp.getResources().getReportIntervalParameter(), this));
baseParameterPane.getChildren().addAll(parameters);
mainContainer.getChildren().add(baseParameterPane);
}
private void initialiseEAParameters(final JCGP jcgp) {
eaPane = new VBox(2);
Text header = new Text("Evolutionary Strategy");
header.setFont(Font.font("Arial", 14));
header.setUnderline(true);
final ComboBox<EvolutionaryStrategy> eaCBox = new ComboBox<EvolutionaryStrategy>();
eaCBox.getItems().addAll(jcgp.getEvolutionaryStrategies());
eaCBox.getSelectionModel().select(jcgp.getEvolutionaryStrategy());
eaCBox.prefWidthProperty().bind(mainContainer.widthProperty());
final VBox eaParameters = new VBox(2);
refreshParameters(jcgp.getEvolutionaryStrategy().getLocalParameters(), eaParameters);
eaCBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
jcgp.setEvolutionaryStrategy(eaCBox.getSelectionModel().getSelectedIndex());
refreshParameters(eaCBox.getSelectionModel().getSelectedItem().getLocalParameters(), eaParameters);
gui.flushConsole();
}
});
eaPane.getChildren().addAll(header, eaCBox, eaParameters);
mainContainer.getChildren().add(eaPane);
}
private void initialiseMutatorParameters(final JCGP jcgp) {
mutatorPane = new VBox(2);
Text header = new Text("Mutator");
header.setFont(Font.font("Arial", 14));
header.setUnderline(true);
final ComboBox<Mutator> mutatorCBox = new ComboBox<Mutator>();
mutatorCBox.getItems().addAll(jcgp.getMutators());
mutatorCBox.getSelectionModel().select(jcgp.getMutator());
mutatorCBox.prefWidthProperty().bind(mainContainer.widthProperty());
final VBox mutatorParameters = new VBox(2);
refreshParameters(jcgp.getMutator().getLocalParameters(), mutatorParameters);
mutatorCBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
jcgp.setMutator(mutatorCBox.getSelectionModel().getSelectedIndex());
refreshParameters(mutatorCBox.getSelectionModel().getSelectedItem().getLocalParameters(), mutatorParameters);
gui.flushConsole();
}
});
mutatorPane.getChildren().addAll(header, mutatorCBox, mutatorParameters);
mainContainer.getChildren().add(mutatorPane);
}
private void initialiseProblemTypeParameters(final JCGP jcgp, final GUI gui) {
updateArity();
problemPane= new VBox(2);
Text header = new Text("Problem Type");
header.setFont(Font.font("Arial", 14));
header.setUnderline(true);
final ComboBox<Problem> problemCBox = new ComboBox<Problem>();
problemCBox.getItems().addAll(jcgp.getProblems());
problemCBox.getSelectionModel().select(jcgp.getProblem());
problemCBox.prefWidthProperty().bind(mainContainer.widthProperty());
final VBox problemParameters = new VBox(2);
problemParameters.setPadding(new Insets(0, 0, 4, 0));
refreshParameters(jcgp.getProblem().getLocalParameters(), problemParameters);
final HBox testCaseControlContainer = new HBox(2);
final Button showTestCaseButton = makeTestCaseButton();
final Button loadProblemDataButton = makeLoadTestCaseButton();
HBox.setHgrow(showTestCaseButton, Priority.ALWAYS);
showTestCaseButton.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(loadProblemDataButton, Priority.ALWAYS);
loadProblemDataButton.setMaxWidth(Double.MAX_VALUE);
if (jcgp.getProblem() instanceof TestCaseProblem<?>) {
testCaseControlContainer.getChildren().addAll(showTestCaseButton, loadProblemDataButton);
remakeTestCaseTable();
} else {
testCaseControlContainer.getChildren().add(loadProblemDataButton);
}
nodeFunctions = new VBox(2);
nodeFunctions.setPadding(new Insets(0, 0, 4, 0));
refreshFunctions();
problemCBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
jcgp.setProblem(problemCBox.getSelectionModel().getSelectedIndex());
updateArity();
refreshParameters(jcgp.getProblem().getLocalParameters(), problemParameters);
if (testCaseTable != null) {
testCaseTable.close();
}
gui.setEvaluating(false);
refreshFunctions();
testCaseControlContainer.getChildren().clear();
if (jcgp.getProblem() instanceof TestCaseProblem) {
testCaseControlContainer.getChildren().addAll(showTestCaseButton, loadProblemDataButton);
remakeTestCaseTable();
} else {
testCaseControlContainer.getChildren().add(loadProblemDataButton);
}
gui.reset();
}
});
problemPane.getChildren().addAll(header, problemCBox, problemParameters, nodeFunctions, testCaseControlContainer);
mainContainer.getChildren().add(problemPane);
}
private Button makeLoadTestCaseButton() {
Button b = new Button("Load data");
b.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
FileChooser fc = new FileChooser();
fc.setTitle("Open problem file...");
fc.getExtensionFilters().add(new ExtensionFilter("CGP " + gui.getExperiment().getProblem() + " files", "*" + ((TestCaseProblem<?>) gui.getExperiment().getProblem()).getFileExtension()));
fc.getExtensionFilters().add(new ExtensionFilter("All files", "*.*"));
File chrFile = fc.showOpenDialog(gui.getStage());
if (chrFile != null) {
gui.getExperiment().loadProblemData(chrFile);
remakeTestCaseTable();
gui.reDraw();
}
}
});
return b;
}
private Button makeTestCaseButton() {
Button b = new Button("Show data");
b.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
testCaseTable.show();
}
});
return b;
}
private void createControls(final GUI gui) {
Text header = new Text("Experiment controls");
header.setFont(Font.font("Arial", 14));
header.setUnderline(true);
final VBox controls = new VBox(2);
controls.setFillWidth(true);
final HBox flowButtons = new HBox(2);
runPause.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
gui.runPause();
}
});
step.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
gui.step();
}
});
reset.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
gui.reset();
}
});
HBox.setHgrow(runPause, Priority.ALWAYS);
runPause.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(step, Priority.ALWAYS);
step.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(reset, Priority.ALWAYS);
reset.setMaxWidth(Double.MAX_VALUE);
flowButtons.getChildren().addAll(runPause, step, reset);
flowButtons.setPadding(new Insets(0, 0, 10, 0));
loadParameters.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
FileChooser fc = new FileChooser();
fc.setTitle("Open .par file...");
fc.getExtensionFilters().add(new ExtensionFilter("CGP parameter files", "*.par"));
fc.getExtensionFilters().add(new ExtensionFilter("All files", "*.*"));
File parFile = fc.showOpenDialog(gui.getStage());
if (parFile != null) {
gui.getExperiment().loadParameters(parFile);
gui.reDraw();
refreshFunctions();
}
gui.flushConsole();
}
});
loadChromosome.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
FileChooser fc = new FileChooser();
fc.setTitle("Load .chr file...");
fc.getExtensionFilters().add(new ExtensionFilter("CGP chromosome files", "*.chr"));
fc.getExtensionFilters().add(new ExtensionFilter("All files", "*.*"));
File chrFile = fc.showOpenDialog(gui.getStage());
if (chrFile != null) {
gui.getExperiment().loadChromosome(chrFile, gui.getChromosomeIndex());
gui.reDraw();
}
gui.flushConsole();
}
});
saveChromosome.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
FileChooser fc = new FileChooser();
fc.setTitle("Save .chr file...");
fc.getExtensionFilters().add(new ExtensionFilter("CGP chromosome files", "*.chr"));
fc.getExtensionFilters().add(new ExtensionFilter("All files", "*.*"));
File chrFile = fc.showSaveDialog(gui.getStage());
if (chrFile != null) {
gui.getExperiment().saveChromosome(chrFile, gui.getChromosomeIndex());
}
gui.flushConsole();
}
});
HBox.setHgrow(loadParameters, Priority.ALWAYS);
loadParameters.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(loadChromosome, Priority.ALWAYS);
loadChromosome.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(saveChromosome, Priority.ALWAYS);
saveChromosome.setMaxWidth(Double.MAX_VALUE);
controls.getChildren().addAll(header, flowButtons, loadParameters, loadChromosome, saveChromosome);
mainContainer.getChildren().add(controls);
}
/**
* @param cgp
* @param vb
*/
private void refreshParameters(ArrayList<Parameter<?>> newParameters, VBox vb) {
parameters.removeAll(vb.getChildren());
vb.getChildren().clear();
if (newParameters != null) {
for (int i = 0; i < newParameters.size(); i++) {
GUIParameter<?> gp = GUIParameter.create(newParameters.get(i), this);
parameters.add(gp);
vb.getChildren().add(gp);
}
}
revalidateParameters();
}
/**
* This method handles a problem type change by updating the list of allowed
* node functions.
*/
private void refreshFunctions() {
nodeFunctions.getChildren().clear();
CheckBox cb;
final FunctionSet fs = gui.getExperiment().getResources().getFunctionSet();
for (int i = 0; i < fs.getTotalFunctionCount(); i++) {
cb = new CheckBox(fs.getFunction(i).toString());
cb.setId(String.valueOf(i));
cb.setSelected(fs.isEnabled(fs.getFunction(i)));
final int index = i;
cb.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (((CheckBox) event.getSource()).isSelected()) {
fs.enableFunction(index);
} else {
fs.disableFunction(index);
}
gui.updateFunctionSelector();
revalidateParameters();
}
});
nodeFunctions.getChildren().add(cb);
gui.updateFunctionSelector();
}
}
public boolean isExperimentRunning() {
return gui.isWorking();
}
/**
*
* @return true if the experiment needs to be reset, false if otherwise.
*/
public boolean isResetRequired() {
for (GUIParameter<?> parameter : parameters) {
if (parameter.requiresReset()) {
return true;
}
}
if (arityChanged()) {
return true;
}
return false;
}
/**
* @return true if no parameters have their status set to ParameterStatus.INVALID.
*/
public boolean areParametersValid() {
for (GUIParameter<?> parameter : parameters) {
if (!parameter.isValid()) {
return false;
}
}
return true;
}
/**
* Calls validate() on every parameter. This is called whenever a parameter changes,
* so that other parameters update their status in case they were dependent on the
* changed parameter.
*/
public void revalidateParameters() {
boolean disableControls = false;
for (GUIParameter<?> parameter : parameters) {
parameter.validate();
if (parameter.requiresReset()) {
disableControls = true;
}
}
if (arityChanged()) {
disableControls = true;
}
runPause.setDisable(disableControls);
step.setDisable(disableControls);
}
/**
* Calls applyValue() on every parameter. This is called when a reset occurs, so that
* the new value will be used as a reference instead of the old reference value.
*/
public void applyParameters() {
for (GUIParameter<?> parameter : parameters) {
parameter.applyValue();
}
updateArity();
if (testCaseTable != null) {
testCaseTable.close();
}
}
/**
* Updates all of the controls to their appropriate state based on the status of the
* experiment, in order to prevent inappropriate operations if the experiment is
* running or finished.
*
* @param running true if the experiment is running
* @param finished true if the experiment is finished
*/
public void updateControls(boolean running, boolean finished) {
baseParameterPane.setDisable(running);
eaPane.setDisable(running);
mutatorPane.setDisable(running);
problemPane.setDisable(running);
runPause.setText(running ? "Pause" : "Run");
runPause.setDisable(finished);
step.setDisable(running || finished);
reset.setDisable(running);
loadParameters.setDisable(running);
loadChromosome.setDisable(running);
saveChromosome.setDisable(running);
testCaseTable.getTable().setDisable(running);
}
private void remakeTestCaseTable() {
boolean wasShowing = false;
if (testCaseTable != null) {
wasShowing = testCaseTable.isShowing();
testCaseTable.close();
}
testCaseTable = new TestCaseTable((TestCaseProblem<Object>) gui.getExperiment().getProblem(), gui);
if (wasShowing) {
testCaseTable.show();
}
}
public TestCaseTable getTestCaseTable() {
return testCaseTable;
}
private void updateArity() {
currentArity = gui.getExperiment().getProblem().getFunctionSet().getMaxArity();
}
private boolean arityChanged() {
return currentArity != gui.getExperiment().getProblem().getFunctionSet().getMaxArity();
}
}
|