diff options
Diffstat (limited to 'src/jcgp/tests/ChromosomeTests.java')
-rw-r--r-- | src/jcgp/tests/ChromosomeTests.java | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java index 3b7319f..cb2cb95 100644 --- a/src/jcgp/tests/ChromosomeTests.java +++ b/src/jcgp/tests/ChromosomeTests.java @@ -18,6 +18,7 @@ import jcgp.population.Node; import jcgp.population.Output; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; /** @@ -26,8 +27,8 @@ import org.junit.Test; * * - The chromosome should be able to return a specified node, input or output. * - It should be able to return a random MutableElement. - * - It should be able to return a random allowed connection given a column - * - It should be able to return any random connection + * - It should be able to return a random allowed connection given a column. + * - 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 @@ -36,6 +37,8 @@ import org.junit.Test; * specified Chromosome object. * - It should be able to return a list of active nodes. * + * TODO: bashing (strange value ranges, etc) + * * WARNING: changing parameters may cause the tests to incorrectly fail! * * @author Eduardo Pedroni @@ -45,8 +48,8 @@ public class ChromosomeTests { private Chromosome chromosome; - @Before - public void setUp() throws Exception { + @BeforeClass + public static void setUpBeforeClass() { // initialise function set FunctionSet functionSet = new FunctionSet(new Addition(), new Subtraction()); @@ -54,18 +57,20 @@ public class ChromosomeTests { Utilities.setResources(new Random(1234), functionSet); // initialise parameters - Parameters.setColumns(20); - Parameters.setRows(10); + Parameters.setColumns(1); + Parameters.setRows(20); Parameters.setInputs(2); Parameters.setOutputs(4); - Parameters.setLevelsBack(20); + Parameters.setLevelsBack(1); Parameters.setMutationRate(10); Parameters.setTotalGenerations(100); Parameters.setTotalRuns(5); Parameters.setMaxArity(functionSet.getMaxArity()); + } + @Before + public void setUp() throws Exception { chromosome = new Chromosome(); - } /** @@ -76,7 +81,7 @@ public class ChromosomeTests { int[] testInputs; // 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++) { @@ -84,15 +89,15 @@ 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()); + clone.getOutput(i).calculate()); } - + // change clone inputs, outputs should no longer match testInputs = new int[Parameters.getInputs()]; for (int i = 0; i < Parameters.getInputs(); i++) { @@ -101,7 +106,7 @@ public class ChromosomeTests { clone.setInputs(testInputs); for (int i = 0; i < Parameters.getOutputs(); i++) { assertTrue("Incorrect output returned.", chromosome.getOutput(i).calculate() != - clone.getOutput(i).calculate()); + clone.getOutput(i).calculate()); } } @@ -126,10 +131,10 @@ public class ChromosomeTests { assertTrue("Connection is not an input.", connectionReturn); } - // get random connections with column 2 + // get random connections with column 1 // they should all be nodes, and their columns should be within range int connectionNodes = 0, connectionOutOfRange = 0, connectionInputs = 0, connectionPicks = 100000; - int chosenColumn = 2; + int chosenColumn = 1; for (int i = 0; i < connectionPicks; i++) { Connection c = chromosome.getRandomConnection(chosenColumn); if (c instanceof Node) { @@ -222,7 +227,7 @@ public class ChromosomeTests { } } } - + /** * */ @@ -233,12 +238,12 @@ public class ChromosomeTests { 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 connection not in list.", chromosome.getActiveNodes().contains(chromosome.getInput(0))); assertTrue("Active connection not in list.", chromosome.getActiveNodes().contains(chromosome.getNode(0, 0))); - + // change outputs, print list chromosome.getOutput(0).setConnection(chromosome.getNode(0, Parameters.getColumns() - 1)); |