-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParamManager.pde
330 lines (309 loc) · 11.9 KB
/
ParamManager.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
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.time.Instant;
public class ParamManager {
private Params params;
private Configurations configs;
private File configFile;
private Instant lastModifiedTime;
public Configurations getConfigs() {
return configs;
}
public Params getParams() {
return params;
}
public ParamManager() {
params = new Params();
configs = new Configurations();
configFile = new File("simulator.ini");
this.setDefaults();
}
public void setDefaults() {
//Immutable, set by Configuration enum only
configs.agentSize = (Integer)Configuration.AGENT_SIZE.getValue();
configs.sizeX = (Integer)Configuration.SIZE_X.getValue();
configs.sizeY = (Integer)Configuration.SIZE_Y.getValue(); // 2..0x10000
// Can be set by configuration values
configs.population = (Integer)Configuration.POPULATION.getValue(); // >= 0
//configs.numThreads; // > 0
configs.signalLayers = (Integer)Configuration.SIGNAL_LAYERS.getValue(); // >= 0
configs.genomeMaxLength = (Integer)Configuration.GENOME_MAX_LENGTH.getValue(); // > 0
configs.maxNumberNeurons = (Integer)Configuration.MAX_NUMBER_NEURONS.getValue(); // > 0
configs.genomeInitialLengthMin = (Integer)Configuration.GENOME_INITIAL_LENGTH_MIN.getValue(); // > 0 and < genomeInitialLengthMax
configs.genomeInitialLengthMax = (Integer)Configuration.GENOME_INITIAL_LENGTH_MAX.getValue(); // > 0 and < genomeInitialLengthMin
//configs.logDir;
//configs.imageDir;
//configs.graphLogUpdateCommand;
configs.RNGSeed = (Long)Configuration.RNG_SEED.getValue(); // >= 0
configs.deterministic = (Boolean)Configuration.DETERMINISTIC.getValue();
// Can change during simulation run
params.stepsPerGeneration = (Integer) Parameters.STEPS_PER_GENERATION.getValue();
params.maxGenerations = (Integer) Parameters.MAX_GENERATIONS.getValue();
params.populationSensorRadius = (Double) Parameters.POPULATION_SENSOR_RADIUS.getValue();
params.signalSensorRadius = (Double) Parameters.SIGNAL_SENSOR_RADIUS.getValue();
params.responsiveness = (Double) Parameters.RESPONSIVENESS.getValue();
params.responsivenessCurveKFactor = (Double) Parameters.RESPONSIVENESS_CURVE_K_FACTOR.getValue();
params.longProbeDistance = (Integer) Parameters.LONG_PROBE_DISTANCE.getValue();
params.shortProbeBarrierDistance = (Integer) Parameters.SHORT_PROBE_BARRIER_DISTANCE.getValue();
params.valenceSaturationMag = (Double) Parameters.VALENCE_SATURATION_MAG.getValue();
params.pointMutationRate = (Double) Parameters.POINT_MUTATION_RATE.getValue();
params.geneInsertionDeletionRate = (Double) Parameters.GENE_INSERTION_DELETION_RATE.getValue();
params.deletionRatio = (Double) Parameters.DELETION_RATIO.getValue();
params.sexualReproduction = (Boolean) Parameters.SEXUAL_REPRODUCTION.getValue();
params.chooseParentsByFitness = (Boolean) Parameters.CHOOSE_PARENTS_BY_FITNESS.getValue();
params.challenge = (Challenge) Parameters.CHALLENGE.getValue();
params.barrierType = (BarrierType) Parameters.BARRIER_TYPE.getValue();
params.killEnable = (Boolean) Parameters.KILL_ENABLE.getValue();
params.genomeAnalysisStride = (Integer) Parameters.GENOME_ANALYSIS_STRIDE.getValue();
params.displaySampleGenomes = (Integer) Parameters.DISPLAY_SAMPLE_GENOMES.getValue();
params.genomeComparisonMethod = (ComparisonMethod) Parameters.GENOME_COMPARISON_METHOD.getValue();
params.updateGraphLog = (Boolean) Parameters.UPDATE_GRAPH_LOG.getValue();
params.updateGraphLogStride = (Integer) Parameters.UPDATE_GRAPH_LOG_STRIDE.getValue();
params.saveVideo = (Boolean) Parameters.SAVE_VIDEO.getValue();
params.videoStride = (Integer) Parameters.VIDEO_STRIDE.getValue();
params.videoSaveFirstFrames = (Integer) Parameters.VIDEO_SAVE_FIRST_FRAMES.getValue();
params.displayScale = (Integer) Parameters.DISPLAY_SCALE.getValue();
}
public void registerConfigFile(File file) {
configFile = (file == null)?configFile:file;
}
public void updateFromConfigFile(int generationNumber) {
String[] lines = loadStrings(configFile.getAbsolutePath());
if (lines == null) return;
for (String line : lines) {
String config = line.trim();
if (config.startsWith("#") || config.isEmpty()) continue;
String[] nameValue = config.split("=");
String name = nameValue[0].trim();
String value = nameValue[1].trim();
String[] nameGeneration = name.split("@");
if (nameGeneration.length > 1) {
try {
int generationSpecifier = Integer.parseInt(nameGeneration[1].trim());
if (generationSpecifier > generationNumber) continue;
else if (generationSpecifier == generationNumber) params.parameterChangeGenerationNumber = generationNumber;
name = nameGeneration[0];
}
catch (NumberFormatException e) {
System.out.printf("invalid specifier in configs: %s\n", line);
}
}
String[] valueComment = value.split("#");
value = valueComment[0].trim();
ingestParameter(name, value);
}
}
private void ingestParameter(String name, String stringValue) {
name = name.toLowerCase();
Object value = decodeValue(stringValue);
switch (name) {
case "challenge":
params.challenge = (Challenge)value;
break;
case "genomeinitiallengthmin":
configs.genomeInitialLengthMin = (Integer)value;
break;
case "genomeinitiallengthmax":
configs.genomeInitialLengthMax = (Integer)value;
break;
case "population":
configs.population = (Integer)value;
break;
case "stepspergeneration":
params.stepsPerGeneration = (Integer)value;
break;
case "maxgenerations":
params.maxGenerations = (Integer)value;
break;
case "barriertype":
params.barrierType = (BarrierType)value;
break;
//case "numthreads":
// params.numThreads = uVal;
// break;
case "signallayers":
configs.signalLayers = (Integer)value;
break;
case "genomemaxlength":
configs.genomeMaxLength = (Integer)value;
break;
case "maxnumberneurons":
configs.maxNumberNeurons = (Integer)value;
break;
case "pointmutationrate":
params.pointMutationRate = (Double)value;
break;
case "geneinsertiondeletionrate":
params.geneInsertionDeletionRate = (Double)value;
break;
case "deletionratio":
params.deletionRatio = (Double)value;
break;
case "killenable":
params.killEnable = (Boolean)value;
break;
case "sexualreproduction":
params.sexualReproduction = (Boolean)value;
break;
case "chooseparentsbyfitness":
params.chooseParentsByFitness = (Boolean)value;
break;
case "populationsensorradius":
params.populationSensorRadius = (Double)value;
break;
case "signalsensorradius":
params.signalSensorRadius = (Double)value;
break;
case "responsiveness":
params.responsiveness = (Double)value;
break;
case "responsivenesscurvekfactor":
params.responsivenessCurveKFactor = (Double)value;
break;
case "longprobedistance":
params.longProbeDistance = (Integer)value;
break;
case "shortprobebarrierdistance":
params.shortProbeBarrierDistance = (Integer)value;
break;
case "valencesaturationmag":
params.valenceSaturationMag = (Double)value;
break;
case "savevideo":
params.saveVideo = (Boolean)value;
break;
case "videostride":
params.videoStride = (Integer)value;
break;
case "videosavefirstframes":
params.videoSaveFirstFrames = (Integer)value;
break;
case "displayscale":
params.displayScale = (Integer)value;
break;
case "genomeanalysisstride":
params.genomeAnalysisStride = (Integer)value;
break;
case "displaysamplegenomes":
params.displaySampleGenomes = (Integer)value;
break;
case "genomecomparisonmethod":
params.genomeComparisonMethod = (ComparisonMethod)value;
break;
case "updategraphlog":
params.updateGraphLog = (Boolean)value;
break;
case "updategraphlogstride":
params.updateGraphLogStride = (Integer)value;
break;
case "deterministic":
configs.deterministic = (Boolean)value;
break;
case "rngseed":
configs.RNGSeed = (Long)value;
break;
default:
System.out.printf("Invalid param: %s = %s\n", name, value);
}
}
public void checkParameters() {
// implementation code
}
private Object decodeValue(String input) {
Object value = null;
try {
// Try to parse as integer
value = Integer.parseInt(input);
}
catch (NumberFormatException e1) {
try {
// Try to parse as integer
value = Double.parseDouble(input);
}
catch (NumberFormatException e5) {
try {
// Try to parse as integer
value = Long.parseLong(input);
}
catch (NumberFormatException e7) {
try {
// Try to parse as boolean
value = Boolean.parseBoolean(input);
}
catch (IllegalArgumentException e2) {
try {
// Try to get enum by name
value = Enum.valueOf(Challenge.class, input);
}
catch (IllegalArgumentException e3) {
try {
// Try to get enum by name
value = Enum.valueOf(BarrierType.class, input);
}
catch (IllegalArgumentException e4) {
try {
// Try to get enum by name
value = Enum.valueOf(ComparisonMethod.class, input);
}
catch (IllegalArgumentException e6) {
// Could not parse as any of the above
System.err.println("Unable to parse input string: " + input);
}
}
}
}
}
}
}
return value;
}
}
public class Params {
public int stepsPerGeneration; // > 0
public int maxGenerations; // >= 0
public double pointMutationRate; // 0.0..1.0
public double geneInsertionDeletionRate; // 0.0..1.0
public double deletionRatio; // 0.0..1.0
public boolean killEnable;
public boolean sexualReproduction;
public boolean chooseParentsByFitness;
public double populationSensorRadius; // > 0.0
public double signalSensorRadius; // > 0
public double responsiveness; // >= 0.0
public double responsivenessCurveKFactor; // 1, 2, 3, or 4
public long longProbeDistance; // > 0
public int shortProbeBarrierDistance; // > 0
public double valenceSaturationMag;
public boolean saveVideo;
public int videoStride; // > 0
public int videoSaveFirstFrames; // >= 0, overrides videoStride
public int displayScale;
public int genomeAnalysisStride; // > 0
public int displaySampleGenomes; // >= 0
public ComparisonMethod genomeComparisonMethod; // 0 = Jaro-Winkler; 1 = Hamming
public boolean updateGraphLog;
public int updateGraphLogStride; // > 0
public Challenge challenge;
public BarrierType barrierType; // >= 0
// These are updated automatically and not set via the parameter file
public int parameterChangeGenerationNumber; // the most recent generation number that an automatic parameter change occurred at
}
public class Configurations {
public int agentSize;
public int sizeX; // 2..0x10000
public int sizeY; // 2..0x10000
public int population; // >= 0
public int numThreads; // > 0
public int signalLayers; // >= 0
public int genomeMaxLength; // > 0
public int maxNumberNeurons; // > 0
public int genomeInitialLengthMin; // > 0 and < genomeInitialLengthMax
public int genomeInitialLengthMax; // > 0 and < genomeInitialLengthMin
public String logDir;
public String imageDir;
public String graphLogUpdateCommand;
public long RNGSeed; // >= 0
public boolean deterministic;
}