diff options
Diffstat (limited to 'src/jcgp/backend/tests/ChromosomeTests.java')
-rw-r--r-- | src/jcgp/backend/tests/ChromosomeTests.java | 54 |
1 files changed, 29 insertions, 25 deletions
diff --git a/src/jcgp/backend/tests/ChromosomeTests.java b/src/jcgp/backend/tests/ChromosomeTests.java index a16ba75..07326a7 100644 --- a/src/jcgp/backend/tests/ChromosomeTests.java +++ b/src/jcgp/backend/tests/ChromosomeTests.java @@ -2,6 +2,7 @@ package jcgp.backend.tests; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import jcgp.backend.function.IntegerArithmetic; import jcgp.backend.population.Chromosome; import jcgp.backend.population.Connection; import jcgp.backend.population.Input; @@ -45,6 +46,7 @@ public class ChromosomeTests { @BeforeClass public static void setUpBeforeClass() { resources = new ModifiableResources(); + resources.setFunctionSet(new IntegerArithmetic()); } @Before @@ -62,7 +64,7 @@ public class ChromosomeTests { // compare all elements, one by one // check outputs - for (int o = 0; o < resources.getInt("outputs"); o++) { + for (int o = 0; o < resources.outputs(); 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) && @@ -80,8 +82,8 @@ public class ChromosomeTests { } } // check nodes, rows first - for (int row = 0; row < resources.getInt("rows"); row++) { - for (int column = 0; column < resources.getInt("columns"); column++) { + for (int row = 0; row < resources.rows(); row++) { + for (int column = 0; column < resources.columns(); 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 @@ -91,7 +93,7 @@ public class ChromosomeTests { assertTrue("Equivalent nodes have different functions.", clone.getNode(row, column).getFunction() == chromosome.getNode(row, column).getFunction()); // compare each connection - for (int connection = 0; connection < resources.getInt("arity"); connection++) { + for (int connection = 0; connection < resources.arity(); 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)); @@ -130,12 +132,12 @@ public class ChromosomeTests { clone.setInputs((Object[]) testInputs); // check that both chromosomes have the same outputs - for (int i = 0; i < resources.getInt("outputs"); i++) { + for (int i = 0; i < resources.outputs(); i++) { assertTrue("Incorrect output returned", ((Integer) chromosome.getOutput(i).calculate()) == ((Integer) clone.getOutput(i).calculate())); } // mutate an output in clone, check that the same node in chromosome produces a different output - clone.getOutput(1).setConnection(resources.getRandomInt(resources.getInt("arity")), clone.getInput(2)); + clone.getOutput(1).setConnection(resources.getRandomInt(resources.arity()), clone.getInput(2)); assertTrue("Mutation affected nodes in both chromosomes.", clone.getOutput(1).calculate() != chromosome.getOutput(1).calculate()); @@ -164,7 +166,7 @@ public class ChromosomeTests { // get random connections with the last column as reference, check that they're all within range int connectionNodes = 0, connectionOutOfRange = 0, connectionInputs = 0, connectionPicks = 100000; - int chosenColumn = resources.getInt("columns") - 1; + int chosenColumn = resources.columns() - 1; for (int i = 0; i < connectionPicks; i++) { Connection c = chromosome.getRandomConnection(chosenColumn); if (c instanceof Node) { @@ -180,10 +182,10 @@ public class ChromosomeTests { } } - System.out.println("Out of " + connectionPicks + " connections picked from " + ((chosenColumn >= resources.getInt("levelsBack")) ? resources.getInt("levelsBack") : chosenColumn) * resources.getInt("rows") + - " allowed nodes and " + resources.getInt("inputs") + " inputs, " + connectionNodes + " were nodes and " + connectionInputs + " were inputs."); + System.out.println("Out of " + connectionPicks + " connections picked from " + ((chosenColumn >= resources.levelsBack()) ? resources.levelsBack() : chosenColumn) * resources.rows() + + " allowed nodes and " + resources.inputs() + " inputs, " + connectionNodes + " were nodes and " + connectionInputs + " were inputs."); - System.out.println("Node/input ratio: " + (chosenColumn >= resources.getInt("levelsBack") ? resources.getInt("levelsBack") : chosenColumn) * resources.getDouble("rows") / resources.getDouble("inputs") + + System.out.println("Node/input ratio: " + (chosenColumn >= resources.levelsBack() ? resources.levelsBack() : chosenColumn) * (double) resources.rows() / (double) resources.inputs() + ", picked ratio: " + (double) connectionNodes / (double) connectionInputs); System.out.println(connectionOutOfRange + " nodes that disrespected levels back were picked."); @@ -207,10 +209,10 @@ public class ChromosomeTests { fail("Return is neither Node nor Output."); } } - System.out.println("Out of " + mutablePicks + " mutable elements picked from " + resources.getInt("nodes") + - " nodes and " + resources.getInt("outputs") + " outputs, " + mutableNodes + " were nodes and " + + System.out.println("Out of " + mutablePicks + " mutable elements picked from " + resources.nodes() + + " nodes and " + resources.outputs() + " outputs, " + mutableNodes + " were nodes and " + mutableOutputs + " were outputs."); - System.out.println("Node/output ratio: " + resources.getDouble("nodes") / resources.getDouble("outputs") + + System.out.println("Node/output ratio: " + (double) resources.nodes() / (double) resources.outputs() + ", picked ratio: " + (double) mutableNodes / (double) mutableOutputs + "\n"); } @@ -234,12 +236,12 @@ public class ChromosomeTests { @Test public void setInputTest() { // set input values, check that acquired values are correct - Integer[] testInputs = new Integer[resources.getInt("inputs")]; - for (int i = 0; i < resources.getInt("inputs"); i++) { + Integer[] testInputs = new Integer[resources.inputs()]; + for (int i = 0; i < resources.inputs(); i++) { testInputs[i] = i * 2 - 3; } chromosome.setInputs((Object[]) testInputs); - for (int i = 0; i < resources.getInt("inputs"); i++) { + for (int i = 0; i < resources.inputs(); i++) { assertTrue("Incorrect input returned.", ((Integer) chromosome.getInput(i).getValue()) == i * 2 - 3); } } @@ -250,8 +252,8 @@ public class ChromosomeTests { @Test public void getNodeTest() { // get all nodes one by one, check that they are all correct - for (int r = 0; r < resources.getInt("rows"); r++) { - for (int c = 0; c < resources.getInt("columns"); c++) { + for (int r = 0; r < resources.rows(); r++) { + for (int c = 0; c < resources.columns(); c++) { assertTrue("Incorrect node returned.", chromosome.getNode(r, c).getColumn() == c && chromosome.getNode(r, c).getRow() == r); } @@ -271,7 +273,9 @@ public class ChromosomeTests { 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); + chromosome.printNodes(); + + assertTrue("List has the wrong number of nodes: " + chromosome.getActiveNodes(), chromosome.getActiveNodes().size() == 3); } /** @@ -314,11 +318,11 @@ public class ChromosomeTests { */ private Chromosome createKnownConfiguration() { // with a small topology, build a chromosome of known connections and check outputs - resources.set("columns", 3); - resources.set("rows", 3); - resources.set("inputs", 3); - resources.set("outputs", 2); - resources.set("levelsBack", 3); + resources.setColumns(3); + resources.setRows(3); + resources.setInputs(3); + resources.setOutputs(2); + resources.setLevelsBack(3); Chromosome c = new Chromosome(resources); @@ -328,7 +332,7 @@ public class ChromosomeTests { c.getOutput(0).setConnection(0, c.getNode(0, 0)); c.getOutput(1).setConnection(0, c.getNode(1, 2)); - + return c; } } |