aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/tests/NodeTests.java
blob: 74b8140a72d0139609a1b622fef0d93105145bd3 (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
package jcgp.tests;

import static org.junit.Assert.assertTrue;

import java.util.Random;

import jcgp.Utilities;
import jcgp.modules.function.Arithmetic;
import jcgp.modules.function.Function;
import jcgp.parameters.Parameters;
import jcgp.population.Chromosome;
import jcgp.population.Connection;
import jcgp.population.Node;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * 
 * Tests which cover the behaviour specified for a node.
 * 
 *  - A node should contain read-only row and column values which are set upon construction.
 *  - It should contain a fully accessible Function object.
 *  - It should contain a set of connections which can be initialised and randomly 
 *    modified. It should be able to return an addressed connection.
 *  - It should be able to compute a value using its function with its connections as arguments.
 *  
 *  WARNING: changing parameters may cause the tests to incorrectly fail!
 * 
 * @author Eduardo Pedroni
 *
 */
public class NodeTests {

	private Node node;
	private static Chromosome chromosome;
	// these numbers will be used by the node under test
	private final int arg1 = 2;
	private final int arg2 = 5;

	@BeforeClass
	public static void setUpBeforeClass() {
		// initialise utilities
		Utilities.setResources(new Random(1234), null);

		// initialise parameters
		Parameters.setColumns(0);
		Parameters.setRows(0);
		Parameters.setInputs(0);
		Parameters.setOutputs(0);
		Parameters.setLevelsBack(0);
		Parameters.setMutationRate(10);
		Parameters.setTotalGenerations(100);
		Parameters.setTotalRuns(5);
		Parameters.setMaxArity(2);

		chromosome = new Chromosome();
	}

	@Before
	public void setUp() throws Exception {
		node = new Node(chromosome, 0, 0);
		// make node with anonymous addition function and hard-coded value connections
		node.initialise(new Arithmetic.Addition(),
				new Connection[]{new Connection() {

					@Override
					public Object getValue() {
						// hardcode a value
						return arg1;
					}

					@Override
					public String getDescription() {
						// blank
						return null;
					}

				},
				new Connection() {

					@Override
					public Object getValue() {
						// hardcode a value
						return arg2;
					}

					@Override
					public String getDescription() {
						// blank
						return null;
					}

				}});
	}

	@Test
	public void rowAndColumnTest() {
		assertTrue("Incorrect row.", node.getRow() == 0);
		assertTrue("Incorrect column.", node.getColumn() == 0);
	}

	@Test
	public void functionTest() {
		// make a new function and assign to node
		Function f = new Function() {

			@Override
			public Object run(Connection... connections) {
				// blank
				return 0;
			}

			@Override
			public int getArity() {
				// blank
				return 0;
			}
		};

		node.setFunction(f);

		// check that the function returned by the node is f
		assertTrue("Incorrect function returned.", node.getFunction() == f);
		// check that it outputs 0 as it should
		assertTrue("Incorrect function output.", ((Integer) node.getValue()) == 0);
	}

	@Test
	public void evaluationTest() {
		// check that addition is working
		assertTrue("Node did not return expected value (sum of arguments). Output was: " + ((int) node.getValue()), 
				((int) node.getValue()) == arg1 + arg2);

		// put in a different function, check the output has changed appropriately
		node.setFunction(new Arithmetic.Subtraction());

		assertTrue("Node did not return expected value (difference of arguments).", ((Integer) node.getValue()) == arg1 - arg2);

	}

	@Test
	public void connectionsTest() {
		// make new blank connections, check that they are returned correctly when addressed
		Connection conn0, conn1, conn2;
		conn0 = new Connection() {

			@Override
			public Object getValue() {
				// blank
				return 0;
			}

			@Override
			public String getDescription() {
				// blank
				return null;
			}

		};
		conn1 = new Connection() {

			@Override
			public Object getValue() {
				// blank
				return 0;
			}

			@Override
			public String getDescription() {
				// blank
				return null;
			}

		};
		node.initialise(null, conn0, conn1);

		assertTrue("Connection 0 is incorrect.", node.getConnection(0) == conn0);
		assertTrue("Connection 1 is incorrect.", node.getConnection(1) == conn1);

		// make yet another connection, set it randomly, check that it is one of the node's connections
		conn2 = new Connection() {

			@Override
			public Object getValue() {
				// blank
				return 0;
			}

			@Override
			public String getDescription() {
				// blank
				return null;
			}
		};
		node.setConnection(conn2);

		assertTrue("Connection was not found in node.", node.getConnection(0) == conn2 || node.getConnection(1) == conn2);

	}


}