-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNeuralNetwork.cpp
800 lines (741 loc) · 27.7 KB
/
NeuralNetwork.cpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
#include "NeuralNetwork.h"
#include <queue>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
// Default Constructor
NeuralNetwork::NeuralNetwork()
{
char buff[200];
sprintf(buff, "Creating Neural Network.\nCue initialization seed: %d\nNumber of Reservoirs: %d\nReservoir dimension size: %dx%dx%d\nNumber of max inputs: %d\nMax axon length: %d\nMax number of synapses: %d\n",
CUE_SEED, NUM_RESERVOIRS, MAX_RES_SIZE, MAX_RES_SIZE, MAX_RES_SIZE, MAX_INPUTS, MAX_AXON_LENGTH, MAX_SYNAPSES);
logger (file, buff);
sprintf(buff, "Search Radius: %d\nAction Potential: %d\nThreshold: %d\nAlpha learning rate(cue): %f\nBeta learning rate(weight): %f\nSynapse lifespan increase: %f\nSynapse lifespan decrease: %f\n",
SEARCH_RADIUS, ACTION_POTENTIAL, THRESHOLD, CUE_CHANGE, WEIGHT_CHANGE, LIFESPAN_INCREASE, LIFESPAN_DECREASE);
logger (file, buff);
numReservoirs = NUM_RESERVOIRS;
resDimension = MAX_RES_SIZE;
numSensors = NUM_SENSORS;
numMotors = NUM_MOTORS;
maxInputs = MAX_INPUTS;
maxAxonLength = MAX_AXON_LENGTH;
maxSynapses = MAX_SYNAPSES;
criticalPeriod = false;
sensitivityPeriod = false;
// Create Neurons.
for (int i = 0; i < numSensors; i++) {
// sensors[i] = new Neuron(-10 - i - i, -10 - i - i, -10 - i - i);
sensors.push_back(new Neuron(-10 - i - i, -10 - i - i, -10 - i - i));
}
for (int i = 0; i < numReservoirs; i++) {
// reservoir[i] = new Reservoir(resDimension);
reservoir.push_back(new Reservoir(resDimension));
}
for (int i = 0; i < numMotors; i++) {
// motors[i] = new Neuron(resDimension + 10 + i + i, resDimension + 10 + i + i, resDimension + 10 + i + i);
motors.push_back(new Neuron(resDimension + 10 + i + i, resDimension + 10 + i + i, resDimension + 10 + i + i));
motors[i]->makeOutput(true);
}
// Force link sensors and motors and reservoirs. Unique to the network.
int sensorCount = 0;
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 7; j++) {
for (int k = 0; k < 7; k++) {
linkSensor(sensors[sensorCount], reservoir[i]->getNeuron(0, j, k));
sensorCount += 1;
}
}
}
int counter = 0;
for (int i = 0; i < 16; i++) {
linkReservoir(reservoir[i]->getNeuron(6, 3, 3), reservoir[16]->getNeuron(0, 3 + (i > 6) + (i > 11), counter % 7));
counter += 1;
}
// linkReservoir(reservoir[0]->getNeuron(1, 1, 0), reservoir[1]->getNeuron(0, 1, 1));
// linkReservoir(reservoir[1]->getNeuron(1, 1, 0), reservoir[1]->getNeuron(1, 1, 2));
counter = 0;
for (int i = 0; i < numMotors; i++) {
linkMotor(reservoir[16]->getNeuron(resDimension - 1, 3 + (i > 6), counter % 7), motors[i]);
counter += 1;
}
logger (file, "Neural Network Successfully Created.\n");
}
// Use for loading reservoir dimensions from file.
NeuralNetwork::NeuralNetwork(char* filename)
{
char buff[200];
sprintf(buff, "Creating Neural Network.\nCue initialization seed: %d\nNumber of Reservoirs: %d\nReservoir dimension size: %dx%dx%d\nNumber of max inputs: %d\nMax axon length: %d\nMax number of synapses: %d\n",
CUE_SEED, NUM_RESERVOIRS, MAX_RES_SIZE, MAX_RES_SIZE, MAX_RES_SIZE, MAX_INPUTS, MAX_AXON_LENGTH, MAX_SYNAPSES);
logger (file, buff);
sprintf(buff, "Search Radius: %d\nAction Potential: %d\nThreshold: %d\nAlpha learning rate(cue): %f\nBeta learning rate(weight): %f\nSynapse lifespan increase: %f\nSynapse lifespan decrease: %f\n",
SEARCH_RADIUS, ACTION_POTENTIAL, THRESHOLD, CUE_CHANGE, WEIGHT_CHANGE, LIFESPAN_INCREASE, LIFESPAN_DECREASE);
logger (file, buff);
numReservoirs = NUM_RESERVOIRS;
resDimension = MAX_RES_SIZE;
numSensors = NUM_SENSORS;
numMotors = NUM_MOTORS;
maxInputs = MAX_INPUTS;
maxAxonLength = MAX_AXON_LENGTH;
maxSynapses = MAX_SYNAPSES;
criticalPeriod = false;
sensitivityPeriod = false;
// Create Neurons.
for (int i = 0; i < numSensors; i++) {
// sensors[i] = new Neuron(-10 - i - i, -10 - i - i, -10 - i - i);
sensors.push_back(new Neuron(-10 - i - i, -10 - i - i, -10 - i - i));
}
// Load reservoir settings from file.
FILE* resFile = fopen(filename, "r");
if (resFile == NULL)
printf("File was not opened successfully.\n");
for (int i = 0; i < numReservoirs; ) {
// Process the config file.
while (!feof(resFile)) {
// Read a line from the file.
char line[150];
char *l = fgets(line, sizeof line, resFile);
// Process the line.
if (l == line) {
// Ignore comments.
if (!(line[0] == CONFIG_COMMENT_CHAR || strncmp(line,"\n",1)==0)) {
// Extract config parameter name and value.
char name[100];
char value[50];
int items = sscanf(line, "%s %s\n", name, value);
// Process this line.
if (strcmp(name, "RANDOM") == 0) {
// Only read the dimensions from value.
int temp[3];
sscanf(value, "%d,%d,%d", &temp[0], &temp[1], &temp[2]);
reservoir.push_back(new Reservoir(temp[0], temp[1], temp[2]));
i += 1;
} else {
// Read specific reservoir file settings here.
}
}
}
else if (!feof(resFile))
printf("Error occurred while reading the file.\n");
}
}
for (int i = 0; i < numMotors; i++) {
// motors[i] = new Neuron(resDimension + 10 + i + i, resDimension + 10 + i + i, resDimension + 10 + i + i);
motors.push_back(new Neuron(reservoir[0]->getResDim(0) + 10 + i + i, reservoir[0]->getResDim(1) + 10 + i + i, reservoir[0]->getResDim(2) + 10 + i + i));
motors[i]->makeOutput(true);
}
// Force link sensors and motors and reservoirs. Unique to the network.
int sensorCount = 0;
for (int i = 0; i < 28; i++) {
for (int j = 0; j < 28; j++) {
linkSensor(sensors[sensorCount], reservoir[0]->getNeuron(i, j, 0));
sensorCount += 1;
}
}
// linkReservoir(reservoir[0]->getNeuron(1, 1, 0), reservoir[1]->getNeuron(0, 1, 1));
// linkReservoir(reservoir[1]->getNeuron(1, 1, 0), reservoir[1]->getNeuron(1, 1, 2));
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
linkMotor(reservoir[0]->getNeuron(i * 6 + 2, j * 8 + 2, reservoir[0]->getResDim(2) - 1), motors[i]);
}
}
logger (file, "Neural Network Successfully Created.\n");
}
void NeuralNetwork::linkSensor(Neuron* sensor, Neuron* target) {
// Link the sensor Neuron to target Neuron by means of axon and synapses.
sensor->getAxon()->forceLink(target);
}
void NeuralNetwork::linkMotor(Neuron* target, Neuron* motor) {
// Link the target Neuron to motor Neuron by means of axon and synapses.
target->getAxon()->forceLink(motor);
target->setCue(1.0);
}
void NeuralNetwork::linkReservoir(Neuron* source, Neuron* target) {
source->getAxon()->forceLink(target);
}
void NeuralNetwork::trainAND() {
// Start timing.
struct timeval start_time, end_time;
gettimeofday(&start_time,NULL);
double t1=start_time.tv_sec+(start_time.tv_usec/1000000.0);
// Method to train. Probably specific to what the DANN is being trained for.
std::vector<float> values;
values.resize(NUM_SENSORS);
// printf ("Test? (y[1]/n[2]): ");
int input = 0;
int correct = 0;
int iterations = 0;
srand(0);
// fscanf (stdin, "%d", &input);
while (true) {
// usleep(999999);
// if (input == 1) {
// Test all combinations of 0 and 1 to see if the correct output is made.
for (int i = 0; i < 2; i++) {
values[0] = i * 100;
for (int j = 0; j < 2; j++) {
values[1] = j * 100;
updateSensors(values);
process(false);
if (motors[0]->isTriggered() == (i && j)) {
correct += 1;
motors[0]->resetTrigger();
}
}
}
printf ("The DANN obtained a score of %d out of 4.\n", correct);
if (correct == 4) {
break;
}
correct = 0;
// }
// else if (input == 2) {
// Run through a random combination to train.
values[0] = (rand() % 2) * 100;
values[1] = (rand() % 2) * 100;
updateSensors(values);
process(true);
// Use updateCues() to update cues.
if (motors[0]->isTriggered() != ((values[0] != 0) && (values[1] != 0))) {
updateCues(motors[0], motors[0]->isTriggered());
}
else {
//updateCues(motors[0], false);
}
// Show input-output relation.
if (values[0]) {
printf ("Input 0 is on.\n");
}
else {
printf ("Input 0 is off.\n");
}
if (values[1]) {
printf("Input 1 is on.\n");
}
else {
printf ("Input 1 is off.\n");
}
outputMotors();
iterations += 1;
// }
// printf ("Test? (y/n): ");
// fscanf(stdin, "%d", &input);
}
gettimeofday(&end_time,NULL);
double t2=end_time.tv_sec+(end_time.tv_usec/1000000.0);
double totaltime=t2-t1;
printf ("Network successfully learned AND gate behaviour in %d iterations.\n", iterations);
if (LOGGING) {
char buff[100];
sprintf(buff, "Network successfully learned AND gate behaviour in %d iterations and %.6lf seconds.\n", iterations, totaltime);
logger(file, buff);
}
}
void NeuralNetwork::trainOR() {
// Start timing.
struct timeval start_time, end_time;
gettimeofday(&start_time,NULL);
double t1=start_time.tv_sec+(start_time.tv_usec/1000000.0);
// Method to train. Probably specific to what the DANN is being trained for.
std::vector<float> values;
values.resize(NUM_SENSORS);
// printf ("Test? (y[1]/n[2]): ");
int input = 0;
int correct = 0;
int iterations = 0;
srand(0);
// fscanf (stdin, "%d", &input);
while (true) {
//usleep(999999);
// if (input == 1) {
// Test all combinations of 0 and 1 to see if the correct output is made.
for (int i = 0; i < 2; i++) {
values[0] = i * 100;
for (int j = 0; j < 2; j++) {
values[1] = j * 100;
updateSensors(values);
process(false);
if (motors[0]->isTriggered() == (i || j)) {
correct += 1;
motors[0]->resetTrigger();
}
}
}
printf ("The DANN obtained a score of %d out of 4.\n", correct);
if (correct == 4) {
break;
}
correct = 0;
// }
// else if (input == 2) {
// Run through a random combination to train.
values[0] = (rand() % 2) * 100;
values[1] = (rand() % 2) * 100;
updateSensors(values);
process(true);
// Use updateCues() to update cues.
if (motors[0]->isTriggered() != (values[0] || values[1])) {
updateCues(motors[0], motors[0]->isTriggered());
}
else {
//updateCues(motors[0], false);
}
// Show input-output relation.
if (values[0]) {
printf ("Input 0 is on.\n");
}
else {
printf ("Input 0 is off.\n");
}
if (values[1]) {
printf("Input 1 is on.\n");
}
else {
printf ("Input 1 is off.\n");
}
outputMotors();
iterations += 1;
// }
// printf ("Test? (y/n): ");
// fscanf(stdin, "%d", &input);
}
gettimeofday(&end_time,NULL);
double t2=end_time.tv_sec+(end_time.tv_usec/1000000.0);
double totaltime=t2-t1;
printf ("Network successfully learned OR gate behaviour in %d iterations.\n", iterations);
if (LOGGING) {
char buff[100];
sprintf(buff, "Network successfully learned OR gate behaviour in %d iterations and %.6lf seconds.\n", iterations, totaltime);
logger(file, buff);
}
}
void NeuralNetwork::trainXOR() {
// Start timing.
struct timeval start_time, end_time;
gettimeofday(&start_time,NULL);
double t1=start_time.tv_sec+(start_time.tv_usec/1000000.0);
// Method to train. Probably specific to what the DANN is being trained for.
std::vector<float> values;
values.resize(NUM_SENSORS);
// printf ("Test? (y[1]/n[2]): ");
int input = 0;
int correct = 0;
int iterations = 0;
srand(0);
// fscanf (stdin, "%d", &input);
while (true) {
//usleep(999999);
// if (input == 1) {
// Test all combinations of 0 and 1 to see if the correct output is made.
for (int i = 0; i < 2; i++) {
values[0] = i * 100;
for (int j = 0; j < 2; j++) {
values[1] = j * 100;
updateSensors(values);
process(false);
if (motors[0]->isTriggered() == (i ^ j)) {
correct += 1;
motors[0]->resetTrigger();
}
}
}
printf ("The DANN obtained a score of %d out of 4.\n", correct);
if (correct == 4) {
break;
}
correct = 0;
// }
// else if (input == 2) {
// Run through a random combination to train.
values[0] = (rand() % 2) * 100;
values[1] = (rand() % 2) * 100;
updateSensors(values);
process(true);
// Use updateCues() to update cues.
int i = 0;
int j = 0;
if (values[0] > 0)
i = 1;
if (values[1] > 0)
j = 1;
if (motors[0]->isTriggered() != (i ^ j)) {
updateCues(motors[0], motors[0]->isTriggered());
}
else {
//updateCues(motors[0], false);
}
// Show input-output relation.
if (values[0]) {
printf ("Input 0 is on.\n");
}
else {
printf ("Input 0 is off.\n");
}
if (values[1]) {
printf("Input 1 is on.\n");
}
else {
printf ("Input 1 is off.\n");
}
outputMotors();
iterations += 1;
// }
// printf ("Test? (y/n): ");
// fscanf(stdin, "%d", &input);
}
gettimeofday(&end_time,NULL);
double t2=end_time.tv_sec+(end_time.tv_usec/1000000.0);
double totaltime=t2-t1;
printf ("Network successfully learned XOR gate behaviour in %d iterations.\n", iterations);
if (LOGGING) {
char buff[100];
sprintf(buff, "Network successfully learned XOR gate behaviour in %d iterations and %.6lf seconds.\n", iterations, totaltime);
logger(file, buff);
}
}
void NeuralNetwork::trainMNIST(int numTrain, int numTest) {
// Start timing.
struct timeval start_time, end_time;
gettimeofday(&start_time,NULL);
double t1=start_time.tv_sec+(start_time.tv_usec/1000000.0);
int*** trainValues = new int** [numTrain];
for (int i = 0; i < numTrain; i++) {
trainValues[i] = new int* [28];
for (int j = 0; j < 28; j++) {
trainValues[i][j] = new int [28];
}
}
int* trainLabels = new int [numTrain];
int*** testValues = new int** [numTest];
for (int i = 0; i < numTest; i++) {
testValues[i] = new int* [28];
for (int j = 0; j < 28; j++) {
testValues[i][j] = new int [28];
}
}
int* testLabels = new int [numTest];
if (readMNIST(MNIST_TRAIN_IMAGES, MNIST_TRAIN_LABELS, numTrain, trainValues, trainLabels) == -1)
return;
if (readMNIST(MNIST_TEST_IMAGES, MNIST_TEST_LABELS, numTest, testValues, testLabels) == -1)
return;
// Method to train MNIST data.
for (int i = 0; i < numTrain; i++) {
if (i % (numTrain/10) == 0 && (i != 0)) {
printf ("Completed %.1f%% of training.\n", (((float) i / (float) numTrain) * 100));
}
updateSensorsMNIST(trainValues[i]);
process(true);
if (i % (numTrain/10) == 0) {
printf("At iteration %d, output:\n", i);
printf("0 1 2 3 4 5 6 7 8 9\n");
}
for (int j = 0; j < numMotors; j++) {
if (i % (numTrain/10) == 0) {
if (motors[j]->isTriggered())
printf ("* ");
else {
printf(" ");
}
}
if (motors[j]->isTriggered() && (j != trainLabels[i]) || (!motors[j]->isTriggered() && (j == trainLabels[i]))) {
updateCues(motors[j], motors[j]->isTriggered());
}
motors[j]->resetTrigger();
}
if (i % (numTrain/10) == 0) {
printf("\n");
}
}
printf ("Training Complete.\n");
// Method to test MNIST data.
bool right = false;
int wrong = 0;
int correct = 0;
int partially[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
for (int i = 0; i < numTest; i++) {
if (i % (numTest/10) == 0 && (i != 0)) {
printf ("Completed %.1f%% of testing.\n", (((float) i / (float) numTest) * 100));
}
updateSensorsMNIST(testValues[i]);
process(false);
if (motors[testLabels[i]]->isTriggered()) {
right = true;
}
if (i % (numTest/10) == 0) {
printf("At iteration %d, output:\n", i);
printf("0 1 2 3 4 5 6 7 8 9\n");
}
for (int j = 0; j < numMotors; j++) {
if (i % (numTest/10) == 0) {
if (motors[j]->isTriggered())
printf ("* ");
else {
printf(" ");
}
}
if (!motors[j]->isTriggered() && (j == testLabels[i])) {
motors[j]->resetTrigger();
j = numMotors;
}
else {
if (motors[j]->isTriggered() && (j != testLabels[i])) {
wrong += 1;
}
motors[j]->resetTrigger();
}
}
if (i % (numTest/10) == 0) {
printf("\n");
}
if (right) {
if (wrong > 0) {
partially[wrong - 1] += 1;
}
else {
correct += 1;
}
right = false;
}
wrong = 0;
}
gettimeofday(&end_time,NULL);
double t2=end_time.tv_sec+(end_time.tv_usec/1000000.0);
double totaltime=t2-t1;
printf ("Network successfully completed MNIST training and testing in %.6lf seconds.\n", totaltime);
printf ("Correct: %d\n", correct);
for (int i = 0; i < 9; i++) {
printf ("Partially correct (%d wrong): %d\n", i + 1, partially[i]);
}
if (LOGGING) {
char buff[100];
sprintf(buff, "Network successfully completed MNIST training and testing in %.6lf seconds with %d correct and %d partially correct.\n", totaltime, correct, partially);
logger(file, buff);
}
for (int i = 0; i < numTrain; i++) {
for (int j = 0; j < 28; j++) {
delete[] trainValues[i][j];
}
delete[] trainValues[i];
}
delete[] trainValues;
delete[] trainLabels;
for (int i = 0; i < numTest; i++) {
for (int j = 0; j < 28; j++) {
delete[] testValues[i][j];
}
delete[] testValues[i];
}
delete[] testLabels;
}
void NeuralNetwork::updateSensors(std::vector<float> values) {
// Update sensor Neuron dendrite values. Specific to each Sensor.
for (int i = 0; i < values.size(); i++) {
sensors[i]->acceptSignal(values[i], NULL);
}
}
void NeuralNetwork::updateSensorsMNIST(int** values) {
for (int i = 0; i < 28; i++) {
for (int j = 0; j < 28; j++) {
sensors[(28 * i) + j]->acceptSignal(values[i][j], NULL);
}
}
}
void NeuralNetwork::process(bool train) {
// Commence a breadth first signal pass starting from the sensor Neurons.
std::list<Neuron*> unprocessed;
std::list<Neuron*> processed;
Neuron* current;
Neuron* check;
Synapse* head;
Synapse* curr;
bool isUnique = true;
for (int i = 0; i < numSensors; i++) {
unprocessed.push_back(sensors[i]);
}
// int counter = 0;
while (!unprocessed.empty()) {
// Process the next Neuron.
current = unprocessed.front();
// counter += 1;
// printf ("%d ", current->getAxon()->getNumSynapses());
// current->printPosition();
curr = current->getAxon()->getSynapseHead();
int numSynapses = current->getAxon()->getNumSynapses();
// Send an action potential if necessary.
if (current->activatePotential(current->process())) {
// Add new Neurons to the queue if necessary
for (int i = 0; i < numSynapses; i++) {
isUnique = true;
check = curr->getTarget();
curr->trigger(ACTION_POTENTIAL * curr->getWeight(), train);
// Check for duplicates before pushing new Neurons into the queue.
for (std::list<Neuron*>::iterator it = processed.begin(); it != processed.end(); ++it) {
if ((*it)->equals(check)) {
isUnique = false;
break;
}
}
if (isUnique) {
for (std::list<Neuron*>::iterator it = unprocessed.begin(); it != unprocessed.end(); ++it) {
if ((*it)->equals(check)) {
isUnique = false;
break;
}
}
}
if (isUnique) {
unprocessed.push_back(check);
}
curr = curr->getNext();
}
}
processed.push_front(current);
// Remove Neuron as it has just been processed.
unprocessed.pop_front();
}
// printf ("%d neurons processed.\n", counter);
// Used to empty all dendrites. Most likely won't use.
// for (int res = 0; res < numReservoirs; res++) {
// for (int i = 0; i < resDimension; i++) {
// for (int j = 0; j < resDimension; j++) {
// for (int k = 0; k < resDimension; k++) {
// current = reservoir[res]->getNeuron(i, j, k);
// current->resetDendrites();
// }
// }
// }
// }
}
void NeuralNetwork::updateCues(Neuron* motor, bool reinforce) {
// Back propagate starting from the target motor Neuron and change cues accordingly.
// Commence a backward breadth first search starting from the motor Neuron.
std::list<Neuron*> unprocessed;
std::list<Neuron*> processed;
Neuron* current;
Neuron* check;
bool isUnique;
bool state = motor->isTriggered();
int counter = 0;
float change = CUE_CHANGE;
if (reinforce) {
change = change * (-1);
}
unprocessed.push_back(motor);
for (int i = 0; i < numSensors; i++) {
processed.push_back(sensors[i]);
}
while (!unprocessed.empty()) {
// Process the next Neuron.
current = unprocessed.front();
// current->printPosition();
// printf ("%f\n", current->getCue());
// Add new Neurons to the queue if necessary
for (int i = 0; i < current->getConnSize(); i++) {
isUnique = true;
check = current->getConnection(i);
// Check for duplicates before pushing new Neurons into the queue.
for (std::list<Neuron*>::iterator it = processed.begin(); it != processed.end(); it++) {
if ((*it)->equals(check)) {
isUnique = false;
break;
}
}
if (isUnique) {
for (std::list<Neuron*>::iterator it = unprocessed.begin(); it != unprocessed.end(); it++) {
if ((*it)->equals(check)) {
isUnique = false;
break;
}
}
}
if (isUnique && (check->isTriggered() == state)) {
unprocessed.push_back(check);
}
}
// Update cues if necessary.
if (!current->isOutput()) {
current->changeCue(change);
}
counter += 1;
processed.push_front(current);
// Remove Neuron as it has just been processed.
unprocessed.pop_front();
}
Synapse* curr;
// Update weights based on the targets' cue values and reset triggers.
for (int res = 0; res < numReservoirs; res++) {
Reservoir* currRes = reservoir[res];
int resSize[3];
resSize[0] = currRes->getResDim(0);
resSize[1] = currRes->getResDim(1);
resSize[2] = currRes->getResDim(2);
for (int i = 0; i < resSize[0]; i++) {
for (int j = 0; j < resSize[1]; j++) {
for (int k = 0; k < resSize[2]; k++) {
current = currRes->getNeuron(i, j, k);
current->resetTrigger();
curr = current->getAxon()->getSynapseHead();
int numSyn = current->getAxon()->getNumSynapses();
for (int s = 0; s < numSyn; s++) {
curr->changeWeight(WEIGHT_CHANGE * curr->getTarget()->getCue()); // Some function of the target cue.
if (!(curr->getTarget()->isOutput())) {
curr->changeLifespan(-1 * LIFESPAN_DECREASE);
}
if (curr->getLifespan() <= 0) {
Synapse* temp = curr->getNext();
current->getAxon()->removeSynapse(curr);
curr = temp;
s += 1;
}
else {
curr = curr->getNext();
}
}
}
}
}
}
}
void NeuralNetwork::determineState() {
// Check output and apply consequences (change critical/sensitivityPeriods).
}
void NeuralNetwork::outputMotors() {
// Highly specific to what each motor Neuron is able to do.
for (int i = 0; i < numMotors; i++) {
if (motors[i]->isTriggered()) {
printf ("Motor %d is on.\n", i);
motors[i]->resetTrigger();
}
else {
printf ("Motor %d is off.\n", i);
}
motors[i]->resetDendrites();
}
}
void NeuralNetwork::resetMotorTriggers() {
for (int i = 0; i < numMotors; i++) {
motors[i]->resetTrigger();
}
}
NeuralNetwork::~NeuralNetwork()
{
logger (file, "Destroying Neural Network.\n");
for (int i = 0; i < numSensors; i++) {
delete sensors[i]->getAxon();
}
for (int i = 0; i < numMotors; i++) {
delete motors[i]->getAxon();
}
for (int i = 0; i < numReservoirs; i++) {
delete reservoir[i];
}
for (int i = 0; i < numSensors; i++) {
delete sensors[i];
}
for (int i = 0; i < numMotors; i++) {
delete motors[i];
}
logger (file, "Neural Network Successfully destroyed.\n");
}