From afa484021ba94d12e98da682a9ff69c3837d5dbb Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Fri, 14 Feb 2014 18:13:21 +0000 Subject: Generic data type functionality implemented. All tests were refactored to reflect this, and some chromosome tests were rewritten with more rigorous assertions. --- src/jcgp/tests/ChromosomeTests.java | 124 ++++++++++++++++++++---------------- 1 file changed, 70 insertions(+), 54 deletions(-) (limited to 'src/jcgp/tests/ChromosomeTests.java') diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java index d2407a6..8092d60 100644 --- a/src/jcgp/tests/ChromosomeTests.java +++ b/src/jcgp/tests/ChromosomeTests.java @@ -6,7 +6,6 @@ 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; @@ -31,8 +30,7 @@ import org.junit.Test; * - 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. + * - For truth table evaluations, the output should return a value according to the inputs. * - 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. @@ -40,7 +38,6 @@ import org.junit.Test; * 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! * @@ -60,19 +57,19 @@ public class ChromosomeTests { 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.setTotalGenerations(10); Parameters.setTotalRuns(5); Parameters.setMaxArity(functionSet.getMaxArity()); } @Before public void setUp() throws Exception { + Parameters.setColumns(10); + Parameters.setRows(2); + Parameters.setInputs(2); + Parameters.setOutputs(4); + Parameters.setLevelsBack(10); chromosome = new Chromosome(); } @@ -81,7 +78,6 @@ public class ChromosomeTests { */ @Test public void cloneTest() { - int[] testInputs; // create a clone, check to see if it really is a clone Chromosome clone = new Chromosome(chromosome); @@ -146,29 +142,26 @@ public class ChromosomeTests { } } - // 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 cloning given a known topology + chromosome = createKnownConfiguration(); + clone = new Chromosome(chromosome); + + Integer[] testInputs = new Integer[] {5, 8, 4}; + chromosome.setInputs((Object[]) testInputs); + clone.setInputs((Object[]) 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()); + assertTrue("Incorrect output returned", ((Integer) chromosome.getOutput(i).calculate()) == ((Integer) 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())); + // mutate an output in clone, check that the same node in chromosome produces a different output + clone.getOutput(1).setConnection(clone.getInput(2)); - assertTrue("Mutation affected both nodes in both chromosomes.", node.getValue() != chromosome.getNode(node.getRow(), node.getColumn()).getValue()); + assertTrue("Mutation affected nodes in both chromosomes.", + clone.getOutput(1).calculate() != chromosome.getOutput(1).calculate()); } - /** * */ @@ -217,7 +210,6 @@ public class ChromosomeTests { System.out.println(connectionOutOfRange + " nodes that disrespected levels back were picked."); } - /** * */ @@ -249,27 +241,28 @@ public class ChromosomeTests { */ @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()); - } + chromosome = createKnownConfiguration(); + + chromosome.setInputs(5, 8, 4); + + // with this configuration, the outputs should be 13 and 25. + assertTrue("Incorrect output returned.", (Integer) chromosome.getOutput(0).calculate() == 13); + assertTrue("Incorrect output returned.", (Integer) chromosome.getOutput(1).calculate() == 25); } /** - * @throws ParameterMismatchException + * */ @Test - public void setInputTest() throws ParameterMismatchException { + public void setInputTest() { // set input values, check that acquired values are correct - int[] testInputs = new int[Parameters.getInputs()]; + Integer[] testInputs = new Integer[Parameters.getInputs()]; for (int i = 0; i < Parameters.getInputs(); i++) { testInputs[i] = i * 2 - 3; } - chromosome.setInputs(testInputs); + chromosome.setInputs((Object[]) testInputs); for (int i = 0; i < Parameters.getInputs(); i++) { - assertTrue("Incorrect input returned.", chromosome.getInput(i).getValue() == i * 2 - 3); + assertTrue("Incorrect input returned.", ((Integer) chromosome.getInput(i).getValue()) == i * 2 - 3); } } @@ -294,20 +287,15 @@ public class ChromosomeTests { 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"); + chromosome = createKnownConfiguration(); + + assertTrue("Active node missing from list.", chromosome.getActiveNodes().contains(chromosome.getNode(0, 0))); + assertTrue("Active node missing from list.", chromosome.getActiveNodes().contains(chromosome.getNode(1, 1))); + assertTrue("Active node missing from list.", chromosome.getActiveNodes().contains(chromosome.getNode(1, 2))); + + assertTrue("List has the wrong number of nodes.", chromosome.getActiveNodes().size() == 3); } - + /** * */ @@ -317,12 +305,12 @@ public class ChromosomeTests { 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)); } - + /** * */ @@ -332,9 +320,37 @@ public class ChromosomeTests { 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)); } + /** + * Utility for creating a chromosome of known configuration. + * Topology is 3x3, with 3 inputs and 2 outputs. + * Given inputs 5, 8 and 4 outputs should be 13 and 25. + * + * Active nodes (r, c): [0, 0], [1, 1], [1, 2] + * + * @return the configured chromosome + */ + private Chromosome createKnownConfiguration() { + // with a small topology, build a chromosome of known connections and check outputs + Parameters.setColumns(3); + Parameters.setRows(3); + Parameters.setInputs(3); + Parameters.setOutputs(2); + Parameters.setLevelsBack(3); + + Chromosome c = new Chromosome(); + + c.getNode(0, 0).initialise(Utilities.getFunction(0), c.getInput(0), c.getInput(1)); + c.getNode(1, 1).initialise(Utilities.getFunction(0), c.getNode(0, 0), c.getInput(1)); + c.getNode(1, 2).initialise(Utilities.getFunction(0), c.getNode(1, 1), c.getInput(2)); + + c.getOutput(0).setConnection(c.getNode(0, 0)); + c.getOutput(1).setConnection(c.getNode(1, 2)); + + return c; + } } -- cgit v1.2.3