diff options
Diffstat (limited to 'src/jcgp/population')
-rw-r--r-- | src/jcgp/population/Chromosome.java | 45 | ||||
-rw-r--r-- | src/jcgp/population/Node.java | 62 | ||||
-rw-r--r-- | src/jcgp/population/Output.java | 32 | ||||
-rw-r--r-- | src/jcgp/population/Population.java | 10 |
4 files changed, 63 insertions, 86 deletions
diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java index 6f50025..297d298 100644 --- a/src/jcgp/population/Chromosome.java +++ b/src/jcgp/population/Chromosome.java @@ -55,22 +55,22 @@ public class Chromosome { * */ private void instantiateElements() { - inputs = new Input[((int) resources.get("inputs"))]; - for (int i = 0; i < ((int) resources.get("inputs")); i++) { + inputs = new Input[(resources.getInt("inputs"))]; + for (int i = 0; i < (resources.getInt("inputs")); i++) { inputs[i] = new Input(i); } - int arity = (int) resources.get("arity"); + int arity = resources.getInt("arity"); // rows first - 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 = new Node[(resources.getInt("rows"))][(resources.getInt("columns"))]; + for (int r = 0; r < (resources.getInt("rows")); r++) { + for (int c = 0; c < (resources.getInt("columns")); c++) { nodes[r][c] = new Node(this, r, c, arity); } } - outputs = new Output[((int) resources.get("outputs"))]; - for (int o = 0; o < ((int) resources.get("outputs")); o++) { + outputs = new Output[(resources.getInt("outputs"))]; + for (int o = 0; o < (resources.getInt("outputs")); o++) { outputs[o] = new Output(this, o); } } @@ -80,7 +80,7 @@ public class Chromosome { */ private void initialiseConnections() { - int arity = (int) resources.get("arity"); + int arity = resources.getInt("arity"); // initialise nodes - [rows][columns] for (int r = 0; r < nodes.length; r++) { @@ -103,7 +103,7 @@ public class Chromosome { * @param clone */ public void copyConnections(Chromosome clone) { - int arity = (int) resources.get("arity"); + int arity = resources.getInt("arity"); // copy nodes - [rows][columns] for (int r = 0; r < nodes.length; r++) { @@ -186,7 +186,7 @@ public class Chromosome { */ public MutableElement getRandomMutableElement() { // choose output or node - int index = resources.getRandomInt(outputs.length + ((int) resources.get("rows")) * ((int) resources.get("columns"))); + int index = resources.getRandomInt(outputs.length + (resources.getInt("rows")) * (resources.getInt("columns"))); if (index < outputs.length) { // outputs @@ -194,7 +194,7 @@ public class Chromosome { } else { // node index -= outputs.length; - return nodes[index / ((int) resources.get("columns"))][index % ((int) resources.get("columns"))]; + return nodes[index / (resources.getInt("columns"))][index % (resources.getInt("columns"))]; } } @@ -208,7 +208,7 @@ public class Chromosome { */ public Connection getRandomConnection(int column) { // work out the allowed range obeying levels back - int allowedColumns = ((column >= ((int) resources.get("levelsBack"))) ? ((int) resources.get("levelsBack")) : column); + int allowedColumns = ((column >= (resources.getInt("levelsBack"))) ? (resources.getInt("levelsBack")) : column); int offset = ((column - allowedColumns) * nodes.length) - inputs.length; // choose input or allowed node @@ -235,15 +235,14 @@ public class Chromosome { */ public Connection getRandomConnection() { // choose output or node - int index = resources.getRandomInt(inputs.length + ((int) resources.get("columns")) * ((int) resources.get("rows"))); - + int index = resources.getRandomInt(inputs.length + (resources.getInt("columns")) * (resources.getInt("rows"))); if (index < inputs.length) { // outputs return inputs[index]; } else { // node index -= inputs.length; - return nodes[index / ((int) resources.get("columns"))][index % ((int) resources.get("columns"))]; + return nodes[index / (resources.getInt("columns"))][index % (resources.getInt("columns"))]; } } @@ -278,15 +277,15 @@ public class Chromosome { } public boolean compareTo(Chromosome chromosome) { - for (int r = 0; r < ((int) resources.get("rows")); r++) { - for (int c = 0; c < ((int) resources.get("columns")); c++) { + for (int r = 0; r < (resources.getInt("rows")); r++) { + for (int c = 0; c < (resources.getInt("columns")); c++) { if (!(nodes[r][c].copyOf(chromosome.getNode(r, c)))) { return false; } } } - for (int o = 0; o < ((int) resources.get("outputs")); o++) { + for (int o = 0; o < (resources.getInt("outputs")); o++) { if (!(outputs[o].copyOf(chromosome.getOutput(o)))) { return false; } @@ -311,11 +310,11 @@ public class Chromosome { } public void printNodes() { - int arity = (int) resources.get("arity"); + int arity = resources.getInt("arity"); - for (int r = 0; r < ((int) resources.get("rows")); r++) { + for (int r = 0; r < (resources.getInt("rows")); r++) { System.out.print("r: " + r + "\t"); - for (int c = 0; c < ((int) resources.get("columns")); c++) { + for (int c = 0; c < (resources.getInt("columns")); c++) { System.out.print("N: (" + r + ", " + c + ") "); for (int i = 0; i < arity; i++) { System.out.print("C" + i + ": (" + nodes[r][c].getConnection(i).getDescription() + ") "); @@ -325,7 +324,7 @@ public class Chromosome { System.out.print("\n"); } - for (int o = 0; o < ((int) resources.get("outputs")); o++) { + for (int o = 0; o < (resources.getInt("outputs")); o++) { System.out.print("o: " + o + " (" + outputs[o].getSource().getDescription() + ")\t"); } } diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java index d7013d3..141a32a 100644 --- a/src/jcgp/population/Node.java +++ b/src/jcgp/population/Node.java @@ -1,58 +1,46 @@ package jcgp.population; import java.util.ArrayList; +import java.util.Arrays; -import javafx.beans.property.SimpleObjectProperty; import jcgp.exceptions.InsufficientConnectionsException; import jcgp.function.Function; public class Node extends Gene implements MutableElement, Connection { - private SimpleObjectProperty<Function> function; - private ArrayList<SimpleObjectProperty<Connection>> connections; + private Function function; + private Connection[] connections; private int column, row; private Chromosome chromosome; public Node(Chromosome chromosome, int row, int column, int arity) { - this.function = new SimpleObjectProperty<Function>(); this.chromosome = chromosome; this.column = column; this.row = row; - this.connections = new ArrayList<SimpleObjectProperty<Connection>>(arity); - for (int c = 0; c < arity; c++) { - connections.add(new SimpleObjectProperty<Connection>()); - } } @Override public Object getValue() { //System.out.print("Calculating node: (" + row + ", " + column + ") > "); - Connection[] list = new Connection[function.get().getArity()]; - for (int i = 0; i < list.length; i++) { - list[i] = connections.get(i).get(); - } - - return function.get().run(list); + return function.run(Arrays.copyOfRange(connections, 0, function.getArity())); } public void setFunction(Function newFunction) { - function.set(newFunction); + function = newFunction; chromosome.recomputeActiveNodes(); } @Override public void setConnection(int index, Connection newConnection) { - connections.get(index).set(newConnection); + connections[index] = newConnection; chromosome.recomputeActiveNodes(); } public void initialise(Function newFunction, Connection ... newConnections) throws InsufficientConnectionsException { - function.set(newFunction); - if (newConnections.length >= connections.size()) { - for (int i = 0; i < connections.size(); i++) { - connections.get(i).set(newConnections[i]); - } + function = newFunction; + if (newConnections.length == function.getArity()) { + connections = newConnections; } else { throw new InsufficientConnectionsException(); } @@ -67,28 +55,20 @@ public class Node extends Gene implements MutableElement, Connection { } public Function getFunction() { - return function.get(); - } - - public SimpleObjectProperty<Function> functionProperty() { return function; } - - public ArrayList<SimpleObjectProperty<Connection>> connections() { - return connections; - } public Connection getConnection(int index) { - return connections.get(index).get(); + return connections[index]; } public void getActive(ArrayList<Node> activeNodes) { if (!activeNodes.contains(this)) { activeNodes.add(this); } - for (int i = 0; i < function.get().getArity(); i++) { - if (connections.get(i).get() instanceof Node) { - ((Node) connections.get(i).get()).getActive(activeNodes); + for (int i = 0; i < function.getArity(); i++) { + if (connections[i] instanceof Node) { + ((Node) connections[i]).getActive(activeNodes); } } @@ -99,17 +79,17 @@ public class Node extends Gene implements MutableElement, Connection { if (this != m) { if (m instanceof Node) { Node n = (Node) m; - if (function.get() == n.getFunction()) { + if (function == n.getFunction()) { if (column == n.getColumn() && row == n.getRow()) { - for (int i = 0; i < connections.size(); i++) { - if (connections.get(i).get() != n.getConnection(i)) { - if (connections.get(i).get() instanceof Input && n.getConnection(i) instanceof Input) { - if (((Input) connections.get(i).get()).getIndex() != ((Input) n.getConnection(i)).getIndex()) { + for (int i = 0; i < connections.length; i++) { + if (connections[i] != n.getConnection(i)) { + if (connections[i] instanceof Input && n.getConnection(i) instanceof Input) { + if (((Input) connections[i]).getIndex() != ((Input) n.getConnection(i)).getIndex()) { return false; } - } else if (connections.get(i).get() instanceof Node && n.getConnection(i) instanceof Node) { - if (((Node) connections.get(i).get()).getRow() != ((Node) n.getConnection(i)).getRow() && - ((Node) connections.get(i).get()).getColumn() != ((Node) n.getConnection(i)).getColumn()) { + } else if (connections[i] instanceof Node && n.getConnection(i) instanceof Node) { + if (((Node) connections[i]).getRow() != ((Node) n.getConnection(i)).getRow() && + ((Node) connections[i]).getColumn() != ((Node) n.getConnection(i)).getColumn()) { return false; } } else { diff --git a/src/jcgp/population/Output.java b/src/jcgp/population/Output.java index b7c6128..5cc6dcf 100644 --- a/src/jcgp/population/Output.java +++ b/src/jcgp/population/Output.java @@ -2,18 +2,16 @@ package jcgp.population; import java.util.ArrayList; -import javafx.beans.property.SimpleObjectProperty; - public class Output extends Gene implements MutableElement { - private SimpleObjectProperty<Connection> source; + private Connection source; private Chromosome chromosome; private int index; public Output(Chromosome chromosome, int index) { this.chromosome = chromosome; this.index = index; - this.source = new SimpleObjectProperty<Connection>(); + //this.source = new SimpleObjectProperty<Connection>(); } public Object calculate() { @@ -23,7 +21,7 @@ public class Output extends Gene implements MutableElement { @Override public void setConnection(int index, Connection newConnection) { - source.set(newConnection); + source = newConnection; chromosome.recomputeActiveNodes(); } @@ -32,16 +30,16 @@ public class Output extends Gene implements MutableElement { } public Connection getSource() { - return source.get(); - } - - public SimpleObjectProperty<Connection> sourceProperty() { return source; } + +// public SimpleObjectProperty<Connection> sourceProperty() { +// return source; +// } public void getActiveNodes(ArrayList<Node> activeNodes) { - if (source.get() instanceof Node) { - ((Node) source.get()).getActive(activeNodes); + if (source instanceof Node) { + ((Node) source).getActive(activeNodes); } } @@ -51,14 +49,14 @@ public class Output extends Gene implements MutableElement { if (m instanceof Output) { Output o = (Output) m; if (index == o.getIndex()) { - if (source.get() != o.getSource()) { - if (source.get() instanceof Input && o.getSource() instanceof Input) { - if (((Input) source.get()).getIndex() == ((Input) o.getSource()).getIndex()) { + if (source != o.getSource()) { + if (source instanceof Input && o.getSource() instanceof Input) { + if (((Input) source).getIndex() == ((Input) o.getSource()).getIndex()) { return true; } - } else if (source.get() instanceof Node && o.getSource() instanceof Node) { - if (((Node) source.get()).getRow() == ((Node) o.getSource()).getRow() && - ((Node) source.get()).getColumn() == ((Node) o.getSource()).getColumn()) { + } else if (source instanceof Node && o.getSource() instanceof Node) { + if (((Node) source).getRow() == ((Node) o.getSource()).getRow() && + ((Node) source).getColumn() == ((Node) o.getSource()).getColumn()) { return true; } } diff --git a/src/jcgp/population/Population.java b/src/jcgp/population/Population.java index 586f556..f5e0517 100644 --- a/src/jcgp/population/Population.java +++ b/src/jcgp/population/Population.java @@ -8,15 +8,15 @@ public class Population { private Chromosome[] chromosomes; private int fittest; - public Population(Resources parameters) { - chromosomes = new Chromosome[((int) parameters.get("popSize"))]; + public Population(Resources resources) { + chromosomes = new Chromosome[(resources.getInt("popSize"))]; for (int c = 0; c < chromosomes.length; c++) { - chromosomes[c] = new Chromosome(parameters); + chromosomes[c] = new Chromosome(resources); } } - public Population(Chromosome parent, Resources parameters) { - chromosomes = new Chromosome[((int) parameters.get("popSize"))]; + public Population(Chromosome parent, Resources resources) { + chromosomes = new Chromosome[(resources.getInt("popSize"))]; // make a clone for safety this.chromosomes[0] = new Chromosome(parent); // generate the rest of the individuals |