-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenome.cpp
3136 lines (2420 loc) · 83.7 KB
/
genome.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 "genome.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <sstream>
#include <cstring>
using namespace NEAT;
using namespace std;
Genome::Genome(int id, std::vector<Trait*> t, std::vector<NNode*> n, std::vector<Gene*> g) {
genome_id=id;
traits=t;
nodes=n;
genes=g;
//added by JAL
parent1=-1;
parent2=-1;
struct_change=-1;
}
Genome::Genome(int id, std::vector<Trait*> t, std::vector<NNode*> n, std::vector<Link*> links) {
std::vector<Link*>::iterator curlink;
Gene *tempgene;
traits=t;
nodes=n;
genome_id=id;
//We go through the links and turn them into original genes
for(curlink=links.begin();curlink!=links.end();++curlink) {
//Create genes one at a time
tempgene=new Gene((*curlink)->linktrait, (*curlink)->weight,(*curlink)->in_node,(*curlink)->out_node,(*curlink)->is_recurrent,1.0,0.0);
genes.push_back(tempgene);
}
//added by JAL
parent1=-1;
parent2=-1;
struct_change=-1;
}
Genome::Genome(const Genome& genome)
{
genome_id = genome.genome_id;
std::vector<Trait*>::const_iterator curtrait;
std::vector<NNode*>::const_iterator curnode;
std::vector<Gene*>::const_iterator curgene;
for(curtrait=genome.traits.begin(); curtrait!=genome.traits.end(); ++curtrait) {
traits.push_back(new Trait(**curtrait));
}
Trait *assoc_trait;
//Duplicate NNodes
for(curnode=genome.nodes.begin();curnode!=genome.nodes.end();++curnode) {
//First, find the trait that this node points to
if (((*curnode)->nodetrait)==0) assoc_trait=0;
else {
curtrait=traits.begin();
while(((*curtrait)->trait_id)!=(((*curnode)->nodetrait)->trait_id))
++curtrait;
assoc_trait=(*curtrait);
}
NNode* newnode=new NNode(*curnode,assoc_trait);
(*curnode)->dup=newnode; //Remember this node's old copy
// (*curnode)->activation_count=55;
nodes.push_back(newnode);
}
NNode *inode; //For forming a gene
NNode *onode; //For forming a gene
Trait *traitptr;
//Duplicate Genes
for(curgene=genome.genes.begin(); curgene!=genome.genes.end(); ++curgene) {
//First find the nodes connected by the gene's link
inode=(((*curgene)->lnk)->in_node)->dup;
onode=(((*curgene)->lnk)->out_node)->dup;
//Get a pointer to the trait expressed by this gene
traitptr=((*curgene)->lnk)->linktrait;
if (traitptr==0) assoc_trait=0;
else {
curtrait=traits.begin();
while(((*curtrait)->trait_id)!=(traitptr->trait_id))
++curtrait;
assoc_trait=(*curtrait);
}
Gene* newgene=new Gene(*curgene,assoc_trait,inode,onode);
genes.push_back(newgene);
}
//added by JAL
parent1=genome.parent1;
parent2=genome.parent2;
struct_change=genome.struct_change;
}
Genome::Genome(int id, std::ifstream &iFile) {
char curword[128]; //max word size of 128 characters
char curline[1024]; //max line size of 1024 characters
char delimiters[] = " \n";
int done=0;
//int pause;
genome_id=id;
iFile.getline(curline, sizeof(curline));
int wordcount = NEAT::getUnitCount(curline, delimiters);
int curwordnum = 0;
//Loop until file is finished, parsing each line
while (!done && !iFile.eof()) {
//std::cout << curline << std::endl;
if (curwordnum > wordcount || wordcount == 0) {
iFile.getline(curline, sizeof(curline));
wordcount = NEAT::getUnitCount(curline, delimiters);
curwordnum = 0;
}
std::stringstream ss(curline);
//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
ss >> curword;
curwordnum++;
//printf(curword);
//printf(" test\n");
//Check for end of Genome
if (strcmp(curword,"genomeend")==0) {
//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
ss >> curword;
curwordnum++;
int idcheck = atoi(curword);
//iFile>>idcheck;
if (idcheck!=genome_id) printf("ERROR: id mismatch in genome");
done=1;
}
//Ignore genomestart if it hasn't been gobbled yet
else if (strcmp(curword,"genomestart")==0) {
++curwordnum;
//cout<<"genomestart"<<endl;
}
//Ignore comments surrounded by - they get printed to screen
else if (strcmp(curword,"/*")==0) {
//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
ss >> curword;
curwordnum++;
while (strcmp(curword,"*/")!=0) {
//cout<<curword<<" ";
//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
ss >> curword;
curwordnum++;
}
//cout<<endl;
}
//Read in a trait
else if (strcmp(curword,"trait")==0) {
Trait *newtrait;
char argline[1024];
//strcpy(argline, NEAT::getUnits(curline, curwordnum, wordcount, delimiters));
curwordnum = wordcount + 1;
ss.getline(argline, 1024);
//Allocate the new trait
newtrait=new Trait(argline);
//Add trait to vector of traits
traits.push_back(newtrait);
}
//Read in a node
else if (strcmp(curword,"node")==0) {
NNode *newnode;
char argline[1024];
//strcpy(argline, NEAT::getUnits(curline, curwordnum, wordcount, delimiters));
curwordnum = wordcount + 1;
ss.getline(argline, 1024);
//Allocate the new node
newnode=new NNode(argline,traits);
//Add the node to the list of nodes
nodes.push_back(newnode);
}
//Read in a Gene
else if (strcmp(curword,"gene")==0) {
Gene *newgene;
char argline[1024];
//strcpy(argline, NEAT::getUnits(curline, curwordnum, wordcount, delimiters));
curwordnum = wordcount + 1;
ss.getline(argline, 1024);
//std::cout << "New gene: " << ss.str() << std::endl;
//Allocate the new Gene
newgene=new Gene(argline,traits,nodes);
//Add the gene to the genome
genes.push_back(newgene);
//std::cout<<"Added gene " << newgene << std::endl;
}
}
}
Genome::Genome(int new_id,int i, int o, int n,int nmax, bool r, double linkprob) {
int totalnodes;
bool *cm; //The connection matrix which will be randomized
bool *cmp; //Connection matrix pointer
int matrixdim;
int count;
int ncount; //Node and connection counters
int ccount;
int row; //For navigating the matrix
int col;
double new_weight;
int maxnode; //No nodes above this number for this genome
int first_output; //Number of first output node
totalnodes=i+o+nmax;
matrixdim=totalnodes*totalnodes;
cm=new bool[matrixdim]; //Dimension the connection matrix
maxnode=i+n;
first_output=totalnodes-o+1;
//For creating the new genes
NNode *newnode;
Gene *newgene;
Trait *newtrait;
NNode *in_node;
NNode *out_node;
//Retrieves the nodes pointed to by connection genes
std::vector<NNode*>::iterator node_iter;
//Assign the id
genome_id=new_id;
//cout<<"Assigned id "<<genome_id<<endl;
//Step through the connection matrix, randomly assigning bits
cmp=cm;
for(count=0;count<matrixdim;count++) {
if (randfloat()<linkprob)
*cmp=true;
else *cmp=false;
cmp++;
}
//Create a dummy trait (this is for future expansion of the system)
newtrait=new Trait(1,0,0,0,0,0,0,0,0,0);
traits.push_back(newtrait);
//Build the input nodes
for(ncount=1;ncount<=i;ncount++) {
if (ncount<i)
newnode=new NNode(SENSOR,ncount,INPUT);
else newnode=new NNode(SENSOR,ncount,BIAS);
newnode->nodetrait=newtrait;
//Add the node to the list of nodes
nodes.push_back(newnode);
}
//Build the hidden nodes
for(ncount=i+1;ncount<=i+n;ncount++) {
newnode=new NNode(NEURON,ncount,HIDDEN);
newnode->nodetrait=newtrait;
//Add the node to the list of nodes
nodes.push_back(newnode);
}
//Build the output nodes
for(ncount=first_output;ncount<=totalnodes;ncount++) {
newnode=new NNode(NEURON,ncount,OUTPUT);
newnode->nodetrait=newtrait;
//Add the node to the list of nodes
nodes.push_back(newnode);
}
//cout<<"Built nodes"<<endl;
//Connect the nodes
ccount=1; //Start the connection counter
//Step through the connection matrix, creating connection genes
cmp=cm;
count=0;
for(col=1;col<=totalnodes;col++)
for(row=1;row<=totalnodes;row++) {
//Only try to create a link if it is in the matrix
//and not leading into a sensor
if ((*cmp==true)&&(col>i)&&
((col<=maxnode)||(col>=first_output))&&
((row<=maxnode)||(row>=first_output))) {
//If it isn't recurrent, create the connection no matter what
if (col>row) {
//Retrieve the in_node
node_iter=nodes.begin();
while((*node_iter)->node_id!=row)
node_iter++;
in_node=(*node_iter);
//Retrieve the out_node
node_iter=nodes.begin();
while((*node_iter)->node_id!=col)
node_iter++;
out_node=(*node_iter);
//Create the gene
new_weight=randposneg()*randfloat();
newgene=new Gene(newtrait,new_weight, in_node, out_node,false,count,new_weight);
//Add the gene to the genome
genes.push_back(newgene);
}
else if (r) {
//Create a recurrent connection
//Retrieve the in_node
node_iter=nodes.begin();
while((*node_iter)->node_id!=row)
node_iter++;
in_node=(*node_iter);
//Retrieve the out_node
node_iter=nodes.begin();
while((*node_iter)->node_id!=col)
node_iter++;
out_node=(*node_iter);
//Create the gene
new_weight=randposneg()*randfloat();
newgene=new Gene(newtrait,new_weight, in_node, out_node,true,count,new_weight);
//Add the gene to the genome
genes.push_back(newgene);
}
}
count++; //increment gene counter
cmp++;
}
delete [] cm;
}
Genome::Genome(int num_in,int num_out,int num_hidden,int type) {
//Temporary lists of nodes
std::vector<NNode*> inputs;
std::vector<NNode*> outputs;
std::vector<NNode*> hidden;
NNode *bias; //Remember the bias
std::vector<NNode*>::iterator curnode1; //Node iterator1
std::vector<NNode*>::iterator curnode2; //Node iterator2
std::vector<NNode*>::iterator curnode3; //Node iterator3
//For creating the new genes
NNode *newnode;
Gene *newgene;
Trait *newtrait;
int count;
int ncount;
//Assign the id 0
genome_id=0;
//Create a dummy trait (this is for future expansion of the system)
newtrait=new Trait(1,0,0,0,0,0,0,0,0,0);
traits.push_back(newtrait);
//Adjust hidden number
if (type==0)
num_hidden=0;
else if (type==1)
num_hidden=num_in*num_out;
//Create the inputs and outputs
//Build the input nodes
for(ncount=1;ncount<=num_in;ncount++) {
if (ncount<num_in)
newnode=new NNode(SENSOR,ncount,INPUT);
else {
newnode=new NNode(SENSOR,ncount,BIAS);
bias=newnode;
}
//newnode->nodetrait=newtrait;
//Add the node to the list of nodes
nodes.push_back(newnode);
inputs.push_back(newnode);
}
//Build the hidden nodes
for(ncount=num_in+1;ncount<=num_in+num_hidden;ncount++) {
newnode=new NNode(NEURON,ncount,HIDDEN);
//newnode->nodetrait=newtrait;
//Add the node to the list of nodes
nodes.push_back(newnode);
hidden.push_back(newnode);
}
//Build the output nodes
for(ncount=num_in+num_hidden+1;ncount<=num_in+num_hidden+num_out;ncount++) {
newnode=new NNode(NEURON,ncount,OUTPUT);
//newnode->nodetrait=newtrait;
//Add the node to the list of nodes
nodes.push_back(newnode);
outputs.push_back(newnode);
}
//Create the links depending on the type
if (type==0) {
//Just connect inputs straight to outputs
count=1;
//Loop over the outputs
for(curnode1=outputs.begin();curnode1!=outputs.end();++curnode1) {
//Loop over the inputs
for(curnode2=inputs.begin();curnode2!=inputs.end();++curnode2) {
//Connect each input to each output
newgene=new Gene(newtrait,0, (*curnode2), (*curnode1),false,count,0);
//Add the gene to the genome
genes.push_back(newgene);
count++;
}
}
} //end type 0
//A split link from each input to each output
else if (type==1) {
count=1; //Start the gene number counter
curnode3=hidden.begin(); //One hidden for ever input-output pair
//Loop over the outputs
for(curnode1=outputs.begin();curnode1!=outputs.end();++curnode1) {
//Loop over the inputs
for(curnode2=inputs.begin();curnode2!=inputs.end();++curnode2) {
//Connect Input to hidden
newgene=new Gene(newtrait,0, (*curnode2), (*curnode3),false,count,0);
//Add the gene to the genome
genes.push_back(newgene);
count++; //Next gene
//Connect hidden to output
newgene=new Gene(newtrait,0, (*curnode3), (*curnode1),false,count,0);
//Add the gene to the genome
genes.push_back(newgene);
++curnode3; //Next hidden node
count++; //Next gene
}
}
}//end type 1
//Fully connected
else if (type==2) {
count=1; //Start gene counter at 1
//Connect all inputs to all hidden nodes
for(curnode1=hidden.begin();curnode1!=hidden.end();++curnode1) {
//Loop over the inputs
for(curnode2=inputs.begin();curnode2!=inputs.end();++curnode2) {
//Connect each input to each hidden
newgene=new Gene(newtrait,0, (*curnode2), (*curnode1),false,count,0);
//Add the gene to the genome
genes.push_back(newgene);
count++;
}
}
//Connect all hidden units to all outputs
for(curnode1=outputs.begin();curnode1!=outputs.end();++curnode1) {
//Loop over the inputs
for(curnode2=hidden.begin();curnode2!=hidden.end();++curnode2) {
//Connect each input to each hidden
newgene=new Gene(newtrait,0, (*curnode2), (*curnode1),false,count,0);
//Add the gene to the genome
genes.push_back(newgene);
count++;
}
}
//Connect the bias to all outputs
for(curnode1=outputs.begin();curnode1!=outputs.end();++curnode1) {
newgene=new Gene(newtrait,0, bias, (*curnode1),false,count,0);
//Add the gene to the genome
genes.push_back(newgene);
count++;
}
//Recurrently connect the hidden nodes
for(curnode1=hidden.begin();curnode1!=hidden.end();++curnode1) {
//Loop Over all Hidden
for(curnode2=hidden.begin();curnode2!=hidden.end();++curnode2) {
//Connect each hidden to each hidden
newgene=new Gene(newtrait,0, (*curnode2), (*curnode1),true,count,0);
//Add the gene to the genome
genes.push_back(newgene);
count++;
}
}
}//end type 2
}
Genome* Genome::new_Genome_load(char *filename) {
Genome *newgenome;
int id;
//char curline[1024];
char curword[20]; //max word size of 20 characters
//char delimiters[] = " \n";
//int curwordnum = 0;
std::ifstream iFile(filename);
//Make sure it worked
//if (!iFile) {
// cerr<<"Can't open "<<filename<<" for input"<<endl;
// return 0;
//}
iFile>>curword;
//iFile.getline(curline, sizeof(curline));
//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
//Bypass initial comment
if (strcmp(curword,"/*")==0) {
//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
iFile>>curword;
while (strcmp(curword,"*/")!=0) {
printf("%s ",curword);
//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
iFile>>curword;
}
//cout<<endl;
iFile>>curword;
//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
}
//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
//id = atoi(curword);
iFile>>id;
newgenome=new Genome(id,iFile);
iFile.close();
return newgenome;
}
Genome::~Genome() {
std::vector<Trait*>::iterator curtrait;
std::vector<NNode*>::iterator curnode;
std::vector<Gene*>::iterator curgene;
for(curtrait=traits.begin();curtrait!=traits.end();++curtrait) {
delete (*curtrait);
}
for(curnode=nodes.begin();curnode!=nodes.end();++curnode) {
delete (*curnode);
}
for(curgene=genes.begin();curgene!=genes.end();++curgene) {
delete (*curgene);
}
}
Network *Genome::genesis(int id) {
std::vector<NNode*>::iterator curnode;
std::vector<Gene*>::iterator curgene;
NNode *newnode;
Trait *curtrait;
Link *curlink;
Link *newlink;
double maxweight=0.0; //Compute the maximum weight for adaptation purposes
double weight_mag; //Measures absolute value of weights
//Inputs and outputs will be collected here for the network
//All nodes are collected in an all_list-
//this will be used for later safe destruction of the net
std::vector<NNode*> inlist;
std::vector<NNode*> outlist;
std::vector<NNode*> all_list;
//Gene translation variables
NNode *inode;
NNode *onode;
//The new network
Network *newnet;
//Create the nodes
for(curnode=nodes.begin();curnode!=nodes.end();++curnode) {
newnode=new NNode((*curnode)->type,(*curnode)->node_id);
//Derive the node parameters from the trait pointed to
curtrait=(*curnode)->nodetrait;
newnode->derive_trait(curtrait);
//Check for input or output designation of node
if (((*curnode)->gen_node_label)==INPUT)
inlist.push_back(newnode);
if (((*curnode)->gen_node_label)==BIAS)
inlist.push_back(newnode);
if (((*curnode)->gen_node_label)==OUTPUT)
outlist.push_back(newnode);
//Keep track of all nodes, not just input and output
all_list.push_back(newnode);
//Have the node specifier point to the node it generated
(*curnode)->analogue=newnode;
}
//Create the links by iterating through the genes
for(curgene=genes.begin();curgene!=genes.end();++curgene) {
//Only create the link if the gene is enabled
if (((*curgene)->enable)==true) {
curlink=(*curgene)->lnk;
inode=(curlink->in_node)->analogue;
onode=(curlink->out_node)->analogue;
//NOTE: This line could be run through a recurrency check if desired
// (no need to in the current implementation of NEAT)
//check rounding
double unrounded = curlink->weight;
double rounded = (double)(((int)((unrounded*1000)+.5))/1000.0);
newlink=new Link(rounded,inode,onode,curlink->is_recurrent);
(onode->incoming).push_back(newlink);
(inode->outgoing).push_back(newlink);
//Derive link's parameters from its Trait pointer
curtrait=(curlink->linktrait);
newlink->derive_trait(curtrait);
//Keep track of maximum weight
if (newlink->weight>0)
weight_mag=newlink->weight;
else weight_mag=-newlink->weight;
if (weight_mag>maxweight)
maxweight=weight_mag;
}
}
//Create the new network
newnet=new Network(inlist,outlist,all_list,id);
//Attach genotype and phenotype together
newnet->genotype=this;
phenotype=newnet;
newnet->maxweight=maxweight;
return newnet;
}
bool Genome::verify() {
std::vector<NNode*>::iterator curnode;
std::vector<Gene*>::iterator curgene;
std::vector<Gene*>::iterator curgene2;
NNode *inode;
NNode *onode;
bool disab;
int last_id;
//int pause;
//cout<<"Verifying Genome id: "<<this->genome_id<<endl;
if (this==0) {
//cout<<"ERROR GENOME EMPTY"<<endl;
//cin>>pause;
}
//Check each gene's nodes
for(curgene=genes.begin();curgene!=genes.end();++curgene) {
inode=((*curgene)->lnk)->in_node;
onode=((*curgene)->lnk)->out_node;
//Look for inode
curnode=nodes.begin();
while((curnode!=nodes.end())&&
((*curnode)!=inode))
++curnode;
if (curnode==nodes.end()) {
//cout<<"MISSING iNODE FROM GENE NOT IN NODES OF GENOME!!"<<endl;
//cin>>pause;
return false;
}
//Look for onode
curnode=nodes.begin();
while((curnode!=nodes.end())&&
((*curnode)!=onode))
++curnode;
if (curnode==nodes.end()) {
//cout<<"MISSING oNODE FROM GENE NOT IN NODES OF GENOME!!"<<endl;
//cin>>pause;
return false;
}
}
//Check for NNodes being out of order
last_id=0;
for(curnode=nodes.begin();curnode!=nodes.end();++curnode) {
if ((*curnode)->node_id<last_id) {
//cout<<"ALERT: NODES OUT OF ORDER in "<<this<<endl;
//cin>>pause;
return false;
}
last_id=(*curnode)->node_id;
}
//Make sure there are no duplicate genes
for(curgene=genes.begin();curgene!=genes.end();++curgene) {
for(curgene2=genes.begin();curgene2!=genes.end();++curgene2) {
if (((*curgene)!=(*curgene2))&&
((((*curgene)->lnk)->is_recurrent)==(((*curgene2)->lnk)->is_recurrent))&&
((((((*curgene2)->lnk)->in_node)->node_id)==((((*curgene)->lnk)->in_node)->node_id))&&
(((((*curgene2)->lnk)->out_node)->node_id)==((((*curgene)->lnk)->out_node)->node_id)))) {
//cout<<"ALERT: DUPLICATE GENES: "<<(*curgene)<<(*curgene2)<<endl;
//cout<<"INSIDE GENOME: "<<this<<endl;
//cin>>pause;
}
}
}
//See if a gene is not disabled properly
//Note this check does not necessary mean anything is wrong
//
//if (nodes.size()>=15) {
//disab=false;
////Go through genes and see if one is disabled
//for(curgene=genes.begin();curgene!=genes.end();++curgene) {
//if (((*curgene)->enable)==false) disab=true;
//}
//if (disab==false) {
//cout<<"ALERT: NO DISABLED GENE IN GENOME: "<<this<<endl;
////cin>>pause;
//}
//}
//
//Check for 2 disables in a row
//Note: Again, this is not necessarily a bad sign
if (nodes.size()>=500) {
disab=false;
for(curgene=genes.begin();curgene!=genes.end();++curgene) {
if ((((*curgene)->enable)==false)&&(disab==true)) {
//cout<<"ALERT: 2 DISABLES IN A ROW: "<<this<<endl;
}
if (((*curgene)->enable)==false) disab=true;
else disab=false;
}
}
//cout<<"GENOME OK!"<<endl;
return true;
}
//Print the genome to a file
void Genome::print_to_file(std::ofstream &outFile) {
std::vector<Trait*>::iterator curtrait;
std::vector<NNode*>::iterator curnode;
std::vector<Gene*>::iterator curgene;
outFile<< setprecision(10);
outFile<<"genomestart "<<genome_id<<std::endl;
//Output the traits
for(curtrait=traits.begin();curtrait!=traits.end();++curtrait) {
(*curtrait)->trait_id=curtrait-traits.begin()+1;
(*curtrait)->print_to_file(outFile);
}
//Output the nodes
for(curnode=nodes.begin();curnode!=nodes.end();++curnode) {
(*curnode)->print_to_file(outFile);
}
//Output the genes
for(curgene=genes.begin();curgene!=genes.end();++curgene) {
(*curgene)->print_to_file(outFile);
}
outFile<<"genomeend "<<genome_id<<std::endl;
}
void Genome::print_to_file(std::ostream &outFile) {
std::vector<Trait*>::iterator curtrait;
std::vector<NNode*>::iterator curnode;
std::vector<Gene*>::iterator curgene;
//char tempbuf[128];
//sprintf(tempbuf, "genomestart %d\n", genome_id);
//outFile.write(strlen(tempbuf), tempbuf);
outFile<< setprecision(10);
outFile<<"genomestart "<<genome_id<<std::endl;
//Output the traits
for(curtrait=traits.begin();curtrait!=traits.end();++curtrait) {
(*curtrait)->trait_id=curtrait-traits.begin()+1;
(*curtrait)->print_to_file(outFile);
}
//Output the nodes
for(curnode=nodes.begin();curnode!=nodes.end();++curnode) {
(*curnode)->print_to_file(outFile);
}
//Output the genes
for(curgene=genes.begin();curgene!=genes.end();++curgene) {
(*curgene)->print_to_file(outFile);
}
//char tempbuf2[128];
//sprintf(tempbuf2, sizeof(tempbuf2), "genomeend %d\n", genome_id);
//outFile.write(strlen(tempbuf2), tempbuf2);
outFile << "genomeend " << genome_id << std::endl << std::endl << std::endl;
//char tempbuf4[1024];
//sprintf(tempbuf4, sizeof(tempbuf4), "\n\n");
//outFile.write(strlen(tempbuf4), tempbuf4);
}
void Genome::print_to_filename(char *filename) {
std::ofstream oFile(filename);
//oFile.open(filename, std::ostream::Write);
print_to_file(oFile);
oFile.close();
}
int Genome::get_last_node_id() {
return ((*(nodes.end() - 1))->node_id)+1;
}
double Genome::get_last_gene_innovnum() {
return ((*(genes.end() - 1))->innovation_num)+1;
}
void Genome::round_weights()
{
std::vector<Gene*>::iterator curgene;
for(curgene=genes.begin();curgene!=genes.end();++curgene) {
double unrounded = ((*curgene)->lnk)->weight;
double rounded = (double)(((int)((unrounded*1000)+.5))/1000.0);
((*curgene)->lnk)->weight=rounded;
}
}
Genome *Genome::duplicate(int new_id) {
//Collections for the new Genome
std::vector<Trait*> traits_dup;
std::vector<NNode*> nodes_dup;
std::vector<Gene*> genes_dup;
//Iterators for the old Genome
std::vector<Trait*>::iterator curtrait;
std::vector<NNode*>::iterator curnode;
std::vector<Gene*>::iterator curgene;
//New item pointers
Trait *newtrait;
NNode *newnode;
Gene *newgene;
Trait *assoc_trait; //Trait associated with current item
NNode *inode; //For forming a gene
NNode *onode; //For forming a gene
Trait *traitptr;
Genome *newgenome;
//verify();
//Duplicate the traits
for(curtrait=traits.begin();curtrait!=traits.end();++curtrait) {
newtrait=new Trait(*curtrait);
traits_dup.push_back(newtrait);
}
//Duplicate NNodes
for(curnode=nodes.begin();curnode!=nodes.end();++curnode) {
//First, find the trait that this node points to
if (((*curnode)->nodetrait)==0) assoc_trait=0;
else {
curtrait=traits_dup.begin();
while(((*curtrait)->trait_id)!=(((*curnode)->nodetrait)->trait_id))
++curtrait;
assoc_trait=(*curtrait);
}
newnode=new NNode(*curnode,assoc_trait);
(*curnode)->dup=newnode; //Remember this node's old copy
// (*curnode)->activation_count=55;
nodes_dup.push_back(newnode);
}
//Duplicate Genes
for(curgene=genes.begin();curgene!=genes.end();++curgene) {
//First find the nodes connected by the gene's link
inode=(((*curgene)->lnk)->in_node)->dup;