aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/tests/ChromosomeTests.java
blob: c3268059092623ec0d8b44015792e37d3e9c4909 (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
package jcgp.backend.tests;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Connection;
import jcgp.backend.population.Input;
import jcgp.backend.population.MutableElement;
import jcgp.backend.population.Node;
import jcgp.backend.population.Output;
import jcgp.backend.resources.ModifiableResources;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * 
 * Tests which cover the behaviour specified for a chromosome.
 * 
 *  - The chromosome should be able to return a specified node, input or output.
 *  - It should be able to return a random MutableElement.
 *  - It should be able to return a random allowed connection given a column.
 *  - It should be able to return a random connection.
 *  - It should contain a freely modifiable fitness value.
 *  - For truth table evaluations, it should be able to have its inputs set.
 *  - For truth table evaluations, the output should return a value according to the inputs.
 *  - It should feature a copy method, which creates a deep copy of a specified Chromosome object.
 *  - It should be able to return a list of active nodes.
 *  - It should contain a method to evaluate whether a given chromosome is identical
 *    to it.
 *  - Same as above, but only looking at the active portion of a chromosome.
 *  
 *  
 *  WARNING: changing parameters may cause the tests to incorrectly fail!
 * 
 * @author Eduardo Pedroni
 *
 */
public class ChromosomeTests {

	private Chromosome chromosome;
	private static ModifiableResources resources;

	@BeforeClass
	public static void setUpBeforeClass() {
		resources = new ModifiableResources();
		resources.setFunctionSet(new TestFunctionSet());
	}

	@Before
	public void setUp() throws Exception {
		chromosome = new Chromosome(resources);
	}

	/**
	 * 
	 */
	@Test
	public void cloneTest() {
		// create a clone, check to see if it really is a clone
		Chromosome clone = new Chromosome(chromosome);

		// compare all elements, one by one
		// check outputs
		for (int o = 0; o < resources.outputs(); o++) {
			// check that no cross-references exist between chromosomes
			assertTrue("Cloned chromosome contains a reference to a member of the original chromosome.",
					clone.getOutput(o) != chromosome.getOutput(o) && 
					clone.getOutput(o).getSource() != chromosome.getOutput(o).getSource());
			// check that the connections are equivalent
			if (clone.getOutput(o).getSource() instanceof Input && chromosome.getOutput(o).getSource() instanceof Input) {
				assertTrue("Outputs did not connect to equivalent inputs.", 
						((Input) clone.getOutput(o).getSource()).getIndex() == ((Input) chromosome.getOutput(o).getSource()).getIndex());
			} else if (clone.getOutput(o).getSource() instanceof Node && chromosome.getOutput(o).getSource() instanceof Node) {
				assertTrue("Outputs did not connect to equivalent nodes.", 
						((Node) clone.getOutput(o).getSource()).getRow() == ((Node) chromosome.getOutput(o).getSource()).getRow() && 
						((Node) clone.getOutput(o).getSource()).getColumn() == ((Node) chromosome.getOutput(o).getSource()).getColumn());
			} else {
				fail("Output source types did not match.");
			}
		}
		// check nodes, rows first
		for (int row = 0; row < resources.rows(); row++) {
			for (int column = 0; column < resources.columns(); column++) {
				// check that nodes are not pointers to the same instance
				assertTrue("Both chromosomes contain a reference to the same node.", clone.getNode(row, column) != chromosome.getNode(row, column));
				// check that both nodes reference their own position in the grid correctly
				assertTrue("Equivalent nodes self-reference differently.", clone.getNode(row, column).getRow() == chromosome.getNode(row, column).getRow() &&
						clone.getNode(row, column).getColumn() == chromosome.getNode(row, column).getColumn());
				// check that the two nodes have the same function
				assertTrue("Equivalent nodes have different functions.", clone.getNode(row, column).getFunction() == chromosome.getNode(row, column).getFunction());

				// compare each connection
				for (int connection = 0; connection < resources.arity(); connection++) {
					// first look at whether they are actually the same instance
					assertTrue("Nodes are connected to the same connection instance.", 
							clone.getNode(row, column).getConnection(connection) !=	chromosome.getNode(row, column).getConnection(connection));

					// if the connections aren't the same instance, check that their addresses are the same
					if (clone.getNode(row, column).getConnection(connection) instanceof Input && 
							chromosome.getNode(row, column).getConnection(connection) instanceof Input) {

						assertTrue("Nodes did not connect to equivalent inputs.", 
								((Input) clone.getNode(row, column).getConnection(connection)).getIndex() ==
								((Input) chromosome.getNode(row, column).getConnection(connection)).getIndex());

					} else if (clone.getNode(row, column).getConnection(connection) instanceof Node && 
							chromosome.getNode(row, column).getConnection(connection) instanceof Node) {

						assertTrue("Nodes did not connect to equivalent nodes.", 
								((Node) clone.getNode(row, column).getConnection(connection)).getRow() ==
								((Node) chromosome.getNode(row, column).getConnection(connection)).getRow() &&

								((Node) clone.getNode(row, column).getConnection(connection)).getColumn() ==
								((Node) chromosome.getNode(row, column).getConnection(connection)).getColumn());

					} else {
						fail("Connection types did not match.");
					}
				}
			}
		}

		// check cloning given a known topology
		chromosome = createKnownConfiguration();
		clone = new Chromosome(chromosome);
		
		Integer[] testInputs = new Integer[] {5, 8, 4};
		chromosome.setInputs((Object[]) testInputs);
		clone.setInputs((Object[]) testInputs);

		// check that both chromosomes have the same outputs
		for (int i = 0; i < resources.outputs(); i++) {
			assertTrue("Incorrect output returned", ((Integer) chromosome.getOutput(i).calculate()) == ((Integer) clone.getOutput(i).calculate()));
		}

		// mutate an output in clone, check that the same node in chromosome produces a different output
		clone.getOutput(1).setConnection(resources.getRandomInt(resources.arity()), clone.getInput(2));

		assertTrue("Mutation affected nodes in both chromosomes.",
				clone.getOutput(1).calculate() != chromosome.getOutput(1).calculate());

	}
	/**
	 * 
	 */
	@Test
	public void fitnessTest() {
		// set a fitness value, check if returned value is the same
		chromosome.setFitness(10);
		assertTrue("Incorrect fitness returned.", chromosome.getFitness() == 10);
	}

	/**
	 * 
	 */
	@Test
	public void randomConnectionTest() {
		// get random connections with column 0, check that they are all inputs
		for (int i = 0; i < 10000; i++) {
			boolean connectionReturn = chromosome.getRandomConnection(0) instanceof Input;
			assertTrue("Connection is not an input.", connectionReturn);
		}

		// get random connections with the last column as reference, check that they're all within range
		int connectionNodes = 0, connectionOutOfRange = 0, connectionInputs = 0, connectionPicks = 100000;
		int chosenColumn = resources.columns() - 1;
		for (int i = 0; i < connectionPicks; i++) {
			Connection c = chromosome.getRandomConnection(chosenColumn);
			if (c instanceof Node) {
				connectionNodes++;
				if (((Node) c).getColumn() >= chosenColumn) {
					connectionOutOfRange++;
				}
				assertTrue("Connection is not allowed : " + ((Node) c).getColumn(), ((Node) c).getColumn() < chosenColumn && ((Node) c).getColumn() < chosenColumn);
			} else if (c instanceof Input) {
				connectionInputs++;
			} else {
				fail("Return is neither Node nor Input.");

			}	
		}
		System.out.println("Out of " + connectionPicks + " connections picked from " + ((chosenColumn >= resources.levelsBack()) ? resources.levelsBack() : chosenColumn) * resources.rows() +
				" allowed nodes and " + resources.inputs() + " inputs, " + connectionNodes + " were nodes and " + connectionInputs + " were inputs.");

		System.out.println("Node/input ratio: " + (chosenColumn >= resources.levelsBack() ? resources.levelsBack() : chosenColumn) * (double) resources.rows() / (double) resources.inputs() +
				", picked ratio: " + (double) connectionNodes / (double) connectionInputs);

		System.out.println(connectionOutOfRange + " nodes that disrespected levels back were picked.");
	}
	/**
	 * 
	 */
	@Test
	public void randomMutableTest() {
		// get mutable elements, check Node to Output ratio
		int mutablePicks = 100000;
		int mutableNodes = 0, mutableOutputs = 0;
		for (int i = 0; i < mutablePicks; i++) {
			MutableElement m = chromosome.getRandomMutableElement();

			if (m instanceof Node) {
				mutableNodes++;
			} else if (m instanceof Output) {
				mutableOutputs++;
			} else {
				fail("Return is neither Node nor Output.");
			}
		}
		System.out.println("Out of " + mutablePicks + " mutable elements picked from " + resources.nodes() +
				" nodes and " + resources.outputs() + " outputs, " + mutableNodes + " were nodes and " +
				mutableOutputs + " were outputs.");
		System.out.println("Node/output ratio: " + (double) resources.nodes() / (double) resources.outputs() +
				", picked ratio: " + (double) mutableNodes / (double) mutableOutputs + "\n");
	}

	/**
	 * 
	 */
	@Test
	public void getOutputsTest() {
		chromosome = createKnownConfiguration();

		chromosome.setInputs(5, 8, 4);

		Integer output0 = (Integer) chromosome.getOutput(0).calculate();
		Integer output1 = (Integer) chromosome.getOutput(1).calculate();
		
		// with this configuration, the outputs should be 13 and 25.
		assertTrue("Incorrect output returned: " + output0, output0 == 13.0);
		assertTrue("Incorrect output returned: " + output1, output1 == 25.0);
	}

	/**
	 * 
	 */
	@Test
	public void setInputTest() {
		// set input values, check that acquired values are correct
		Integer[] testInputs = new Integer[resources.inputs()];
		for (int i = 0; i < resources.inputs(); i++) {
			testInputs[i] = i * 2 - 3;
		}
		chromosome.setInputs((Object[]) testInputs);
		for (int i = 0; i < resources.inputs(); i++) {
			assertTrue("Incorrect input returned.", ((Integer) chromosome.getInput(i).getValue()) == i * 2 - 3);
		}
	}

	/**
	 * 
	 */
	@Test
	public void getNodeTest() {
		// get all nodes one by one, check that they are all correct
		for (int r = 0; r < resources.rows(); r++) {
			for (int c = 0; c < resources.columns(); c++) {
				assertTrue("Incorrect node returned.", chromosome.getNode(r, c).getColumn() == c &&
						chromosome.getNode(r, c).getRow() == r);
			}
		}
	}

	/**
	 * 
	 */
	@Test
	public void activeNodeTest() {
		// active node detection happens recursively, the user only calls a single method
		// set connections to a known configuration
		chromosome = createKnownConfiguration();
		
		assertTrue("Active node missing from list.", chromosome.getActiveNodes().contains(chromosome.getNode(0, 0)));
		assertTrue("Active node missing from list.", chromosome.getActiveNodes().contains(chromosome.getNode(1, 1)));
		assertTrue("Active node missing from list.", chromosome.getActiveNodes().contains(chromosome.getNode(1, 2)));
		
		chromosome.printNodes();
		
		assertTrue("List has the wrong number of nodes: " + chromosome.getActiveNodes(), chromosome.getActiveNodes().size() == 3);
	}

	/**
	 * 
	 */
	@Test
	public void compareActiveTest() {
		// create a clone of the chromosome, compare active nodes - should return true
		Chromosome c = new Chromosome(chromosome);
		assertTrue("Active nodes did not match.", chromosome.compareActiveGenesTo(c));
		assertTrue("Symmetry not obeyed.", c.compareActiveGenesTo(chromosome));

		// create a new random chromosome, this time they should not match
		c = new Chromosome(resources);
		assertTrue("Active nodes did match.", !chromosome.compareActiveGenesTo(c));
	}

	/**
	 * 
	 */
	@Test
	public void compareTest() {
		// create a clone of the chromosome, compare - should return true
		Chromosome c = new Chromosome(chromosome);
		assertTrue("Chromosomes did not match.", chromosome.compareGenesTo(c));
		assertTrue("Symmetry not obeyed.", c.compareGenesTo(chromosome));

		// create a new random chromosome, this time they should not match
		c = new Chromosome(resources);
		assertTrue("Chromosomes did match.", !chromosome.compareGenesTo(c));
	}
	/**
	 * Utility for creating a chromosome of known configuration.
	 * Topology is 3x3, with 3 inputs and 2 outputs.
	 * Given inputs 5, 8 and 4 outputs should be 13 and 25.
	 * 
	 * Active nodes (r, c): [0, 0], [1, 1], [1, 2]
	 * 
	 * @return the configured chromosome
	 */
	private Chromosome createKnownConfiguration() {
		// with a small topology, build a chromosome of known connections and check outputs
		resources.setColumns(3);
		resources.setRows(3);
		resources.setInputs(3);
		resources.setOutputs(2);
		resources.setLevelsBack(3);

		Chromosome c = new Chromosome(resources);

		c.getNode(0, 0).initialise(resources.getFunction(0), c.getInput(0), c.getInput(1));
		c.getNode(1, 1).initialise(resources.getFunction(0), c.getNode(0, 0), c.getInput(1));
		c.getNode(1, 2).initialise(resources.getFunction(0), c.getNode(1, 1), c.getInput(2));

		c.getOutput(0).setConnection(0, c.getNode(0, 0));
		c.getOutput(1).setConnection(0, c.getNode(1, 2));
		
		return c;
	}
}