diff options
Diffstat (limited to 'src/jcgp/tests/PopulationTests.java')
-rw-r--r-- | src/jcgp/tests/PopulationTests.java | 94 |
1 files changed, 33 insertions, 61 deletions
diff --git a/src/jcgp/tests/PopulationTests.java b/src/jcgp/tests/PopulationTests.java index d646b90..474b8d5 100644 --- a/src/jcgp/tests/PopulationTests.java +++ b/src/jcgp/tests/PopulationTests.java @@ -1,13 +1,7 @@ package jcgp.tests; -import static org.junit.Assert.*; - -import java.util.Random; - -import jcgp.Utilities; -import jcgp.modules.function.Arithmetic; -import jcgp.modules.function.FunctionSet; -import jcgp.parameters.Parameters; +import static org.junit.Assert.assertTrue; +import jcgp.CGP.Resources; import jcgp.population.Chromosome; import jcgp.population.Population; @@ -19,9 +13,7 @@ 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. + * - 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 @@ -34,81 +26,61 @@ import org.junit.Test; public class PopulationTests { private Population population; + private static Resources resources; @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()); + resources = new Resources(); + + +// // initialise function set +// FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction()); +// +// // initialise utilities +// Utilities.setResources(new Random(1234), functionSet); +// +// // initialise parameters +// Resources.setColumns(20); +// Resources.setRows(20); +// Resources.setInputs(2); +// Resources.setOutputs(4); +// Resources.setLevelsBack(1); +// Resources.setMutationRate(10); +// Resources.setTotalGenerations(100); +// Resources.setTotalRuns(5); +// Resources.setPopulationSize(1, 4); +// Resources.setMaxArity(functionSet.getMaxArity()); } @Before public void setUp() throws Exception { - population = new Population(); + population = new Population(resources); } @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++; - } + int chromosomes = 0; while (true) { try { - population.getParent(parent); + population.getChromosome(chromosomes); } catch (IndexOutOfBoundsException e) { break; } - parent++; + chromosomes++; } - 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)); + + assertTrue("Incorrect number of chromosomes generated.", chromosomes == (int) resources.get("popSize")); } @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(); + Chromosome oc = new Chromosome(resources); // initialise a population with a copy of it - population = new Population(oc); + 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.getParent(0).compareTo(oc) && population.getParent(0) != oc); + assertTrue("Incorrect chromosome in population.", population.getChromosome(0).compareTo(oc) && population.getChromosome(0) != oc); } } |