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
|
package jcgp.tests;
import static org.junit.Assert.*;
import java.util.Random;
import jcgp.Parameters;
import jcgp.Utilities;
import jcgp.function.Arithmetic;
import jcgp.function.FunctionSet;
import jcgp.population.Chromosome;
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 should be able to return parents and offspring separately.
* - It should be possible to iterate through all the chromosomes in a population
* with one indexing system - parents then offspring.
* - When constructed with no arguments, it should generate populationSize
* random chromosomes, distributed according to the EA parameters.
* - If one or more chromosomes are passed into the constructor, it should use them
* as parents to create the rest of the population.
*
*
* @author Eduardo Pedroni
*
*/
public class PopulationTests {
private Population population;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
// initialise function set
FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.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(1, 4);
Parameters.setMaxArity(functionSet.getMaxArity());
}
@Before
public void setUp() throws Exception {
population = new Population();
}
@Test
public void defaultPopulationTest() {
// check that the constructor really generates populationSize chromosomes when none is given
int offspring = 0, parent = 0;
while (true) {
try {
population.getOffspring(offspring);
} catch (IndexOutOfBoundsException e) {
break;
}
offspring++;
}
while (true) {
try {
population.getParent(parent);
} catch (IndexOutOfBoundsException e) {
break;
}
parent++;
}
assertTrue("Incorrect number of chromosomes generated.", offspring + parent == Parameters.getPopulationSize());
}
@Test
public void offspringParentTest() {
// the first parent should not be the same as the first offspring
assertTrue("Same chromosome returned as parent and offspring", population.getOffspring(0) != population.getParent(0));
}
@Test
public void singleIndexTest() {
// assuming 1+4
// the first chromosome should be the first (and only) parent
assertTrue("Incorrect chromosome returned.", population.getChromosome(0) == population.getParent(0));
// the next 4 chromosomes should be the offspring, in order
for (int i = 0; i < Parameters.getOffspringCount(); i++) {
assertTrue("Incorrect chromosome returned.", population.getChromosome(i + 1) == population.getOffspring(i));
}
}
@Test
public void preinitialisedChromosomeTest() {
// the original chromosome that will be cloned
Chromosome oc = new Chromosome();
// initialise a population with a copy of it
population = new Population(oc);
// check that the first parent chromosome is identical to, but not the same instance as, the one given
assertTrue("Incorrect chromosome in population.", population.getParent(0).compareTo(oc) && population.getParent(0) != oc);
}
}
|