diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-13 22:41:26 +0000 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-13 22:41:26 +0000 |
commit | 6419b69faeb4736db1ccb50cfa5a030f9aa818aa (patch) | |
tree | ca424a5ea85abf044cd4b22c3c43608163ae5f74 /src/jcgp/population/Node.java | |
parent | 3326c58f4d2d7e8c77738277dcd093aa864ad2a5 (diff) |
Added methods in Chromosome to compare active and all nodes. Associated tests also written.
Diffstat (limited to 'src/jcgp/population/Node.java')
-rw-r--r-- | src/jcgp/population/Node.java | 60 |
1 files changed, 48 insertions, 12 deletions
diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java index c09532c..b2e29df 100644 --- a/src/jcgp/population/Node.java +++ b/src/jcgp/population/Node.java @@ -1,6 +1,7 @@ package jcgp.population; import java.util.ArrayList; +import java.util.Arrays; import jcgp.Parameters; import jcgp.Utilities; @@ -8,7 +9,7 @@ import jcgp.function.Function; public class Node implements MutableElement, Connection { - + private Function function; private Connection[] connections; private int column, row; @@ -18,28 +19,29 @@ public class Node implements MutableElement, Connection { this.chromosome = chromosome; this.column = column; this.row = row; + this.connections = new Connection[Parameters.getMaxArity()]; } - + @Override public int getValue() { - return function.run(connections); + return function.run(Arrays.copyOfRange(connections, 0, function.getArity())); } - + public void setFunction(Function newFunction) { function = newFunction; chromosome.recomputeActiveNodes(); } - + @Override public void setConnection(Connection newConnection) { connections[Utilities.getRandomInt(connections.length)] = newConnection; chromosome.recomputeActiveNodes(); } - + public void initialise(Function newFunction, Connection ... newConnections) throws InsufficientConnectionsException { - + function = newFunction; - + if (newConnections.length >= Parameters.getMaxArity()) { connections = newConnections; } else { @@ -48,7 +50,6 @@ public class Node implements MutableElement, Connection { } public int getColumn() { - return column; } @@ -64,13 +65,48 @@ public class Node implements MutableElement, Connection { return connections[index]; } - @Override - public void getActive(ArrayList<Connection> activeNodes) { + public void getActive(ArrayList<Node> activeNodes) { if (!activeNodes.contains(this)) { activeNodes.add(this); } for (int i = 0; i < function.getArity(); i++) { - connections[i].getActive(activeNodes); + if (connections[i] instanceof Node) { + ((Node) connections[i]).getActive(activeNodes); + } + + } + } + + @Override + public boolean copyOf(MutableElement m) { + if (this != m) { + if (m instanceof Node) { + Node n = (Node) m; + if (function == n.getFunction()) { + if (column == n.getColumn() && row == n.getRow()) { + 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[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 { + return false; + } + } else { + return false; + } + } + return true; + } + } + } } + return false; } } |