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

import static org.junit.Assert.*;

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.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. Chromosomes should never be removed
 *    via the iterator.
 *  - When constructed with no arguments, it should generate populationSize
 *    random chromosomes.
 *  - If an array of chromosomes is passed as an argument to the constructor,
 *    it should use those chromosomes as 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 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;
		for (Chromosome chromosome : population) {
			iterationCount++;
			assertTrue("Same chromosome returned twice.", comparison != chromosome);
			comparison = chromosome;
			population.iterator().remove();
			assertTrue("Chromosome removed.", comparison == chromosome);			
		}
		// check that all chromosomes were iterated through
		assertTrue("Not enough itearations occurred.", iterationCount == Parameters.getPopulationSize());
	}
	
	@Test
	public void contentsTest() {
		// check that the constructor really generates populationSize chromosomes when none is given
		int populationCount = 0;
		for (Chromosome chromosome : population) {
			populationCount++;
		}
		assertTrue("Incorrect number of chromosomes generated.", populationCount == Parameters.getPopulationSize());
	}

}