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
|
package jcgp.tests;
import static org.junit.Assert.*;
import java.util.Iterator;
import java.util.Random;
import jcgp.Parameters;
import jcgp.Utilities;
import jcgp.function.Addition;
import jcgp.function.FunctionSet;
import jcgp.function.Subtraction;
import jcgp.population.Chromosome;
import jcgp.population.Input;
import jcgp.population.Node;
import jcgp.population.Population;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* Tests which cover the behaviour specified for a population.
*
* - A population is a collection of chromosomes. It should be Iterable
* so the chromosomes can be accessed.
* - Iterator.remove() should generate an UnsupportedOperationException
* since chromosomes may not be removed.
* - When constructed with no arguments, it should generate populationSize
* random chromosomes.
* - If a chromosome is passed as an argument to the constructor, it should
* create a population of copies of that chromosome.
*
*
* @author Eduardo Pedroni
*
*/
public class PopulationTests {
private Population population;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
// initialise function set
FunctionSet functionSet = new FunctionSet(new Addition(), new Subtraction());
// initialise utilities
Utilities.setResources(new Random(1234), functionSet);
// initialise parameters
Parameters.setColumns(20);
Parameters.setRows(20);
Parameters.setInputs(2);
Parameters.setOutputs(4);
Parameters.setLevelsBack(1);
Parameters.setMutationRate(10);
Parameters.setTotalGenerations(100);
Parameters.setTotalRuns(5);
Parameters.setPopulationSize(5);
Parameters.setMaxArity(functionSet.getMaxArity());
}
@Before
public void setUp() throws Exception {
population = new Population();
}
@Test
public void iterableTest() {
// check that Population is really Iterable
assertTrue("Population must implement Iterable.", population instanceof Iterable);
// iterate through, check that different chromosomes are returned and no chromosome can be removed
Chromosome comparison = null;
int iterationCount = 0;
boolean exceptionThrown = false;
for (Chromosome chromosome : population) {
iterationCount++;
assertTrue("Same chromosome returned twice.", comparison != chromosome);
comparison = chromosome;
try {
population.iterator().remove();
} catch (UnsupportedOperationException e) {
exceptionThrown = true;
}
assertTrue("Chromosome removed.", exceptionThrown);
exceptionThrown = false;
}
// check that all chromosomes were iterated through
assertTrue("Number of elements in population does not match specified parameter size.",
iterationCount == Parameters.getPopulationSize());
}
@Test
public void defaultPopulationTest() {
// check that the constructor really generates populationSize chromosomes when none is given
int populationCount = 0;
for (Iterator<Chromosome> iterator = population.iterator(); iterator.hasNext();) {
populationCount++;
iterator.next();
}
assertTrue("Incorrect number of chromosomes generated.", populationCount == Parameters.getPopulationSize());
}
@Test
public void preinitialisedChromosomeTest() {
// the original chromosome that will be cloned
Chromosome oc = new Chromosome();
// initialise a population with copies of it
population = new Population(oc);
// check that the chromosomes returned are identical to the one given
int index = 0;
for (Chromosome c : population) {
assertTrue("Incorrect chromosome in population.", c != oc);
index++;
}
// check that the right number of copies was made
assertTrue("Wrong number of chromosomes in population.", index == Parameters.getPopulationSize());
// check that the copies are exact copies
for (Chromosome c : population) {
// check outputs
for (int o = 0; o < Parameters.getOutputs(); o++) {
// check that no cross-references exist between chromosomes
assertTrue("Cloned chromosome contained a reference to a member of the original chromosome.",
c.getOutput(o) != oc.getOutput(o) &&
c.getOutput(o).getSource() != oc.getOutput(o).getSource());
// check that the connections are equivalent
if (c.getOutput(o).getSource() instanceof Input && oc.getOutput(o).getSource() instanceof Input) {
assertTrue("Outputs did not connect to equivalent inputs.",
((Input) c.getOutput(o).getSource()).getIndex() == ((Input) oc.getOutput(o).getSource()).getIndex());
} else if (c.getOutput(o).getSource() instanceof Node && oc.getOutput(o).getSource() instanceof Node) {
assertTrue("Outputs did not connect to equivalent nodes.",
((Node) c.getOutput(o).getSource()).getRow() == ((Node) oc.getOutput(o).getSource()).getRow() &&
((Node) c.getOutput(o).getSource()).getColumn() == ((Node) oc.getOutput(o).getSource()).getColumn());
} else {
fail("Output source types did not match.");
}
}
// check nodes, rows first
for (int row = 0; row < Parameters.getRows(); row++) {
for (int column = 0; column < Parameters.getColumns(); column++) {
// look at each connection
for (int connection = 0; connection < Parameters.getMaxArity(); connection++) {
if (c.getNode(row, column).getConnection(connection) instanceof Input &&
oc.getNode(row, column).getConnection(connection) instanceof Input) {
assertTrue("Nodes did not connect to equivalent inputs.",
((Input) c.getNode(row, column).getConnection(connection)).getIndex() ==
((Input) oc.getNode(row, column).getConnection(connection)).getIndex());
} else if (c.getNode(row, column).getConnection(connection) instanceof Node &&
oc.getNode(row, column).getConnection(connection) instanceof Node) {
assertTrue("Nodes did not connect to equivalent nodes.",
((Node) c.getNode(row, column).getConnection(connection)).getRow() ==
((Node) oc.getNode(row, column).getConnection(connection)).getRow() &&
((Node) c.getNode(row, column).getConnection(connection)).getColumn() ==
((Node) oc.getNode(row, column).getConnection(connection)).getColumn());
} else {
fail("Connection types did not match.");
}
}
}
}
}
}
}
|