From 6419b69faeb4736db1ccb50cfa5a030f9aa818aa Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Thu, 13 Feb 2014 22:41:26 +0000 Subject: Added methods in Chromosome to compare active and all nodes. Associated tests also written. --- src/jcgp/tests/ChromosomeTests.java | 113 +++++++++++++++++++++++++----------- src/jcgp/tests/NodeTests.java | 28 +-------- src/jcgp/tests/OutputTests.java | 11 ---- 3 files changed, 81 insertions(+), 71 deletions(-) (limited to 'src/jcgp/tests') diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java index ff1de18..d2407a6 100644 --- a/src/jcgp/tests/ChromosomeTests.java +++ b/src/jcgp/tests/ChromosomeTests.java @@ -36,6 +36,9 @@ import org.junit.Test; * - 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) * @@ -82,29 +85,21 @@ public class ChromosomeTests { // create a clone, check to see if it really is a clone Chromosome clone = new Chromosome(chromosome); - // set input values - testInputs = new int[Parameters.getInputs()]; - for (int i = 0; i < Parameters.getInputs(); i++) { - testInputs[i] = i * 2 - 3; - } - chromosome.setInputs(testInputs); - clone.setInputs(testInputs); - // 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()); + 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 (c.getOutput(o).getSource() instanceof Input && oc.getOutput(o).getSource() instanceof Input) { + if (clone.getOutput(o).getSource() instanceof Input && chromosome.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) { + ((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) c.getOutput(o).getSource()).getRow() == ((Node) oc.getOutput(o).getSource()).getRow() && - ((Node) c.getOutput(o).getSource()).getColumn() == ((Node) oc.getOutput(o).getSource()).getColumn()); + ((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."); } @@ -112,24 +107,37 @@ public class ChromosomeTests { // check nodes, rows first for (int row = 0; row < Parameters.getRows(); row++) { for (int column = 0; column < Parameters.getColumns(); column++) { - // look at each connection + // 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++) { - if (c.getNode(row, column).getConnection(connection) instanceof Input && - oc.getNode(row, column).getConnection(connection) instanceof Input) { + // 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) c.getNode(row, column).getConnection(connection)).getIndex() == - ((Input) oc.getNode(row, column).getConnection(connection)).getIndex()); + ((Input) clone.getNode(row, column).getConnection(connection)).getIndex() == + ((Input) chromosome.getNode(row, column).getConnection(connection)).getIndex()); - } else if (c.getNode(row, column).getConnection(connection) instanceof Node && - oc.getNode(row, column).getConnection(connection) instanceof Node) { + } 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) c.getNode(row, column).getConnection(connection)).getRow() == - ((Node) oc.getNode(row, column).getConnection(connection)).getRow() && + ((Node) clone.getNode(row, column).getConnection(connection)).getRow() == + ((Node) chromosome.getNode(row, column).getConnection(connection)).getRow() && - ((Node) c.getNode(row, column).getConnection(connection)).getColumn() == - ((Node) oc.getNode(row, column).getConnection(connection)).getColumn()); + ((Node) clone.getNode(row, column).getConnection(connection)).getColumn() == + ((Node) chromosome.getNode(row, column).getConnection(connection)).getColumn()); } else { fail("Connection types did not match."); @@ -138,17 +146,27 @@ public class ChromosomeTests { } } - - // change clone inputs, outputs should no longer match + // set input values testInputs = new int[Parameters.getInputs()]; for (int i = 0; i < Parameters.getInputs(); i++) { - testInputs[i] = i * 2; + 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() != + 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()); + } /** @@ -282,12 +300,41 @@ public class ChromosomeTests { chromosome.getNode(0, 0).setConnection(chromosome.getInput(0)); - assertTrue("Active connection not in list.", chromosome.getActiveNodes().contains(chromosome.getInput(0))); - assertTrue("Active connection not in list.", chromosome.getActiveNodes().contains(chromosome.getNode(0, 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)); + } } diff --git a/src/jcgp/tests/NodeTests.java b/src/jcgp/tests/NodeTests.java index ecc87ff..5b378d2 100644 --- a/src/jcgp/tests/NodeTests.java +++ b/src/jcgp/tests/NodeTests.java @@ -2,7 +2,6 @@ package jcgp.tests; import static org.junit.Assert.assertTrue; -import java.util.ArrayList; import java.util.Random; import jcgp.Parameters; @@ -92,11 +91,6 @@ public class NodeTests { return arg1; } - @Override - public void getActive(ArrayList activeNodes) { - // irrelevant for this test - } - }, new Connection() { @@ -106,11 +100,6 @@ public class NodeTests { return arg2; } - @Override - public void getActive(ArrayList activeNodes) { - // irrelevant for this test - } - }}); } @@ -189,11 +178,6 @@ public class NodeTests { return 0; } - @Override - public void getActive(ArrayList activeNodes) { - // blank - - } }; conn1 = new Connection() { @@ -203,11 +187,6 @@ public class NodeTests { return 0; } - @Override - public void getActive(ArrayList activeNodes) { - // blank - - } }; node.initialise(null, conn0, conn1); @@ -222,17 +201,12 @@ public class NodeTests { // blank return 0; } - - @Override - public void getActive(ArrayList activeNodes) { - // blank - - } }; node.setConnection(conn2); assertTrue("Connection was not found in node.", node.getConnection(0) == conn2 || node.getConnection(1) == conn2); } + } diff --git a/src/jcgp/tests/OutputTests.java b/src/jcgp/tests/OutputTests.java index 06295ae..04eac7f 100644 --- a/src/jcgp/tests/OutputTests.java +++ b/src/jcgp/tests/OutputTests.java @@ -2,7 +2,6 @@ package jcgp.tests; import static org.junit.Assert.assertTrue; -import java.util.ArrayList; import java.util.Random; import jcgp.Parameters; @@ -69,11 +68,6 @@ public class OutputTests { // test value return outputValue; } - - @Override - public void getActive(ArrayList activeNodes) { - // blank - } }); assertTrue("Incorrect evaluation.", output.calculate() == outputValue); @@ -89,11 +83,6 @@ public class OutputTests { // blank return 0; } - - @Override - public void getActive(ArrayList activeNodes) { - // blank - } }; output.setConnection(newConn); -- cgit v1.2.3