aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/CGP.java
blob: 16d2be7c83ee585f918fb06af8cad5c7f998c3f9 (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
package jcgp;

import java.util.HashMap;
import java.util.Random;

import javafx.beans.property.Property;
import javafx.beans.property.SimpleIntegerProperty;
import jcgp.modules.ea.EvolutionaryAlgorithm;
import jcgp.modules.ea.StandardEA;
import jcgp.modules.fitness.FitnessFunction;
import jcgp.modules.fitness.TestCase;
import jcgp.modules.fitness.TruthTableEvaluator;
import jcgp.modules.function.Arithmetic;
import jcgp.modules.function.BitwiseLogic;
import jcgp.modules.function.BooleanLogic;
import jcgp.modules.function.FunctionSet;
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;

public class CGP {

	// CGP components
	private Mutator[] mutators = new Mutator[] {
			new PointMutator()};
	private Mutator mutator = mutators[0];
	
	private EvolutionaryAlgorithm[] evolutionaryAlgorithms = new EvolutionaryAlgorithm[] {
			new StandardEA()};
	private EvolutionaryAlgorithm evolutionaryAlgorithm = evolutionaryAlgorithms[0];
	
	private FitnessFunction[] fitnessFunctions = new FitnessFunction[] {
			new TruthTableEvaluator()};
	private FitnessFunction fitnessFunction = fitnessFunctions[0];

	private FunctionSet[] functionSets =  new FunctionSet[] {
			new FunctionSet("Arithmetic",
					new Arithmetic.Addition(),
					new Arithmetic.Subtraction(),
					new Arithmetic.Multiplication(),
					new Arithmetic.Division()),

			new FunctionSet("32-bit logic",
					new BitwiseLogic.And(), 
					new BitwiseLogic.Or(), 
					new BitwiseLogic.Nand(), 
					new BitwiseLogic.Nor(), 
					new BitwiseLogic.Xor(),
					new BitwiseLogic.Xnor(),
					new BitwiseLogic.Not()),

			new FunctionSet("1-bit logic",
					new BooleanLogic.And(), 
					new BooleanLogic.Or(), 
					new BooleanLogic.Nand(), 
					new BooleanLogic.Nor(), 
					new BooleanLogic.Xor(),
					new BooleanLogic.Xnor(),
					new BooleanLogic.Not())};

	
	
	private Population population;

	private HashMap<String, Parameter> parameters = new HashMap<String, Parameter>();

	public CGP() {
		createBaseParameters();

		for (int i = 0; i < (int) get("generations"); i++) {
			
			set("currentGen", ((int) get("currentGen")) + 1);
			fitnessFunction.evaluate(population);
			evolutionaryAlgorithm.evolve(population, mutator);
			
			if (evolutionaryAlgorithm.getFittestChromosome().getFitness() >= 3) {
				break;
			}
		}	
	}

	private void createBaseParameters() {
		// make fundamental parameters
		parameters.put("rows", new IntegerParameter(3, "Rows"));
		parameters.put("columns", new IntegerParameter(3, "Columns"));
		parameters.put("inputs", new IntegerParameter(3, "Inputs"));
		parameters.put("outputs", new IntegerParameter(3, "Outputs"));
		parameters.put("popSize", new IntegerParameter(5, "Population"));
		
		parameters.put("nodes", new IntegerParameter(9, "", true, true));
		
		parameters.put("gens", new IntegerParameter(100, "Generations"));
		parameters.put("currentGen", new IntegerParameter(0, "Generation"));
		parameters.put("runs", new IntegerParameter(5, "Runs"));
		
		parameters.put("debug", new BooleanParameter(false, "Debug"));
	}

//	/**
//	 * 
//	 */
//	private void loadModules() {
//		// initialise function set
//		FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction(), new Arithmetic.Multiplication());
//		
//		// initialise utilities
//		Utilities.setResources(new Random(1234), functionSet);
//		
//		// initialise fitness function and truth table
//		TruthTable.setTestCases(new TestCase(new Object[] {2, 5, 4}, new Object[] {1, 10, 15}));
//		fitnessFunction = new TruthTableEvaluator();
//
//		// initialise EA
//		evolutionaryAlgorithm = new StandardEA();
//		mutator = new StandardMutator();
//		
//		// initialise population
//		population = new Population();
//	}
	
	// Parameter methods
	public void register(String key, Parameter value) {
		parameters.put(key, value);
	}
	
	/**
	 * @param key
	 * @return
	 */
	public Object get(String key) {
		return parameters.get(key).getValue();
	}
	
	public void set(String key, Object value) {
		parameters.get(key).setValue(value);
	}
	
	@SuppressWarnings("rawtypes")
	public Property getProperty(String key) {
		return parameters.get(key).valueProperty();
	}
	
	public boolean contains(String key) {
		return parameters.containsKey(key);
	}
	
	// Utility methods
	
}