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

import static org.junit.Assert.assertTrue;
import jcgp.backend.function.SymbolicRegressionFunctions;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Population;
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 population.
 * 
 *  - It should be possible to iterate through all the chromosomes in a population.
 *  - 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;
	private static ModifiableResources resources;

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

	@Before
	public void setUp() throws Exception {
		population = new Population(resources);
	}

	@Test
	public void defaultPopulationTest() {
		// check that the constructor really generates populationSize chromosomes when none is given
		int chromosomes = 0;
		while (true) {
			try {
				population.get(chromosomes);
			} catch (IndexOutOfBoundsException e) {
				break;
			}
			chromosomes++;
		}

		assertTrue("Incorrect number of chromosomes generated.", chromosomes == resources.populationSize());
	}
	
	@Test
	public void preinitialisedChromosomeTest() {
		// the original chromosome that will be cloned
		Chromosome oc = new Chromosome(resources);

		// initialise a population with a copy of it
		population = new Population(oc, resources);
		// check that the first parent chromosome is identical to, but not the same instance as, the one given
		assertTrue("Incorrect chromosome in population.", population.get(0).compareGenesTo(oc) && population.get(0) != oc);
	}
}