aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/population/Node.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/population/Node.java')
-rw-r--r--src/jcgp/population/Node.java62
1 files changed, 21 insertions, 41 deletions
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 {