aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-02-11 22:17:30 +0000
committerEduardo Pedroni <ep625@york.ac.uk>2014-02-11 22:17:30 +0000
commitccdecd80ffe482fbe994515e98eeae68fb4ca401 (patch)
tree50013c63d2e524ac69350256dd87fc0902dce0f9 /src
parentdd3b6446671f31d716eb07e546c6150b4d080abd (diff)
Finished writing chromosome tests, implemented active node detection, started writing node tests.
Diffstat (limited to 'src')
-rw-r--r--src/jcgp/Main.java2
-rw-r--r--src/jcgp/ea/StandardMutator.java22
-rw-r--r--src/jcgp/population/Chromosome.java11
-rw-r--r--src/jcgp/population/Connection.java7
-rw-r--r--src/jcgp/population/Input.java9
-rw-r--r--src/jcgp/population/Node.java14
-rw-r--r--src/jcgp/population/Output.java7
-rw-r--r--src/jcgp/tests/ChromosomeTests.java16
-rw-r--r--src/jcgp/tests/NodeTests.java34
9 files changed, 99 insertions, 23 deletions
diff --git a/src/jcgp/Main.java b/src/jcgp/Main.java
index 39f3c4b..598c720 100644
--- a/src/jcgp/Main.java
+++ b/src/jcgp/Main.java
@@ -4,7 +4,7 @@ public class Main {
public static void main(String[] args) {
CGP cgp = new CGP();
-
+ cgp.getClass();
}
}
diff --git a/src/jcgp/ea/StandardMutator.java b/src/jcgp/ea/StandardMutator.java
index 6a4af1a..447338a 100644
--- a/src/jcgp/ea/StandardMutator.java
+++ b/src/jcgp/ea/StandardMutator.java
@@ -15,17 +15,17 @@ public class StandardMutator implements Mutator {
for (int i = 0; i < mutations; i++) {
MutableElement m = chromosome.getRandomMutableElement();
-//
-// if (m instanceof Output) {
-// m.setConnection(chromosome.getRandomConnection(m.getColumn()));
-// } else if (m instanceof Node) {
-// int geneType = Utilities.getRandomInt(1 + Parameters.getMaxArity());
-// if (geneType < 1) {
-// ((Node) m).setFunction(Utilities.getRandomFunction());
-// } else {
-// m.setConnection(chromosome.getRandomConnection(m.getColumn()));
-// }
-// }
+
+ if (m instanceof Output) {
+ m.setConnection(chromosome.getRandomConnection());
+ } else if (m instanceof Node) {
+ int geneType = Utilities.getRandomInt(1 + Parameters.getMaxArity());
+ if (geneType < 1) {
+ ((Node) m).setFunction(Utilities.getRandomFunction());
+ } else {
+ m.setConnection(chromosome.getRandomConnection(((Node) m).getColumn()));
+ }
+ }
}
}
}
diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java
index 1f264f2..08ff9b9 100644
--- a/src/jcgp/population/Chromosome.java
+++ b/src/jcgp/population/Chromosome.java
@@ -104,7 +104,7 @@ public class Chromosome {
// populate with connections equivalent to clone
Connection copyConnection;
for (int i = 0; i < connections.length; i++) {
- copyConnection = clone.getNode(r, c).getConnections(i);
+ copyConnection = clone.getNode(r, c).getConnection(i);
if (copyConnection instanceof Input) {
connections[i] = inputs[((Input) copyConnection).getIndex()];
} else if (copyConnection instanceof Node) {
@@ -251,12 +251,11 @@ public class Chromosome {
recomputeActiveNodes = false;
activeNodes = new ArrayList<Connection>();
- for (int r = 0; r < nodes.length; r++) {
- for (int c = 0; c < nodes[r].length; c++) {
-
- }
+ for (Output output : outputs) {
+ output.getActiveNodes(activeNodes);
}
- }
+
+ }
return activeNodes;
}
diff --git a/src/jcgp/population/Connection.java b/src/jcgp/population/Connection.java
index 4e69e99..12e92d6 100644
--- a/src/jcgp/population/Connection.java
+++ b/src/jcgp/population/Connection.java
@@ -1,6 +1,11 @@
package jcgp.population;
+import java.util.ArrayList;
+
public interface Connection {
- public abstract int getValue();
+ public int getValue();
+
+ public void getActive(ArrayList<Connection> activeNodes);
+
}
diff --git a/src/jcgp/population/Input.java b/src/jcgp/population/Input.java
index ee008ce..f3199b8 100644
--- a/src/jcgp/population/Input.java
+++ b/src/jcgp/population/Input.java
@@ -1,5 +1,7 @@
package jcgp.population;
+import java.util.ArrayList;
+
public class Input implements Connection {
private int value = 0, index;
@@ -21,4 +23,11 @@ public class Input implements Connection {
return index;
}
+ @Override
+ public void getActive(ArrayList<Connection> activeNodes) {
+ if (!activeNodes.contains(this)) {
+ activeNodes.add(this);
+ }
+ }
+
}
diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java
index 40ffa52..c09532c 100644
--- a/src/jcgp/population/Node.java
+++ b/src/jcgp/population/Node.java
@@ -1,5 +1,7 @@
package jcgp.population;
+import java.util.ArrayList;
+
import jcgp.Parameters;
import jcgp.Utilities;
import jcgp.function.Function;
@@ -58,7 +60,17 @@ public class Node implements MutableElement, Connection {
return function;
}
- public Connection getConnections(int index) {
+ public Connection getConnection(int index) {
return connections[index];
}
+
+ @Override
+ public void getActive(ArrayList<Connection> activeNodes) {
+ if (!activeNodes.contains(this)) {
+ activeNodes.add(this);
+ }
+ for (int i = 0; i < function.getArity(); i++) {
+ connections[i].getActive(activeNodes);
+ }
+ }
}
diff --git a/src/jcgp/population/Output.java b/src/jcgp/population/Output.java
index 68045b0..b3cb648 100644
--- a/src/jcgp/population/Output.java
+++ b/src/jcgp/population/Output.java
@@ -1,5 +1,7 @@
package jcgp.population;
+import java.util.ArrayList;
+
public class Output implements MutableElement {
@@ -29,7 +31,8 @@ public class Output implements MutableElement {
public Connection getSource() {
return source;
}
-
-
+ public void getActiveNodes(ArrayList<Connection> activeNodes) {
+ source.getActive(activeNodes);
+ }
}
diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java
index ece8203..3b7319f 100644
--- a/src/jcgp/tests/ChromosomeTests.java
+++ b/src/jcgp/tests/ChromosomeTests.java
@@ -146,7 +146,7 @@ public class ChromosomeTests {
}
}
System.out.println("Out of " + connectionPicks + " connections picked from " + ((chosenColumn >= Parameters.getLevelsBack()) ? Parameters.getLevelsBack() : chosenColumn) * Parameters.getRows() +
- " nodes and " + Parameters.getInputs() + " allowed nodes, " + connectionNodes + " were nodes and " + connectionInputs + " were inputs.");
+ " allowed nodes and " + Parameters.getInputs() + " 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() +
", picked ratio: " + (double) connectionNodes / (double) connectionInputs);
@@ -228,6 +228,20 @@ public class ChromosomeTests {
*/
@Test
public void activeNodeTest() {
+ // active node detection happens recursively, the user only calls a single method
+ // set connections to a known configuration
+ 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));
+
+ System.out.println("Active connections: " + chromosome.getActiveNodes().toString() + "\n");
}
}
diff --git a/src/jcgp/tests/NodeTests.java b/src/jcgp/tests/NodeTests.java
new file mode 100644
index 0000000..921879f
--- /dev/null
+++ b/src/jcgp/tests/NodeTests.java
@@ -0,0 +1,34 @@
+package jcgp.tests;
+
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ * Tests which cover the behaviour specified for a node.
+ *
+ * - A node should contain read-only row and column values which are set upon construction.
+ * - It should contain a fully accessible Function object.
+ * - It should contain a set of connections which can be initialised and randomly
+ * modified.
+ *
+ * WARNING: changing parameters may cause the tests to incorrectly fail!
+ *
+ * @author Eduardo Pedroni
+ *
+ */
+public class NodeTests {
+
+ @Before
+ public void setUp() throws Exception {
+
+ }
+
+ @Test
+ public void test() {
+
+ }
+
+}