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
|
package jcgp.backend.resources;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import jcgp.backend.function.FunctionSet;
import jcgp.backend.modules.problem.BestFitness;
import jcgp.backend.parameters.IntegerParameter;
import jcgp.backend.parameters.ParameterStatus;
import jcgp.backend.parameters.monitors.IntegerMonitor;
/**
*
* This subclass of Resources allows modifications to be made.
* A read-only cast of this class is passed to modules for safety,
* and only classes with access to a JCGP instance may modify
* the resources.
*
* @author Eduardo Pedroni
*
*/
public class ModifiableResources extends Resources {
/**
* Creates an instance of this class and initialises
* all base parameters to default values. See
* {@code createBaseParameters} for the exact parameter
* initialisation.
*/
public ModifiableResources() {
createBaseParameters();
}
/**
* @param rows the number of rows to set.
*/
public void setRows(int rows) {
this.rows.set(rows);
}
/**
* @param columns the number of columns to set.
*/
public void setColumns(int columns) {
this.columns.set(columns);
}
/**
* @param inputs the number of inputs to set.
*/
public void setInputs(int inputs) {
this.inputs.set(inputs);
}
/**
* @param outputs the number of outputs to set.
*/
public void setOutputs(int outputs) {
this.outputs.set(outputs);
}
/**
* @param populationSize the population size to set.
*/
public void setPopulationSize(int populationSize) {
this.populationSize.set(populationSize);
}
/**
* @param levelsBack the levels back to set.
*/
public void setLevelsBack(int levelsBack) {
this.levelsBack.set(levelsBack);
}
/**
* @param currentGeneration the current generation to set.
*/
public void setCurrentGeneration(int currentGeneration) {
this.currentGeneration.set(currentGeneration);
}
/**
* Adds 1 to the current generation.
*/
public void incrementGeneration() {
this.currentGeneration.set(currentGeneration.get() + 1);
}
/**
* @param generations the total generations to set.
*/
public void setGenerations(int generations) {
this.generations.set(generations);
}
/**
* @param currentRun the current run to set.
*/
public void setCurrentRun(int currentRun) {
this.currentRun.set(currentRun);
}
/**
* Adds 1 to the current generation.
*/
public void incrementRun() {
currentRun.set(currentRun.get() + 1);
}
/**
* @param runs the total runs to set.
*/
public void setRuns(int runs) {
this.runs.set(runs);
}
/**
* This is called automatically by the experiment when the arity changes.
*
* @param arity the arity to set.
*/
public void setArity(int arity) {
this.arity.set(arity);
}
/**
* @param seed the seed to set.
*/
public void setSeed(long seed) {
this.seed.set(seed);
}
/**
* @param report the report interval to set.
*/
public void setReportInterval(int report) {
this.reportInterval.set(report);
}
/**
* @param newOrientation the new orientation to set.
*/
public void setFitnessOrientation(BestFitness newOrientation) {
this.fitnessOrientation = newOrientation;
}
/**
* @return the rows parameter.
*/
public IntegerParameter getRowsParameter() {
return rows;
}
/**
* @return the columns parameter.
*/
public IntegerParameter getColumnsParameter() {
return columns;
}
/**
* @return the inputs parameter.
*/
public IntegerParameter getInputsParameter() {
return inputs;
}
/**
* @return the outputs parameter.
*/
public IntegerParameter getOutputsParameter() {
return outputs;
}
/**
* @return the population size parameter.
*/
public IntegerParameter getPopulationSizeParameter() {
return populationSize;
}
/**
* @return the levels back parameter.
*/
public IntegerParameter getLevelsBackParameter() {
return levelsBack;
}
/**
* @return the current generation parameter.
*/
public IntegerParameter getCurrentGenerationParameter() {
return currentGeneration;
}
/**
* @return the total generations parameter.
*/
public IntegerParameter getGenerationsParameter() {
return generations;
}
/**
* @return the current run parameter.
*/
public IntegerParameter getCurrentRunParameter() {
return currentRun;
}
/**
* @return the total runs parameter.
*/
public IntegerParameter getRunsParameter() {
return runs;
}
/**
* @return the arity parameter.
*/
public IntegerParameter getArityParameter() {
return arity;
}
/**
* @return the seed parameter.
*/
public IntegerParameter getSeedParameter() {
return seed;
}
/**
* @return the report interval parameter.
*/
public IntegerParameter getReportIntervalParameter() {
return reportInterval;
}
/**
* Update the current function set.
*
* @param functionSet the new function set.
*/
public void setFunctionSet(FunctionSet functionSet) {
this.functionSet = functionSet;
setArity(functionSet.getMaxArity());
}
/**
* This can be set to null if no extra console is desired.
*
* @param console the extra console for the experiment to use.
*/
public void setConsole(Console console) {
this.console = console;
}
/**
* For internal use only, this initialises all base parameters to default values.
*/
private void createBaseParameters() {
rows = new IntegerParameter(5, "Rows", false, true) {
@Override
public void validate(Number newValue) {
if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Chromosome must have at least 1 row.");
} else {
status = ParameterStatus.VALID;
}
}
};
columns = new IntegerParameter(5, "Columns", false, true) {
@Override
public void validate(Number newValue) {
if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Chromosome must have at least 1 column.");
} else {
status = ParameterStatus.VALID;
}
}
};
inputs = new IntegerMonitor(3, "Inputs");
outputs = new IntegerMonitor(3, "Outputs");
populationSize = new IntegerParameter(5, "Population", false, true) {
@Override
public void validate(Number newValue) {
if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Population size must be at least 1.");
} else {
status = ParameterStatus.VALID;
}
}
};
levelsBack = new IntegerParameter(2, "Levels back", false, true) {
@Override
public void validate(Number newValue) {
if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Levels back must be at least 1.");
} else if (newValue.intValue() > columns()) {
status = ParameterStatus.INVALID;
status.setDetails("Levels back must be less than or equal to the number of columns.");
} else {
status = ParameterStatus.VALID;
}
}
};
generations = new IntegerParameter(1000000, "Generations") {
@Override
public void validate(Number newValue) {
if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Number of generations must be greater than 0.");
} else if (newValue.intValue() < currentGeneration.get()) {
status = ParameterStatus.WARNING_RESET;
status.setDetails("Setting generations to less than the current generation will cause the experiment to restart.");
} else {
status = ParameterStatus.VALID;
}
}
};
currentGeneration = new IntegerMonitor(1, "Generation");
runs = new IntegerParameter(5, "Runs") {
@Override
public void validate(Number newValue) {
if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Number of runs must be greater than 0.");
} else if (newValue.intValue() < currentRun.get()) {
status = ParameterStatus.WARNING_RESET;
status.setDetails("Setting runs to less than the current run will cause the experiment to restart.");
} else {
status = ParameterStatus.VALID;
}
}
};
currentRun = new IntegerMonitor(1, "Run");
arity = new IntegerMonitor(0, "Max arity");
seed = new IntegerParameter(1234, "Seed", false, true) {
@Override
public void validate(Number newValue) {
status = ParameterStatus.VALID;
}
};
seed.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(
ObservableValue<? extends Number> observable,
Number oldValue, Number newValue) {
numberGenerator.setSeed(newValue.longValue());
}
});
numberGenerator.setSeed(seed.get());
reportInterval = new IntegerParameter(1, "Report interval", false, false) {
@Override
public void validate(Number newValue) {
if (newValue.intValue() > generations.get()) {
status = ParameterStatus.WARNING;
status.setDetails("No reports will be printed.");
} else {
status = ParameterStatus.VALID;
}
}
};
}
}
|