-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtestinternal.cpp
2608 lines (2172 loc) · 99.6 KB
/
testinternal.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
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
#include "testinternal.h"
#include "simulation.h"
#include <QCryptographicHash>
#include <QRandomGenerator>
testinternal::testinternal(MainWindow *theMainWindowCon)
{
theMainWindow = theMainWindowCon;
error = false;
//This is currently used (June 2024) to return the test description
//Long term it would be good to create a test class for each test which returns a description and includes the test code
testList.insert(0, "Fitness Algorithm");
testList.insert(1, "Mask initialisation");
testList.insert(2, "Masks during simulation");
testList.insert(3, "Initialisation of playing field");
testList.insert(4, "Stochastic mapping");
testList.insert(5, "Mutation rates");
testList.insert(6, "Coin toss");
testList.insert(7, "Speciation");
testList.insert(8, "Overwrite");
testList.insert(9, "Perturbations");
testList.insert(10, "Strip uninformative");
testList.insert(11, "Unresolvable taxa");
testList.insert(12, "Memory use");
testList.insert(13, "Extinction");
testList.insert(14, "New species ID");
testList.insert(15, "Print matrix");
testList.insert(16, "Print tree");
testList.insert(17, "Ecosystem engineers");
testList.insert(18, "Playing field mixing");
testList.insert(19, "Match Peaks");
testList.insert(20, "Organism operators");
}
QString testinternal::testDescription(int testNumber)
{
if (testList.contains(testNumber)) return testList.value(testNumber);
else return "No description available";
}
bool testinternal::callTest(int testNumber, QString &outString)
{
bool pass;
switch (testNumber)
{
case 0:
pass = testZero(outString);
return pass;
case 1:
pass = testOne(outString);
return pass;
case 2:
pass = testTwo(outString);
return pass;
case 3:
pass = testThree(outString);
return pass;
case 4:
pass = testFour(outString);
return pass;
case 5:
pass = testFive(outString);
return pass;
case 6:
pass = testSix(outString);
return pass;
case 7:
pass = testSeven(outString);
return pass;
case 8:
pass = testEight(outString);
return pass;
case 9:
pass = testNine(outString);
return pass;
case 10:
pass = testTen(outString);
return pass;
case 11:
pass = testEleven(outString);
return pass;
case 12:
pass = testTwelve(outString);
return pass;
case 13:
pass = testThirteen(outString);
return pass;
case 14:
pass = testFourteen(outString);
return pass;
case 15:
pass = testFifteen(outString);
return pass;
case 16:
pass = testSixteen(outString);
return pass;
case 17:
pass = testSeventeen(outString);
return pass;
case 18:
pass = testEighteen(outString);
return pass;
case 19:
pass = testNineteen(outString);
return pass;
case 20:
pass = testTwenty(outString);
return pass;
}
return false;
}
//Test fitness algorithm - send fitness function known masks and organism, and check output
bool testinternal::testZero(QString &outString)
{
bool testFlag = true;
QTextStream out(&outString);
out << "Testing fitness algorithm.\n\n";
//Create default setting object and then a simulation object for the test
simulationVariables simSettings;
simSettings.genomeSize = 50;
simSettings.fitnessSize = 50;
simSettings.speciesSelectSize = 50;
//Test is for three masks and a target of zero (the defaults in v2.0.0)
simSettings.fitnessTarget = 0;
simSettings.maskNumber = 3;
simulation x(0, &simSettings, &error, theMainWindow);
if (error) return false;
//Fitness requires an organism - create an organism with 50 bits, no stochastic genome, all bits are initialised to zero
Organism org(simSettings.genomeSize, false);
out << "Organism genome is: " << x.printGenomeString(&org) << "\n";
//Now set masks in simulation to 1
for (auto p : std::as_const(x.playingFields))
for (int k = 0; k < simSettings.environmentNumber; k++)
for (int j = 0; j < simSettings.maskNumber; j++)
for (auto &i : p->masks[k][j]) i = true;
QString maskString = x.printMasks(x.playingFields);
QStringList l = maskString .split('\n');
out << "Masks are:\n" << l[2] << "\n" << l[3] << "\n" << l[4] << "\n";
int fitness = x.fitness(&org, x.playingFields[0]->masks, simSettings.fitnessSize, simSettings.fitnessTarget, simSettings.maskNumber);
if (fitness != 150) testFlag = false;
out << "Fitness, with fitness target of " << simSettings.fitnessTarget << " is " << fitness << ". It should be 150.\n";
simSettings.fitnessTarget = 75;
fitness = x.fitness(&org, x.playingFields[0]->masks, simSettings.fitnessSize, simSettings.fitnessTarget, simSettings.maskNumber);
if (fitness != 75) testFlag = false;
out << "Fitness, with fitness target of 75, is " << fitness << ". It should be 75.\n";
for (int i = 0; i < 25; i++)org.genome[i] = true;
out << "Fitness target is still 75, genome is now: " << x.printGenomeString(&org) << "\n";
fitness = x.fitness(&org, x.playingFields[0]->masks, simSettings.fitnessSize, simSettings.fitnessTarget, simSettings.maskNumber);
if (fitness != 0) testFlag = false;
out << "Fitness is " << fitness << ". It should be 0.\n";
simSettings.maskNumber = 2;
simSettings.fitnessTarget = 50;
out << "Set masks to two, and fitness target back to 50.\n";
simulation y(0, &simSettings, &error, theMainWindow);
if (error) return false;
//Now set masks in simulation to 1
for (auto p : std::as_const(y.playingFields))
for (int k = 0; k < simSettings.environmentNumber; k++)
for (int j = 0; j < simSettings.maskNumber; j++)
for (auto &i : p->masks[k][j]) i = true;
for (int i = 0; i < simSettings.fitnessSize; i++)org.genome[i] = true;
maskString = y.printMasks(y.playingFields);
l = maskString .split('\n');
out << "Organism genome is: " << y.printGenomeString(&org) << "\n";
out << "Masks are:\n" << l[2] << "\n" << l[3] << "\n";
fitness = y.fitness(&org, y.playingFields[0]->masks, simSettings.fitnessSize, simSettings.fitnessTarget, simSettings.maskNumber);
if (fitness != 50) testFlag = false;
out << "Fitness is " << fitness << ". It should be 50.\n\n";
//Now let's test with two environments
out << "Two environments, one all ones, the other all zeros, target is zero.\n";
simSettings.environmentNumber = 2;
simSettings.fitnessTarget = 0;
simulation z(0, &simSettings, &error, theMainWindow);
if (error) return false;
//Now set masks in environment 0 simulation to 1
for (auto p : std::as_const(z.playingFields))
for (int j = 0; j < simSettings.maskNumber; j++)
for (auto &i : p->masks[0][j]) i = true;
//Now set masks in environment 1 simulation to 0
for (auto p : std::as_const(z.playingFields))
for (int j = 0; j < simSettings.maskNumber; j++)
for (auto &i : p->masks[1][j]) i = false;
for (int i = 0; i < 10; i++)org.genome[i] = false;
maskString = z.printMasks(z.playingFields);
l = maskString.split('\n');
out << "Organism genome is: " << y.printGenomeString(&org) << " fitness target is " << simSettings.fitnessTarget << "\n";
out << maskString;
int environmentZeroFitness = z.fitness(&org, z.playingFields[0]->masks, simSettings.fitnessSize, simSettings.fitnessTarget, simSettings.maskNumber, 0);
out << "Environment zero fitness should be 20, it is " << environmentZeroFitness << ".\n";
if (environmentZeroFitness != 20) testFlag = false;
int environmentOneFitness = z.fitness(&org, z.playingFields[0]->masks, simSettings.fitnessSize, simSettings.fitnessTarget, simSettings.maskNumber, 1);
out << "Environment one fitness should be 80, it is " << environmentOneFitness << ".\n";
if (environmentOneFitness != 80) testFlag = false;
if (environmentOneFitness == environmentZeroFitness) testFlag = false;
int environmentBothFitness = z.fitness(&org, z.playingFields[0]->masks, simSettings.fitnessSize, simSettings.fitnessTarget, simSettings.maskNumber);
out << "Fitness based on both environments should be 20, it is " << environmentBothFitness << ".\n";
if (environmentBothFitness != 20 || environmentBothFitness != environmentZeroFitness) testFlag = false;
if (testFlag) out << "\nFitness tests passed.\n";
return testFlag;
}
//Test mask initialisation, and behaviour with multiple playing fields in different mask modes
bool testinternal::testOne(QString &outString)
{
bool testFlag = true;
QTextStream out(&outString);
out << "Mask mode at initialisation. If there is more than one playing field this will set the playing field masks/environments to be the same or different as the mode dictates.\n";
//Create default setting sonject and then a simulation object for the test
simulationVariables simSettings;
simSettings.genomeSize = 50;
simSettings.fitnessSize = 50;
simSettings.speciesSelectSize = 50;
simSettings.test = 1;
//Mode identical
simSettings.playingfieldMasksMode = MASKS_MODE_IDENTICAL;
simSettings.playingfieldNumber = 3;
simulation x(0, &simSettings, &error, theMainWindow);
if (error) return false;
if (x.playingFields[0]->masks != x.playingFields[1]->masks || x.playingFields[1]->masks != x.playingFields[2]->masks) testFlag = false;
QString flagString = testFlag ? "true" : "false";
QString masks(x.printMasks(x.playingFields, 1));
QString masks2(x.printMasks(x.playingFields, 2));
out << "\nMode identical:\n" << QCryptographicHash::hash(masks.toUtf8(), QCryptographicHash::Md5).toHex() << "\n" << QCryptographicHash::hash(masks2.toUtf8(),
QCryptographicHash::Md5).toHex() << "\nTREvoSim has tested whether these are identical (they should be) and outputs " << flagString << ".\n";
//Mode independent
simSettings.playingfieldMasksMode = MASKS_MODE_INDEPENDENT;
simulation y(0, &simSettings, &error, theMainWindow);
if (error) return false;
if (y.playingFields[0]->masks == y.playingFields[1]->masks || y.playingFields[1]->masks == y.playingFields[2]->masks) testFlag = false;
flagString = testFlag ? "true" : "false";
masks = (y.printMasks(y.playingFields, 1));
masks2 = (y.printMasks(y.playingFields, 2));
out << "\nMode independent:\n" << QCryptographicHash::hash(masks.toUtf8(), QCryptographicHash::Md5).toHex() << "\n" << QCryptographicHash::hash(masks2.toUtf8(),
QCryptographicHash::Md5).toHex() << "\nTREvoSim has tested whether these are not identical and outputs " << flagString << ".\n";
//Mode identical at start
simSettings.playingfieldMasksMode = MASKS_MODE_IDENTICAL_START;
simulation z(0, &simSettings, &error, theMainWindow);
if (error) return false;
if (z.playingFields[0]->masks != z.playingFields[1]->masks || z.playingFields[1]->masks != z.playingFields[2]->masks) testFlag = false;
flagString = testFlag ? "true" : "false";
masks = (z.printMasks(z.playingFields, 1));
masks2 = (z.printMasks(z.playingFields, 2));
out << "\nMode start identical:\n" << QCryptographicHash::hash(masks.toUtf8(), QCryptographicHash::Md5).toHex() << "\n" << QCryptographicHash::hash(masks2.toUtf8(),
QCryptographicHash::Md5).toHex() << "\nTREvoSim has tested whether these are identical and outputs " << flagString << ".\n";
if (testFlag) out << "\nMask initiation tests passed.\n";
return testFlag;
}
//Test masks in different playing fields for different modes after a simulation has been running
bool testinternal::testTwo(QString &outString)
{
bool testFlag = true;
QTextStream out(&outString);
out << "Masks after 100 iterations. This will differ based on mode.\n\n";
//Create default setting object and then a simulation object for the test
simulationVariables simSettings;
simSettings.genomeSize = 50;
simSettings.fitnessSize = 50;
simSettings.speciesSelectSize = 50;
simSettings.playingfieldNumber = 3;
simSettings.runForTaxa = 5;
simSettings.test = 2;
if (theMainWindow)
theMainWindow->resizeGrid(simSettings.runForTaxa, simSettings.genomeSize);
//Mode identical
simSettings.playingfieldMasksMode = MASKS_MODE_IDENTICAL;
simulation x(0, &simSettings, &error, theMainWindow);
if (error) return false;
x.run();
if (x.playingFields[0]->masks != x.playingFields[1]->masks || x.playingFields[1]->masks != x.playingFields[2]->masks) testFlag = false;
QString flagString = testFlag ? "true" : "false";
QString masks(x.printMasks(x.playingFields, 1));
QString masks2(x.printMasks(x.playingFields, 2));
out << "Mode identical:\n" << QCryptographicHash::hash(masks.toUtf8(), QCryptographicHash::Md5).toHex() << "\n" << QCryptographicHash::hash(masks2.toUtf8(),
QCryptographicHash::Md5).toHex() << "\nTREvoSim has tested whether these are identical and outputs " << flagString << ".\n";
//Mode independent
simSettings.playingfieldMasksMode = MASKS_MODE_INDEPENDENT;
simulation y(0, &simSettings, &error, theMainWindow);
if (error) return false;
y.run();
if (y.playingFields[0]->masks == y.playingFields[1]->masks || y.playingFields[1]->masks == y.playingFields[2]->masks) testFlag = false;
flagString = testFlag ? "true" : "false";
masks = (y.printMasks(y.playingFields, 1));
masks2 = (y.printMasks(y.playingFields, 2));
out << "\nMode independent:\n" << QCryptographicHash::hash(masks.toUtf8(), QCryptographicHash::Md5).toHex() << "\n" << QCryptographicHash::hash(masks2.toUtf8(),
QCryptographicHash::Md5).toHex() << "\nTREvoSim has tested whether these are not identical and outputs " << flagString << ".\n";
//Mode identical at start
simSettings.playingfieldMasksMode = MASKS_MODE_IDENTICAL_START;
simulation z(0, &simSettings, &error, theMainWindow);
if (error) return false;
z.run();
if (z.playingFields[0]->masks == z.playingFields[1]->masks || z.playingFields[1]->masks == z.playingFields[2]->masks) testFlag = false;
flagString = testFlag ? "true" : "false";
masks = (z.printMasks(z.playingFields, 1));
masks2 = (z.printMasks(z.playingFields, 2));
out << "\nMode start identical:\n" << QCryptographicHash::hash(masks.toUtf8(), QCryptographicHash::Md5).toHex() << "\n" << QCryptographicHash::hash(masks2.toUtf8(),
QCryptographicHash::Md5).toHex() << "\nTREvoSim has tested whether these are not identical and outputs " << flagString << ".\n";
if (testFlag) out << "\nMask mode tests passed.\n";
simulationVariables simSettingsReset;
if (theMainWindow)
{
theMainWindow->resizeGrid(simSettingsReset.runForTaxa, simSettingsReset.genomeSize);
theMainWindow->resetDisplays();
}
return testFlag;
}
//Test initialisation of playing fields - should be populated with identical individual
bool testinternal::testThree(QString &outString)
{
bool testFlag = true;
QTextStream out(&outString);
out << "Checking that all organisms are identical after initialisation in all mask modes.\n\n";
simulationVariables simSettings;
simSettings.genomeSize = 50;
simSettings.fitnessSize = 50;
simSettings.speciesSelectSize = 50;
simSettings.playingfieldNumber = 3;
simSettings.runForTaxa = 5;
simSettings.test = 3;
//simSettings.workingLog = true;
if (theMainWindow)
theMainWindow->resizeGrid(simSettings.runForTaxa, simSettings.genomeSize);
//Mode identical
simSettings.playingfieldMasksMode = MASKS_MODE_IDENTICAL;
simulation x(0, &simSettings, &error, theMainWindow);
if (error) return false;
x.run();
Organism org(*x.playingFields[0]->playingField[0]);
for (auto p : std::as_const(x.playingFields))
for (auto o : std::as_const(p->playingField))
if (!(*o == org)) testFlag = false;
QString flagString = testFlag ? "true" : "false";
out << "Mode identical - checked if all organisms are the same after initialisation and returned " << flagString << ".\n";
//Mode independent
simSettings.playingfieldMasksMode = MASKS_MODE_INDEPENDENT;
simulation y(0, &simSettings, &error, theMainWindow);
if (error) return false;
y.run();
org = *y.playingFields[0]->playingField[0];
for (auto p : std::as_const(y.playingFields))
for (auto o : std::as_const(p->playingField))
if (!(*o == org)) testFlag = false;
flagString = testFlag ? "true" : "false";
out << "Mode independent - checked if all organisms are the same after initialisation and returned " << flagString << ".\n";
//Mode identical at start
simSettings.playingfieldMasksMode = MASKS_MODE_IDENTICAL_START;
simulation z(0, &simSettings, &error, theMainWindow);
if (error) return false;
z.run();
org = *z.playingFields[0]->playingField[0];
for (auto p : std::as_const(z.playingFields))
for (auto o : std::as_const(p->playingField))
if (!(*o == org)) testFlag = false;
flagString = testFlag ? "true" : "false";
out << "Mode identical at start - checked if all organisms are the same after initialisation and returned " << flagString << ".\n";
if (testFlag) out << "\nInitialisation tests passed.\n";
simulationVariables simSettingsReset;
if (theMainWindow)
{
theMainWindow->resizeGrid(simSettingsReset.runForTaxa, simSettingsReset.genomeSize);
theMainWindow->resetDisplays();
}
return testFlag;
}
//Test stochastic mapping - creation of genome from stochastic layer using user defined map
bool testinternal::testFour(QString &outString)
{
bool testFlag = true;
QTextStream out(&outString);
simulationVariables simSettings;
simSettings.stochasticLayer = true;
simSettings.test = 4;
for (auto &i : simSettings.stochasticMap) i = 0;
out << "Testing stochastic mapping.\n\nStochastic map is currently:\n";
for (auto i : simSettings.stochasticMap) i ? out << "1" : out << "0";
Organism org(20, true);
org.initialise(20, simSettings.stochasticMap);
out << "\nStochastic genome is: ";
for (auto i : std::as_const(org.stochasticGenome)) i ? out << "1" : out << "0";
out << "\nGenome is: ";
for (auto i : std::as_const(org.genome)) i ? out << "1" : out << "0";
for (auto i : std::as_const(org.genome)) if (i) testFlag = false;
out << "\nStochastic map has been updated and is now:\n";
for (auto &i : simSettings.stochasticMap) i = 1;
for (auto i : simSettings.stochasticMap) i ? out << "1" : out << "0";
org.initialise(20, simSettings.stochasticMap);
out << "\nStochastic genome is: ";
for (auto i : std::as_const(org.stochasticGenome)) i ? out << "1" : out << "0";
out << "\nGenome is: ";
for (auto i : std::as_const(org.genome)) i ? out << "1" : out << "0";
for (auto i : std::as_const(org.genome)) if (!i) testFlag = false;
QString flagString = testFlag ? "true" : "false";
out << "\n\nGiven the mapping the first genome should be all zeros, no matter what the stochastic genome, and the second all ones. TRevoSim tested this and returned " << flagString << ".";
if (testFlag) out << "\n\nStochastic mapping tests passed.\n";
return testFlag;
}
//Test mutation rates for environment and organism
bool testinternal::testFive(QString &outString)
{
bool testFlag = true;
QTextStream out(&outString);
out << "Testing mutation rates.\n\n";
//How many times do we want to run these tests?
int replicates = 10000;
if (theMainWindow)
{
theMainWindow->addProgressBar(0, replicates);
theMainWindow->setStatus("Doing organism mutation tests");
}
simulationVariables simSettings;
simSettings.organismMutationRate = 1.;
simSettings.test = 5;
simulation x(0, &simSettings, &error, theMainWindow);
if (error) return false;
Organism org1(simSettings.genomeSize, false);
int cnt = 0;
for (int j = 0; j < replicates; j++)
{
if (theMainWindow) theMainWindow->setProgressBar(j);
org1.initialise(simSettings.genomeSize);
Organism org2(org1);
x.mutateOrganism(org1, x.playingFields[0]);
for (int i = 0; i < org1.genome.length(); i++)if (org1.genome[i] != org2.genome[i]) cnt++;
}
double mean = static_cast<double>(cnt) / static_cast<double>(replicates);
if (mean < 1.25 || mean > 1.31) testFlag = false;
QString flagString = testFlag ? "true" : "false";
out << "Ran 10000 iterations on a 128 bit organism. At a rate of " << simSettings.organismMutationRate << " mutation per hundred characters per iteration this resulted in a mean of ";
out << mean << " mutations. TREvoSim expects this to be between 1.25 and 1.31 and returned " << flagString << "\n";
simSettings.organismMutationRate = 2.;
Organism org3(simSettings.genomeSize, false);
cnt = 0;
for (int j = 0; j < replicates; j++)
{
if (theMainWindow) theMainWindow->setProgressBar(j);
org3.initialise(simSettings.genomeSize);
Organism org4(org3);
x.mutateOrganism(org3, x.playingFields[0]);
for (int i = 0; i < org3.genome.length(); i++) if (org3.genome[i] != org4.genome[i]) cnt++;
}
mean = static_cast<double>(cnt) / static_cast<double>(replicates);
if (mean < 2.5 || mean > 2.62) testFlag = false;
flagString = testFlag ? "true" : "false";
out << "Ran 10000 iterations on a 128 bit organism. At a rate of " << simSettings.organismMutationRate << " mutations per hundred characters per iteration this resulted in a mean of ";
out << mean << " mutations. TREvoSim expects this to be between 2.5 and 2.62 and returned " << flagString;
out << " (Note that due to the possibility of multiple hits on a single site, we will expect this to be marginally smaller than the expected mean).\n";
if (theMainWindow) theMainWindow->setStatus("Doing environment mutation tests without mathcing peaks.");
out << "Now testing environment mutation across two playing fields (mode independent), and two environments for each. Same test for each as above. "
"Below you can see a table showing the mean mutations per 128 bits. \nPlaying field 1:\nEnvironment 1:\t";
simSettings.environmentNumber = 2;
simSettings.playingfieldNumber = 2;
simSettings.playingfieldMasksMode = MASKS_MODE_INDEPENDENT;
simulation y(0, &simSettings, &error, theMainWindow);
if (error) return false;
QVector <QVector <QVector <bool> > > masks;
QVector <QVector <QVector <bool> > > masks2;
int cnts[12] = {0};
//Count the number of ones - this may change if we do not match peaks
int maxDiff = 0;
for (int i = 0; i < replicates; i++)
{
if (theMainWindow) theMainWindow->setProgressBar(i);
masks = y.playingFields[0]->masks;
masks2 = y.playingFields[1]->masks;
y.mutateEnvironment();
//Count the differences - i.e. the number of mutations
for (int k = 0; k < masks[0][0].length(); k++)
{
for (int j = 0; j < 3; j++) if (y.playingFields[0]->masks[0][j][k] != masks[0][j][k])cnts[j]++;
for (int j = 0; j < 3; j++) if (y.playingFields[0]->masks[1][j][k] != masks[1][j][k])cnts[j + 3]++;
for (int j = 0; j < 3; j++) if (y.playingFields[1]->masks[0][j][k] != masks2[0][j][k])cnts[j + 6]++;
for (int j = 0; j < 3; j++) if (y.playingFields[1]->masks[1][j][k] != masks2[1][j][k])cnts[j + 9]++;
}
//Now count the ones
int count0 = 0;
for (auto p : std::as_const(y.playingFields))
for (auto &e : p->masks)
for (auto &m : e)
for (auto b : m) if (b) count0++;
int count1 = 0;
for (auto &e : masks)
for (auto &m : e)
for (auto b : m) if (b) count1++;
for (auto &e : masks2)
for (auto &m : e)
for (auto b : m) if (b) count1++;
int difference = abs(count0 - count1);
if (difference > maxDiff)maxDiff = difference;
}
double dCnts[12] = {0.};
double dCntsSum = 0.;
for (int i = 0; i < 12; i++)
{
dCnts[i] = (static_cast<double>(cnts[i]) / static_cast<double>(replicates));
if (dCnts[i] < 1.25 || dCnts[i] > 1.31) testFlag = false;
dCntsSum += dCnts[i];
if (i == 3) out << "Environment 2: ";
if (i == 6) out << "Playing field 2:\nEnvironment 1: ";
if (i == 9) out << "Environment 2: ";
out << dCnts[i] << "\t";
if ((i + 1) % 3 == 0) out << "\n";
}
flagString = testFlag ? "true" : "false";
out << "The mean of these values is " << dCntsSum / 12. << ".\n";
out << "TREvoSim expects all above to be between 1.25 and 1.31 and returned " << flagString << ".\n";
if (maxDiff == 0)
{
testFlag = false;
out << "Fail at ones count with matching peaks off";
}
//Repeat this test with matching peaks, which should equate to the same number, but only do two mutations at once, resulting in the same number of ones
simSettings.matchFitnessPeaks = true;
simulation z(0, &simSettings, &error, theMainWindow);
if (error) return false;
out << "Now testing environment mutation across two playing fields (mode independent), and two environments for each, with matching peaks turned on. Same test for each as above. \nPlaying field 1:\nEnvironment 1:\t";
//Reset counts
for (auto &i : cnts) i = 0;
maxDiff = 0;
dCntsSum = 0;
if (theMainWindow) theMainWindow->setStatus("Doing environment mutation tests with matching peaks.");
for (int i = 0; i < replicates; i++)
{
if (theMainWindow) theMainWindow->setProgressBar(i);
masks = z.playingFields[0]->masks;
masks2 = z.playingFields[1]->masks;
z.mutateEnvironment();
//Calculate rates
for (int k = 0; k < masks[0][0].length(); k++)
{
for (int j = 0; j < 3; j++) if (z.playingFields[0]->masks[0][j][k] != masks[0][j][k])cnts[j]++;
for (int j = 0; j < 3; j++) if (z.playingFields[0]->masks[1][j][k] != masks[1][j][k])cnts[j + 3]++;
for (int j = 0; j < 3; j++) if (z.playingFields[1]->masks[0][j][k] != masks2[0][j][k])cnts[j + 6]++;
for (int j = 0; j < 3; j++) if (z.playingFields[1]->masks[1][j][k] != masks2[1][j][k])cnts[j + 9]++;
}
//Now count the ones
int count0 = 0;
for (auto p : std::as_const(z.playingFields))
for (auto &e : p->masks)
for (auto &m : e)
for (auto b : m) if (b) count0++;
//Note here that above we have two environments fields, below, these are placed into two different mask structures, hence the need to add both to the count
int count1 = 0;
for (auto &e : masks)
for (auto &m : e)
for (auto b : m) if (b) count1++;
for (auto &e : masks2)
for (auto &m : e)
for (auto b : m) if (b) count1++;
int difference = abs(count0 - count1);
if (difference > maxDiff)maxDiff = difference;
}
if (theMainWindow) theMainWindow->hideProgressBar();
for (int i = 0; i < 12; i++)
{
dCnts[i] = (static_cast<double>(cnts[i]) / static_cast<double>(replicates));
dCntsSum += dCnts[i];
if (i == 3) out << "Environment 2: ";
if (i == 6) out << "Playing field 2:\nEnvironment 1: ";
if (i == 9) out << "Environment 2: ";
out << dCnts[i] << "\t";
if ((i + 1) % 3 == 0) out << "\n";
}
double dCntsMean = dCntsSum / 12;
if (dCntsMean < (1.25) || dCntsMean > (1.31)) testFlag = false;
flagString = testFlag ? "true" : "false";
out << "The mean of these values is " << dCntsMean << ".\n";
out << "In this case, the mean values per mask will be more variable as it depends on the distribution of 1s across masks.\n";
out << "TREvoSim thus expects the mean of the above to be between 1.25 and 1.32 and has returned " << flagString << "\n";
out << "In all cases, due to the possibility of multiple hits on a single site, the mean values can be marginally below the expected value of 1.28.\n";
//Here the one count should be the same
if (maxDiff != 0)
{
testFlag = false;
out << "\n\n**Fail at ones count with matching peaks on**\n\n";
}
else
{
out << "TREvoSim has also counted the number of ones before and after mutation process - these should be identical when matching peaks is enabled, and this is the case.";
}
if (testFlag) out << "\nMutation tests passed.\n";
return testFlag;
}
//Test the coin toss
bool testinternal::testSix(QString &outString)
{
if (theMainWindow)
{
theMainWindow->addProgressBar(0, 100000);
theMainWindow->setStatus("Testing geometric distribution.\n");
}
bool testFlag = true;
QTextStream out(&outString);
out << "Testing geometric distribution (== coin toss in previous versions).\n\n";
simulationVariables simSettings;
simSettings.selectionCoinToss = 2.;
simSettings.playingfieldNumber = 4;
simSettings.test = 6;
simulation x(0, &simSettings, &error, theMainWindow);
if (error) return false;
int pfSize = x.playingFields[0]->playingField.length();
for (int i = 0; i < pfSize; i++)
{
x.playingFields[0]->playingField[i]->fitness = i;
x.playingFields[1]->playingField[i]->fitness = 1;
x.playingFields[2]->playingField[i]->fitness = i % 2;
x.playingFields[3]->playingField[i]->fitness = i % 3;
}
QVector<int> hits0(pfSize, 0);
QVector<int> hits1(pfSize, 0);
QVector<int> hits2(pfSize, 0);
QVector<int> hits3(pfSize, 0);
QVector<int> hits4(pfSize, 0);
QVector<int> hits5(pfSize, 0);
for (int i = 0; i < 100000; i++)
{
if (theMainWindow) theMainWindow->setProgressBar(i);
hits0[x.coinToss(x.playingFields[0])]++;
hits1[x.coinToss(x.playingFields[1])]++;
hits2[x.coinToss(x.playingFields[2])]++;
hits3[x.coinToss(x.playingFields[3])]++;
}
//Try also with a different probability
simSettings.selectionCoinToss = 3.0;
for (int i = 0; i < 100000; i++)
{
if (theMainWindow) theMainWindow->setProgressBar(i);
hits4[x.coinToss(x.playingFields[0])]++;
}
//And with no selection enabled, which should make these all equal
simSettings.noSelection = true;
for (int i = 0; i < 100000; i++)
{
if (theMainWindow) theMainWindow->setProgressBar(i);
hits5[x.coinToss(x.playingFields[0])]++;
}
out << "<table><tr><th>Position</th><th>"
"</th><th>Fittest at top - selection 2</th><th>"
"</th><th>Fittest at top - selection 3</th><th>"
"</th><th>Equal Fitness</th><th>"
"</th><th>Alternating fitness</th><th>"
"</th><th>Three fitnesses</th><th>"
"</th><th>No selection enabled</th></tr>";
for (int i = 0; i < pfSize; i++)
{
out << "<tr><td>" << i << "</td><td> </td><td>" << hits0[i];
out << "</td><td> </td><td>" << hits4[i];
out << "</td><td> </td><td>" << hits1[i];
out << "</td><td> </td><td>" << hits2[i];
out << "</td><td> </td><td>" << hits3[i];
out << "</td><td> </td><td>" << hits5[i] << "</tr>";
}
out << "</table>\nRunning a bunch of tests comparing expected distribution of selected organisms to actual distribution of selected organisms. "
"Doing this by dividing each item for each column on the list by the next item, and checking that divisor. I.e. if probability is 2, we should halve selected number every row, and the divisor should be 2."
"When these fall outside the expectations, they are printed in a list below. This does not necessarily mean the test has failed. Positions towards bottom of playing field are based on small numbers, "
"and so maybe be printed below, but tests fail only when the numbers involved are high enought to be significant.\n";
for (int i = 0; i < pfSize - 1; i++)
{
//Dividing by zero is bad
if (hits0[i + 1] == 0 || hits4[i + 1] == 0 || hits1[i + 1] == 0) continue;
double divisor = static_cast<double>(hits0[i]) / static_cast<double>(hits0[i + 1]);
if (divisor < 1.8 || divisor > 2.2)
{
//Only fail if this happens in the first five - with small counts at the end of the playing field there is more variability
if (i < 5) testFlag = false;
out << "Position: " << i << "; Test 1, divisor: " << divisor << "\n";
}
divisor = static_cast<double>(hits4[i]) / static_cast<double>(hits4[i + 1]);
if (divisor < 1.3 || divisor > 2.7)
{
//Only fail if this happens in the first five - with small counts at the end of the playing field there is more variability
if (i < 5) testFlag = false;
out << "Position: " << i << "; Test 4, divisor: " << divisor << "\n";
}
divisor = static_cast<double>(hits1[i]) / static_cast<double>(hits1[i + 1]);
if (divisor < 0.9 || divisor > 1.1)
{
testFlag = false;
out << "Test 2, divisor: " << divisor << "\n";
}
divisor = static_cast<double>(hits5[i]) / static_cast<double>(hits5[i + 1]);
if (divisor < 0.9 || divisor > 1.1)
{
testFlag = false;
out << "Test 6, divisor: " << divisor << "\n";
}
}
int cnt = 0, cnt2 = 0;
for (int i = 0; i < pfSize / 2; i++) cnt += hits0[i];
for (int i = 0; i < pfSize; i++)
if (i % 2 == 0)cnt2 += hits2[i];
double divisor = static_cast<double>(cnt) / static_cast<double>(cnt2);
if (divisor < 0.9 || divisor > 1.1)
{
testFlag = false;
out << "Test 3, divisor: " << divisor << "\n";
}
out << "\nTested four playing fields, with decreasing fitness, equal fitness, and probabilities of selection of two and three, plus no selection mode. Any failures to test printed above, and a failed test where there are high enoiugh numbers for this to be problematic, will result in green text.\n";
if (testFlag) out << "\nCoin toss tests passed.\n";
if (theMainWindow)
theMainWindow->hideProgressBar();
return testFlag;
}
//Test the new species function - called when SD exceeded
bool testinternal::testSeven(QString &outString)
{
bool testFlag = true;
QTextStream out(&outString);
out << "Testing the creation of a new species - new species created at iteration 66 with a genome of all 1's.\n\n";
simulationVariables simSettings;
simSettings.genomeSize = 50;
simSettings.fitnessSize = 50;
simSettings.speciesSelectSize = 50;
simSettings.test = 7;
simulation x(0, &simSettings, &error, theMainWindow);
if (error)
{
out << "Error initialising test simulation";
return false;
}
//This should update the counters on new species
x.iterations = 66;
x.speciesCount = 14;
//This will have initialised values - i.e. zero for everything except the genome, which will be 50% 1's
Organism newSpecies(50, false);
newSpecies.initialise(50);
newSpecies.speciesID = 10;
for (auto &i : newSpecies.genome) i = true;
Organism parentSpecies(50, false);
for (auto &i : parentSpecies.genome)i = true;
for (auto &i : parentSpecies.parentGenomes[0])i = true;
parentSpecies.speciesID = 10;
parentSpecies.parentSpeciesID = 9;
parentSpecies.born = 15;
parentSpecies.extinct = 20;
parentSpecies.cladogenesis = 25;
parentSpecies.fitness = 5;
x.playingFields[0]->playingField[8]->speciesID = 10;
x.newSpecies(newSpecies, parentSpecies, x.playingFields[0]);
//Ok, so first, newspecies should be born iteration 66
if (newSpecies.born != 66)testFlag = false;
out << "New species born at iteration " << newSpecies.born << " (should be 66).\n";
if (x.speciesCount != 15)testFlag = false;
out << "Species number " << x.speciesCount << " (should be 15).\n";
if (newSpecies.speciesID != 15)testFlag = false;
out << "New species is " << newSpecies.speciesID << " (should be 15).\n";
if (newSpecies.cladogenesis != 66)testFlag = false;
out << "New species cladogenesis at iteration " << newSpecies.cladogenesis << " (should be 66).\n";
if (parentSpecies.cladogenesis != 66)testFlag = false;
out << "Parent cladogenesis at iteration " << parentSpecies.cladogenesis << " (should be 66).\n";
if (newSpecies.parentSpeciesID != 10)testFlag = false;
out << "New species parent species ID " << newSpecies.parentSpeciesID << " (should be 10).\n";
for (auto i : std::as_const(newSpecies.parentGenomes[0])) if (i != 1)testFlag = false;
out << "New species parent genome: ";
for (auto i : std::as_const(newSpecies.parentGenomes[0])) i ? out << "1" : out << "0";
out << " (should be all 1s).\n";
for (auto i : std::as_const(newSpecies.genome)) if (i != 1)testFlag = false;
out << "New species genome: ";
for (auto i : std::as_const(newSpecies.genome)) i ? out << "1" : out << "0";
out << " (should be all 1s).\n";
for (auto i : std::as_const(x.playingFields[0]->playingField[8]->parentGenomes[0])) if (i != 0)testFlag = false;
out << "Species 10 parent genome in playing field is now: ";
for (auto i : std::as_const(x.playingFields[0]->playingField[8]->parentGenomes[0])) i ? out << "1" : out << "0";
out << " (should be all 0s).\n";
for (auto i : std::as_const(x.playingFields[0]->playingField[8]->parentGenomes[1])) if (i != 1)testFlag = false;
out << "Species 10 last speciation genome in playing field is now:";
for (auto i : std::as_const(x.playingFields[0]->playingField[8]->parentGenomes[1])) i ? out << "1" : out << "0";
out << " (should be all 1s).\n";
if (testFlag) out << "\nNew species tests passed.\n";
return testFlag;
}
//Test which organism to overwrite in playing field
bool testinternal::testEight(QString &outString)
{
bool testFlag = true;
QTextStream out(&outString);
out << "Testing overwrite code.\n\n";
simulationVariables simSettings;
simSettings.randomOverwrite = false;
simSettings.test = 8;
simulation x(0, &simSettings, &error, theMainWindow);
if (error) return false;
for (auto &o : x.playingFields[0]->playingField) o->fitness = 0;
int pfSize = x.playingFields[0]->playingField.length();
simulation y(0, &simSettings, &error, theMainWindow);
if (error) return false;
for (int i = 0; i < pfSize ; i++) y.playingFields[0]->playingField[i]->fitness = i;
simulation z(0, &simSettings, &error, theMainWindow);
if (error) return false;
for (int i = 0; i < pfSize ; i++)
if (i > pfSize / 2) z.playingFields[0]->playingField[i]->fitness = 0;
else z.playingFields[0]->playingField[i]->fitness = 1;
simulationVariables simSettings2;
simSettings2.randomOverwrite = true;
simSettings2.test = 8;
simulation a(0, &simSettings2, &error, theMainWindow);
if (error) return false;
for (auto &o : a.playingFields[0]->playingField) o->fitness = 0;
simulation b(0, &simSettings2, &error, theMainWindow);
if (error) return false;
for (int i = 0; i < pfSize; i++) b.playingFields[0]->playingField[i]->fitness = i;
simulationVariables simSettings3;
simSettings3.expandingPlayingfield = true;
simulation c(0, &simSettings3, &error, theMainWindow);
if (error) return false;
QVector<int> hits0(pfSize, 0);
QVector<int> hits1(pfSize, 0);
QVector<int> hits2(pfSize, 0);
QVector<int> hits3(pfSize, 0);
QVector<int> hits4(pfSize, 0);
QVector<int> hits5(pfSize, 0);
if (theMainWindow)
{
theMainWindow->addProgressBar(0, 100000);
theMainWindow->setStatus("Testing overwrite code.");
}
for (int i = 0; i < 100000; i++)
{
if (theMainWindow)
theMainWindow->setProgressBar(i);
hits0[x.calculateOverwrite(x.playingFields[0], 0)]++;
hits1[y.calculateOverwrite(y.playingFields[0], 0)]++;
hits2[z.calculateOverwrite(z.playingFields[0], 0)]++;
hits3[a.calculateOverwrite(a.playingFields[0], 0)]++;
hits4[b.calculateOverwrite(b.playingFields[0], 0)]++;
hits5[c.calculateOverwrite(c.playingFields[0], 4)]++;
}
if (theMainWindow) theMainWindow->hideProgressBar();
out << "<table><tr>"
"<th>Position</th>"
"<th></th>"
"<th>Equal fitness</th>"
"<th></th>"
"<th>Fittest at top</th>"
"<th></th>"
"<th>Half top fitness</th>"
"<th></th>"
"<th>Equal Fitness - random overwrite</th>"
"<th></th>"
"<th>Fittest at top - random overwrite</th>"
"<th></th>"
"<th>Expanding - species #4</th>"
"</tr>";
for (int i = 0; i < pfSize; i++)