aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/population/Node.java
blob: 6a558a4a2c601f4ec63a9d16b3fb6226426047f6 (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
package jcgp.backend.population;

import java.util.ArrayList;
import java.util.Arrays;

import jcgp.backend.exceptions.InsufficientConnectionsException;
import jcgp.backend.function.Function;


public class Node extends Gene implements MutableElement, Connection {

	private Function function;
	private Connection[] connections;
	private int column, row;
	private Chromosome chromosome;

	public Node(Chromosome chromosome, int row, int column, int arity) {
		this.chromosome = chromosome;
		this.column = column;
		this.row = row;
	}

	@Override
	public Object getValue() {
		return function.run(Arrays.copyOfRange(connections, 0, function.getArity()));
	}

	public void setFunction(Function newFunction) {
		function = newFunction;
	}

	@Override
	public void setConnection(int index, Connection newConnection) {
		connections[index] = newConnection;
		chromosome.recomputeActiveNodes();
	}

	public void initialise(Function newFunction, Connection ... newConnections) throws InsufficientConnectionsException {
		function = newFunction;
		if (newConnections.length == chromosome.getResources().arity()) {
			connections = newConnections;
		} else {
			throw new InsufficientConnectionsException();
		}
	}

	public int getColumn() {
		return column;
	}

	public int getRow() {
		return row;
	}

	public Function getFunction() {
		return function;
	}
	
	public Connection getConnection(int index) {
		return connections[index];
	}

	public void getActive(ArrayList<Node> 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);
			}

		}
	}

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

	@Override
	public String toString() {
		return "Node [" + row + ", " + column + "]";
	}
}