aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/population/Node.java
blob: 6696694b39ba5a5892a38627d5905d91814df3e4 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package jcgp.backend.population;

import java.util.ArrayList;

import jcgp.backend.function.Function;
import jcgp.backend.resources.Resources;

/**
 * Nodes make up the main part of the chromosome, 
 * where the actual functions are evolved. Each node
 * contains a function and a number of connections.
 * The node outputs the result of performing its function
 * on the values of its connections. Nodes therefore
 * implement both {@code Mutable} and {@code Connection}
 * since they can be mutated but also connected to. 
 * Nodes are constructed with a fixed number of connections
 * (determined by the maximum arity of the function set)
 * and must be reinstantiated if the experiment arity
 * changes.
 * <br><br>
 * When mutating a node, it is easiest to use {@code mutate()}.
 * Alternatively, you may also perform a specific mutation using
 * {@code setConnection(...)} and {@code setFunction(...)}.
 * 
 * @author Eduardo Pedroni
 *
 */
public class Node implements Mutable, Connection {

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

	/**
	 * Constructs a new instance of {@code Node} with the
	 * specified parameters. Nodes must contain their
	 * own row and column for ease of copying.
	 * 
	 * @param chromosome the chromosome this node belongs to.
	 * @param row the node's row.
	 * @param column the node's column.
	 */
	public Node(Chromosome chromosome, int row, int column) {
		this.chromosome = chromosome;
		this.column = column;
		this.row = row;
	}
	
	/**
	 * Initialises the node with the specified values.
	 * The number of connections passed as argument must
	 * be exactly the same as the experiment arity, or 
	 * an {@code IllegalArgumentException} will be thrown.
	 * 
	 * @param newFunction the node function to set.
	 * @param newConnections the node connections to set.
	 */
	public void initialise(Function newFunction, Connection... newConnections) {
		function = newFunction;
		if (newConnections.length == chromosome.getResources().arity()) {
			connections = newConnections;
		} else {
			throw new IllegalArgumentException("Received " + newConnections.length + " connections but needed exactly " + chromosome.getResources().arity());
		}
	}

	/**
	 * @return this node's column.
	 */
	public int getColumn() {
		return column;
	}

	/**
	 * @return this node's row.
	 */
	public int getRow() {
		return row;
	}

	/**
	 * @return this node's function.
	 */
	public Function getFunction() {
		return function;
	}
	
	/**
	 * Sets the node function.
	 * 
	 * @param newFunction the new function to set.
	 */
	public void setFunction(Function newFunction) {
		function = newFunction;
	}
	
	/**
	 * @param index the connection to return.
	 * @return the indexed connection.
	 */
	public Connection getConnection(int index) {
		return connections[index];
	}
	
	/**
	 * This method sets the indexed connection to the specified new connection.
	 * If the given connection is null or disrespects levels back, it is discarded
	 * and no connections are changed.
	 * 
	 * @param index the connection index to set.
	 * @param newConnection the {@code Connection} to connect to.
	 */
	public void setConnection(int index, Connection newConnection) {
		// connection must not be null
		if (newConnection != null) {
			connections[index] = newConnection;
			chromosome.recomputeActiveNodes();
		}
	}

	/**
	 * For package use, this is a recursive method
	 * used to create a collection of active nodes
	 * in the chromosome. If this node is not already
	 * in the active node list, this method adds it.
	 * It then calls {@code getActive()} on each of its
	 * connections, therefore recursively adding every
	 * single active node to the given list.
	 * 
	 * @param activeNodes the list of active nodes being built.
	 */
	protected void getActive(ArrayList<Node> activeNodes) {
		// don't add the same node twice
		if (!activeNodes.contains(this)) {
			activeNodes.add(this);
		}
		// call getActive on all connections - they are all active
		for (int i = 0; i < function.getArity(); i++) {
			if (connections[i] instanceof Node) {
				((Node) connections[i]).getActive(activeNodes);
			}
		}
	}

	@Override
	public boolean copyOf(Mutable element) {
		// both cannot be the same instance
		if (this != element) {
			// element must be instance of node
			if (element instanceof Node) {
				Node n = (Node) element;
				// must have the same function
				if (function == n.getFunction()) {
					// row and column must be the same
					if (column == n.getColumn() && row == n.getRow()) {
						// connections must be the equivalent, but not the same instance
						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;
							}
						}
						// all connections checked, this really is a copy
						return true;
					}
				}
			}
		}
		return false;
	}
	
	@Override
	public Object getValue() {
		// build list of arguments recursively
		Object[] args = new Object[function.getArity()];
		for (int i = 0; i < function.getArity(); i++) {
			args[i] = connections[i].getValue();
		}
		// return function result
		return function.run(args);
	}

	@Override
	public void mutate() {
		Resources resources = chromosome.getResources();
		
		// choose to mutate the function or a connection
		int geneType = resources.getRandomInt(1 + resources.arity());
		
		// if the int is less than 1, mutate function, else mutate connections 
		if (geneType < 1) {			
			setFunction(resources.getRandomFunction());
		} else {
			// if we decided to mutate connection, subtract 1 from geneType so it fits into the arity range
			geneType--;
			setConnection(geneType, chromosome.getRandomConnection(column));
		}
	}

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