/
PushGP.java
599 lines (477 loc) · 18.5 KB
/
PushGP.java
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
596
597
598
599
/*
* Copyright 2009-2010 Jon Klein
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.spiderland.Psh;
import java.util.*;
/**
* The Push Genetic Programming core class.
*/
abstract public class PushGP extends GA {
private static final long serialVersionUID = 1L;
protected Interpreter _interpreter;
protected int _maxRandomCodeSize;
protected int _maxPointsInProgram;
protected int _executionLimit;
protected boolean _useFairMutation;
protected float _fairMutationRange;
protected String _nodeSelectionMode;
protected float _nodeSelectionLeafProbability;
protected int _nodeSelectionTournamentSize;
protected float _averageSize;
protected int _bestSize;
protected float _simplificationPercent;
protected float _simplifyFlattenPercent;
protected int _reproductionSimplifications;
protected int _reportSimplifications;
protected int _finalSimplifications;
protected String _targetFunctionString;
protected void InitFromParameters() throws Exception {
// Default parameters to be used when optional parameters are not
// given.
float defaultFairMutationRange = 0.3f;
float defaultsimplifyFlattenPercent = 20f;
String defaultInterpreterClass = "org.spiderland.Psh.Interpreter";
String defaultInputPusherClass = "org.spiderland.Psh.InputPusher";
String defaultTargetFunctionString = "";
float defaultNodeSelectionLeafProbability = 10;
int defaultNodeSelectionTournamentSize = 2;
// Limits
_maxRandomCodeSize = (int) GetFloatParam("max-random-code-size");
_executionLimit = (int) GetFloatParam("execution-limit");
_maxPointsInProgram = (int) GetFloatParam("max-points-in-program");
// Fair mutation parameters
_useFairMutation = "fair".equals(GetParam("mutation-mode", true));
_fairMutationRange = GetFloatParam("fair-mutation-range", true);
if (Float.isNaN(_fairMutationRange)) {
_fairMutationRange = defaultFairMutationRange;
}
// Node selection parameters
_nodeSelectionMode = GetParam("node-selection-mode", true);
if (_nodeSelectionMode != null) {
if (!_nodeSelectionMode.equals("unbiased")
&& !_nodeSelectionMode.equals("leaf-probability")
&& !_nodeSelectionMode.equals("size-tournament")) {
throw new Exception(
"node-selection-mode must be set to unbiased,\n"
+ "leaf-probability, or size-tournament. Currently set to "
+ _nodeSelectionMode);
}
_nodeSelectionLeafProbability = GetFloatParam(
"node-selection-leaf-probability", true);
if (Float.isNaN(_nodeSelectionLeafProbability)) {
_nodeSelectionLeafProbability = defaultNodeSelectionLeafProbability;
}
_nodeSelectionTournamentSize = (int) GetFloatParam(
"node-selection-tournament-size", true);
if (Float.isNaN(GetFloatParam("node-selection-tournament-size", true))) {
_nodeSelectionTournamentSize = defaultNodeSelectionTournamentSize;
}
} else {
_nodeSelectionMode = "unbiased";
}
// Simplification parameters
_simplificationPercent = GetFloatParam("simplification-percent");
_simplifyFlattenPercent = GetFloatParam("simplify-flatten-percent",
true);
if (Float.isNaN(_simplifyFlattenPercent)) {
_simplifyFlattenPercent = defaultsimplifyFlattenPercent;
}
_reproductionSimplifications = (int) GetFloatParam("reproduction-simplifications");
_reportSimplifications = (int) GetFloatParam("report-simplifications");
_finalSimplifications = (int) GetFloatParam("final-simplifications");
// ERC parameters
int minRandomInt;
int defaultMinRandomInt = -10;
int maxRandomInt;
int defaultMaxRandomInt = 10;
int randomIntResolution;
int defaultRandomIntResolution = 1;
if (Float.isNaN(GetFloatParam("min-random-integer", true))) {
minRandomInt = defaultMinRandomInt;
} else {
minRandomInt = (int) GetFloatParam("min-random-integer", true);
}
if (Float.isNaN(GetFloatParam("max-random-integer", true))) {
maxRandomInt = defaultMaxRandomInt;
} else {
maxRandomInt = (int) GetFloatParam("max-random-integer", true);
}
if (Float.isNaN(GetFloatParam("random-integer-resolution", true))) {
randomIntResolution = defaultRandomIntResolution;
} else {
randomIntResolution = (int) GetFloatParam(
"random-integer-resolution", true);
}
float minRandomFloat;
float defaultMinRandomFloat = -10.0f;
float maxRandomFloat;
float defaultMaxRandomFloat = 10.0f;
float randomFloatResolution;
float defaultRandomFloatResolution = 0.01f;
if (Float.isNaN(GetFloatParam("min-random-float", true))) {
minRandomFloat = defaultMinRandomFloat;
} else {
minRandomFloat = GetFloatParam("min-random-float", true);
}
if (Float.isNaN(GetFloatParam("max-random-float", true))) {
maxRandomFloat = defaultMaxRandomFloat;
} else {
maxRandomFloat = GetFloatParam("max-random-float", true);
}
if (Float.isNaN(GetFloatParam("random-float-resolution", true))) {
randomFloatResolution = defaultRandomFloatResolution;
} else {
randomFloatResolution = GetFloatParam("random-float-resolution",
true);
}
// Setup our custom interpreter class based on the params we're given
String interpreterClass = GetParam("interpreter-class", true);
if (interpreterClass == null) {
interpreterClass = defaultInterpreterClass;
}
Class<?> iclass = Class.forName(interpreterClass);
Object iObject = iclass.newInstance();
if (!(iObject instanceof Interpreter))
throw (new Exception(
"interpreter-class must inherit from class Interpreter"));
_interpreter = (Interpreter) iObject;
_interpreter.SetInstructions(new Program(_interpreter,
GetParam("instruction-set")));
_interpreter.SetRandomParameters(minRandomInt, maxRandomInt,
randomIntResolution, minRandomFloat, maxRandomFloat,
randomFloatResolution, _maxRandomCodeSize, _maxPointsInProgram);
// Frame mode and input pusher class
String framemode = GetParam("push-frame-mode", true);
String inputpusherClass = GetParam("inputpusher-class", true);
if (inputpusherClass == null) {
inputpusherClass = defaultInputPusherClass;
}
iclass = Class.forName(inputpusherClass);
iObject = iclass.newInstance();
if (!(iObject instanceof InputPusher))
throw new Exception(
"inputpusher-class must inherit from class InputPusher");
_interpreter.setInputPusher((InputPusher) iObject);
// Initialize the interpreter
InitInterpreter(_interpreter);
if (framemode != null && framemode.equals("pushstacks"))
_interpreter.SetUseFrames(true);
// Target function string
_targetFunctionString = GetParam("target-function-string", true);
if(_targetFunctionString == null){
_targetFunctionString = defaultTargetFunctionString;
}
// Init the GA
super.InitFromParameters();
// Print important parameters
Print(" Important Parameters\n");
Print(" ======================\n");
if(!_targetFunctionString.equals("")){
Print("Target Function: " + _targetFunctionString + "\n\n");
}
Print("Population Size: " + (int) GetFloatParam("population-size")
+ "\n");
Print("Generations: " + _maxGenerations + "\n");
Print("Execution Limit: " + _executionLimit + "\n\n");
Print("Crossover Percent: " + _crossoverPercent + "\n");
Print("Mutation Percent: " + _mutationPercent + "\n");
Print("Simplification Percent: " + _simplificationPercent + "\n");
Print("Clone Percent: "
+ (100 - _crossoverPercent - _mutationPercent - _simplificationPercent)
+ "\n\n");
Print("Tournament Size: " + _tournamentSize + "\n");
if (_trivialGeographyRadius != 0) {
Print("Trivial Geography Radius: " + _trivialGeographyRadius + "\n");
}
Print("Node Selection Mode: " + _nodeSelectionMode);
Print("\n");
Print("Instructions: " + _interpreter.GetInstructionsString() + "\n");
Print("\n");
}
public void InitIndividual(GAIndividual inIndividual) {
PushGPIndividual i = (PushGPIndividual) inIndividual;
int randomCodeSize = _RNG.nextInt(_maxRandomCodeSize) + 2;
Program p = _interpreter.RandomCode(randomCodeSize);
i.SetProgram(p);
}
protected void BeginGeneration() throws Exception {
_averageSize = 0;
}
protected void EndGeneration() {
_averageSize /= _populations[0].length;
}
protected void Evaluate() {
float totalFitness = 0;
_bestMeanFitness = Float.MAX_VALUE;
for (int n = 0; n < _populations[_currentPopulation].length; n++) {
GAIndividual i = _populations[_currentPopulation][n];
EvaluateIndividual(i);
totalFitness += i.GetFitness();
if (i.GetFitness() < _bestMeanFitness) {
_bestMeanFitness = i.GetFitness();
_bestIndividual = n;
_bestSize = ((PushGPIndividual) i)._program.programsize();
_bestErrors = i.GetErrors();
}
}
_populationMeanFitness = totalFitness
/ _populations[_currentPopulation].length;
}
public void EvaluateIndividual(GAIndividual inIndividual) {
EvaluateIndividual(inIndividual, false);
}
protected void EvaluateIndividual(GAIndividual inIndividual,
boolean duringSimplify) {
ArrayList<Float> errors = new ArrayList<Float>();
if (!duringSimplify)
_averageSize += ((PushGPIndividual) inIndividual)._program
.programsize();
long t = System.currentTimeMillis();
for (int n = 0; n < _testCases.size(); n++) {
GATestCase test = _testCases.get(n);
float e = EvaluateTestCase(inIndividual, test._input, test._output);
errors.add(e);
}
t = System.currentTimeMillis() - t;
inIndividual.SetFitness(AbsoluteAverageOfErrors(errors));
inIndividual.SetErrors(errors);
// System.out.println("Evaluated individual in " + t + " msec: fitness "
// + inIndividual.GetFitness());
}
abstract protected void InitInterpreter(Interpreter inInterpreter)
throws Exception;
protected String Report() {
String report = super.Report();
if (Double.isInfinite(_populationMeanFitness))
_populationMeanFitness = Double.MAX_VALUE;
report += ";; Best Program:\n "
+ _populations[_currentPopulation][_bestIndividual] + "\n\n";
report += ";; Best Program Fitness (mean): " + _bestMeanFitness + "\n";
if (_testCases.size() == _bestErrors.size()) {
report += ";; Best Program Errors: (";
for (int i = 0; i < _testCases.size(); i++) {
if (i != 0)
report += " ";
report += "(" + _testCases.get(i)._input + " ";
report += Math.abs(_bestErrors.get(i)) + ")";
}
report += ")\n";
}
report += ";; Best Program Size: " + _bestSize + "\n\n";
report += ";; Mean Fitness: " + _populationMeanFitness + "\n";
report += ";; Mean Program Size: " + _averageSize + "\n";
PushGPIndividual simplified = Autosimplify(
(PushGPIndividual) _populations[_currentPopulation][_bestIndividual],
_reportSimplifications);
report += ";; Number of Evaluations Thus Far: "
+ _interpreter.GetEvaluationExecutions() + "\n";
String mem = String
.valueOf(Runtime.getRuntime().totalMemory() / 10000000.0f);
report += ";; Memory usage: " + mem + "\n\n";
report += ";; Partial Simplification (may beat best):\n ";
report += simplified._program + "\n";
report += ";; Partial Simplification Size: ";
report += simplified._program.programsize() + "\n\n";
return report;
}
protected String FinalReport() {
String report = "";
report += super.FinalReport();
if(!_targetFunctionString.equals("")){
report += ">> Target Function: " + _targetFunctionString + "\n\n";
}
PushGPIndividual simplified = Autosimplify(
(PushGPIndividual) _populations[_currentPopulation][_bestIndividual],
_finalSimplifications);
// Note: The number of evaluations here will likely be higher than that
// given during the last generational report, since evaluations made
// during simplification count towards the total number of
// simplifications.
report += ">> Number of Evaluations: "
+ _interpreter.GetEvaluationExecutions() + "\n";
report += ">> Best Program: "
+ _populations[_currentPopulation][_bestIndividual] + "\n";
report += ">> Fitness (mean): " + _bestMeanFitness + "\n";
if (_testCases.size() == _bestErrors.size()) {
report += ">> Errors: (";
for (int i = 0; i < _testCases.size(); i++) {
if (i != 0)
report += " ";
report += "(" + _testCases.get(i)._input + " ";
report += Math.abs(_bestErrors.get(i)) + ")";
}
report += ")\n";
}
report += ">> Size: " + _bestSize + "\n\n";
report += "<<<<<<<<<< After Simplification >>>>>>>>>>\n";
report += ">> Best Program: ";
report += simplified._program + "\n";
report += ">> Size: ";
report += simplified._program.programsize() + "\n\n";
return report;
}
public String GetTargetFunctionString(){
return _targetFunctionString;
}
protected PushGPIndividual Autosimplify(PushGPIndividual inIndividual,
int steps) {
PushGPIndividual simplest = (PushGPIndividual) inIndividual.clone();
PushGPIndividual trial = (PushGPIndividual) inIndividual.clone();
EvaluateIndividual(simplest, true);
float bestError = simplest.GetFitness();
boolean madeSimpler = false;
for (int i = 0; i < steps; i++) {
madeSimpler = false;
float method = _RNG.nextInt(100);
if (trial._program.programsize() <= 0)
break;
if (method < _simplifyFlattenPercent) {
// Flatten random thing
int pointIndex = _RNG.nextInt(trial._program.programsize());
Object point = trial._program.Subtree(pointIndex);
if (point instanceof Program) {
trial._program.Flatten(pointIndex);
madeSimpler = true;
}
} else {
// Remove small number of random things
int numberToRemove = _RNG.nextInt(3) + 1;
for (int j = 0; j < numberToRemove; j++) {
int trialSize = trial._program.programsize();
if (trialSize > 0) {
int pointIndex = _RNG.nextInt(trialSize);
trial._program.ReplaceSubtree(pointIndex, new Program(
_interpreter));
trial._program.Flatten(pointIndex);
madeSimpler = true;
}
}
}
if (madeSimpler) {
EvaluateIndividual(trial, true);
if (trial.GetFitness() <= bestError) {
simplest = (PushGPIndividual) trial.clone();
bestError = trial.GetFitness();
}
}
trial = (PushGPIndividual) simplest.clone();
}
return simplest;
}
protected void Reproduce() {
int nextPopulation = _currentPopulation == 0 ? 1 : 0;
for (int n = 0; n < _populations[_currentPopulation].length; n++) {
float method = _RNG.nextInt(100);
GAIndividual next;
if (method < _mutationPercent) {
next = ReproduceByMutation(n);
} else if (method < _crossoverPercent + _mutationPercent) {
next = ReproduceByCrossover(n);
} else if (method < _crossoverPercent + _mutationPercent
+ _simplificationPercent) {
next = ReproduceBySimplification(n);
} else {
next = ReproduceByClone(n);
}
_populations[nextPopulation][n] = next;
}
}
protected GAIndividual ReproduceByCrossover(int inIndex) {
PushGPIndividual a = (PushGPIndividual) ReproduceByClone(inIndex);
PushGPIndividual b = (PushGPIndividual) TournamentSelect(
_tournamentSize, inIndex);
if (a._program.programsize() <= 0) {
return b;
}
if (b._program.programsize() <= 0) {
return a;
}
int aindex = ReproductionNodeSelection(a);
int bindex = ReproductionNodeSelection(b);
if (a._program.programsize() + b._program.SubtreeSize(bindex)
- a._program.SubtreeSize(aindex) <= _maxPointsInProgram)
a._program.ReplaceSubtree(aindex, b._program.Subtree(bindex));
return a;
}
protected GAIndividual ReproduceByMutation(int inIndex) {
PushGPIndividual i = (PushGPIndividual) ReproduceByClone(inIndex);
int totalsize = i._program.programsize();
int which = ReproductionNodeSelection(i);
int oldsize = i._program.SubtreeSize(which);
int newsize = 0;
if (_useFairMutation) {
int range = (int) Math.max(1, _fairMutationRange * oldsize);
newsize = Math.max(1, oldsize + _RNG.nextInt(2 * range) - range);
} else {
newsize = _RNG.nextInt(_maxRandomCodeSize);
}
Object newtree;
if (newsize == 1)
newtree = _interpreter.RandomAtom();
else
newtree = _interpreter.RandomCode(newsize);
if (newsize + totalsize - oldsize <= _maxPointsInProgram)
i._program.ReplaceSubtree(which, newtree);
return i;
}
/**
* Selects a node to use during crossover or mutation. The selection
* mechanism depends on the global parameter _nodeSelectionMode.
* @param inInd = Individual to select node from.
* @return Index of the node to use for reproduction.
*/
protected int ReproductionNodeSelection(PushGPIndividual inInd) {
int totalSize = inInd._program.programsize();;
int selectedNode = 0;
if(totalSize <= 1){
selectedNode = 0;
}
else if(_nodeSelectionMode.equals("unbiased")){
selectedNode = _RNG.nextInt(totalSize);
}
else if(_nodeSelectionMode.equals("leaf-probability")){
// TODO Implement. Currently runs unbiased
// note: if there aren't any internal nodes, must select leaf, and
// if no leaf, must select internal
selectedNode = _RNG.nextInt(totalSize);
}
else {
// size-tournament
int maxSize = -1;
selectedNode = 0;
for(int j = 0; j < _nodeSelectionTournamentSize; j++){
int nextwhich = _RNG.nextInt(totalSize);
int nextwhichsize = inInd._program.SubtreeSize(nextwhich);
if(nextwhichsize > maxSize){
selectedNode = nextwhich;
maxSize = nextwhichsize;
}
}
}
return selectedNode;
}
protected GAIndividual ReproduceBySimplification(int inIndex) {
PushGPIndividual i = (PushGPIndividual) ReproduceByClone(inIndex);
i = Autosimplify(i, _reproductionSimplifications);
return i;
}
public void RunTestProgram(Program p, int inTestCaseIndex) {
PushGPIndividual i = new PushGPIndividual(p);
GATestCase test = _testCases.get(inTestCaseIndex);
System.out.println("Executing program: " + p);
EvaluateTestCase(i, test._input, test._output);
System.out.println(_interpreter);
}
}