package jcgp.tests; import static org.junit.Assert.assertTrue; 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.Input; import jcgp.population.Node; import jcgp.population.Output; import org.junit.Before; import org.junit.Test; /** * * Tests which cover the behaviour specified for a chromosome. * * - The chromosome should be able to return a specified node, input or output. * - It should be able to return a random MutableElement. * - It should be able to return a random Connection * - It should contain a freely modifiable fitness value. * - It should be a good citizen - fully initialised upon instantiation. * - It should feature a clone constructor, which creates a deep copy of a * specified Chromosome object. * - It should be able to return the number of active nodes. * - For truth table evaluations, it should be able to have its inputs set. * * @author Eduardo Pedroni * */ public class ChromosomeTests { Chromosome chromosome; int inputCount = 3, rows = 3, columns = 3, outputCount = 3; @Before public void setUp() 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(columns); Parameters.setRows(rows); Parameters.setInputs(inputCount); Parameters.setOutputs(outputCount); Parameters.setLevelsBack(1); Parameters.setMutationRate(10); Parameters.setTotalGenerations(100); Parameters.setTotalRuns(5); Parameters.setMaxArity(functionSet.getMaxArity()); chromosome = new Chromosome(inputCount, rows, columns, outputCount); } @Test public void test() { // pick arbitrary node, assume that if one node is right, all nodes are right boolean nodeReturn = chromosome.getNode(1, 2).getColumn() == 2 && chromosome.getNode(1, 2).getRow() == 1 && chromosome.getNode(1, 2) instanceof Node; assertTrue("Incorrect node returned.", nodeReturn); // set input values, check that acquired values are correct chromosome.setInputs(4, 5, 6); boolean inputReturn = chromosome.getInput(0).getValue() == 4 && chromosome.getInput(0) instanceof Input && chromosome.getInput(1).getValue() == 5 && chromosome.getInput(0) instanceof Input && chromosome.getInput(2).getValue() == 6 && chromosome.getInput(0) instanceof Input; assertTrue("Incorrect inputs returned.", inputReturn); // connect outputs to inputs, check that calculated outputs return input values for (int i = 0; i < outputCount; i++) { chromosome.getOutput(i).setConnection(chromosome.getInput(0)); } boolean outputReturn = chromosome.getOutput(0).calculate() == 4 && chromosome.getOutput(0) instanceof Output && chromosome.getOutput(1).calculate() == 4 && chromosome.getOutput(0) instanceof Output && chromosome.getOutput(2).calculate() == 4 && chromosome.getOutput(0) instanceof Output; assertTrue("Incorrect output returned.", outputReturn); // get a mutable element, check that it is a Mutable boolean mutableReturn = chromosome.getRandomMutableElement() instanceof Output; // set a fitness value, check if returned value is the same chromosome.setFitness(10); assertTrue("Incorrect fitness returned.", chromosome.getFitness() == 10); } }