From 0c288cc1952809294c8d70d86b9f41b04878ac2e Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 23 Mar 2014 18:05:13 +0000 Subject: Majorly refactored, node grid is fully implemented. About to attempt active path locking. --- src/jcgp/tests/ChromosomeTests.java | 93 +++++++++++++++---------------------- 1 file changed, 37 insertions(+), 56 deletions(-) (limited to 'src/jcgp/tests/ChromosomeTests.java') diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java index c61785e..6323b88 100644 --- a/src/jcgp/tests/ChromosomeTests.java +++ b/src/jcgp/tests/ChromosomeTests.java @@ -1,13 +1,8 @@ package jcgp.tests; -import static org.junit.Assert.*; - -import java.util.Random; - -import jcgp.Utilities; -import jcgp.modules.function.Arithmetic; -import jcgp.modules.function.FunctionSet; -import jcgp.parameters.Parameters; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import jcgp.CGP.Resources; import jcgp.population.Chromosome; import jcgp.population.Connection; import jcgp.population.Input; @@ -45,30 +40,16 @@ import org.junit.Test; public class ChromosomeTests { private Chromosome chromosome; + private static Resources resources; @BeforeClass public static void setUpBeforeClass() { - // initialise function set - FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction()); - - // initialise utilities - Utilities.setResources(new Random(1234), functionSet); - - // initialise parameters - Parameters.setMutationRate(10); - Parameters.setTotalGenerations(10); - Parameters.setTotalRuns(5); - Parameters.setMaxArity(functionSet.getMaxArity()); + resources = new Resources(); } @Before public void setUp() throws Exception { - Parameters.setColumns(5); - Parameters.setRows(2); - Parameters.setInputs(2); - Parameters.setOutputs(4); - Parameters.setLevelsBack(5); - chromosome = new Chromosome(); + chromosome = new Chromosome(resources); } /** @@ -81,7 +62,7 @@ public class ChromosomeTests { // compare all elements, one by one // check outputs - for (int o = 0; o < Parameters.getOutputs(); o++) { + for (int o = 0; o < (int) resources.get("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) && @@ -99,8 +80,8 @@ public class ChromosomeTests { } } // check nodes, rows first - for (int row = 0; row < Parameters.getRows(); row++) { - for (int column = 0; column < Parameters.getColumns(); column++) { + for (int row = 0; row < (int) resources.get("rows"); row++) { + for (int column = 0; column < (int) resources.get("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 @@ -110,7 +91,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 < Parameters.getMaxArity(); connection++) { + for (int connection = 0; connection < (int) resources.get("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)); @@ -149,12 +130,12 @@ public class ChromosomeTests { clone.setInputs((Object[]) testInputs); // check that both chromosomes have the same outputs - for (int i = 0; i < Parameters.getOutputs(); i++) { + for (int i = 0; i < (int) resources.get("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(clone.getInput(2)); + clone.getOutput(1).setConnection(resources.getRandomInt((int) resources.get("arity")), clone.getInput(2)); assertTrue("Mutation affected nodes in both chromosomes.", clone.getOutput(1).calculate() != chromosome.getOutput(1).calculate()); @@ -183,7 +164,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 = Parameters.getColumns() - 1; + int chosenColumn = (int) resources.get("columns") - 1; for (int i = 0; i < connectionPicks; i++) { Connection c = chromosome.getRandomConnection(chosenColumn); if (c instanceof Node) { @@ -199,10 +180,10 @@ public class ChromosomeTests { } } - 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("Out of " + connectionPicks + " connections picked from " + ((chosenColumn >= (int) resources.get("levelsBack")) ? (int) resources.get("levelsBack") : chosenColumn) * (int) resources.get("rows") + + " allowed nodes and " + (int) resources.get("inputs") + " 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() + + System.out.println("Node/input ratio: " + ((Integer) (((chosenColumn >= (int) resources.get("levelsBack")) ? (int) resources.get("levelsBack") : chosenColumn) * (Integer) resources.get("rows"))).doubleValue() / ((Integer) resources.get("inputs")).doubleValue() + ", picked ratio: " + (double) connectionNodes / (double) connectionInputs); System.out.println(connectionOutOfRange + " nodes that disrespected levels back were picked."); @@ -226,10 +207,10 @@ public class ChromosomeTests { 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 " + + System.out.println("Out of " + mutablePicks + " mutable elements picked from " + (int) resources.get("nodes") + + " nodes and " + (int) resources.get("outputs") + " outputs, " + mutableNodes + " were nodes and " + mutableOutputs + " were outputs."); - System.out.println("Node/output ratio: " + (double) Parameters.getNodeCount() / (double) Parameters.getOutputs() + + System.out.println("Node/output ratio: " + ((Integer) resources.get("nodes")).doubleValue() / ((Integer) resources.get("outputs")).doubleValue() + ", picked ratio: " + (double) mutableNodes / (double) mutableOutputs + "\n"); } @@ -253,12 +234,12 @@ public class ChromosomeTests { @Test public void setInputTest() { // set input values, check that acquired values are correct - Integer[] testInputs = new Integer[Parameters.getInputs()]; - for (int i = 0; i < Parameters.getInputs(); i++) { + Integer[] testInputs = new Integer[(int) resources.get("inputs")]; + for (int i = 0; i < (int) resources.get("inputs"); i++) { testInputs[i] = i * 2 - 3; } chromosome.setInputs((Object[]) testInputs); - for (int i = 0; i < Parameters.getInputs(); i++) { + for (int i = 0; i < (int) resources.get("inputs"); i++) { assertTrue("Incorrect input returned.", ((Integer) chromosome.getInput(i).getValue()) == i * 2 - 3); } } @@ -269,8 +250,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 < Parameters.getRows(); r++) { - for (int c = 0; c < Parameters.getColumns(); c++) { + for (int r = 0; r < (int) resources.get("rows"); r++) { + for (int c = 0; c < (int) resources.get("columns"); c++) { assertTrue("Incorrect node returned.", chromosome.getNode(r, c).getColumn() == c && chromosome.getNode(r, c).getRow() == r); } @@ -304,7 +285,7 @@ public class ChromosomeTests { assertTrue("Symmetry not obeyed.", c.compareActiveTo(chromosome)); // create a new random chromosome, this time they should not match - c = new Chromosome(); + c = new Chromosome(resources); assertTrue("Active nodes did match.", !chromosome.compareActiveTo(c)); } @@ -319,7 +300,7 @@ public class ChromosomeTests { assertTrue("Symmetry not obeyed.", c.compareTo(chromosome)); // create a new random chromosome, this time they should not match - c = new Chromosome(); + c = new Chromosome(resources); assertTrue("Chromosomes did match.", !chromosome.compareTo(c)); } /** @@ -333,20 +314,20 @@ public class ChromosomeTests { */ 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); + resources.set("columns", 3); + resources.set("rows", 3); + resources.set("inputs", 3); + resources.set("outputs", 2); + resources.set("levelsBack", 3); - Chromosome c = new Chromosome(); + Chromosome c = new Chromosome(resources); - 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.getNode(0, 0).initialise(resources.getFunction(0), c.getInput(0), c.getInput(1)); + c.getNode(1, 1).initialise(resources.getFunction(0), c.getNode(0, 0), c.getInput(1)); + c.getNode(1, 2).initialise(resources.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)); + c.getOutput(0).setConnection(0, c.getNode(0, 0)); + c.getOutput(1).setConnection(0, c.getNode(1, 2)); return c; } -- cgit v1.2.3