package jcgp.population; import java.util.ArrayList; import javafx.beans.property.SimpleObjectProperty; import jcgp.exceptions.InsufficientConnectionsException; import jcgp.function.Function; public class Node extends Gene implements MutableElement, Connection { private SimpleObjectProperty function; private ArrayList> connections; private int column, row; private Chromosome chromosome; 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 ArrayList>(arity); for (int c = 0; c < arity; c++) { connections.add(new SimpleObjectProperty()); } } @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); } public void setFunction(Function newFunction) { function.set(newFunction); chromosome.recomputeActiveNodes(); } @Override public void setConnection(int index, Connection newConnection) { connections.get(index).set(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]); } } else { throw new InsufficientConnectionsException(); } } public int getColumn() { return column; } public int getRow() { return row; } public Function getFunction() { return function.get(); } public SimpleObjectProperty functionProperty() { return function; } public ArrayList> connections() { return connections; } public Connection getConnection(int index) { return connections.get(index).get(); } public void getActive(ArrayList 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); } } } @Override public boolean copyOf(MutableElement m) { if (this != m) { if (m instanceof Node) { Node n = (Node) m; if (function.get() == 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()) { 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()) { return false; } } else { return false; } } else { return false; } } return true; } } } } return false; } @Override public String getDescription() { return "n: " + row + ", " + column; } }