-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpopulation.cpp
1473 lines (1162 loc) · 42.5 KB
/
population.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 "population.h"
#include "organism.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstring>
using namespace std;
using namespace NEAT;
extern int NEAT::time_alive_minimum;
Population::Population(Genome *g,int size) {
winnergen=0;
highest_fitness=0.0;
highest_last_changed=0;
spawn(g,size);
}
Population::Population(Genome *g,int size, float power) {
winnergen=0;
highest_fitness=0.0;
highest_last_changed=0;
clone(g, size, power);
}
//Population::Population(int size,int i,int o, int nmax, bool r, double linkprob) {
//int count;
//Genome *new_genome;
//cout<<"Making a random pop"<<endl;
//winnergen=0;
//highest_fitness=0.0;
//highest_last_changed=0;
//for(count=0;count<size;count++) {
//new_genome=new Genome(count,i,o,randint(0,nmax),nmax,r,linkprob);
//organisms.push_back(new Organism(0,new_genome,1));
//}
//cur_node_id=i+o+nmax+1;;
//cur_innov_num=(i+o+nmax)*(i+o+nmax)+1;
//cout<<"Calling speciate"<<endl;
//speciate();
//}
//MSC Addition
//Added the ability for a population to be spawned
//off of a vector of Genomes. Useful when converging.
Population::Population(std::vector<Genome*> genomeList, float power) {
winnergen=0;
highest_fitness=0.0;
highest_last_changed=0;
int count;
Genome *new_genome;
Organism *new_organism;
//Create size copies of the Genome
//Start with perturbed linkweights
for (std::vector<Genome*>::iterator iter = genomeList.begin(); iter != genomeList.end(); ++iter)
{
new_genome=(*iter);
if(power>0)
new_genome->mutate_link_weights(power,1.0,GAUSSIAN);
//new_genome->mutate_link_weights(1.0,1.0,COLDGAUSSIAN);
new_genome->randomize_traits();
new_organism=new Organism(0.0,new_genome,1);
organisms.push_back(new_organism);
}
//Keep a record of the innovation and node number we are on
cur_node_id=new_genome->get_last_node_id();
cur_innov_num=new_genome->get_last_gene_innovnum();
//Separate the new Population into species
speciate();
}
Population::Population(const char *filename) {
char curword[4096]; //max word size of 128 characters
char curline[8000]; //max line size of 1024 characters
char delimiters[] = " \n";
Genome *new_genome;
winnergen=0;
highest_fitness=0.0;
highest_last_changed=0;
cur_node_id=0;
cur_innov_num=0.0;
int curwordnum = 0;
std::ifstream iFile(filename);
if (!iFile) {
printf("Can't open genomes file for input");
return;
}
else {
bool md = false;
char metadata[4024];
//Loop until file is finished, parsing each line
while (!iFile.eof())
{
iFile.getline(curline, sizeof(curline));
std::stringstream ss(curline);
//strcpy(curword, NEAT::getUnit(curline, 0, delimiters));
ss >> curword;
if(iFile.eof()) break;
//std::cout << curline << std::endl;
//Check for next
if (strcmp(curword,"genomestart")==0)
{
//strcpy(curword, NEAT::getUnit(curline, 1, delimiters));
//int idcheck = atoi(curword);
int idcheck;
ss >> idcheck;
// If there isn't metadata, set metadata to ""
if(md == false) {
strcpy(metadata, "");
}
md = false;
new_genome=new Genome(idcheck,iFile);
organisms.push_back(new Organism(0,new_genome,1, metadata));
if (cur_node_id<(new_genome->get_last_node_id()))
cur_node_id=new_genome->get_last_node_id();
if (cur_innov_num<(new_genome->get_last_gene_innovnum()))
cur_innov_num=new_genome->get_last_gene_innovnum();
}
else if (strcmp(curword,"/*")==0)
{
// New metadata possibly, so clear out the metadata
strcpy(metadata, "");
curwordnum=1;
//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
ss >> curword;
while(strcmp(curword,"*/")!=0)
{
// If we've started to form the metadata, put a space in the front
if(md) {
strncat(metadata, " ", 4024 - strlen(metadata));
}
// Append the next word to the metadata, and say that there is metadata
strncat(metadata, curword, 4024 - strlen(metadata));
md = true;
//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
ss >> curword;
}
}
//Ignore comments - they get printed to screen
//else if (strcmp(curword,"/*")==0) {
// iFile>>curword;
// while (strcmp(curword,"*/")!=0) {
//cout<<curword<<" ";
// iFile>>curword;
// }
// cout<<endl;
//}
//Ignore comments surrounded by - they get printed to screen
}
iFile.close();
speciate();
}
}
Population::~Population() {
std::vector<Species*>::iterator curspec;
std::vector<Organism*>::iterator curorg;
//std::vector<Generation_viz*>::iterator cursnap;
if (species.begin()!=species.end()) {
for(curspec=species.begin();curspec!=species.end();++curspec) {
delete (*curspec);
}
}
else {
for(curorg=organisms.begin();curorg!=organisms.end();++curorg) {
delete (*curorg);
}
}
for (std::vector<Innovation*>::iterator iter = innovations.begin(); iter != innovations.end(); ++iter)
delete *iter;
//Delete the snapshots
// for(cursnap=generation_snapshots.begin();cursnap!=generation_snapshots.end();++cursnap) {
// delete (*cursnap);
// }
}
bool Population::verify() {
std::vector<Organism*>::iterator curorg;
bool verification;
for(curorg=organisms.begin();curorg!=organisms.end();++curorg) {
verification=((*curorg)->gnome)->verify();
}
return verification;
}
bool Population::clone(Genome *g,int size, float power) {
int count;
Genome *new_genome;
Organism *new_organism;
new_genome = g->duplicate(1);
new_organism = new Organism(0.0,new_genome,1);
organisms.push_back(new_organism);
//Create size copies of the Genome
//Start with perturbed linkweights
for(count=2;count<=size;count++) {
//cout<<"CREATING ORGANISM "<<count<<endl;
new_genome=g->duplicate(count);
if(power>0)
new_genome->mutate_link_weights(power,1.0,GAUSSIAN);
new_genome->randomize_traits();
new_organism=new Organism(0.0,new_genome,1);
organisms.push_back(new_organism);
}
//Keep a record of the innovation and node number we are on
cur_node_id=new_genome->get_last_node_id();
cur_innov_num=new_genome->get_last_gene_innovnum();
//Separate the new Population into species
speciate();
return true;
}
bool Population::spawn(Genome *g,int size) {
int count;
Genome *new_genome;
Organism *new_organism;
//Create size copies of the Genome
//Start with perturbed linkweights
for(count=1;count<=size;count++) {
//cout<<"CREATING ORGANISM "<<count<<endl;
new_genome=g->duplicate(count);
//new_genome->mutate_link_weights(1.0,1.0,GAUSSIAN);
new_genome->mutate_link_weights(1.0,1.0,COLDGAUSSIAN);
new_genome->randomize_traits();
new_organism=new Organism(0.0,new_genome,1);
organisms.push_back(new_organism);
}
//Keep a record of the innovation and node number we are on
cur_node_id=new_genome->get_last_node_id();
cur_innov_num=new_genome->get_last_gene_innovnum();
//Separate the new Population into species
speciate();
return true;
}
bool Population::speciate() {
std::vector<Organism*>::iterator curorg; //For stepping through Population
std::vector<Species*>::iterator curspecies; //Steps through species
Organism *comporg=0; //Organism for comparison
Species *newspecies; //For adding a new species
int counter=0; //Species counter
//Step through all existing organisms
for(curorg=organisms.begin();curorg!=organisms.end();++curorg) {
//For each organism, search for a species it is compatible to
curspecies=species.begin();
if (curspecies==species.end()){
//Create the first species
newspecies=new Species(++counter);
species.push_back(newspecies);
newspecies->add_Organism(*curorg); //Add the current organism
(*curorg)->species=newspecies; //Point organism to its species
}
else {
comporg=(*curspecies)->first();
while((comporg!=0)&&
(curspecies!=species.end())) {
if ((((*curorg)->gnome)->compatibility(comporg->gnome))<NEAT::compat_threshold) {
//Found compatible species, so add this organism to it
(*curspecies)->add_Organism(*curorg);
(*curorg)->species=(*curspecies); //Point organism to its species
comporg=0; //Note the search is over
}
else {
//Keep searching for a matching species
++curspecies;
if (curspecies!=species.end())
comporg=(*curspecies)->first();
}
}
//If we didn't find a match, create a new species
if (comporg!=0) {
newspecies=new Species(++counter);
species.push_back(newspecies);
newspecies->add_Organism(*curorg); //Add the current organism
(*curorg)->species=newspecies; //Point organism to its species
}
} //end else
} //end for
last_species=counter; //Keep track of highest species
return true;
}
bool Population::print_to_file_by_species(char *filename) {
std::vector<Species*>::iterator curspecies;
std::ofstream outFile(filename,std::ios::out);
//Make sure it worked
if (!outFile) {
std::cerr<<"Can't open "<<filename<<" for output"<<std::endl;
return false;
}
//Step through the Species and print them to the file
for(curspecies=species.begin();curspecies!=species.end();++curspecies) {
(*curspecies)->print_to_file(outFile);
}
outFile.close();
return true;
}
bool Population::print_to_file_by_species(std::ostream& outFile) {
std::vector<Species*>::iterator curspecies;
//ofstream outFile(filename,ios::out);
//std::ostream outFile;
//ResourceManager->openFileForWrite(outFile, fileName, std::ostream::Write);
//Make sure it worked
//if (!outFile) {
// cerr<<"Can't open "<<filename<<" for output"<<endl;
// return false;
//}
//Step through the Species and print them to the file
for(curspecies=species.begin();curspecies!=species.end();++curspecies) {
(*curspecies)->print_to_file(outFile);
}
return true;
}
bool Population::epoch(int generation) {
std::vector<Species*>::iterator curspecies;
std::vector<Species*>::iterator deadspecies; //For removing empty Species
std::vector<Organism*>::iterator curorg;
std::vector<Organism*>::iterator deadorg;
std::vector<Innovation*>::iterator curinnov;
std::vector<Innovation*>::iterator deadinnov; //For removing old Innovs
double total=0.0; //Used to compute average fitness over all Organisms
double overall_average; //The average modified fitness among ALL organisms
int orgcount;
static double max_fit=0.0;
double high_fit=0.0;
//The fractional parts of expected offspring that can be
//Used only when they accumulate above 1 for the purposes of counting
//Offspring
double skim;
int total_expected; //precision checking
int total_organisms=organisms.size();
int max_expected;
Species *best_species;
int final_expected;
int pause;
//Rights to make babies can be stolen from inferior species
//and given to their superiors, in order to concentrate exploration on
//the best species
int NUM_STOLEN=NEAT::babies_stolen; //Number of babies to steal
int one_fifth_stolen;
int one_tenth_stolen;
std::vector<Species*> sorted_species; //Species sorted by max fit org in Species
int stolen_babies; //Babies taken from the bad species and given to the champs
int half_pop;
int best_species_num; //Used in debugging to see why (if) best species dies
bool best_ok;
//We can try to keep the number of species constant at this number
int num_species_target=10;
int num_species=species.size();
double compat_mod=0.3; //Modify compat thresh to control speciation
//Keeping species diverse
//This commented out code forces the system to aim for
// num_species species at all times, enforcing diversity
//This tinkers with the compatibility threshold, which
// normally would be held constant
if (generation>1) {
if (num_species<num_species_target)
NEAT::compat_threshold-=compat_mod;
else if (num_species>num_species_target)
NEAT::compat_threshold+=compat_mod;
if (NEAT::compat_threshold<0.3) NEAT::compat_threshold=0.3;
}
//Stick the Species pointers into a new Species list for sorting
for(curspecies=species.begin();curspecies!=species.end();++curspecies) {
sorted_species.push_back(*curspecies);
}
//Sort the Species by max fitness (Use an extra list to do this)
//These need to use ORIGINAL fitness
//sorted_species.qsort(order_species);
std::sort(sorted_species.begin(), sorted_species.end(), order_species);
//Flag the lowest performing species over age 20 every 30 generations
//NOTE: THIS IS FOR COMPETITIVE COEVOLUTION STAGNATION DETECTION
/*
curspecies=sorted_species.end();
curspecies--;
while((curspecies!=sorted_species.begin())&&
((*curspecies)->age<20))
--curspecies;
if ((generation%30)==0)
(*curspecies)->obliterate=true;
*/
std::cout<<"Number of Species: "<<num_species<<std::endl;
std::cout<<"compat_thresh: "<<compat_threshold<<std::endl;
//Use Species' ages to modify the objective fitness of organisms
// in other words, make it more fair for younger species
// so they have a chance to take hold
//Also penalize stagnant species
//Then adjust the fitness using the species size to "share" fitness
//within a species.
//Then, within each Species, mark for death
//those below survival_thresh*average
for(curspecies=species.begin();curspecies!=species.end();++curspecies) {
(*curspecies)->adjust_fitness();
}
//Go through the organisms and add up their fitnesses to compute the
//overall average
for(curorg=organisms.begin();curorg!=organisms.end();++curorg) {
total+=(*curorg)->fitness;
}
overall_average=total/total_organisms;
std::cout<<"Generation "<<generation<<": "<<"overall_average = "<<overall_average<<std::endl;
//Now compute expected number of offspring for each individual organism
for(curorg=organisms.begin();curorg!=organisms.end();++curorg) {
(*curorg)->expected_offspring=(((*curorg)->fitness)/overall_average);
}
//Now add those offspring up within each Species to get the number of
//offspring per Species
skim=0.0;
total_expected=0;
for(curspecies=species.begin();curspecies!=species.end();++curspecies) {
skim=(*curspecies)->count_offspring(skim);
total_expected+=(*curspecies)->expected_offspring;
}
//Need to make up for lost foating point precision in offspring assignment
//If we lost precision, give an extra baby to the best Species
if (total_expected<total_organisms) {
//Find the Species expecting the most
max_expected=0;
final_expected=0;
for(curspecies=species.begin();curspecies!=species.end();++curspecies) {
if ((*curspecies)->expected_offspring>=max_expected) {
max_expected=(*curspecies)->expected_offspring;
best_species=(*curspecies);
}
final_expected+=(*curspecies)->expected_offspring;
}
//Give the extra offspring to the best species
++(best_species->expected_offspring);
final_expected++;
//If we still arent at total, there is a problem
//Note that this can happen if a stagnant Species
//dominates the population and then gets killed off by its age
//Then the whole population plummets in fitness
//If the average fitness is allowed to hit 0, then we no longer have
//an average we can use to assign offspring.
if (final_expected<total_organisms) {
// cout<<"Population died!"<<endl;
//cin>>pause;
for(curspecies=species.begin();curspecies!=species.end();++curspecies) {
(*curspecies)->expected_offspring=0;
}
best_species->expected_offspring=total_organisms;
}
}
//Sort the Species by max fitness (Use an extra list to do this)
//These need to use ORIGINAL fitness
//sorted_species.qsort(order_species);
std::sort(sorted_species.begin(), sorted_species.end(), order_species);
best_species_num=(*(sorted_species.begin()))->id;
for(curspecies=sorted_species.begin();curspecies!=sorted_species.end();++curspecies) {
//Print out for Debugging/viewing what's going on
std::cout<<"orig fitness of Species"<<(*curspecies)->id<<"(Size "<<(*curspecies)->organisms.size()<<"): "<<(*((*curspecies)->organisms).begin())->orig_fitness<<" last improved "<<((*curspecies)->age-(*curspecies)->age_of_last_improvement)<<std::endl;
}
//Check for Population-level stagnation
curspecies=sorted_species.begin();
(*(((*curspecies)->organisms).begin()))->pop_champ=true; //DEBUG marker of the best of pop
if (((*(((*curspecies)->organisms).begin()))->orig_fitness)>
highest_fitness) {
highest_fitness=((*(((*curspecies)->organisms).begin()))->orig_fitness);
highest_last_changed=0;
std::cout<<"NEW POPULATION RECORD FITNESS: "<<highest_fitness<<std::endl;
}
else {
++highest_last_changed;
std::cout<<highest_last_changed<<" generations since last population fitness record: "<<highest_fitness<<std::endl;
}
if(highest_fitness>max_fit)
{
max_fit=highest_fitness;
}
//Check for stagnation- if there is stagnation, perform delta-coding
if (highest_last_changed>=NEAT::dropoff_age+5) {
// cout<<"PERFORMING DELTA CODING"<<endl;
highest_last_changed=0;
half_pop=NEAT::pop_size/2;
// cout<<"half_pop"<<half_pop<<" pop_size-halfpop: "<<pop_size-half_pop<<endl;
curspecies=sorted_species.begin();
(*(((*curspecies)->organisms).begin()))->super_champ_offspring=half_pop;
(*curspecies)->expected_offspring=half_pop;
(*curspecies)->age_of_last_improvement=(*curspecies)->age;
++curspecies;
if (curspecies!=sorted_species.end()) {
(*(((*curspecies)->organisms).begin()))->super_champ_offspring=NEAT::pop_size-half_pop;
(*curspecies)->expected_offspring=NEAT::pop_size-half_pop;
(*curspecies)->age_of_last_improvement=(*curspecies)->age;
++curspecies;
//Get rid of all species under the first 2
while(curspecies!=sorted_species.end()) {
(*curspecies)->expected_offspring=0;
++curspecies;
}
}
else {
curspecies=sorted_species.begin();
(*(((*curspecies)->organisms).begin()))->super_champ_offspring+=NEAT::pop_size-half_pop;
(*curspecies)->expected_offspring=NEAT::pop_size-half_pop;
}
}
//STOLEN BABIES: The system can take expected offspring away from
// worse species and give them to superior species depending on
// the system parameter babies_stolen (when babies_stolen > 0)
else if (NEAT::babies_stolen>0) {
//Take away a constant number of expected offspring from the worst few species
stolen_babies=0;
curspecies=sorted_species.end();
curspecies--;
while ((stolen_babies<NUM_STOLEN)&&
(curspecies!=sorted_species.begin())) {
//cout<<"Considering Species "<<(*curspecies)->id<<": age "<<(((*curspecies)->age))<<" expected offspring "<<(((*curspecies)->expected_offspring))<<endl;
if ((((*curspecies)->age)>5)&&
(((*curspecies)->expected_offspring)>2)) {
//cout<<"STEALING!"<<endl;
//This species has enough to finish off the stolen pool
if (((*curspecies)->expected_offspring-1)>=(NUM_STOLEN-stolen_babies)) {
(*curspecies)->expected_offspring-=(NUM_STOLEN-stolen_babies);
stolen_babies=NUM_STOLEN;
}
//Not enough here to complete the pool of stolen
else {
stolen_babies+=(*curspecies)->expected_offspring-1;
(*curspecies)->expected_offspring=1;
}
}
curspecies--;
//if (stolen_babies>0)
//cout<<"stolen babies so far: "<<stolen_babies<<endl;
}
//cout<<"STOLEN BABIES: "<<stolen_babies<<endl;
//Mark the best champions of the top species to be the super champs
//who will take on the extra offspring for cloning or mutant cloning
curspecies=sorted_species.begin();
//Determine the exact number that will be given to the top three
//They get , in order, 1/5 1/5 and 1/10 of the stolen babies
one_fifth_stolen=NEAT::babies_stolen/5;
one_tenth_stolen=NEAT::babies_stolen/10;
//Don't give to dying species even if they are champs
while((curspecies!=sorted_species.end())&& ((*curspecies)->last_improved()>NEAT::dropoff_age))
++curspecies;
//Concentrate A LOT on the number one species
if ((curspecies!=sorted_species.end()) && (stolen_babies>=one_fifth_stolen)) {
(*(((*curspecies)->organisms).begin()))->super_champ_offspring=one_fifth_stolen;
(*curspecies)->expected_offspring+=one_fifth_stolen;
stolen_babies-=one_fifth_stolen;
//cout<<"Gave "<<one_fifth_stolen<<" babies to Species "<<(*curspecies)->id<<endl;
// cout<<"The best superchamp is "<<(*(((*curspecies)->organisms).begin()))->gnome->genome_id<<endl;
//Print this champ to file "champ" for observation if desired
//IMPORTANT: This causes generational file output
//print_Genome_tofile((*(((*curspecies)->organisms).begin()))->gnome,"champ");
curspecies++;
}
//Don't give to dying species even if they are champs
while((curspecies!=sorted_species.end())&&((*curspecies)->last_improved()>NEAT::dropoff_age))
++curspecies;
if ((curspecies!=sorted_species.end())) {
if (stolen_babies>=one_fifth_stolen) {
(*(((*curspecies)->organisms).begin()))->super_champ_offspring=one_fifth_stolen;
(*curspecies)->expected_offspring+=one_fifth_stolen;
stolen_babies-=one_fifth_stolen;
//cout<<"Gave "<<one_fifth_stolen<<" babies to Species "<<(*curspecies)->id<<endl;
curspecies++;
}
}
//Don't give to dying species even if they are champs
while(((*curspecies)->last_improved()>NEAT::dropoff_age)&&(curspecies!=sorted_species.end()))
++curspecies;
if (curspecies!=sorted_species.end())
if (stolen_babies>=one_tenth_stolen) {
(*(((*curspecies)->organisms).begin()))->super_champ_offspring=one_tenth_stolen;
(*curspecies)->expected_offspring+=one_tenth_stolen;
stolen_babies-=one_tenth_stolen;
//cout<<"Gave "<<one_tenth_stolen<<" babies to Species "<<(*curspecies)->id<<endl;
curspecies++;
}
//Don't give to dying species even if they are champs
while(((*curspecies)->last_improved()>NEAT::dropoff_age)&&(curspecies!=sorted_species.end()))
++curspecies;
while((stolen_babies>0)&&
(curspecies!=sorted_species.end())) {
//Randomize a little which species get boosted by a super champ
if (randfloat()>0.1)
if (stolen_babies>3) {
(*(((*curspecies)->organisms).begin()))->super_champ_offspring=3;
(*curspecies)->expected_offspring+=3;
stolen_babies-=3;
//cout<<"Gave 3 babies to Species "<<(*curspecies)->id<<endl;
}
else {
//cout<<"3 or less babies available"<<endl;
(*(((*curspecies)->organisms).begin()))->super_champ_offspring=stolen_babies;
(*curspecies)->expected_offspring+=stolen_babies;
//cout<<"Gave "<<stolen_babies<<" babies to Species "<<(*curspecies)->id<<endl;
stolen_babies=0;
}
curspecies++;
//Don't give to dying species even if they are champs
while((curspecies!=sorted_species.end())&&((*curspecies)->last_improved()>NEAT::dropoff_age))
++curspecies;
}
//cout<<"Done giving back babies"<<endl;
//If any stolen babies aren't taken, give them to species #1's champ
if (stolen_babies>0) {
//cout<<"Not all given back, giving to best Species"<<endl;
curspecies=sorted_species.begin();
(*(((*curspecies)->organisms).begin()))->super_champ_offspring+=stolen_babies;
(*curspecies)->expected_offspring+=stolen_babies;
stolen_babies=0;
}
}
//Kill off all Organisms marked for death. The remainder
//will be allowed to reproduce.
curorg=organisms.begin();
while(curorg!=organisms.end()) {
if (((*curorg)->eliminate)) {
//Remove the organism from its Species
((*curorg)->species)->remove_org(*curorg);
//Delete the organism from memory
delete (*curorg);
//Remember where we are
deadorg=curorg;
++curorg;
//iter2 = v.erase(iter);
//Remove the organism from the master list
curorg=organisms.erase(deadorg);
}
else {
++curorg;
}
}
//cout<<"Reproducing"<<endl;
//Perform reproduction. Reproduction is done on a per-Species
//basis. (So this could be paralellized potentially.)
// for(curspecies=species.begin();curspecies!=species.end();++curspecies) {
//KENHACK
// for(std::vector<Species*>::iterator curspecies2=species.begin();curspecies2!=species.end();++curspecies2) {
// std::cout<<"PRE in repro specloop SPEC EXISTING number "<<(*curspecies2)->id<<std::endl;
// }
// (*curspecies)->reproduce(generation,this,sorted_species);
//}
curspecies=species.begin();
int last_id=(*curspecies)->id;
while(curspecies!=species.end()) {
(*curspecies)->reproduce(generation,this,sorted_species);
//Set the current species to the id of the last species checked
//(the iterator must be reset because there were possibly vector insertions during reproduce)
std::vector<Species*>::iterator curspecies2=species.begin();
while(curspecies2!=species.end()) {
if (((*curspecies2)->id)==last_id)
curspecies=curspecies2;
curspecies2++;
}
//Move to the next on the list
curspecies++;
//Record where we are
if (curspecies!=species.end())
last_id=(*curspecies)->id;
}
//cout<<"Reproduction Complete"<<endl;
//Destroy and remove the old generation from the organisms and species
curorg=organisms.begin();
while(curorg!=organisms.end()) {
//Remove the organism from its Species
((*curorg)->species)->remove_org(*curorg);
//std::cout<<"deleting org # "<<(*curorg)->gnome->genome_id<<std::endl;
//Delete the organism from memory
delete (*curorg);
//Remember where we are
deadorg=curorg;
++curorg;
//std::cout<<"next org # "<<(*curorg)->gnome->genome_id<<std::endl;
//Remove the organism from the master list
curorg=organisms.erase(deadorg);
//std::cout<<"nnext org # "<<(*curorg)->gnome->genome_id<<std::endl;
}
//Remove all empty Species and age ones that survive
//As this happens, create master organism list for the new generation
curspecies=species.begin();
orgcount=0;
while(curspecies!=species.end()) {
if (((*curspecies)->organisms.size())==0) {
delete (*curspecies);
deadspecies=curspecies;
++curspecies;
curspecies=species.erase(deadspecies);
}
//Age surviving Species and
//Rebuild master Organism list: NUMBER THEM as they are added to the list
else {
//Age any Species that is not newly created in this generation
if ((*curspecies)->novel) {
(*curspecies)->novel=false;
}
else ++((*curspecies)->age);
//Go through the organisms of the curspecies and add them to
//the master list
for(curorg=((*curspecies)->organisms).begin();curorg!=((*curspecies)->organisms).end();++curorg) {
((*curorg)->gnome)->genome_id=orgcount++;
organisms.push_back(*curorg);
}
++curspecies;
}
}
//Remove the innovations of the current generation
curinnov=innovations.begin();
while(curinnov!=innovations.end()) {
delete (*curinnov);
deadinnov=curinnov;
++curinnov;
curinnov=innovations.erase(deadinnov);
}
//DEBUG: Check to see if the best species died somehow
// We don't want this to happen
curspecies=species.begin();
best_ok=false;
while(curspecies!=species.end()) {
if (((*curspecies)->id)==best_species_num) best_ok=true;
++curspecies;
}
if (!best_ok) {
//cout<<"ERROR: THE BEST SPECIES DIED!"<<endl;
}
else {
//cout<<"The best survived: "<<best_species_num<<endl;
}
//DEBUG: Checking the top organism's duplicate in the next gen
//This prints the champ's child to the screen
for(curorg=organisms.begin();curorg!=organisms.end();++curorg) {
if ((*curorg)->pop_champ_child) {
//cout<<"At end of reproduction cycle, the child of the pop champ is: "<<(*curorg)->gnome<<endl;
}
}
//cout<<"babies_stolen at end: "<<babies_stolen<<endl;
//cout<<"Epoch complete"<<endl;
return true;
}
bool Population::rank_within_species() {
std::vector<Species*>::iterator curspecies;
//Add each Species in this generation to the snapshot
for(curspecies=species.begin();curspecies!=species.end();++curspecies) {
(*curspecies)->rank();
}
return true;
}
void Population::estimate_all_averages() {
std::vector<Species*>::iterator curspecies;
for(curspecies=species.begin();curspecies!=species.end();++curspecies) {
(*curspecies)->estimate_average();
}
}
Species *Population::choose_parent_species() {
double total_fitness=0;
std::vector<Species*>::iterator curspecies;
double marble; //The roulette marble
double spin; //Spins until the marble reaches its chosen point
//We can try to keep the number of species constant at this number
int num_species_target=4;
int num_species=species.size();
double compat_mod=0.3; //Modify compat thresh to control speciation
//Keeping species diverse
//This commented out code forces the system to aim for
// num_species species at all times, enforcing diversity
//This tinkers with the compatibility threshold, which
// normally would be held constant
//if (num_species<num_species_target)
// NEAT::compat_threshold-=compat_mod;
//else if (num_species>num_species_target)
// NEAT::compat_threshold+=compat_mod;
//if (NEAT::compat_threshold<0.3) NEAT::compat_threshold=0.3;
//Use the roulette method to choose the species
//Sum all the average fitness estimates of the different species
//for the purposes of the roulette
for(curspecies=species.begin();curspecies!=species.end();++curspecies) {
total_fitness+=(*curspecies)->average_est;
}