-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtests.py
1850 lines (1487 loc) · 103 KB
/
tests.py
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import unittest
import numpy as np
from NEAT import NEAT
import os
from NEAT_multiclass import NEATMultiClass
from config import Config
from data_storage import get_circle_data
from genome_multiclass import GenomeMultiClass
from genome_neural_network import GenomeNeuralNetwork
from genome_neural_network_multiclass import GenomeNeuralNetworkMultiClass
from neural_network import ForwardProp, ActivationFunctions, BackProp, NeuralNetwork, create_architecture, create_data
from deconstruct_genome import DeconstructGenome
from genome import Genome
from gene import ConnectionGene, NodeGene
from read_mat_files import get_shm_two_class_data
from reproduce import Reproduce
from stagnation import Stagnation
import pickle
import sklearn.metrics
class TestForwardProp(unittest.TestCase):
def setUp(self):
pass
def test_compute_layer_single(self):
"""
Tests compute_layer for a single perceptron
:return:
"""
# Single perceptron
input_array = np.array([[1, 2], [2, 3], [3, 4]])
weights = np.array([[1], [2]])
bias = np.array([[1]])
expected_output_nobias = np.array([[5], [8], [11]])
# This includes a bias test
expected_output_bias = np.array([[6], [9], [12]])
# No bias test
self.assertEqual(ForwardProp.compute_layer(input_array, weights).tolist(), expected_output_nobias.tolist())
# Bias test
self.assertEqual(ForwardProp.compute_layer(input_array, weights, bias).tolist(),
expected_output_bias.tolist())
def test_compute_layer_multiple(self):
"""
Tests compute_layer for multiple hidden nodes for a single layer
:return:
"""
input_array = np.array([[1, 2], [2, 3], [3, 4]])
weights = np.array([[1, 2], [2, 1]])
bias = np.array([[1, 2]])
expected_output_nobias = np.array([[5, 4], [8, 7], [11, 10]])
# This includes a bias test
expected_output_bias = np.array([[6, 6], [9, 9], [12, 12]])
# No bias test
self.assertEqual(ForwardProp.compute_layer(input_array, weights).tolist(), expected_output_nobias.tolist())
# Bias test
self.assertEqual(ForwardProp.compute_layer(input_array, weights, bias).tolist(), expected_output_bias.tolist())
def test_forward_prop(self):
"""
Test for a one hidden layer neural network to see what the forward propagation output is
"""
input_array = np.array([[1, 2], [2, 3], [3, 4]])
layer_1_weights = np.array([[1, 2], [2, 1]])
layer_2_weights = np.array([[3], [4]])
layer_1_bias = np.array([[1, 2]])
layer_2_bias = np.array([[1]])
expected_output_nobias = np.array([[31], [52], [73]])
expected_output_bias = np.array([[43], [64], [85]])
weight_dict = {1: layer_1_weights, 2: layer_2_weights}
bias_dict = {1: layer_1_bias, 2: layer_2_bias}
activation_function_dict = {1: ActivationFunctions.relu, 2: ActivationFunctions.relu}
# No bias test
self.assertEqual(
ForwardProp.forward_prop(num_layers=2, initial_input=input_array, layer_weights=weight_dict)[
0].tolist(),
expected_output_nobias.tolist())
# Bias test
self.assertEqual(ForwardProp.forward_prop(num_layers=2, initial_input=input_array, layer_weights=weight_dict,
layer_biases=bias_dict)[0].tolist(),
expected_output_bias.tolist())
# Testing Activation function with bias
self.assertEqual(ForwardProp.forward_prop(num_layers=2, initial_input=input_array, layer_weights=weight_dict,
layer_biases=bias_dict,
layer_activation_functions=activation_function_dict)[
0].tolist(),
expected_output_bias.tolist())
class TestActivationFunctions(unittest.TestCase):
def setUp(self):
pass
def test_relu(self):
input_matrix = np.array([[1, -1], [-1, 1]])
expected_output = np.array([[1, 0], [0, 1]])
self.assertEqual(ActivationFunctions.relu(input_matrix).tolist(), expected_output.tolist())
def test_sigmoid(self):
input_matrix = np.array([[1, -1], [-1, 1]])
expected_output = np.array([[0.73, 0.27], [0.27, 0.73]])
function_output = np.around(ActivationFunctions.sigmoid(input_matrix), 2)
self.assertEqual(function_output.tolist(), expected_output.tolist())
class TestBackProp(unittest.TestCase):
def setUp(self):
pass
def test_back_prop_one_layer(self):
"""
Test a very simple one layer neural network to see if correct gradients are calculated
"""
# Initial data
input_matrix = np.array([[1, 2], [3, 4]])
weights = np.array([[2], [3]])
expected_y = np.array([[2], [2]])
expected_weight_gradients = np.array([[-2], [-3]])
# Dictionary with layer information
weights_dict = {1: weights}
activation_function_dict = {1: ActivationFunctions.sigmoid}
prediction, layer_input_dict = ForwardProp.forward_prop(num_layers=1, initial_input=input_matrix,
layer_weights=weights_dict,
layer_activation_functions=activation_function_dict)
# Excluded bias gradients here
weight_gradients, _ = BackProp.back_prop(num_layers=1, layer_inputs=layer_input_dict,
layer_weights=weights_dict,
layer_activation_functions=activation_function_dict,
expected_y=expected_y, predicted_y=prediction)
self.assertEqual(weight_gradients[1].astype(int).tolist(), expected_weight_gradients.tolist())
def test_back_prop_two_layer(self):
input_matrix = np.array([[1, 2], [3, 4]])
weights_1 = np.array([[2, 2], [3, 3]])
weights_2 = np.array([[2], [3]])
expected_y = np.array([[38], [88]])
expected_weight_gradients_2 = np.array([[26], [26]])
expected_weight_gradients_1 = np.array([[8, 12], [12, 18]])
# Dictionary with layer information
weights_dict = {1: weights_1, 2: weights_2}
activation_function_dict = {1: ActivationFunctions.relu, 2: ActivationFunctions.relu}
prediction, layer_input_dict = ForwardProp.forward_prop(num_layers=2, initial_input=input_matrix,
layer_weights=weights_dict,
layer_activation_functions=activation_function_dict)
# Excluded bias gradients here
weight_gradients, _ = BackProp.back_prop(num_layers=2, layer_inputs=layer_input_dict,
layer_weights=weights_dict,
layer_activation_functions=activation_function_dict,
expected_y=expected_y, predicted_y=prediction)
self.assertEqual(np.round(weight_gradients[2], 0).astype(int).tolist(), expected_weight_gradients_2.tolist())
self.assertEqual(np.round(weight_gradients[1], 0).astype(int).tolist(), expected_weight_gradients_1.tolist())
class TestNeuralNetworkOneLayer(unittest.TestCase):
def setUp(self):
self.data_train, self.labels_train = create_data(n_generated=5000)
self.num_features = self.data_train.shape[1]
self.desired_architecture = [6]
nn_architecture = create_architecture(self.num_features, self.desired_architecture)
# Defines the activation functions used for each layer
activations_dict = {1: ActivationFunctions.sigmoid, 2: ActivationFunctions.sigmoid}
self.neural_network = NeuralNetwork(x_train=self.data_train, y_train=self.labels_train,
layer_sizes=nn_architecture,
activation_function_dict=activations_dict, learning_rate=0.1)
def test_initialise_parameters_shapes(self):
"""
Instead of testing for the values specifically we just test to ensure that the parameters initialise with the
correct shape
"""
expected_shape_layer_1 = (self.num_features, self.desired_architecture[0])
# Number of features from last layer and because it's the last layer should only be one column
expected_shape_layer_2 = (self.desired_architecture[0], 1)
self.assertEqual(self.neural_network.weights_dict[1].shape, expected_shape_layer_1)
self.assertEqual(self.neural_network.weights_dict[2].shape, expected_shape_layer_2)
def test_optimise(self):
epochs, cost = self.neural_network.optimise(print_epoch_cost=False)
# When this was working 0.002 was the error
expected_error_after_1000_epochs = 0.002
self.assertEqual(round(cost[999], 3), expected_error_after_1000_epochs)
class TestNeuralNetworkMultiLayer(unittest.TestCase):
"""
Similar to above test case but this is a 2 layer hidden neural network instead of just one
"""
def setUp(self):
# Test and Train data
data_train, labels_train = create_data(n_generated=5000)
num_features = data_train.shape[1]
# This means it will be a two layer neural network with one layer being hidden with 2 nodes
desired_architecture = [5, 6]
# desired_architecture = [2, 2]
nn_architecture = create_architecture(num_features, desired_architecture)
# Defines the activation functions used for each layer
activations_dict = {1: ActivationFunctions.sigmoid, 2: ActivationFunctions.sigmoid,
3: ActivationFunctions.sigmoid}
self.neural_network = NeuralNetwork(x_train=data_train, y_train=labels_train, layer_sizes=nn_architecture,
activation_function_dict=activations_dict, learning_rate=0.1)
def test_optimise(self):
epochs, cost = self.neural_network.optimise(print_epoch_cost=True)
# When this was working 0.002 was the error
expected_error_after_1000_epochs = 0.0
self.assertEqual(round(cost[999], 3), expected_error_after_1000_epochs)
class TestDeconstructGenomeClass(unittest.TestCase):
def setUp(self):
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='source'),
NodeGene(node_id=3, node_type='hidden'),
NodeGene(node_id=4, node_type='hidden'),
NodeGene(node_id=5, node_type='output')]
connection_list = [ConnectionGene(input_node=1, output_node=3, innovation_number=1),
ConnectionGene(input_node=1, output_node=4, innovation_number=2),
ConnectionGene(input_node=2, output_node=3, innovation_number=3),
ConnectionGene(input_node=2, output_node=4, innovation_number=4),
ConnectionGene(input_node=3, output_node=5, innovation_number=5),
ConnectionGene(input_node=4, output_node=5, innovation_number=6)]
self.genome = Genome(nodes=node_list, connections=connection_list, key=1)
def test_get_node_layer(self):
expected_answer = {1: 1, 2: 1, 3: 2, 4: 2, 5: 3}
self.assertEqual(
DeconstructGenome.get_node_layers(connections=list(self.genome.connections.values()), genome=self.genome)[
0],
expected_answer)
def test_unpack_genome(self):
expected_answer = np.ones((2, 2))
# check that the first layer weights are the correct ones
self.assertEqual(self.genome.connection_matrices_per_layer[1].tolist(), expected_answer.tolist())
def test_unpack_genome_broken_link(self):
"""
Tests unpack genome for a genome which contains a broken link
"""
expected_answer = np.ones((2, 2))
expected_answer[0, 0] = 0
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='source'),
NodeGene(node_id=3, node_type='hidden'),
NodeGene(node_id=4, node_type='hidden'),
NodeGene(node_id=5, node_type='output')]
# Note that one of the connections isn't enabled
connection_list = [ConnectionGene(input_node=1, output_node=3, innovation_number=1, enabled=False),
ConnectionGene(input_node=1, output_node=4, innovation_number=2, enabled=True),
ConnectionGene(input_node=2, output_node=3, innovation_number=3, enabled=True),
ConnectionGene(input_node=2, output_node=4, innovation_number=4, enabled=True),
ConnectionGene(input_node=3, output_node=5, innovation_number=5, enabled=True),
ConnectionGene(input_node=4, output_node=5, innovation_number=6, enabled=True)]
genome = Genome(connections=connection_list, nodes=node_list, key=2)
self.assertEqual(genome.connection_matrices_per_layer[1].tolist(), expected_answer.tolist())
def test_forward_prop(self):
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='source'),
NodeGene(node_id=3, node_type='hidden', bias=0.5),
NodeGene(node_id=4, node_type='hidden', bias=-1.5),
NodeGene(node_id=5, node_type='output', bias=1.5)]
connection_list = [ConnectionGene(input_node=1, output_node=5, innovation_number=1, enabled=True, weight=9),
ConnectionGene(input_node=1, output_node=3, innovation_number=2, enabled=True, weight=2),
ConnectionGene(input_node=2, output_node=3, innovation_number=3, enabled=True, weight=3),
ConnectionGene(input_node=2, output_node=4, innovation_number=4, enabled=True, weight=4),
ConnectionGene(input_node=2, output_node=5, innovation_number=5, enabled=True, weight=3),
ConnectionGene(input_node=3, output_node=5, innovation_number=6, enabled=True, weight=5),
ConnectionGene(input_node=4, output_node=5, innovation_number=7, enabled=True, weight=7)]
genome = Genome(nodes=node_list, connections=connection_list, key=1)
x_data = np.array([[1, 2]])
y_data = np.array([[1]])
genome_nn = GenomeNeuralNetwork(genome=genome, x_train=x_data, y_train=y_data, learning_rate=0.1,
create_weights_bias_from_genome=True, activation_type='relu')
expected_answer = np.array([[104.5]])
# This works because the activation type is relu and there aren't any negative numbers. But it should ideally be
# genome_forward_prop instead of forward_prop
output = ForwardProp.forward_prop(num_layers=genome_nn.num_layers, initial_input=x_data,
layer_weights=genome_nn.weights_dict,
layer_activation_functions=genome_nn.activation_function_dict,
layer_biases=genome_nn.bias_dict, return_number_before_last_activation=True)
self.assertEqual(expected_answer.tolist(), output.tolist())
class TestGenomeUnpack(unittest.TestCase):
def setUp(self):
pass
def test_unpack_genome_3(self):
"""
Testing another genome which would normally fail if the unpack genome method is not coded correctly
"""
for i in range(1000):
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='source'),
NodeGene(node_id=3, node_type='output')]
connection_list = [ConnectionGene(input_node=1, output_node=3, innovation_number=1),
ConnectionGene(input_node=2, output_node=3, innovation_number=2)]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
self.assertTrue(genome)
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=3, node_type='output')]
connection_list = [ConnectionGene(input_node=1, output_node=3, innovation_number=1)]
genome_2 = Genome(connections=connection_list, nodes=node_list, key=2)
self.assertTrue(genome_2)
def test_unpack_genome_2(self):
"""
Testing another genome which would normally fail if the unpack genome method is not coded correctly
"""
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='source'),
NodeGene(node_id=3, node_type='output'),
NodeGene(node_id=4, node_type='hidden')]
connection_list = [ConnectionGene(input_node=1, output_node=4, innovation_number=1),
ConnectionGene(input_node=2, output_node=3, innovation_number=2),
ConnectionGene(input_node=4, output_node=3, innovation_number=6)]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
self.assertTrue(genome)
def test_unpack_genome_4(self):
node_list = [NodeGene(node_id=0, node_type='source'),
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=3, node_type='hidden', bias=0.5),
NodeGene(node_id=4, node_type='hidden', bias=-1.5),
NodeGene(node_id=5, node_type='hidden', bias=0.5),
NodeGene(node_id=6, node_type='hidden', bias=-1.5),
NodeGene(node_id=2, node_type='output', bias=1.5)]
connection_list = [ConnectionGene(input_node=1, output_node=4, innovation_number=1, enabled=True, weight=9),
ConnectionGene(input_node=4, output_node=2, innovation_number=5, enabled=True, weight=3),
ConnectionGene(input_node=0, output_node=5, innovation_number=2, enabled=True, weight=2),
ConnectionGene(input_node=5, output_node=3, innovation_number=4, enabled=True, weight=4),
ConnectionGene(input_node=3, output_node=4, innovation_number=3, enabled=True, weight=3),
ConnectionGene(input_node=1, output_node=2, innovation_number=7, enabled=True, weight=7)]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
self.assertTrue(genome)
def test_unpack_genome_5(self):
# Multiple times due to randomisation of various elements
for i in range(100):
node_list = [NodeGene(node_id=0, node_type='source'),
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='output', bias=1.5),
NodeGene(node_id=3, node_type='hidden', bias=0.5),
NodeGene(node_id=4, node_type='hidden', bias=-1.5),
NodeGene(node_id=5, node_type='hidden', bias=0.5)]
connection_list = [ConnectionGene(input_node=0, output_node=2, innovation_number=1, enabled=True, weight=9),
ConnectionGene(input_node=1, output_node=5, innovation_number=5, enabled=True, weight=3),
ConnectionGene(input_node=3, output_node=4, innovation_number=2, enabled=True, weight=2),
ConnectionGene(input_node=5, output_node=3, innovation_number=4, enabled=True, weight=4),
ConnectionGene(input_node=4, output_node=2, innovation_number=3, enabled=True, weight=3)]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
self.assertTrue(genome)
def test_unpack_genome_6(self):
# Multiple times due to randomisation of various elements
for i in range(100):
node_list = [NodeGene(node_id=0, node_type='source'),
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='output', bias=1.5),
NodeGene(node_id=3, node_type='hidden', bias=0.5),
NodeGene(node_id=4, node_type='hidden', bias=-1.5),
NodeGene(node_id=5, node_type='hidden', bias=0.5)]
connection_list = [ConnectionGene(input_node=0, output_node=2, innovation_number=1, enabled=True, weight=9),
ConnectionGene(input_node=1, output_node=2, innovation_number=5, enabled=True, weight=3),
ConnectionGene(input_node=0, output_node=5, innovation_number=2, enabled=True, weight=2),
ConnectionGene(input_node=3, output_node=4, innovation_number=4, enabled=True, weight=4),
ConnectionGene(input_node=5, output_node=3, innovation_number=3, enabled=True, weight=3),
ConnectionGene(input_node=4, output_node=2, innovation_number=9, enabled=True, weight=3)]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
self.assertTrue(genome)
def test_unpack_genome_7(self):
# Multiple times due to randomisation of various elements
node_list = [NodeGene(node_id=0, node_type='source'),
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='output', bias=1.5),
NodeGene(node_id=3, node_type='hidden', bias=0.5),
NodeGene(node_id=4, node_type='hidden', bias=-1.5),
NodeGene(node_id=5, node_type='hidden', bias=-1.5),
NodeGene(node_id=6, node_type='hidden', bias=-1.5),
NodeGene(node_id=7, node_type='hidden', bias=0.5)]
connection_list = [ConnectionGene(input_node=0, output_node=2, innovation_number=1, enabled=False, weight=9),
ConnectionGene(input_node=3, output_node=5, innovation_number=2, enabled=True, weight=3),
ConnectionGene(input_node=5, output_node=2, innovation_number=3, enabled=True, weight=2),
ConnectionGene(input_node=1, output_node=5, innovation_number=4, enabled=True, weight=4),
ConnectionGene(input_node=1, output_node=2, innovation_number=5, enabled=False, weight=3),
ConnectionGene(input_node=5, output_node=6, innovation_number=6, enabled=True, weight=3),
ConnectionGene(input_node=3, output_node=6, innovation_number=7, enabled=True, weight=3),
ConnectionGene(input_node=1, output_node=7, innovation_number=8, enabled=True, weight=3),
ConnectionGene(input_node=7, output_node=2, innovation_number=9, enabled=True, weight=3),
ConnectionGene(input_node=3, output_node=2, innovation_number=10, enabled=True, weight=3),
ConnectionGene(input_node=1, output_node=3, innovation_number=11, enabled=True, weight=3)]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
self.assertTrue(genome.connections[6].enabled is False)
self.assertTrue(genome.connections[7].enabled is False)
self.assertTrue(genome)
def test_genome_unpack_8(self):
node_list = [NodeGene(node_id=0, node_type='source'),
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='output', bias=1.5),
NodeGene(node_id=3, node_type='hidden', bias=0.5),
NodeGene(node_id=4, node_type='hidden', bias=-1.5),
NodeGene(node_id=5, node_type='hidden', bias=-1.5),
NodeGene(node_id=6, node_type='hidden', bias=-1.5),
NodeGene(node_id=7, node_type='hidden', bias=0.5)]
connection_list = [ConnectionGene(input_node=0, output_node=2, innovation_number=1, enabled=False, weight=9),
ConnectionGene(input_node=0, output_node=4, innovation_number=2, enabled=True, weight=3),
ConnectionGene(input_node=3, output_node=5, innovation_number=3, enabled=True, weight=2),
ConnectionGene(input_node=5, output_node=4, innovation_number=4, enabled=True, weight=4),
ConnectionGene(input_node=1, output_node=6, innovation_number=5, enabled=True, weight=3),
ConnectionGene(input_node=6, output_node=2, innovation_number=6, enabled=True, weight=3),
ConnectionGene(input_node=1, output_node=2, innovation_number=7, enabled=False, weight=3),
ConnectionGene(input_node=1, output_node=7, innovation_number=8, enabled=True, weight=3),
ConnectionGene(input_node=7, output_node=2, innovation_number=9, enabled=True, weight=3),
ConnectionGene(input_node=0, output_node=3, innovation_number=10, enabled=True, weight=3),
ConnectionGene(input_node=3, output_node=2, innovation_number=11, enabled=False, weight=3),
ConnectionGene(input_node=3, output_node=4, innovation_number=12, enabled=False, weight=3),
ConnectionGene(input_node=4, output_node=2, innovation_number=13, enabled=True, weight=3)]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
self.assertTrue(len(genome.connections) == len(connection_list))
self.assertTrue(list(genome.connections.values()) == connection_list)
self.assertTrue(genome)
class TestGenomeNeuralNetwork(unittest.TestCase):
def setUp(self):
pass
def test_genome_convergence(self):
node_list = [
NodeGene(node_id=0, node_type='source'),
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='output', bias=0.5),
NodeGene(node_id=3, node_type='hidden', bias=1),
NodeGene(node_id=4, node_type='hidden', bias=1),
NodeGene(node_id=5, node_type='hidden', bias=1),
NodeGene(node_id=6, node_type='hidden', bias=1),
]
connection_list = [
ConnectionGene(input_node=0, output_node=3, innovation_number=1, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=1, output_node=3, innovation_number=2, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=0, output_node=4, innovation_number=3, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=1, output_node=4, innovation_number=4, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=5, innovation_number=5, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=5, innovation_number=6, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=6, innovation_number=7, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=6, innovation_number=8, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=5, output_node=2, innovation_number=9, enabled=True, weight=np.random.rand()),
ConnectionGene(input_node=6, output_node=2, innovation_number=10, enabled=True, weight=np.random.randn())
]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
x_data, y_data = create_data(n_generated=5000)
genome_nn = GenomeNeuralNetwork(genome=genome, create_weights_bias_from_genome=False, activation_type='sigmoid',
learning_rate=0.1,
x_train=x_data, y_train=y_data)
epoch_list, cost_list = genome_nn.optimise(print_epoch=True)
assert (cost_list[len(cost_list) - 1] < 0.001)
def test_genome_convergence_with_smaller_training_set(self):
node_list = [
NodeGene(node_id=0, node_type='source'),
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='output', bias=0.5),
NodeGene(node_id=3, node_type='hidden', bias=1),
NodeGene(node_id=4, node_type='hidden', bias=1),
NodeGene(node_id=5, node_type='hidden', bias=1),
NodeGene(node_id=6, node_type='hidden', bias=1),
]
connection_list = [
ConnectionGene(input_node=0, output_node=3, innovation_number=1, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=1, output_node=3, innovation_number=2, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=0, output_node=4, innovation_number=3, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=1, output_node=4, innovation_number=4, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=5, innovation_number=5, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=5, innovation_number=6, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=6, innovation_number=7, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=6, innovation_number=8, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=5, output_node=2, innovation_number=9, enabled=True, weight=np.random.rand()),
ConnectionGene(input_node=6, output_node=2, innovation_number=10, enabled=True, weight=np.random.randn())
]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
x_data, y_data = create_data(n_generated=200)
genome_nn = GenomeNeuralNetwork(genome=genome, create_weights_bias_from_genome=True, activation_type='sigmoid',
num_epochs=3000,
batch_size=10,
learning_rate=0.1,
x_train=x_data, y_train=y_data)
epoch_list, cost_list = genome_nn.optimise(print_epoch=True)
assert (cost_list[len(cost_list) - 1] < 0.1)
def test_genome_convergence_with_smaller_training_set_and_noise(self):
np.random.seed(1)
node_list = [
NodeGene(node_id=0, node_type='source'),
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='output', bias=0.5),
NodeGene(node_id=3, node_type='hidden', bias=1),
NodeGene(node_id=4, node_type='hidden', bias=1),
NodeGene(node_id=5, node_type='hidden', bias=1),
NodeGene(node_id=6, node_type='hidden', bias=1),
]
connection_list = [
ConnectionGene(input_node=0, output_node=3, innovation_number=1, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=1, output_node=3, innovation_number=2, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=0, output_node=4, innovation_number=3, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=1, output_node=4, innovation_number=4, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=5, innovation_number=5, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=5, innovation_number=6, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=6, innovation_number=7, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=6, innovation_number=8, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=5, output_node=2, innovation_number=9, enabled=True, weight=np.random.rand()),
ConnectionGene(input_node=6, output_node=2, innovation_number=10, enabled=True, weight=np.random.randn())
]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
x_data, y_data = create_data(n_generated=200, add_noise=True)
print(x_data.shape)
print(y_data.shape)
genome_nn = GenomeNeuralNetwork(genome=genome, create_weights_bias_from_genome=True, activation_type='sigmoid',
num_epochs=10000,
batch_size=10,
learning_rate=0.1,
x_train=x_data, y_train=y_data)
epoch_list, cost_list = genome_nn.optimise(print_epoch=True)
assert (cost_list[len(cost_list) - 1] < 0.1)
def test_genome_convergence_with_circle_data(self):
np.random.seed(1)
node_list = [
NodeGene(node_id=0, node_type='source'),
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=7, node_type='source'),
NodeGene(node_id=8, node_type='source'),
NodeGene(node_id=9, node_type='source'),
NodeGene(node_id=10, node_type='source'),
NodeGene(node_id=11, node_type='source'),
NodeGene(node_id=2, node_type='output', bias=0.5),
NodeGene(node_id=3, node_type='hidden', bias=1),
NodeGene(node_id=4, node_type='hidden', bias=1),
NodeGene(node_id=5, node_type='hidden', bias=1),
NodeGene(node_id=6, node_type='hidden', bias=1),
NodeGene(node_id=12, node_type='hidden', bias=1),
NodeGene(node_id=13, node_type='hidden', bias=1),
NodeGene(node_id=14, node_type='hidden', bias=1),
NodeGene(node_id=15, node_type='hidden', bias=1),
NodeGene(node_id=16, node_type='hidden', bias=1),
NodeGene(node_id=17, node_type='hidden', bias=1),
NodeGene(node_id=18, node_type='hidden', bias=1),
NodeGene(node_id=19, node_type='hidden', bias=1),
]
connection_list = [
ConnectionGene(input_node=0, output_node=3, innovation_number=1, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=1, output_node=3, innovation_number=2, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=0, output_node=4, innovation_number=3, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=1, output_node=4, innovation_number=4, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=7, output_node=3, innovation_number=11, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=8, output_node=3, innovation_number=12, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=9, output_node=3, innovation_number=13, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=10, output_node=3, innovation_number=14, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=11, output_node=3, innovation_number=15, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=7, output_node=4, innovation_number=21, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=8, output_node=4, innovation_number=22, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=9, output_node=4, innovation_number=23, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=10, output_node=4, innovation_number=24, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=11, output_node=4, innovation_number=25, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=12, innovation_number=31, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=13, innovation_number=32, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=14, innovation_number=33, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=15, innovation_number=34, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=5, innovation_number=35, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=6, innovation_number=36, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=12, innovation_number=41, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=13, innovation_number=42, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=14, innovation_number=43, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=15, innovation_number=44, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=5, innovation_number=45, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=6, innovation_number=46, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=12, output_node=2, innovation_number=51, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=13, output_node=2, innovation_number=52, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=14, output_node=2, innovation_number=53, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=15, output_node=2, innovation_number=54, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=5, output_node=2, innovation_number=9, enabled=True, weight=np.random.rand()),
ConnectionGene(input_node=6, output_node=2, innovation_number=10, enabled=True, weight=np.random.randn())
]
x_data, y_data = get_circle_data()
y_data.shape = (200, 1)
for row in range(y_data.shape[0]):
if y_data[row, 0] == -1:
y_data[row, 0] = 0
genome = Genome(connections=connection_list, nodes=node_list, key=1)
genome_nn = GenomeNeuralNetwork(genome=genome, create_weights_bias_from_genome=True, activation_type='sigmoid',
num_epochs=10000,
batch_size=50,
learning_rate=0.1,
x_train=x_data, y_train=y_data)
epoch_list, cost_list = genome_nn.optimise(print_epoch=True)
f1_score = NEAT.calculate_f_statistic(genome=genome, x_test_data=x_data, y_test_data=y_data)
assert (cost_list[len(cost_list) - 1] < 0.1)
assert (f1_score > 0.94)
def test_genome_convergence_with_circle_data_2(self):
np.random.seed(1)
node_list = [
NodeGene(node_id=0, node_type='source'),
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='source'),
NodeGene(node_id=3, node_type='source'),
NodeGene(node_id=4, node_type='source'),
NodeGene(node_id=5, node_type='source'),
NodeGene(node_id=6, node_type='source'),
NodeGene(node_id=7, node_type='output', bias=0.5)]
connection_list = [
ConnectionGene(input_node=0, output_node=7, innovation_number=11, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=1, output_node=7, innovation_number=12, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=2, output_node=7, innovation_number=13, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=7, innovation_number=14, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=7, innovation_number=15, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=5, output_node=7, innovation_number=17, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=6, output_node=7, innovation_number=16, enabled=True, weight=np.random.randn())]
x_data, y_data = get_circle_data()
y_data.shape = (200, 1)
for row in range(y_data.shape[0]):
if y_data[row, 0] == -1:
y_data[row, 0] = 0
genome = Genome(connections=connection_list, nodes=node_list, key=1)
genome_nn = GenomeNeuralNetwork(genome=genome, create_weights_bias_from_genome=True, activation_type='sigmoid',
num_epochs=10000,
batch_size=50,
learning_rate=0.1,
x_train=x_data, y_train=y_data)
epoch_list, cost_list = genome_nn.optimise(print_epoch=True)
f1_score = NEAT.calculate_f_statistic(genome=genome, x_test_data=x_data, y_test_data=y_data)
assert (cost_list[len(cost_list) - 1] < 0.15)
def test_nn_f1_score(self):
node_list = [
NodeGene(node_id=0, node_type='source'),
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='output', bias=0.5),
NodeGene(node_id=3, node_type='hidden', bias=1),
NodeGene(node_id=4, node_type='hidden', bias=1),
NodeGene(node_id=5, node_type='hidden', bias=1),
NodeGene(node_id=6, node_type='hidden', bias=1),
]
connection_list = [
ConnectionGene(input_node=0, output_node=3, innovation_number=1, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=1, output_node=3, innovation_number=2, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=0, output_node=4, innovation_number=3, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=1, output_node=4, innovation_number=4, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=5, innovation_number=5, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=5, innovation_number=6, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=3, output_node=6, innovation_number=7, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=4, output_node=6, innovation_number=8, enabled=True, weight=np.random.randn()),
ConnectionGene(input_node=5, output_node=2, innovation_number=9, enabled=True, weight=np.random.rand()),
ConnectionGene(input_node=6, output_node=2, innovation_number=10, enabled=True, weight=np.random.randn())
]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
x_data, y_data = create_data(n_generated=5000)
genome_nn = GenomeNeuralNetwork(num_epochs=80, genome=genome, create_weights_bias_from_genome=False,
activation_type='sigmoid', learning_rate=1.2,
x_train=x_data, y_train=y_data)
genome_nn.optimise(print_epoch=True)
genome_nn_new = NEAT.create_genome_nn(genome=genome, x_data=x_data, y_data=y_data)
prediction_2 = genome_nn_new.run_one_pass(input_data=x_data, return_prediction_only=True).round()
if not np.array_equal(prediction_2, y_data):
print('The behaviour here is unexpected as they should be equal')
f1_score = NEAT.calculate_f_statistic(genome, x_data, y_data)
self.assertEqual(1.0, f1_score)
def test_f1_score_bug_fix(self):
# This test is for a bug which was occuring before
node_list = [
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='output', bias=0.5),
NodeGene(node_id=3, node_type='hidden', bias=1.2)]
connection_list = [ConnectionGene(input_node=1, output_node=2, innovation_number=2, enabled=False, weight=9),
ConnectionGene(input_node=1, output_node=3, innovation_number=4, enabled=True, weight=5),
ConnectionGene(input_node=3, output_node=2, innovation_number=5, enabled=True, weight=5)]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
x_data, y_data = create_data(n_generated=200)
f1_score = NEAT.calculate_f_statistic(genome=genome, x_test_data=x_data, y_test_data=y_data)
self.assertTrue(f1_score)
def test_creation_genome_nn(self):
node_list = [NodeGene(node_id=0, node_type='source'),
NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='output', bias=0.5),
NodeGene(node_id=3, node_type='hidden', bias=1.2)]
connection_list = [ConnectionGene(input_node=0, output_node=2, innovation_number=1, enabled=True, weight=9),
ConnectionGene(input_node=1, output_node=2, innovation_number=6, enabled=False, weight=5),
ConnectionGene(input_node=1, output_node=3, innovation_number=2, enabled=False, weight=5),
ConnectionGene(input_node=3, output_node=2, innovation_number=7, enabled=True, weight=7)]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
x_data = np.array([[1, 0]])
y_data = np.array([[1]])
genome_nn = GenomeNeuralNetwork(genome=genome, create_weights_bias_from_genome=True, activation_type='sigmoid',
x_train=x_data, y_train=y_data)
self.assertTrue(genome_nn)
def test_update_gene(self):
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='source'),
NodeGene(node_id=3, node_type='hidden', bias=0.5),
NodeGene(node_id=4, node_type='hidden', bias=-1.5),
NodeGene(node_id=5, node_type='output', bias=1.5)]
connection_list = [ConnectionGene(input_node=1, output_node=5, innovation_number=1, enabled=True, weight=9),
ConnectionGene(input_node=1, output_node=3, innovation_number=2, enabled=True, weight=2),
ConnectionGene(input_node=2, output_node=3, innovation_number=3, enabled=True, weight=3),
ConnectionGene(input_node=2, output_node=4, innovation_number=4, enabled=True, weight=4),
ConnectionGene(input_node=3, output_node=5, innovation_number=6, enabled=True, weight=5),
ConnectionGene(input_node=4, output_node=5, innovation_number=7, enabled=True, weight=7)]
genome = Genome(connections=connection_list, nodes=node_list, key=1)
x_data = np.array([[1, 0]])
y_data = np.array([[1]])
genome_nn = GenomeNeuralNetwork(genome=genome, create_weights_bias_from_genome=False, activation_type='sigmoid',
x_train=x_data, y_train=y_data)
genome_nn.weights_dict[1] = np.array([[3, 0, 1], [4, 5, 0]])
genome_nn.weights_dict[2] = np.array([[1], [2], [3]])
genome_nn.bias_dict[1] = np.array([[1, 2, 0]])
genome_nn.bias_dict[2] = np.array([[7]])
expect_weights = {(1, 5): 3, (1, 3): 3, (2, 3): 4, (2, 4): 5, (4, 5): 2, (3, 5): 1}
expected_bias = {3: 1, 4: 2, 5: 7}
genome_nn.update_genes()
for connection in genome.connections.values():
self.assertEqual(expect_weights[(connection.input_node, connection.output_node)], connection.weight)
for node in genome.nodes.values():
if node.node_type != 'source':
self.assertEqual(node.bias, expected_bias[node.node_id])
def test_run_one_pass(self):
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='source'),
NodeGene(node_id=5, node_type='output', bias=0)]
connection_list = [ConnectionGene(input_node=1, output_node=5, innovation_number=1, enabled=True, weight=4),
ConnectionGene(input_node=2, output_node=5, innovation_number=7, enabled=True, weight=0)]
genome = Genome(nodes=node_list, connections=connection_list, key=1)
x_data = np.array([[1, 0]])
y_data = np.array([[1]])
genome_nn = GenomeNeuralNetwork(genome=genome, x_train=x_data, y_train=y_data, learning_rate=0.1,
create_weights_bias_from_genome=True, activation_type='sigmoid')
cost = genome_nn.run_one_pass(input_data=x_data, labels=y_data)
# The output should be sigmoid of the prediction which is 4. Then the loss is calculated using log loss.
expected_answer = round(-np.log(ActivationFunctions.sigmoid(4)), 5)
self.assertEqual(expected_answer, round(cost, 5))
def test_genome_configuration(self):
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='output', bias=0)]
connection_list = [ConnectionGene(input_node=1, output_node=2, innovation_number=1, enabled=True, weight=4)]
genome = Genome(nodes=node_list, connections=connection_list, key=1)
x_data = np.array([[1, 0]])
y_data = np.array([[1]])
genome_nn = GenomeNeuralNetwork(genome=genome, x_train=x_data, y_train=y_data, learning_rate=0.1,
create_weights_bias_from_genome=True, activation_type='sigmoid')
assert genome_nn
class TestGenomeMutatation(unittest.TestCase):
def setUp(self):
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='source'),
NodeGene(node_id=3, node_type='hidden'),
NodeGene(node_id=4, node_type='hidden'),
NodeGene(node_id=5, node_type='output')]
# Note that one of the connections isn't enabled
connection_list = [ConnectionGene(input_node=1, output_node=3, innovation_number=1, enabled=True),
ConnectionGene(input_node=1, output_node=4, innovation_number=2, enabled=True),
ConnectionGene(input_node=2, output_node=3, innovation_number=3, enabled=True),
ConnectionGene(input_node=2, output_node=4, innovation_number=4, enabled=True),
ConnectionGene(input_node=3, output_node=5, innovation_number=5, enabled=True),
ConnectionGene(input_node=4, output_node=5, innovation_number=6, enabled=True)]
self.genome = Genome(connections=connection_list, nodes=node_list, key=2)
def test_add_connection_2(self):
"""
Can't let two source nodes connect to each other
:return:
"""
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='source'),
NodeGene(node_id=3, node_type='output', bias=0)]
connection_list = [ConnectionGene(input_node=1, output_node=3, innovation_number=1),
ConnectionGene(input_node=2, output_node=3, innovation_number=2)]
for i in range(100):
genome = Genome(connections=connection_list, nodes=node_list, key=1)
reproduce = Reproduce(config=Config, stagnation=Stagnation)
reproduce.global_innovation_number = 7
genome.add_connection(reproduction_instance=reproduce, innovation_tracker={})
self.assertTrue(len(genome.connections) == 2)
def test_add_connection(self):
reproduce = Reproduce(config=Config, stagnation=Stagnation)
reproduce.global_innovation_number = 7
new_connection = self.genome.add_connection(
reproduction_instance=reproduce,
innovation_tracker={})
self.assertTrue(len(self.genome.connections) == 7)
# Unpack the new genome
self.genome.unpack_genome()
# Check if connection was where it connected to a node on the same layer
if (new_connection.input_node == 3 and new_connection.output_node == 4) or (
new_connection.input_node == 4 and new_connection.output_node == 3):
# If the connection is one the same layer then the number of layers will have increased
self.assertEqual(self.genome.num_layers_including_input, 4)
# Check that two source nodes aren't connected
elif self.genome.nodes[new_connection.input_node].node_type == 'source':
self.assertTrue(self.genome.nodes[new_connection.output_node].node_type != 'source')
# Can't connect to itself
self.assertTrue(new_connection.input_node != new_connection.output_node)
def test_remove_connection(self):
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='source'),
NodeGene(node_id=3, node_type='hidden'),
NodeGene(node_id=4, node_type='hidden'),
NodeGene(node_id=5, node_type='output')]
# Note that one of the connections isn't enabled
connection_list = [
ConnectionGene(input_node=2, output_node=4, innovation_number=4, enabled=True),
ConnectionGene(input_node=4, output_node=3, innovation_number=5, enabled=True),
ConnectionGene(input_node=3, output_node=5, innovation_number=8, enabled=True)]
genome = Genome(connections=connection_list, nodes=node_list, key=2)
genome.remove_connection()
# Check that the source nodes are always there
self.assertTrue(genome.nodes[1])
self.assertTrue(genome.nodes[2])
self.assertTrue(genome.nodes[5])
genome.unpack_genome()
def test_remove_connections_2(self):
for i in range(100):
node_list = [NodeGene(node_id=1, node_type='source'),
NodeGene(node_id=2, node_type='source'),
NodeGene(node_id=3, node_type='output', bias=0)]
connection_list = [ConnectionGene(input_node=1, output_node=3, innovation_number=1),
ConnectionGene(input_node=2, output_node=3, innovation_number=2)]
genome = Genome(connections=connection_list, nodes=node_list, key=2)
connection_removed = genome.remove_connection()
self.assertTrue(connection_removed in connection_list)
genome.unpack_genome()
self.assertTrue(genome)
def test_add_node(self):
number_of_beginning_connections = len(self.genome.connections)
self.assertEqual(number_of_beginning_connections, 6)
# Because we replaced 1 connection with two
expected_number_connections = number_of_beginning_connections + 2
reproduce = Reproduce(config=Config, stagnation=Stagnation)
reproduce.global_innovation_number = 7
self.genome.add_node(reproduction_instance=reproduce,
innovation_tracker={})
self.assertEqual(len(self.genome.connections), expected_number_connections)
number_of_disabled_connections = 1
disabled_counters = 0
for connection in self.genome.connections.values():
if not connection.enabled:
disabled_counters += 1