aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/tests/ChromosomeTests.java
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-02-10 09:33:54 +0000
committerEduardo Pedroni <ep625@york.ac.uk>2014-02-10 09:33:54 +0000
commit6e7747e5b85f4ca93683ed5166f6e480cc58e6fa (patch)
treee1243f2eb6f743a547b89362e37e516e22f647e8 /src/jcgp/tests/ChromosomeTests.java
parent0e34bfdb60c28a6118ec93893ddc7ceb6fa50cb5 (diff)
Refactored the resources mechanics, implemented a few of the chromosome tests
Diffstat (limited to 'src/jcgp/tests/ChromosomeTests.java')
-rw-r--r--src/jcgp/tests/ChromosomeTests.java103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java
new file mode 100644
index 0000000..123cbd1
--- /dev/null
+++ b/src/jcgp/tests/ChromosomeTests.java
@@ -0,0 +1,103 @@
+package jcgp.tests;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.Random;
+
+import jcgp.Parameters;
+import jcgp.Utilities;
+import jcgp.function.Addition;
+import jcgp.function.FunctionSet;
+import jcgp.function.Subtraction;
+import jcgp.population.Chromosome;
+import jcgp.population.Input;
+import jcgp.population.Node;
+import jcgp.population.Output;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ * Tests which cover the behaviour specified for a chromosome.
+ *
+ * - The chromosome should be able to return a specified node, input or output.
+ * - It should be able to return a MutableElement indexed by row and column,
+ * where column NodeCount returns outputs.
+ * - It should be able to return a Connection indexed by row and column, where
+ * column 0 returns inputs.
+ * - It should contain a freely modifiable fitness value.
+ * - It should be a good citizen - fully initialised upon instantiation.
+ * - It should feature a clone constructor, which creates a deep copy of a
+ * specified Chromosome object.
+ * - It should be able to return the number of active nodes.
+ * - For truth table evaluations, it should be able to have its inputs set.
+ *
+ * @author Eduardo Pedroni
+ *
+ */
+public class ChromosomeTests {
+
+ Chromosome chromosome;
+ int inputCount = 3,
+ rows = 3,
+ columns = 3,
+ outputCount = 3;
+
+ @Before
+ public void setUp() throws Exception {
+ // initialise function set
+ FunctionSet functionSet = new FunctionSet(new Addition(), new Subtraction());
+
+ // initialise utilities
+ Utilities.setResources(new Random(1234), functionSet);
+
+ // initialise parameters
+ Parameters.setColumns(columns);
+ Parameters.setRows(rows);
+ Parameters.setInputs(inputCount);
+ Parameters.setOutputs(outputCount);
+ Parameters.setLevelsBack(1);
+ Parameters.setMutationRate(10);
+ Parameters.setTotalGenerations(100);
+ Parameters.setTotalRuns(5);
+ Parameters.setMaxArity(functionSet.getMaxArity());
+
+ chromosome = new Chromosome(inputCount, rows, columns, outputCount);
+
+ }
+
+ @Test
+ public void test() {
+ // pick arbitrary node, assume that if one node is right, all nodes are right
+ boolean nodeReturn = chromosome.getNode(1, 2).getColumn() == 2
+ && chromosome.getNode(1, 2).getRow() == 1
+ && chromosome.getNode(1, 2) instanceof Node;
+ assertTrue("Incorrect node returned.", nodeReturn);
+
+ // set input values, check that acquired values are correct
+ chromosome.setInputs(4, 5, 6);
+ boolean inputReturn = chromosome.getInput(0).getValue() == 4 && chromosome.getInput(0) instanceof Input
+ && chromosome.getInput(1).getValue() == 5 && chromosome.getInput(0) instanceof Input
+ && chromosome.getInput(2).getValue() == 6 && chromosome.getInput(0) instanceof Input;
+ assertTrue("Incorrect inputs returned.", inputReturn);
+
+ // connect outputs to inputs, check that calculated outputs return input values
+ for (int i = 0; i < outputCount; i++) {
+ chromosome.getOutput(i).setConnection(chromosome.getInput(0));
+ }
+ boolean outputReturn = chromosome.getOutput(0).calculate() == 4 && chromosome.getOutput(0) instanceof Output
+ && chromosome.getOutput(1).calculate() == 4 && chromosome.getOutput(0) instanceof Output
+ && chromosome.getOutput(2).calculate() == 4 && chromosome.getOutput(0) instanceof Output;
+ assertTrue("Incorrect output returned.", outputReturn);
+
+ // get a mutable element, that that it is a Mutable
+ boolean mutableReturn = chromosome.getMutable() instanceof Output;
+
+ // set a fitness value, check if returned value is the same
+ chromosome.setFitness(10);
+ assertTrue("Incorrect fitness returned.", chromosome.getFitness() == 10);
+
+ }
+
+}