From 0c288cc1952809294c8d70d86b9f41b04878ac2e Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 23 Mar 2014 18:05:13 +0000 Subject: Majorly refactored, node grid is fully implemented. About to attempt active path locking. --- src/jcgp/population/Node.java | 75 ++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 30 deletions(-) (limited to 'src/jcgp/population/Node.java') diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java index 9fafe32..d7013d3 100644 --- a/src/jcgp/population/Node.java +++ b/src/jcgp/population/Node.java @@ -1,51 +1,58 @@ package jcgp.population; import java.util.ArrayList; -import java.util.Arrays; -import jcgp.Utilities; +import javafx.beans.property.SimpleObjectProperty; import jcgp.exceptions.InsufficientConnectionsException; -import jcgp.modules.function.Function; -import jcgp.parameters.Parameters; +import jcgp.function.Function; -public class Node implements MutableElement, Connection { +public class Node extends Gene implements MutableElement, Connection { - private Function function; - private Connection[] connections; + private SimpleObjectProperty function; + private ArrayList> connections; private int column, row; private Chromosome chromosome; - public Node(Chromosome chromosome, int row, int column) { + public Node(Chromosome chromosome, int row, int column, int arity) { + this.function = new SimpleObjectProperty(); this.chromosome = chromosome; this.column = column; this.row = row; - this.connections = new Connection[((int) Parameters.get("maxArity").getValue())]; + this.connections = new ArrayList>(arity); + for (int c = 0; c < arity; c++) { + connections.add(new SimpleObjectProperty()); + } } @Override public Object getValue() { //System.out.print("Calculating node: (" + row + ", " + column + ") > "); - return function.run(Arrays.copyOfRange(connections, 0, function.getArity())); + 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); } public void setFunction(Function newFunction) { - function = newFunction; + function.set(newFunction); chromosome.recomputeActiveNodes(); } @Override - public void setConnection(Connection newConnection) { - connections[Utilities.getRandomInt(connections.length)] = newConnection; + public void setConnection(int index, Connection newConnection) { + connections.get(index).set(newConnection); chromosome.recomputeActiveNodes(); } public void initialise(Function newFunction, Connection ... newConnections) throws InsufficientConnectionsException { - - function = newFunction; - - if (newConnections.length >= ((int) Parameters.get("maxArity").getValue())) { - connections = newConnections; + function.set(newFunction); + if (newConnections.length >= connections.size()) { + for (int i = 0; i < connections.size(); i++) { + connections.get(i).set(newConnections[i]); + } } else { throw new InsufficientConnectionsException(); } @@ -60,20 +67,28 @@ public class Node implements MutableElement, Connection { } public Function getFunction() { + return function.get(); + } + + public SimpleObjectProperty functionProperty() { return function; } + public ArrayList> connections() { + return connections; + } + public Connection getConnection(int index) { - return connections[index]; + return connections.get(index).get(); } public void getActive(ArrayList activeNodes) { if (!activeNodes.contains(this)) { activeNodes.add(this); } - for (int i = 0; i < function.getArity(); i++) { - if (connections[i] instanceof Node) { - ((Node) connections[i]).getActive(activeNodes); + for (int i = 0; i < function.get().getArity(); i++) { + if (connections.get(i).get() instanceof Node) { + ((Node) connections.get(i).get()).getActive(activeNodes); } } @@ -84,17 +99,17 @@ public class Node implements MutableElement, Connection { if (this != m) { if (m instanceof Node) { Node n = (Node) m; - if (function == n.getFunction()) { + if (function.get() == 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()) { + 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()) { 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()) { + } 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()) { return false; } } else { -- cgit v1.2.3