From 3326c58f4d2d7e8c77738277dcd093aa864ad2a5 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Thu, 13 Feb 2014 17:43:59 +0000 Subject: Finished population tests, now thinking about methods to compare chromosomes --- src/jcgp/tests/ChromosomeTests.java | 55 ++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 7 deletions(-) (limited to 'src/jcgp/tests/ChromosomeTests.java') diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java index cb2cb95..ff1de18 100644 --- a/src/jcgp/tests/ChromosomeTests.java +++ b/src/jcgp/tests/ChromosomeTests.java @@ -57,7 +57,7 @@ public class ChromosomeTests { Utilities.setResources(new Random(1234), functionSet); // initialise parameters - Parameters.setColumns(1); + Parameters.setColumns(30); Parameters.setRows(20); Parameters.setInputs(2); Parameters.setOutputs(4); @@ -90,13 +90,54 @@ public class ChromosomeTests { chromosome.setInputs(testInputs); clone.setInputs(testInputs); - // connect outputs to inputs, check that the outputs match - for (int i = 0; i < Parameters.getOutputs(); i++) { - chromosome.getOutput(i).setConnection(chromosome.getInput(i % Parameters.getInputs())); - clone.getOutput(i).setConnection(clone.getInput(i % Parameters.getInputs())); - assertTrue("Incorrect output returned.", chromosome.getOutput(i).calculate() == - clone.getOutput(i).calculate()); + // 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 contained a reference to a member of the original chromosome.", + c.getOutput(o) != oc.getOutput(o) && + c.getOutput(o).getSource() != oc.getOutput(o).getSource()); + // check that the connections are equivalent + if (c.getOutput(o).getSource() instanceof Input && oc.getOutput(o).getSource() instanceof Input) { + assertTrue("Outputs did not connect to equivalent inputs.", + ((Input) c.getOutput(o).getSource()).getIndex() == ((Input) oc.getOutput(o).getSource()).getIndex()); + } else if (c.getOutput(o).getSource() instanceof Node && oc.getOutput(o).getSource() instanceof Node) { + assertTrue("Outputs did not connect to equivalent nodes.", + ((Node) c.getOutput(o).getSource()).getRow() == ((Node) oc.getOutput(o).getSource()).getRow() && + ((Node) c.getOutput(o).getSource()).getColumn() == ((Node) oc.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++) { + // look at each connection + for (int connection = 0; connection < Parameters.getMaxArity(); connection++) { + if (c.getNode(row, column).getConnection(connection) instanceof Input && + oc.getNode(row, column).getConnection(connection) instanceof Input) { + + assertTrue("Nodes did not connect to equivalent inputs.", + ((Input) c.getNode(row, column).getConnection(connection)).getIndex() == + ((Input) oc.getNode(row, column).getConnection(connection)).getIndex()); + + } else if (c.getNode(row, column).getConnection(connection) instanceof Node && + oc.getNode(row, column).getConnection(connection) instanceof Node) { + + assertTrue("Nodes did not connect to equivalent nodes.", + ((Node) c.getNode(row, column).getConnection(connection)).getRow() == + ((Node) oc.getNode(row, column).getConnection(connection)).getRow() && + + ((Node) c.getNode(row, column).getConnection(connection)).getColumn() == + ((Node) oc.getNode(row, column).getConnection(connection)).getColumn()); + + } else { + fail("Connection types did not match."); + } + } + } + } + // change clone inputs, outputs should no longer match testInputs = new int[Parameters.getInputs()]; -- cgit v1.2.3