package jcgp.tests; import static org.junit.Assert.*; import java.util.Random; import jcgp.Parameters; import jcgp.Utilities; import jcgp.fitness.ParameterMismatchException; import jcgp.function.Addition; import jcgp.function.FunctionSet; import jcgp.function.Subtraction; import jcgp.population.Chromosome; import jcgp.population.Connection; import jcgp.population.Input; import jcgp.population.MutableElement; import jcgp.population.Node; import jcgp.population.Output; import org.junit.Before; import org.junit.BeforeClass; 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 allowed connection given a column. * - It should be able to return a random connection. * - It should contain a freely modifiable fitness value. * - For truth table evaluations, it should be able to have its inputs set. * - For truth table evaluations, the output should return a value according to what * it is set to. * - It should feature a clone constructor, which creates a deep copy of a * specified Chromosome object. * - It should be able to return a list of active nodes. * - It should contain a method to evaluate whether a given chromosome is identical * to it. * - Same as above, but only looking at the active portion of a chromosome. * * TODO: bashing (strange value ranges, etc) * * WARNING: changing parameters may cause the tests to incorrectly fail! * * @author Eduardo Pedroni * */ public class ChromosomeTests { private Chromosome chromosome; @BeforeClass public static void setUpBeforeClass() { // initialise function set FunctionSet functionSet = new FunctionSet(new Addition(), new Subtraction()); // initialise utilities Utilities.setResources(new Random(1234), functionSet); // initialise parameters Parameters.setColumns(30); Parameters.setRows(20); Parameters.setInputs(2); Parameters.setOutputs(4); Parameters.setLevelsBack(1); Parameters.setMutationRate(10); Parameters.setTotalGenerations(100); Parameters.setTotalRuns(5); Parameters.setMaxArity(functionSet.getMaxArity()); } @Before public void setUp() throws Exception { chromosome = new Chromosome(); } /** * */ @Test public void cloneTest() { int[] testInputs; // create a clone, check to see if it really is a clone Chromosome clone = new Chromosome(chromosome); // compare all elements, one by one // check outputs for (int o = 0; o < Parameters.getOutputs(); o++) { // check that no cross-references exist between chromosomes assertTrue("Cloned chromosome contains a reference to a member of the original chromosome.", clone.getOutput(o) != chromosome.getOutput(o) && clone.getOutput(o).getSource() != chromosome.getOutput(o).getSource()); // check that the connections are equivalent if (clone.getOutput(o).getSource() instanceof Input && chromosome.getOutput(o).getSource() instanceof Input) { assertTrue("Outputs did not connect to equivalent inputs.", ((Input) clone.getOutput(o).getSource()).getIndex() == ((Input) chromosome.getOutput(o).getSource()).getIndex()); } else if (clone.getOutput(o).getSource() instanceof Node && chromosome.getOutput(o).getSource() instanceof Node) { assertTrue("Outputs did not connect to equivalent nodes.", ((Node) clone.getOutput(o).getSource()).getRow() == ((Node) chromosome.getOutput(o).getSource()).getRow() && ((Node) clone.getOutput(o).getSource()).getColumn() == ((Node) chromosome.getOutput(o).getSource()).getColumn()); } else { fail("Output source types did not match."); } } // check nodes, rows first for (int row = 0; row < Parameters.getRows(); row++) { for (int column = 0; column < Parameters.getColumns(); column++) { // check that nodes are not pointers to the same instance assertTrue("Both chromosomes contain a reference to the same node.", clone.getNode(row, column) != chromosome.getNode(row, column)); // check that both nodes reference their own position in the grid correctly assertTrue("Equivalent nodes self-reference differently.", clone.getNode(row, column).getRow() == chromosome.getNode(row, column).getRow() && clone.getNode(row, column).getColumn() == chromosome.getNode(row, column).getColumn()); // check that the two nodes have the same function assertTrue("Equivalent nodes have different functions.", clone.getNode(row, column).getFunction() == chromosome.getNode(row, column).getFunction()); // compare each connection for (int connection = 0; connection < Parameters.getMaxArity(); connection++) { // first look at whether they are actually the same instance assertTrue("Nodes are connected to the same connection instance.", clone.getNode(row, column).getConnection(connection) != chromosome.getNode(row, column).getConnection(connection)); // if the connections aren't the same instance, check that their addresses are the same if (clone.getNode(row, column).getConnection(connection) instanceof Input && chromosome.getNode(row, column).getConnection(connection) instanceof Input) { assertTrue("Nodes did not connect to equivalent inputs.", ((Input) clone.getNode(row, column).getConnection(connection)).getIndex() == ((Input) chromosome.getNode(row, column).getConnection(connection)).getIndex()); } else if (clone.getNode(row, column).getConnection(connection) instanceof Node && chromosome.getNode(row, column).getConnection(connection) instanceof Node) { assertTrue("Nodes did not connect to equivalent nodes.", ((Node) clone.getNode(row, column).getConnection(connection)).getRow() == ((Node) chromosome.getNode(row, column).getConnection(connection)).getRow() && ((Node) clone.getNode(row, column).getConnection(connection)).getColumn() == ((Node) chromosome.getNode(row, column).getConnection(connection)).getColumn()); } else { fail("Connection types did not match."); } } } } // set input values testInputs = new int[Parameters.getInputs()]; for (int i = 0; i < Parameters.getInputs(); i++) { testInputs[i] = (i + 1) * 2 - 3; } chromosome.setInputs(testInputs); clone.setInputs(testInputs); // check that both chromosomes have the same outputs for (int i = 0; i < Parameters.getOutputs(); i++) { assertTrue("Incorrect output returned.", chromosome.getOutput(i).calculate() == clone.getOutput(i).calculate()); } // mutate an active node in clone, check that the same node in chromosome produces a different output // NOTE: given a small grid this mutation may actually pick the same connection (causing no effective change) and therefore the test would fail. Node node = clone.getActiveNodes().get(Utilities.getRandomInt(clone.getActiveNodes().size())); node.setConnection(clone.getRandomConnection(node.getColumn())); assertTrue("Mutation affected both nodes in both chromosomes.", node.getValue() != chromosome.getNode(node.getRow(), node.getColumn()).getValue()); } /** * */ @Test public void fitnessTest() { // set a fitness value, check if returned value is the same chromosome.setFitness(10); assertTrue("Incorrect fitness returned.", chromosome.getFitness() == 10); } /** * */ @Test public void randomConnectionTest() { // get random connections with column 0, check that they are all inputs for (int i = 0; i < 10000; i++) { boolean connectionReturn = chromosome.getRandomConnection(0) instanceof Input; assertTrue("Connection is not an input.", connectionReturn); } // get random connections with column 1 // they should all be nodes, and their columns should be within range int connectionNodes = 0, connectionOutOfRange = 0, connectionInputs = 0, connectionPicks = 100000; int chosenColumn = 1; for (int i = 0; i < connectionPicks; i++) { Connection c = chromosome.getRandomConnection(chosenColumn); if (c instanceof Node) { connectionNodes++; if (((Node) c).getColumn() >= chosenColumn) { connectionOutOfRange++; } assertTrue("Connection is not allowed : " + ((Node) c).getColumn(), ((Node) c).getColumn() < chosenColumn && ((Node) c).getColumn() < chosenColumn); } else if (c instanceof Input) { connectionInputs++; } else { fail("Return is neither Node nor Input."); } } System.out.println("Out of " + connectionPicks + " connections picked from " + ((chosenColumn >= Parameters.getLevelsBack()) ? Parameters.getLevelsBack() : chosenColumn) * Parameters.getRows() + " allowed nodes and " + Parameters.getInputs() + " inputs, " + connectionNodes + " were nodes and " + connectionInputs + " were inputs."); System.out.println("Node/input ratio: " + ((double) ((chosenColumn >= Parameters.getLevelsBack()) ? Parameters.getLevelsBack() : chosenColumn) * Parameters.getRows()) / (double) Parameters.getInputs() + ", picked ratio: " + (double) connectionNodes / (double) connectionInputs); System.out.println(connectionOutOfRange + " nodes that disrespected levels back were picked."); } /** * */ @Test public void randomMutableTest() { // get mutable elements, check Node to Output ratio int mutablePicks = 100000; int mutableNodes = 0, mutableOutputs = 0; for (int i = 0; i < mutablePicks; i++) { MutableElement m = chromosome.getRandomMutableElement(); if (m instanceof Node) { mutableNodes++; } else if (m instanceof Output) { mutableOutputs++; } else { fail("Return is neither Node nor Output."); } } System.out.println("Out of " + mutablePicks + " mutable elements picked from " + Parameters.getNodeCount() + " nodes and " + Parameters.getOutputs() + " outputs, " + mutableNodes + " were nodes and " + mutableOutputs + " were outputs."); System.out.println("Node/output ratio: " + (double) Parameters.getNodeCount() / (double) Parameters.getOutputs() + ", picked ratio: " + (double) mutableNodes / (double) mutableOutputs + "\n"); } /** * */ @Test public void getOutputsTest() { // connect outputs to inputs, check that calculated outputs return input values for (int i = 0; i < Parameters.getOutputs(); i++) { chromosome.getOutput(i).setConnection(chromosome.getInput(i % Parameters.getInputs())); assertTrue("Incorrect output returned.", chromosome.getOutput(i).calculate() == chromosome.getInput(i % Parameters.getInputs()).getValue()); } } /** * @throws ParameterMismatchException */ @Test public void setInputTest() throws ParameterMismatchException { // set input values, check that acquired values are correct int[] testInputs = new int[Parameters.getInputs()]; for (int i = 0; i < Parameters.getInputs(); i++) { testInputs[i] = i * 2 - 3; } chromosome.setInputs(testInputs); for (int i = 0; i < Parameters.getInputs(); i++) { assertTrue("Incorrect input returned.", chromosome.getInput(i).getValue() == i * 2 - 3); } } /** * */ @Test public void getNodeTest() { // get all nodes one by one, check that they are all correct for (int r = 0; r < Parameters.getRows(); r++) { for (int c = 0; c < Parameters.getColumns(); c++) { assertTrue("Incorrect node returned.", chromosome.getNode(r, c).getColumn() == c && chromosome.getNode(r, c).getRow() == r); } } } /** * */ @Test public void activeNodeTest() { // active node detection happens recursively, the user only calls a single method // set connections to a known configuration for (int i = 0; i < Parameters.getOutputs(); i++) { chromosome.getOutput(i).setConnection(chromosome.getNode(0, 0)); } chromosome.getNode(0, 0).setConnection(chromosome.getInput(0)); assertTrue("Active node not in list.", chromosome.getActiveNodes().contains(chromosome.getNode(0, 0))); // change outputs, print list chromosome.getOutput(0).setConnection(chromosome.getNode(0, Parameters.getColumns() - 1)); System.out.println("Active connections: " + chromosome.getActiveNodes().toString() + "\n"); } /** * */ @Test public void compareActiveTest() { // create a clone of the chromosome, compare active nodes - should return true Chromosome c = new Chromosome(chromosome); assertTrue("Active nodes did not match.", chromosome.compareActiveTo(c)); assertTrue("Symmetry not obeyed.", c.compareActiveTo(chromosome)); // create a new random chromosome, this time they should not match c = new Chromosome(); assertTrue("Active nodes did match.", !chromosome.compareActiveTo(c)); } /** * */ @Test public void compareTest() { // create a clone of the chromosome, compare - should return true Chromosome c = new Chromosome(chromosome); assertTrue("Chromosomes did not match.", chromosome.compareTo(c)); assertTrue("Symmetry not obeyed.", c.compareTo(chromosome)); // create a new random chromosome, this time they should not match c = new Chromosome(); assertTrue("Chromosomes did match.", !chromosome.compareTo(c)); } }