aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/population/Node.java
blob: d7013d337aa36bf57f894912c98fb32196d3d719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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> function;
	private ArrayList<SimpleObjectProperty<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);
	}

	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<Function> functionProperty() {
		return function;
	}

	public ArrayList<SimpleObjectProperty<Connection>> connections() {
		return connections;
	}
	
	public Connection getConnection(int index) {
		return connections.get(index).get();
	}

	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);
			}

		}
	}

	@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;
	}
}