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); } }