aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/population
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/population')
-rw-r--r--src/jcgp/population/Chromosome.java57
-rw-r--r--src/jcgp/population/InsufficientConnectionsException.java10
-rw-r--r--src/jcgp/population/MutableElement.java2
-rw-r--r--src/jcgp/population/Node.java20
-rw-r--r--src/jcgp/population/Output.java11
-rw-r--r--src/jcgp/population/Population.java13
6 files changed, 88 insertions, 25 deletions
diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java
index 2e22cf9..76667e5 100644
--- a/src/jcgp/population/Chromosome.java
+++ b/src/jcgp/population/Chromosome.java
@@ -2,16 +2,19 @@ package jcgp.population;
import java.util.ArrayList;
+import jcgp.CGP.Utilities;
+
public class Chromosome {
- private ArrayList<Input> inputs;
- private ArrayList<ArrayList<Node>> nodes;
- private ArrayList<Output> outputs;
+ private final ArrayList<Input> inputs = new ArrayList<Input>();
+ private final ArrayList<ArrayList<Node>> nodes = new ArrayList<ArrayList<Node>>();;
+ private final ArrayList<Output> outputs = new ArrayList<Output>();
private int fitness = 0;
/**
* Good citizen.
+ *
* @param outputs
* @param columns
* @param rows
@@ -20,13 +23,43 @@ public class Chromosome {
*/
public Chromosome(int inputCount, int rows, int columns, int outputCount) {
- inputs = new ArrayList<Input>(inputCount);
+ instantiateElements(inputCount, rows, columns, outputCount);
+
+ initialiseConnections();
+
+ }
+
+ private void initialiseConnections() {
+
+ // initialise nodes
+ for (int r = 0; r < nodes.size(); r++) {
+ for (int c = 0; c < nodes.size(); c++) {
+ Connection[] connections = new Connection[Utilities.getMaxArity()];
+ for (int i = 0; i < connections.length; i++) {
+ connections[i] = Utilities.getRandomConnection(this, c);
+ }
+ nodes.get(r).get(c).initialise(Utilities.getRandomFunction(), connections);
+ }
+ }
+
+ for (Output output : outputs) {
+ output.setConnection(Utilities.getRandomNode(this));
+ }
+
+ }
+
+ /**
+ * @param inputCount
+ * @param rows
+ * @param columns
+ * @param outputCount
+ */
+ private void instantiateElements(int inputCount, int rows, int columns, int outputCount) {
for (int i = 0; i < inputCount; i++) {
inputs.add(new Input());
}
// rows first
- nodes = new ArrayList<ArrayList<Node>>(rows);
for (int r = 0; r < rows; r++) {
nodes.add(new ArrayList<Node>(columns));
for (int c = 0; c < columns; c++) {
@@ -34,11 +67,9 @@ public class Chromosome {
}
}
- outputs = new ArrayList<Output>(outputCount);
for (int o = 0; o < outputCount; o++) {
outputs.add(new Output());
}
-
}
public int getActiveNodeCount() {
@@ -48,33 +79,33 @@ public class Chromosome {
/**
* @return the inputs
*/
- public final ArrayList<Input> getInputs() {
+ public ArrayList<Input> getInputs() {
return inputs;
}
/**
* @return the nodes
*/
- public final ArrayList<ArrayList<Node>> getNodes() {
+ public ArrayList<ArrayList<Node>> getNodes() {
return nodes;
}
/**
* @return the outputs
*/
- public final ArrayList<Output> getOutputs() {
+ public ArrayList<Output> getOutputs() {
return outputs;
}
- public final Node getNode(int row, int column) {
+ public Node getNode(int row, int column) {
return nodes.get(row).get(column);
}
- public final Output getOutput(int index) {
+ public Output getOutput(int index) {
return outputs.get(index);
}
- public final Input getInputs(int index) {
+ public Input getInput(int index) {
return inputs.get(index);
}
diff --git a/src/jcgp/population/InsufficientConnectionsException.java b/src/jcgp/population/InsufficientConnectionsException.java
new file mode 100644
index 0000000..807ec53
--- /dev/null
+++ b/src/jcgp/population/InsufficientConnectionsException.java
@@ -0,0 +1,10 @@
+package jcgp.population;
+
+public class InsufficientConnectionsException extends RuntimeException {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 660740514800883541L;
+
+}
diff --git a/src/jcgp/population/MutableElement.java b/src/jcgp/population/MutableElement.java
index 4397e46..5eae4ef 100644
--- a/src/jcgp/population/MutableElement.java
+++ b/src/jcgp/population/MutableElement.java
@@ -2,4 +2,6 @@ package jcgp.population;
public interface MutableElement {
+ public void setConnection(Connection newConnection);
+
}
diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java
index cce8dfd..8958475 100644
--- a/src/jcgp/population/Node.java
+++ b/src/jcgp/population/Node.java
@@ -1,5 +1,6 @@
package jcgp.population;
+import jcgp.CGP.Utilities;
import jcgp.function.Function;
@@ -8,21 +9,28 @@ public class Node implements MutableElement, Connection {
private Function function;
private Connection[] connections;
- public Node() {
-
- }
-
@Override
public int evaluate() {
- return function.run(connections[0], connections[1]);
+ return function.run(connections);
}
public void setFunction(Function newFunction) {
function = newFunction;
}
+ @Override
public void setConnection(Connection newConnection) {
+ connections[Utilities.getRandomInt(connections.length)] = newConnection;
+ }
+
+ public void initialise(Function newFunction, Connection ... newConnections) throws InsufficientConnectionsException {
+
+ function = newFunction;
+ if (newConnections.length >= Utilities.getMaxArity()) {
+ connections = newConnections;
+ } else {
+ throw new InsufficientConnectionsException();
+ }
}
-
}
diff --git a/src/jcgp/population/Output.java b/src/jcgp/population/Output.java
index 2f2df6e..1640deb 100644
--- a/src/jcgp/population/Output.java
+++ b/src/jcgp/population/Output.java
@@ -2,10 +2,17 @@ package jcgp.population;
public class Output implements MutableElement {
+
+ private Connection source;
public int calculate() {
- // TODO Auto-generated method stub
- return 0;
+ return source.evaluate();
+ }
+
+ @Override
+ public void setConnection(Connection newConnection) {
+ source = newConnection;
+
}
}
diff --git a/src/jcgp/population/Population.java b/src/jcgp/population/Population.java
index e1a9a3c..67b6695 100644
--- a/src/jcgp/population/Population.java
+++ b/src/jcgp/population/Population.java
@@ -3,14 +3,19 @@ package jcgp.population;
import java.util.ArrayList;
import java.util.Iterator;
+import jcgp.CGP.Parameters;
+
public final class Population implements Iterable<Chromosome> {
private ArrayList<Chromosome> population;
- public Population(int inputs, int rows, int columns, int outputs, int size) {
- population = new ArrayList<Chromosome>(size);
- for (int c = 0; c < size; c++) {
- population.add(new Chromosome(inputs, rows, columns, outputs));
+ public Population() {
+ population = new ArrayList<Chromosome>(Parameters.getPopulationSize());
+ for (int c = 0; c < Parameters.getPopulationSize(); c++) {
+ population.add(new Chromosome(Parameters.getInputs(),
+ Parameters.getRows(),
+ Parameters.getColumns(),
+ Parameters.getOutputs()));
}
}