diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-03-23 18:05:13 +0000 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-03-23 18:05:13 +0000 |
commit | 0c288cc1952809294c8d70d86b9f41b04878ac2e (patch) | |
tree | ef9671b711fe665a3156594663c083595861a4e6 /src/jcgp/population/Chromosome.java | |
parent | d3527a63e12c0e5288f1e7d2e2dc18e61d16b760 (diff) |
Majorly refactored, node grid is fully implemented. About to attempt active path locking.
Diffstat (limited to 'src/jcgp/population/Chromosome.java')
-rw-r--r-- | src/jcgp/population/Chromosome.java | 85 |
1 files changed, 50 insertions, 35 deletions
diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java index 1f0b312..a7f2ed3 100644 --- a/src/jcgp/population/Chromosome.java +++ b/src/jcgp/population/Chromosome.java @@ -2,11 +2,12 @@ package jcgp.population; import java.util.ArrayList; -import jcgp.Utilities; +import jcgp.CGP.Resources; import jcgp.exceptions.ParameterMismatchException; -import jcgp.parameters.Parameters; public class Chromosome { + + private Resources resources; private Input[] inputs; private Node[][] nodes; @@ -21,13 +22,12 @@ public class Chromosome { * Initialise a chromosome with the specified parameters. Random valid connections * are created. * - * @param outputs - * @param columns - * @param rows - * @param inputs * */ - public Chromosome() { + public Chromosome(Resources resources) { + // store a reference to the parameters + this.resources = resources; + // allocate memory for all elements of the chromosome instantiateElements(); // set random connections so that the chromosome can be evaluated @@ -42,6 +42,9 @@ public class Chromosome { * @param clone the chromosome to be copied */ public Chromosome(Chromosome clone) { + // store a reference to the parameters + this.resources = clone.getParameters(); + // allocate memory for all elements of the chromosome instantiateElements(); // initialise all connections based on argument @@ -52,20 +55,22 @@ public class Chromosome { * */ private void instantiateElements() { - inputs = new Input[((int) Parameters.get("inputs").getValue())]; - for (int i = 0; i < ((int) Parameters.get("inputs").getValue()); i++) { + inputs = new Input[((int) resources.get("inputs"))]; + for (int i = 0; i < ((int) resources.get("inputs")); i++) { inputs[i] = new Input(i); } + int arity = (int) resources.get("arity"); + // rows first - nodes = new Node[((int) Parameters.get("rows").getValue())][((int) Parameters.get("columns").getValue())]; - for (int r = 0; r < ((int) Parameters.get("rows").getValue()); r++) { - for (int c = 0; c < ((int) Parameters.get("columns").getValue()); c++) { - nodes[r][c] = new Node(this, r, c); + nodes = new Node[((int) resources.get("rows"))][((int) resources.get("columns"))]; + for (int r = 0; r < ((int) resources.get("rows")); r++) { + for (int c = 0; c < ((int) resources.get("columns")); c++) { + nodes[r][c] = new Node(this, r, c, arity); } } - outputs = new Output[((int) Parameters.get("outputs").getValue())]; - for (int o = 0; o < ((int) Parameters.get("outputs").getValue()); o++) { + outputs = new Output[((int) resources.get("outputs"))]; + for (int o = 0; o < ((int) resources.get("outputs")); o++) { outputs[o] = new Output(this, o); } } @@ -75,19 +80,21 @@ public class Chromosome { */ private void initialiseConnections() { + int arity = (int) resources.get("arity"); + // initialise nodes - [rows][columns] for (int r = 0; r < nodes.length; r++) { for (int c = 0; c < nodes[r].length; c++) { - Connection[] connections = new Connection[((int) Parameters.get("maxArity").getValue())]; + Connection[] connections = new Connection[arity]; for (int i = 0; i < connections.length; i++) { connections[i] = getRandomConnection(c); } - nodes[r][c].initialise(Utilities.getRandomFunction(), connections); + nodes[r][c].initialise(resources.getRandomFunction(), connections); } } for (Output output : outputs) { - output.setConnection(getRandomConnection()); + output.setConnection(0, getRandomConnection()); } } @@ -96,11 +103,13 @@ public class Chromosome { * @param clone */ public void copyConnections(Chromosome clone) { + int arity = (int) resources.get("arity"); + // copy nodes - [rows][columns] for (int r = 0; r < nodes.length; r++) { for (int c = 0; c < nodes[r].length; c++) { // make array of connections to initialise with - Connection[] connections = new Connection[((int) Parameters.get("maxArity").getValue())]; + Connection[] connections = new Connection[arity]; // populate with connections equivalent to clone Connection copyConnection; for (int i = 0; i < connections.length; i++) { @@ -123,9 +132,9 @@ public class Chromosome { for (int o = 0; o < outputs.length; o++) { copyOutput = clone.getOutput(o).getSource(); if (copyOutput instanceof Input) { - outputs[o].setConnection(inputs[((Input) copyOutput).getIndex()]); + outputs[o].setConnection(0, inputs[((Input) copyOutput).getIndex()]); } else if (copyOutput instanceof Node) { - outputs[o].setConnection(nodes[((Node) copyOutput).getRow()][((Node) copyOutput).getColumn()]); + outputs[o].setConnection(0, nodes[((Node) copyOutput).getRow()][((Node) copyOutput).getColumn()]); } else { // something bad happened System.out.println("Warning: Connection of subtype " + copyOutput.getClass().toString() + " is not explicitly handled by copy constructor."); @@ -177,7 +186,7 @@ public class Chromosome { */ public MutableElement getRandomMutableElement() { // choose output or node - int index = Utilities.getRandomInt(outputs.length + ((int) Parameters.get("rows").getValue()) * ((int) Parameters.get("columns").getValue())); + int index = resources.getRandomInt(outputs.length + ((int) resources.get("rows")) * ((int) resources.get("columns"))); if (index < outputs.length) { // outputs @@ -185,7 +194,7 @@ public class Chromosome { } else { // node index -= outputs.length; - return nodes[index / ((int) Parameters.get("columns").getValue())][index % ((int) Parameters.get("columns").getValue())]; + return nodes[index / ((int) resources.get("columns"))][index % ((int) resources.get("columns"))]; } } @@ -199,11 +208,11 @@ public class Chromosome { */ public Connection getRandomConnection(int column) { // work out the allowed range obeying levels back - int allowedColumns = ((column >= ((int) Parameters.get("levelsBack").getValue())) ? ((int) Parameters.get("levelsBack").getValue()) : column); + int allowedColumns = ((column >= ((int) resources.get("levelsBack"))) ? ((int) resources.get("levelsBack")) : column); int offset = ((column - allowedColumns) * nodes.length) - inputs.length; // choose input or allowed node - int index = Utilities.getRandomInt(inputs.length + (nodes.length * allowedColumns)); + int index = resources.getRandomInt(inputs.length + (nodes.length * allowedColumns)); if (index < inputs.length) { // input return inputs[index]; @@ -226,7 +235,7 @@ public class Chromosome { */ public Connection getRandomConnection() { // choose output or node - int index = Utilities.getRandomInt(inputs.length + ((int) Parameters.get("columns").getValue()) * ((int) Parameters.get("rows").getValue())); + int index = resources.getRandomInt(inputs.length + ((int) resources.get("columns")) * ((int) resources.get("rows"))); if (index < inputs.length) { // outputs @@ -234,7 +243,7 @@ public class Chromosome { } else { // node index -= inputs.length; - return nodes[index / ((int) Parameters.get("columns").getValue())][index % ((int) Parameters.get("columns").getValue())]; + return nodes[index / ((int) resources.get("columns"))][index % ((int) resources.get("columns"))]; } } @@ -269,15 +278,15 @@ public class Chromosome { } public boolean compareTo(Chromosome chromosome) { - for (int r = 0; r < ((int) Parameters.get("rows").getValue()); r++) { - for (int c = 0; c < ((int) Parameters.get("columns").getValue()); c++) { + for (int r = 0; r < ((int) resources.get("rows")); r++) { + for (int c = 0; c < ((int) resources.get("columns")); c++) { if (!(nodes[r][c].copyOf(chromosome.getNode(r, c)))) { return false; } } } - for (int o = 0; o < ((int) Parameters.get("outputs").getValue()); o++) { + for (int o = 0; o < ((int) resources.get("outputs")); o++) { if (!(outputs[o].copyOf(chromosome.getOutput(o)))) { return false; } @@ -302,20 +311,26 @@ public class Chromosome { } public void printNodes() { - for (int r = 0; r < ((int) Parameters.get("rows").getValue()); r++) { + int arity = (int) resources.get("arity"); + + for (int r = 0; r < ((int) resources.get("rows")); r++) { System.out.print("r: " + r + "\t"); - for (int c = 0; c < ((int) Parameters.get("columns").getValue()); c++) { + for (int c = 0; c < ((int) resources.get("columns")); c++) { System.out.print("N: (" + r + ", " + c + ") "); - for (int i = 0; i < ((int) Parameters.get("maxArity").getValue()); i++) { + for (int i = 0; i < arity; i++) { System.out.print("C" + i + ": (" + nodes[r][c].getConnection(i).getDescription() + ") "); } - System.out.print("F: " + nodes[r][c].getFunction().toString() + "\t"); + System.out.print("F: " + nodes[r][c].getFunction().getName() + "\t"); } System.out.print("\n"); } - for (int o = 0; o < ((int) Parameters.get("outputs").getValue()); o++) { + for (int o = 0; o < ((int) resources.get("outputs")); o++) { System.out.print("o: " + o + " (" + outputs[o].getSource().getDescription() + ")\t"); } } + + public Resources getParameters() { + return resources; + } } |