aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/tests
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-04-08 20:04:12 +0100
committerEduardo Pedroni <ep625@york.ac.uk>2014-04-08 20:04:12 +0100
commit260f1baaab10ab9b1db67ab587bc36adcb34494e (patch)
tree408c472a31c3fb26894c02f50803a36b684486bc /src/jcgp/backend/tests
parent226365bd6a0f56143ffbce7379857ff3e126fbaf (diff)
GUIParameters all refactored and commented.
Diffstat (limited to 'src/jcgp/backend/tests')
-rw-r--r--src/jcgp/backend/tests/ChromosomeTests.java54
-rw-r--r--src/jcgp/backend/tests/NodeTests.java12
-rw-r--r--src/jcgp/backend/tests/OutputTests.java8
-rw-r--r--src/jcgp/backend/tests/PopulationTests.java29
4 files changed, 46 insertions, 57 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;
}
}
diff --git a/src/jcgp/backend/tests/NodeTests.java b/src/jcgp/backend/tests/NodeTests.java
index e8fe02f..0e08d92 100644
--- a/src/jcgp/backend/tests/NodeTests.java
+++ b/src/jcgp/backend/tests/NodeTests.java
@@ -7,7 +7,7 @@ import jcgp.backend.function.IntegerArithmetic;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Connection;
import jcgp.backend.population.Node;
-import jcgp.backend.resources.Resources;
+import jcgp.backend.resources.ModifiableResources;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -32,7 +32,7 @@ public class NodeTests {
private Node node;
private static Chromosome chromosome;
- private static Resources resources;
+ private static ModifiableResources resources;
// these numbers will be used by the node under test
private final int arg1 = 2;
private final int arg2 = 5;
@@ -40,14 +40,14 @@ public class NodeTests {
@BeforeClass
public static void setUpBeforeClass() {
- resources = new Resources();
-
+ resources = new ModifiableResources();
+ resources.setFunctionSet(new IntegerArithmetic());
chromosome = new Chromosome(resources);
}
@Before
public void setUp() throws Exception {
- node = new Node(chromosome, 0, 0, resources.getInt("arity"));
+ node = new Node(chromosome, 0, 0, resources.arity());
// make node with anonymous addition function and hard-coded value connections
node.initialise(new IntegerArithmetic.Addition(),
new Connection[]{new Connection() {
@@ -174,7 +174,7 @@ public class NodeTests {
return 0;
}
};
- node.setConnection(resources.getRandomInt(resources.getInt("arity")), conn2);
+ node.setConnection(resources.getRandomInt(resources.arity()), conn2);
assertTrue("Connection was not found in node.", node.getConnection(0) == conn2 || node.getConnection(1) == conn2);
diff --git a/src/jcgp/backend/tests/OutputTests.java b/src/jcgp/backend/tests/OutputTests.java
index afe44a7..a331f2d 100644
--- a/src/jcgp/backend/tests/OutputTests.java
+++ b/src/jcgp/backend/tests/OutputTests.java
@@ -1,10 +1,11 @@
package jcgp.backend.tests;
import static org.junit.Assert.assertTrue;
+import jcgp.backend.function.IntegerArithmetic;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Connection;
import jcgp.backend.population.Output;
-import jcgp.backend.resources.Resources;
+import jcgp.backend.resources.ModifiableResources;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -26,14 +27,15 @@ public class OutputTests {
private Output output;
private static Chromosome chromosome;
- private static Resources resources;
+ private static ModifiableResources resources;
// these are the test values
private final int outputValue = 10;
private final int outputIndex = 2;
@BeforeClass
public static void setUpBeforeClass() {
- resources = new Resources();
+ resources = new ModifiableResources();
+ resources.setFunctionSet(new IntegerArithmetic());
chromosome = new Chromosome(resources);
}
diff --git a/src/jcgp/backend/tests/PopulationTests.java b/src/jcgp/backend/tests/PopulationTests.java
index 31df8b9..fb8ced4 100644
--- a/src/jcgp/backend/tests/PopulationTests.java
+++ b/src/jcgp/backend/tests/PopulationTests.java
@@ -1,9 +1,10 @@
package jcgp.backend.tests;
import static org.junit.Assert.assertTrue;
+import jcgp.backend.function.IntegerArithmetic;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Population;
-import jcgp.backend.resources.Resources;
+import jcgp.backend.resources.ModifiableResources;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -26,30 +27,12 @@ import org.junit.Test;
public class PopulationTests {
private Population population;
- private static Resources resources;
+ private static ModifiableResources resources;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
- 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());
+ resources = new ModifiableResources();
+ resources.setFunctionSet(new IntegerArithmetic());
}
@Before
@@ -70,7 +53,7 @@ public class PopulationTests {
chromosomes++;
}
- assertTrue("Incorrect number of chromosomes generated.", chromosomes == resources.getInt("popSize"));
+ assertTrue("Incorrect number of chromosomes generated.", chromosomes == resources.populationSize());
}
@Test