-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestFunctions.js
More file actions
291 lines (207 loc) · 7.35 KB
/
TestFunctions.js
File metadata and controls
291 lines (207 loc) · 7.35 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
var testCount = 100000;
/* Draws a raycast from the center of the screen and rotates it
* The raycast will hit the circle and draw a line to the point of intersection
* Should be called in the Update function
*/
var testRaycastAngle = 0;
function TestRaycast(delta){
testRaycastAngle += .1 * delta;
var raycastResult = new RaycastResult2D();
var x = app.renderer.width / 2;
var y = app.renderer.height / 2;
// Get the direction of the sight line
var dirX = Math.cos(testRaycastAngle * Math.PI / 180);
var dirY = Math.sin(testRaycastAngle * Math.PI / 180);
// Draw the sight line
Raycast(raycastResult, x, y, dirX, dirY, 500, true);
}
/* Test the speed of the neural network
* Prints the time it takes to run 100,000 neural networks
*/
function TestNNSpeed(){
console.log("Testing neural network speed...");
var testarray = [];
for (var i = 0; i < testCount; i++)
testarray.push(new NeuralNetwork2(3, 3));
// Set random values for the neural network
for (var i = 0; i < testarray.length; i++) {
testarray[i].SetInput(0, Math.random());
testarray[i].SetInput(1, Math.random());
testarray[i].SetInput(2, Math.random());
}
var now = performance.now();
// Run the neural network
for (var i = 0; i < testarray.length; i++)
testarray[i].RunNN();
var end = performance.now();
console.log("NN speed: " + (end - now) + " ms for " + testCount + " neural networks");
}
/* Test the speed of the raycast function
* Prints the time it takes to run 100,000 raycasts
* Each raycast tests against a single circle
*/
function TestRaycastSpeed(){
console.log("Testing raycast speed...");
var food = new Food();
var testarray = [];
var xArray = [];
var yArray = [];
var dirXArray = [];
var dirYArray = [];
var hitCount = 0;
for (var i = 0; i < testCount; i++){
testarray.push(new RaycastResult2D());
xArray.push(Math.random() * 100);
yArray.push(Math.random() * 100);
var angle = Math.random() * 360;
dirXArray.push(Math.cos(angle * Math.PI / 180));
dirYArray.push(Math.sin(angle * Math.PI / 180));
}
var now = performance.now();
// Run the raycast
for (var i = 0; i < testarray.length; i++)
if (RaycastCircle(testarray[i], xArray[i], yArray[i], dirXArray[i], dirYArray[i], food, 1000))
hitCount++;
var end = performance.now();
console.log("Raycast time: " + (end - now) + " ms for " + testCount + " raycasts");
// Print the results
console.log("Hit count: " + hitCount);
}
/* Tests the add and remove node and connection mutations
* Adds and removes nodes and connections every second
*/
function TestAddRemoveNodeMutations(){
var nn = new NeatNN(4, 3);
var sec = 1;
// Draw the neural network
var graphics = new PIXI.Graphics();
app.stage.addChild(graphics);
nn.DrawNN(graphics, width, height);
for (var i = 0; i < 10; i++){
setTimeout(function () {
nn.MutateAddConnection();
nn.DrawNN(graphics, width, height);
}, 1000 * sec++);
}
for (var i = 0; i < 10; i++){
setTimeout(function () {
nn.MutateAddNode();
nn.DrawNN(graphics, width, height);
}, 1000 * sec++);
}
for (var i = 0; i < 10; i++){
setTimeout(function () {
nn.SetInput(0, Math.random() * 2 - 1);
nn.SetInput(1, Math.random() * 2 - 1);
nn.SetInput(2, Math.random() * 2 - 1);
nn.SetInput(3, Math.random() * 2 - 1);
nn.RunNN();
nn.DrawNN(graphics, width, height);
}, 1000 * sec++);
}
for (var i = 0; i < 10; i++){
setTimeout(function () {
nn.MutateRemoveNode();
nn.DrawNN(graphics, width, height);
}, 1000 * sec++);
}
for (var i = 0; i < 10; i++){
setTimeout(function () {
nn.MutateRemoveConnection();
nn.DrawNN(graphics, width, height);
}, 1000 * sec++);
}
}
/* Tests the mutation function
* Mutates the neural network every second
*/
function TestNetworkMutate(){
var nn = new NeatNN(4, 3);
// Draw the neural network
var graphics = new PIXI.Graphics();
app.stage.addChild(graphics);
nn.DrawNN(graphics, width, height);
// Run Mutate every second
window.setInterval(function(){
nn.Mutate();
nn.SetInput(0, Math.random() * 2 - 1);
nn.SetInput(1, Math.random() * 2 - 1);
nn.SetInput(2, Math.random() * 2 - 1);
nn.SetInput(3, Math.random() * 2 - 1);
nn.RunNN();
nn.DrawNN(graphics, width, height);
}, 200);
}
function TestNeatNNSpeed(){
var mutateCount = 100;
console.log("Testing NEAT neural network speed...");
var testarray = [];
for (var i = 0; i < testCount; i++)
testarray.push(new NeatNN(3, 3));
var beforeMutate = performance.now();
// Set random values for the neural network
for (var i = 0; i < testarray.length; i++) {
for (var j = 0; j < mutateCount; j++)
testarray[i].Mutate();
testarray[i].SetInput(0, Math.random());
testarray[i].SetInput(1, Math.random());
testarray[i].SetInput(2, Math.random());
}
var afterMutate = performance.now();
console.log("NEAT NN mutate time: " + (afterMutate - beforeMutate) + " ms for " + testCount + " neural networks and " + mutateCount + " mutations each");
var now = performance.now();
// Run the neural network
for (var i = 0; i < testarray.length; i++)
testarray[i].RunNN();
var end = performance.now();
console.log("NEAT NN speed: " + (end - now) + " ms for " + testCount + " neural networks");
}
/** Tests the clone function
* Creates a mutated neural network and clones it, then tests the cloned neural network
* to see if they produce the same output
* @param {boolean} printNN - If true, prints the neural network
*/
function TestNNClone(printNN = false){
console.log("Testing NN clone...");
var nn = new NeatNN(3, 3);
// Mutate the neural network
for (var i = 0; i < 100; i++)
nn.Mutate();
// Print the neural network if printNN is true
if (printNN){
console.log("Original NN");
nn.PrintNodes();
nn.PrintConnectionIndexes();
}
var clone = nn.Clone();
// Print the neural network if printNN is true
if (printNN){
console.log("Clone NN");
clone.PrintNodes();
clone.PrintConnectionIndexes();
}
// Give both neural networks random inputs
var input1 = Math.random() * 2 - 1;
var input2 = Math.random() * 2 - 1;
var input3 = Math.random() * 2 - 1;
nn.SetInput(0, input1);
nn.SetInput(1, input2);
nn.SetInput(2, input3);
clone.SetInput(0, input1);
clone.SetInput(1, input2);
clone.SetInput(2, input3);
// Run the neural networks
nn.RunNN();
clone.RunNN();
if (nn.GetOutput(0) != clone.GetOutput(0) ||
nn.GetOutput(1) != clone.GetOutput(1) ||
nn.GetOutput(2) != clone.GetOutput(2))
console.log("ERROR: The neural networks are not the same");
else
console.log("The neural networks produced the same outputs. Clone test passed");
}
// Test the speed of the neural network
//TestNNSpeed();
//TestNeatNNSpeed();
//TestNetworkMutate();
//TestNNClone();