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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
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.constants.Constants;
import jcgp.gui.settings.parameters.GUIParameter;
import jcgp.gui.settings.testcase.TestCaseTable;
/**
* This is a fairly hefty class which encapsulates the entire right-hand
* control pane. It contains base parameters, module selectors and their
* associated parameters, flow controls and file loading/saving buttons.
* <br><br>
* A single instance of this class is used in {@code GUI}.
*
*
* @author Eduardo Pedroni
*
*/
public class SettingsPane extends AnchorPane {
/*
* The primary containers, these make up each section of the settings pane.
*/
private VBox mainContainer;
private VBox baseParameterPane, eaPane, mutatorPane, problemPane;
private VBox nodeFunctions;
// all buttons
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");
// this is a list of parameters used for parameter validity checks
private ArrayList<GUIParameter<?>> parameters = new ArrayList<GUIParameter<?>>();
// the test case table stage
private TestCaseTable testCaseTable;
// a reference to the parent GUI
private GUI gui;
private int currentArity;
/**
* Create a new instance of {@code SettingsPane} associated
* with the specified {@code GUI} object.
*
* @param gui a reference to this object's parent.
*/
public SettingsPane(GUI gui) {
super();
this.gui = gui;
// acquire a reference to jcgp, for convenience
final JCGP jcgp = gui.getExperiment();
// make the overarching container
mainContainer = new VBox(8);
mainContainer.setPadding(new Insets(5, Constants.RESIZE_MARGIN, 0, 2));
setMinWidth(Constants.SETTINGS_MIN_WIDTH);
setPrefWidth(Constants.SETTINGS_MIN_WIDTH);
// initialise all sub-divisions
initialiseBaseParameters(jcgp);
initialiseEAParameters(jcgp);
initialiseMutatorParameters(jcgp);
initialiseProblemTypeParameters(jcgp, gui);
createControls(gui);
// prepare the scroll pane
ScrollPane scroll = new ScrollPane();
scroll.setFitToWidth(true);
scroll.setContent(mainContainer);
scroll.setStyle("-fx-background-color: #FFFFFF");
// anchor the scroll pane to itself, bearing in mind the resize margin
AnchorPane.setTopAnchor(scroll, 0.0);
AnchorPane.setBottomAnchor(scroll, 0.0);
AnchorPane.setRightAnchor(scroll, 0.0);
AnchorPane.setLeftAnchor(scroll, Constants.RESIZE_MARGIN);
// add the scroll pane, all done!
getChildren().add(scroll);
}
/**
* Creates the base parameters pane
*
* @param jcgp
*/
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> esCBox = new ComboBox<EvolutionaryStrategy>();
esCBox.getItems().addAll(jcgp.getEvolutionaryStrategies());
esCBox.getSelectionModel().select(jcgp.getEvolutionaryStrategy());
esCBox.prefWidthProperty().bind(mainContainer.widthProperty());
final VBox eaParameters = new VBox(2);
refreshParameters(jcgp.getEvolutionaryStrategy().getLocalParameters(), eaParameters);
esCBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
jcgp.setEvolutionaryStrategy(esCBox.getSelectionModel().getSelectedIndex());
refreshParameters(esCBox.getSelectionModel().getSelectedItem().getLocalParameters(), eaParameters);
gui.flushConsole();
}
});
eaPane.getChildren().addAll(header, esCBox, 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);
}
/**
* Builds {@code GUIParameter}s and adds them to the provided {@code VBox}.
* The parameters built are taken from the specified list.
*
* @param newParameters the list of parameters to add.
* @param container the container to add the parameters to.
*/
private void refreshParameters(ArrayList<Parameter<?>> newParameters, VBox container) {
// remove what is currently in the container from the parameter list
parameters.removeAll(container.getChildren());
// remove everything in the container
container.getChildren().clear();
// if there are parameters to add, add them all
if (newParameters != null) {
for (int i = 0; i < newParameters.size(); i++) {
// factory method returns the right subtype of GUIParameter
GUIParameter<?> guiParameter = GUIParameter.create(newParameters.get(i), this);
// make sure to add it to the parameter list as well
parameters.add(guiParameter);
container.getChildren().add(guiParameter);
}
}
// do a quick refresh just in case something is invalid
revalidateParameters();
}
/**
* This method handles a problem type change by updating the list of allowed
* node functions.
* <br><br>
* It does so by creating new checkboxes for each function in the function set.
*/
private void refreshFunctions() {
// remove all current functions
nodeFunctions.getChildren().clear();
CheckBox checkBox;
// get a reference to the function set
final FunctionSet functionSet = gui.getExperiment().getResources().getFunctionSet();
for (int i = 0; i < functionSet.getTotalFunctionCount(); i++) {
// add a checkbox for each function
checkBox = new CheckBox(functionSet.getFunction(i).toString());
checkBox.setId(String.valueOf(i));
// make sure the selection matches the function set
checkBox.setSelected(functionSet.isEnabled(functionSet.getFunction(i)));
final int index = i;
// set listener so function set gets updated if the checkboxes change
checkBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (((CheckBox) event.getSource()).isSelected()) {
functionSet.enableFunction(index);
} else {
functionSet.disableFunction(index);
}
gui.updateFunctionSelector();
revalidateParameters();
}
});
// add the new checkbox
nodeFunctions.getChildren().add(checkBox);
}
// make sure function selector has all functions
gui.updateFunctionSelector();
}
/**
* @return true if the experiment is currently evolving something, false otherwise.
*/
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.
* <br><br>
* This also disables the controls if a reset is necessary, preventing the experiment
* from running until it has happened.
*/
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.
* <br><br>
* It also closes the test case table, just in case.
*/
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();
}
}
|