aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/JCGP.java
blob: 807739f21adc6ccc2c31cb5def9556f965da4119 (plain)
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
package jcgp;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Random;

import javafx.beans.property.SimpleIntegerProperty;
import jcgp.function.Arithmetic;
import jcgp.function.BitwiseLogic;
import jcgp.function.BooleanLogic;
import jcgp.function.Function;
import jcgp.function.FunctionSet;
import jcgp.modules.ea.EvolutionaryAlgorithm;
import jcgp.modules.ea.MuPlusLambda;
import jcgp.modules.ea.TournamentSelection;
import jcgp.modules.fitness.FitnessFunction;
import jcgp.modules.fitness.TestCase;
import jcgp.modules.fitness.TestCaseEvaluator;
import jcgp.modules.mutator.Mutator;
import jcgp.modules.mutator.PointMutator;
import jcgp.parameters.BooleanParameter;
import jcgp.parameters.DoubleParameter;
import jcgp.parameters.IntegerParameter;
import jcgp.parameters.Parameter;
import jcgp.population.Population;

/**
 * @author Eduardo Pedroni
 *
 */
public class JCGP {

	/**
	 * 
	 * 
	 * @author Eduardo Pedroni
	 *
	 */
	public static class Resources {
		private HashMap<String, Parameter> parameters = new HashMap<String, Parameter>();

		private Random numberGenerator;
		
		private TestCase[] testCases;
		
		// function sets
		private FunctionSet[] functionSets =  new FunctionSet[] {
				new Arithmetic(),
				new BitwiseLogic(),
				new BooleanLogic() };
		private FunctionSet functionSet = functionSets[0];
		
		public Resources() {
			createCoreParameters();
			
			numberGenerator = new Random(getInt("seed"));
			set("arity", functionSet.getMaxArity());
		}
		
		public int getInt(String key) {
			if (parameters.get(key) instanceof IntegerParameter) {
				return ((IntegerParameter) parameters.get(key)).get();
			} else if (parameters.get(key) instanceof DoubleParameter) {
				return (int) ((DoubleParameter) parameters.get(key)).get();
			} else {
				throw new ClassCastException("Could not cast " + parameters.get(key).getClass() + " to int.");
			}
		}
		
		public double getDouble(String key) {
			if (parameters.get(key) instanceof IntegerParameter) {
				return (double) ((IntegerParameter) parameters.get(key)).get();
			} else if (parameters.get(key) instanceof DoubleParameter) {
				return ((DoubleParameter) parameters.get(key)).get();
			} else {
				throw new ClassCastException("Could not cast " + parameters.get(key).getClass() + " to double.");
			}
		}
		
		public boolean getBoolean(String key) {
			if (parameters.get(key) instanceof BooleanParameter) {
				return ((BooleanParameter) parameters.get(key)).get();
			} else {
				throw new ClassCastException("Could not cast " + parameters.get(key).getClass() + " to int.");
			}
		}
		
		public void set(String key, Object value) {
			if (parameters.get(key) instanceof IntegerParameter) {
				((IntegerParameter) parameters.get(key)).set(((Integer) value).intValue());
			} else if (parameters.get(key) instanceof DoubleParameter) {
				((DoubleParameter) parameters.get(key)).set(((Double) value).doubleValue());
			} else if (parameters.get(key) instanceof BooleanParameter) {
				((BooleanParameter) parameters.get(key)).set(((Boolean) value).booleanValue());
			}
		}
		
		public Parameter getParameter(String key) {
			return parameters.get(key);
		}
		
		public boolean contains(String key) {
			return parameters.containsKey(key);
		}
		
		private void createCoreParameters() {
			parameters.put("rows", new IntegerParameter(8, "Rows"));
			parameters.put("columns", new IntegerParameter(9, "Columns"));
			parameters.put("inputs", new IntegerParameter(3, "Inputs"));
			parameters.put("outputs", new IntegerParameter(3, "Outputs"));
			parameters.put("popSize", new IntegerParameter(5, "Population"));
			parameters.put("levelsBack", new IntegerParameter(2, "Levels back"));
			
			IntegerParameter nodes = new IntegerParameter(1, "Nodes", false, true);
			nodes.valueProperty().bind(((SimpleIntegerProperty) ((IntegerParameter) parameters.get("rows")).valueProperty()).multiply((SimpleIntegerProperty) ((IntegerParameter) parameters.get("columns")).valueProperty()));
			parameters.put("nodes", nodes);
						
			parameters.put("generations", new IntegerParameter(1000000, "Generations"));
			parameters.put("currentGen", new IntegerParameter(1, "Generation", false, false));
			
			parameters.put("runs", new IntegerParameter(5, "Runs"));
			parameters.put("currentRun", new IntegerParameter(1, "Run", false, false));

			parameters.put("arity", new IntegerParameter(0, "Max arity", true, true));
			parameters.put("seed", new IntegerParameter(123, "Seed"));
			
			parameters.put("verbose", new BooleanParameter(false, "Verbose"));
			parameters.put("report", new IntegerParameter(1, "Report"));
		}
		
		/**
		 * 
		 * 
		 * @return the iterator for the set of base parameters
		 */
		public Iterator<Entry<String, Parameter>> iterator() {
			return parameters.entrySet().iterator();
		}
		
		/*
		 * Utility functions
		 */
		public int getRandomInt(int limit) {
			return numberGenerator.nextInt(limit);
		}
		
		public double getRandomDouble(int limit) {
			return numberGenerator.nextDouble() * limit;
		}
		
		public double getRandomDouble() {
			return numberGenerator.nextDouble();
		}
		
		/*
		 * FunctionSet functions
		 */
		public Function getRandomFunction() {
			return functionSet.getAllowedFunction(numberGenerator.nextInt(functionSet.getAllowedFunctionCount()));
		}

		public Function getFunction(int index) {
			return functionSet.getAllowedFunction(index);
		}
		
		public void setFunctionSet(int index) {
			functionSet = functionSets[index];
		}
		
		/**
		 * @return the functionSets
		 */
		public FunctionSet[] getFunctionSets() {
			return functionSets;
		}

		/**
		 * @return the functionSet
		 */
		public FunctionSet getFunctionSet() {
			return functionSet;
		}
		
		/*
		 * Test cases
		 */
		public void setTestCases(TestCase ... testCases) {
			this.testCases = testCases;
		}

		public TestCase getTestCase(int index) {
			return testCases[index];
		}

		public int getTestCaseCount() {
			return testCases.length;
		}

	}
	
	private Resources resources = new Resources();
	
	/*
	 * The following arrays contain all available modules. These collections are read by the GUI
	 * when generating menus, so modules not added here will *NOT* be selectable in the GUI.
	 * 
	 * Each array is accompanied by a field which contains a reference to the currently selected
	 * module, 0 by default.
	 */
	// mutators
	private Mutator[] mutators = new Mutator[] {
			new PointMutator() };
	private Mutator mutator;
	
	// evolutionary algorithms
	private EvolutionaryAlgorithm[] evolutionaryAlgorithms = new EvolutionaryAlgorithm[] {
			new MuPlusLambda(),
			new TournamentSelection()};
	private EvolutionaryAlgorithm evolutionaryAlgorithm;
	
	// fitness evaluators
	private FitnessFunction[] fitnessFunctions = new FitnessFunction[] {
			new TestCaseEvaluator() };
	private FitnessFunction fitnessFunction;

	/*
	 * the population of chromosomes
	 */
	private Population population;
	
	
	public JCGP() {
		
		population = new Population(resources);

		evolutionaryAlgorithm = evolutionaryAlgorithms[0];
		
		mutator = mutators[0];
		
		fitnessFunction = fitnessFunctions[0];
		
		resources.setTestCases(new TestCase(new Integer[]{1, 2, 3}, new Integer[]{4, 5, 6}),
							   new TestCase(new Integer[]{1, 12, 4}, new Integer[]{6, 21, 2}));
//		for (int i = 0; i < (int) resources.get("generations"); i++) {
//			
//			resources.set("currentGen", ((int) resources.get("currentGen")) + 1);
//			
//			fitnessFunction.evaluate(population, resources);
//			evolutionaryAlgorithm.evolve(population, mutator, resources);
//			
//			System.out.println("fitness: " + evolutionaryAlgorithm.getFittestChromosome().getFitness());
//			
//			if (evolutionaryAlgorithm.getFittestChromosome().getFitness() >= 6) {
//				System.out.println("solution found");
//				evolutionaryAlgorithm.getFittestChromosome().printNodes();
//				break;
//			}
//		}	
	}


	public Resources getResources() {
		return resources;
	}


	public Population getPopulation() {
		return population;
	}


	/**
	 * @return the mutators
	 */
	public Mutator[] getMutators() {
		return mutators;
	}


	/**
	 * @return the mutator
	 */
	public Mutator getMutator() {
		return mutator;
	}


	/**
	 * @return the evolutionaryAlgorithms
	 */
	public EvolutionaryAlgorithm[] getEvolutionaryAlgorithms() {
		return evolutionaryAlgorithms;
	}


	/**
	 * @return the evolutionaryAlgorithm
	 */
	public EvolutionaryAlgorithm getEvolutionaryAlgorithm() {
		return evolutionaryAlgorithm;
	}


	/**
	 * @return the fitnessFunctions
	 */
	public FitnessFunction[] getFitnessFunctions() {
		return fitnessFunctions;
	}


	/**
	 * @return the fitnessFunction
	 */
	public FitnessFunction getFitnessFunction() {
		return fitnessFunction;
	}
	
	
	/**
	 * @param mutator the mutator to set
	 */
	public void setMutator(int index) {
		this.mutator = mutators[index];
	}


	/**
	 * @param evolutionaryAlgorithm the evolutionaryAlgorithm to set
	 */
	public void setEvolutionaryAlgorithm(int index) {		
		this.evolutionaryAlgorithm = evolutionaryAlgorithms[index];
	}


	/**
	 * @param fitnessFunction the fitnessFunction to set
	 */
	public void setFitnessFunction(int index) {
		this.fitnessFunction = fitnessFunctions[index];
	}
	
	public void nextGeneration() {
		resources.set("currentGen", resources.getInt("currentGen") + 1);
		
		fitnessFunction.evaluate(population, resources);
		evolutionaryAlgorithm.evolve(population, mutator, resources);
		
		if (evolutionaryAlgorithm.getFittestChromosome().getFitness() >= 6) {
			System.out.println("solution found");
			evolutionaryAlgorithm.getFittestChromosome().printNodes();
		} else {
			System.out.println("Generation: " + resources.getInt("currentGen") + ", fitness: " + evolutionaryAlgorithm.getFittestChromosome().getFitness());
		}
	}
	
	public void start() {		
		while (resources.getInt("currentGen") <= resources.getInt("generations")) {
			nextGeneration();
		}
		// TODO print something?
	}	
}