aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/tests
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-03-23 18:05:13 +0000
committerEduardo Pedroni <ep625@york.ac.uk>2014-03-23 18:05:13 +0000
commit0c288cc1952809294c8d70d86b9f41b04878ac2e (patch)
treeef9671b711fe665a3156594663c083595861a4e6 /src/jcgp/tests
parentd3527a63e12c0e5288f1e7d2e2dc18e61d16b760 (diff)
Majorly refactored, node grid is fully implemented. About to attempt active path locking.
Diffstat (limited to 'src/jcgp/tests')
-rw-r--r--src/jcgp/tests/ChromosomeTests.java93
-rw-r--r--src/jcgp/tests/NodeTests.java40
-rw-r--r--src/jcgp/tests/OutputTests.java27
-rw-r--r--src/jcgp/tests/PopulationTests.java94
4 files changed, 92 insertions, 162 deletions
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;
}
diff --git a/src/jcgp/tests/NodeTests.java b/src/jcgp/tests/NodeTests.java
index 74b8140..330ef7a 100644
--- a/src/jcgp/tests/NodeTests.java
+++ b/src/jcgp/tests/NodeTests.java
@@ -1,13 +1,9 @@
package jcgp.tests;
import static org.junit.Assert.assertTrue;
-
-import java.util.Random;
-
-import jcgp.Utilities;
-import jcgp.modules.function.Arithmetic;
-import jcgp.modules.function.Function;
-import jcgp.parameters.Parameters;
+import jcgp.CGP.Resources;
+import jcgp.function.Arithmetic;
+import jcgp.function.Function;
import jcgp.population.Chromosome;
import jcgp.population.Connection;
import jcgp.population.Node;
@@ -35,32 +31,22 @@ public class NodeTests {
private Node node;
private static Chromosome chromosome;
+ private static Resources resources;
// these numbers will be used by the node under test
private final int arg1 = 2;
private final int arg2 = 5;
@BeforeClass
public static void setUpBeforeClass() {
- // initialise utilities
- Utilities.setResources(new Random(1234), null);
-
- // initialise parameters
- Parameters.setColumns(0);
- Parameters.setRows(0);
- Parameters.setInputs(0);
- Parameters.setOutputs(0);
- Parameters.setLevelsBack(0);
- Parameters.setMutationRate(10);
- Parameters.setTotalGenerations(100);
- Parameters.setTotalRuns(5);
- Parameters.setMaxArity(2);
-
- chromosome = new Chromosome();
+
+ resources = new Resources();
+
+ chromosome = new Chromosome(resources);
}
@Before
public void setUp() throws Exception {
- node = new Node(chromosome, 0, 0);
+ node = new Node(chromosome, 0, 0, (int) resources.get("arity"));
// make node with anonymous addition function and hard-coded value connections
node.initialise(new Arithmetic.Addition(),
new Connection[]{new Connection() {
@@ -117,6 +103,12 @@ public class NodeTests {
// blank
return 0;
}
+
+ @Override
+ public String getName() {
+ // blank
+ return null;
+ }
};
node.setFunction(f);
@@ -194,7 +186,7 @@ public class NodeTests {
return null;
}
};
- node.setConnection(conn2);
+ node.setConnection(resources.getRandomInt((int) resources.get("arity")), 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 b8f7d96..00cfea3 100644
--- a/src/jcgp/tests/OutputTests.java
+++ b/src/jcgp/tests/OutputTests.java
@@ -2,10 +2,7 @@ package jcgp.tests;
import static org.junit.Assert.assertTrue;
-import java.util.Random;
-
-import jcgp.Utilities;
-import jcgp.parameters.Parameters;
+import jcgp.CGP.Resources;
import jcgp.population.Chromosome;
import jcgp.population.Connection;
import jcgp.population.Output;
@@ -30,27 +27,15 @@ public class OutputTests {
private Output output;
private static Chromosome chromosome;
+ private static Resources resources;
// these are the test values
private final int outputValue = 10;
private final int outputIndex = 2;
@BeforeClass
public static void setUpBeforeClass() {
- // initialise utilities
- Utilities.setResources(new Random(1234), null);
-
- // initialise parameters
- Parameters.setColumns(0);
- Parameters.setRows(0);
- Parameters.setInputs(0);
- Parameters.setOutputs(0);
- Parameters.setLevelsBack(0);
- Parameters.setMutationRate(10);
- Parameters.setTotalGenerations(100);
- Parameters.setTotalRuns(5);
- Parameters.setMaxArity(2);
-
- chromosome = new Chromosome();
+ resources = new Resources();
+ chromosome = new Chromosome(resources);
}
@Before
@@ -61,7 +46,7 @@ public class OutputTests {
@Test
public void evaluationsTest() {
// set source connection, check that the appropriate value is returned
- output.setConnection(new Connection() {
+ output.setConnection(0, new Connection() {
@Override
public Object getValue() {
@@ -96,7 +81,7 @@ public class OutputTests {
return null;
}
};
- output.setConnection(newConn);
+ output.setConnection(0, newConn);
assertTrue("Incorrect connection returned.", output.getSource() == newConn);
}
diff --git a/src/jcgp/tests/PopulationTests.java b/src/jcgp/tests/PopulationTests.java
index d646b90..474b8d5 100644
--- a/src/jcgp/tests/PopulationTests.java
+++ b/src/jcgp/tests/PopulationTests.java
@@ -1,13 +1,7 @@
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 jcgp.CGP.Resources;
import jcgp.population.Chromosome;
import jcgp.population.Population;
@@ -19,9 +13,7 @@ import org.junit.Test;
*
* Tests which cover the behaviour specified for a population.
*
- * - A population should be able to return parents and offspring separately.
- * - It should be possible to iterate through all the chromosomes in a population
- * with one indexing system - parents then offspring.
+ * - It should be possible to iterate through all the chromosomes in a population.
* - When constructed with no arguments, it should generate populationSize
* random chromosomes, distributed according to the EA parameters.
* - If one or more chromosomes are passed into the constructor, it should use them
@@ -34,81 +26,61 @@ import org.junit.Test;
public class PopulationTests {
private Population population;
+ private static Resources resources;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
- // initialise function set
- FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction());
-
- // initialise utilities
- Utilities.setResources(new Random(1234), functionSet);
-
- // initialise parameters
- Parameters.setColumns(20);
- Parameters.setRows(20);
- Parameters.setInputs(2);
- Parameters.setOutputs(4);
- Parameters.setLevelsBack(1);
- Parameters.setMutationRate(10);
- Parameters.setTotalGenerations(100);
- Parameters.setTotalRuns(5);
- Parameters.setPopulationSize(1, 4);
- Parameters.setMaxArity(functionSet.getMaxArity());
+ resources = new Resources();
+
+
+// // initialise function set
+// FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction());
+//
+// // initialise utilities
+// Utilities.setResources(new Random(1234), functionSet);
+//
+// // initialise parameters
+// Resources.setColumns(20);
+// Resources.setRows(20);
+// Resources.setInputs(2);
+// Resources.setOutputs(4);
+// Resources.setLevelsBack(1);
+// Resources.setMutationRate(10);
+// Resources.setTotalGenerations(100);
+// Resources.setTotalRuns(5);
+// Resources.setPopulationSize(1, 4);
+// Resources.setMaxArity(functionSet.getMaxArity());
}
@Before
public void setUp() throws Exception {
- population = new Population();
+ population = new Population(resources);
}
@Test
public void defaultPopulationTest() {
// check that the constructor really generates populationSize chromosomes when none is given
- int offspring = 0, parent = 0;
- while (true) {
- try {
- population.getOffspring(offspring);
- } catch (IndexOutOfBoundsException e) {
- break;
- }
- offspring++;
- }
+ int chromosomes = 0;
while (true) {
try {
- population.getParent(parent);
+ population.getChromosome(chromosomes);
} catch (IndexOutOfBoundsException e) {
break;
}
- parent++;
+ chromosomes++;
}
- assertTrue("Incorrect number of chromosomes generated.", offspring + parent == Parameters.getPopulationSize());
- }
-
- @Test
- public void offspringParentTest() {
- // the first parent should not be the same as the first offspring
- assertTrue("Same chromosome returned as parent and offspring", population.getOffspring(0) != population.getParent(0));
+
+ assertTrue("Incorrect number of chromosomes generated.", chromosomes == (int) resources.get("popSize"));
}
@Test
- public void singleIndexTest() {
- // assuming 1+4
- // the first chromosome should be the first (and only) parent
- assertTrue("Incorrect chromosome returned.", population.getChromosome(0) == population.getParent(0));
- // the next 4 chromosomes should be the offspring, in order
- for (int i = 0; i < Parameters.getOffspringCount(); i++) {
- assertTrue("Incorrect chromosome returned.", population.getChromosome(i + 1) == population.getOffspring(i));
- }
- }
-
- @Test
public void preinitialisedChromosomeTest() {
// the original chromosome that will be cloned
- Chromosome oc = new Chromosome();
+ Chromosome oc = new Chromosome(resources);
// initialise a population with a copy of it
- population = new Population(oc);
+ population = new Population(oc, resources);
// check that the first parent chromosome is identical to, but not the same instance as, the one given
- assertTrue("Incorrect chromosome in population.", population.getParent(0).compareTo(oc) && population.getParent(0) != oc);
+ assertTrue("Incorrect chromosome in population.", population.getChromosome(0).compareTo(oc) && population.getChromosome(0) != oc);
}
}