forked from jal278/novelty-search-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiments.cpp
1380 lines (1069 loc) · 37.7 KB
/
experiments.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 "experiments.h"
#include "noveltyset.h"
#include <cstring>
//#define NO_SCREEN_OUT
//Perform evolution on single pole balacing, for gens generations
Population *pole1_test(int gens) {
Population *pop;
Genome *start_genome;
char curword[20];
int id;
ostringstream *fnamebuf;
int gen;
int expcount;
int status;
int runs[NEAT::num_runs];
int totalevals;
ifstream iFile("pole1startgenes",ios::in);
cout<<"START SINGLE POLE BALANCING EVOLUTION"<<endl;
cout<<"Reading in the start genome"<<endl;
//Read in the start Genome
iFile>>curword;
iFile>>id;
cout<<"Reading in Genome id "<<id<<endl;
start_genome=new Genome(id,iFile);
iFile.close();
//Run multiple experiments
for(expcount=0;expcount<NEAT::num_runs;expcount++) {
cout<<"EXPERIMENT #"<<expcount<<endl;
cout<<"Start Genome: "<<start_genome<<endl;
//Spawn the Population
cout<<"Spawning Population off Genome"<<endl;
pop=new Population(start_genome,NEAT::pop_size);
cout<<"Verifying Spawned Pop"<<endl;
pop->verify();
for (gen=1;gen<=gens;gen++) {
cout<<"Generation "<<gen<<endl;
fnamebuf=new ostringstream();
(*fnamebuf)<<"gen_"<<gen<<ends; //needs end marker
#ifndef NO_SCREEN_OUT
cout<<"name of fname: "<<fnamebuf->str()<<endl;
#endif
char temp[50];
sprintf (temp, "gen_%d", gen);
status=pole1_epoch(pop,gen,temp);
//status=(pole1_epoch(pop,gen,fnamebuf->str()));
if (status) {
runs[expcount]=status;
gen=gens+1;
}
fnamebuf->clear();
delete fnamebuf;
}
}
totalevals=0;
for(expcount=0;expcount<NEAT::num_runs;expcount++) {
cout<<runs[expcount]<<endl;
totalevals+=runs[expcount];
}
cout<<"Average evals: "<<totalevals/NEAT::num_runs<<endl;
return pop;
}
int pole1_epoch(Population *pop,int generation,char *filename) {
vector<Organism*>::iterator curorg;
vector<Species*>::iterator curspecies;
//char cfilename[100];
//strncpy( cfilename, filename.c_str(), 100 );
//ofstream cfilename(filename.c_str());
bool win=false;
int winnernum;
//Evaluate each organism on a test
for(curorg=(pop->organisms).begin();curorg!=(pop->organisms).end();++curorg) {
if (pole1_evaluate(*curorg)) win=true;
}
//Average and max their fitnesses for dumping to file and snapshot
for(curspecies=(pop->species).begin();curspecies!=(pop->species).end();++curspecies) {
//This experiment control routine issues commands to collect ave
//and max fitness, as opposed to having the snapshot do it,
//because this allows flexibility in terms of what time
//to observe fitnesses at
(*curspecies)->compute_average_fitness();
(*curspecies)->compute_max_fitness();
}
//Take a snapshot of the population, so that it can be
//visualized later on
//if ((generation%1)==0)
// pop->snapshot();
//Only print to file every print_every generations
if (win||
((generation%(NEAT::print_every))==0))
pop->print_to_file_by_species(filename);
if (win) {
for(curorg=(pop->organisms).begin();curorg!=(pop->organisms).end();++curorg) {
if ((*curorg)->winner) {
winnernum=((*curorg)->gnome)->genome_id;
cout<<"WINNER IS #"<<((*curorg)->gnome)->genome_id<<endl;
}
}
}
//Create the next generation
pop->epoch(generation);
if (win) return ((generation-1)*NEAT::pop_size+winnernum);
else return 0;
}
bool pole1_evaluate(Organism *org) {
Network *net;
int numnodes; /* Used to figure out how many nodes
should be visited during activation */
int thresh; /* How many visits will be allowed before giving up
(for loop detection) */
// int MAX_STEPS=120000;
int MAX_STEPS=100000;
net=org->net;
numnodes=((org->gnome)->nodes).size();
thresh=numnodes*2; //Max number of visits allowed per activation
//Try to balance a pole now
org->fitness = go_cart(net,MAX_STEPS,thresh);
#ifndef NO_SCREEN_OUT
cout<<"Org "<<(org->gnome)->genome_id<<" fitness: "<<org->fitness<<endl;
#endif
//Decide if its a winner
if (org->fitness>=MAX_STEPS) {
org->winner=true;
return true;
}
else {
org->winner=false;
return false;
}
}
// cart_and_pole() was take directly from the pole simulator written
// by Richard Sutton and Charles Anderson.
int go_cart(Network *net,int max_steps,int thresh)
{
float x, /* cart position, meters */
x_dot, /* cart velocity */
theta, /* pole angle, radians */
theta_dot; /* pole angular velocity */
int steps=0,y;
int random_start=1;
double in[5]; //Input loading array
double out1;
double out2;
// double one_degree= 0.0174532; /* 2pi/360 */
// double six_degrees=0.1047192;
double twelve_degrees=0.2094384;
// double thirty_six_degrees= 0.628329;
// double fifty_degrees=0.87266;
vector<NNode*>::iterator out_iter;
if (random_start) {
/*set up random start state*/
x = (lrand48()%4800)/1000.0 - 2.4;
x_dot = (lrand48()%2000)/1000.0 - 1;
theta = (lrand48()%400)/1000.0 - .2;
theta_dot = (lrand48()%3000)/1000.0 - 1.5;
}
else
x = x_dot = theta = theta_dot = 0.0;
/*--- Iterate through the action-learn loop. ---*/
while (steps++ < max_steps)
{
/*-- setup the input layer based on the four iputs --*/
//setup_input(net,x,x_dot,theta,theta_dot);
in[0]=1.0; //Bias
in[1]=(x + 2.4) / 4.8;;
in[2]=(x_dot + .75) / 1.5;
in[3]=(theta + twelve_degrees) / .41;
in[4]=(theta_dot + 1.0) / 2.0;
net->load_sensors(in);
//activate_net(net); /*-- activate the network based on the input --*/
//Activate the net
//If it loops, exit returning only fitness of 1 step
if (!(net->activate())) return 1;
/*-- decide which way to push via which output unit is greater --*/
out_iter=net->outputs.begin();
out1=(*out_iter)->activation;
++out_iter;
out2=(*out_iter)->activation;
if (out1 > out2)
y = 0;
else
y = 1;
/*--- Apply action to the simulated cart-pole ---*/
cart_pole(y, &x, &x_dot, &theta, &theta_dot);
/*--- Check for failure. If so, return steps ---*/
if (x < -2.4 || x > 2.4 || theta < -twelve_degrees ||
theta > twelve_degrees)
return steps;
}
return steps;
}
// cart_and_pole() was take directly from the pole simulator written
// by Richard Sutton and Charles Anderson.
// This simulator uses normalized, continous inputs instead of
// discretizing the input space.
/*----------------------------------------------------------------------
cart_pole: Takes an action (0 or 1) and the current values of the
four state variables and updates their values by estimating the state
TAU seconds later.
----------------------------------------------------------------------*/
void cart_pole(int action, float *x,float *x_dot, float *theta, float *theta_dot) {
float xacc,thetaacc,force,costheta,sintheta,temp;
const float GRAVITY=9.8;
const float MASSCART=1.0;
const float MASSPOLE=0.1;
const float TOTAL_MASS=(MASSPOLE + MASSCART);
const float LENGTH=0.5; /* actually half the pole's length */
const float POLEMASS_LENGTH=(MASSPOLE * LENGTH);
const float FORCE_MAG=10.0;
const float TAU=0.02; /* seconds between state updates */
const float FOURTHIRDS=1.3333333333333;
force = (action>0)? FORCE_MAG : -FORCE_MAG;
costheta = cos(*theta);
sintheta = sin(*theta);
temp = (force + POLEMASS_LENGTH * *theta_dot * *theta_dot * sintheta)
/ TOTAL_MASS;
thetaacc = (GRAVITY * sintheta - costheta* temp)
/ (LENGTH * (FOURTHIRDS - MASSPOLE * costheta * costheta
/ TOTAL_MASS));
xacc = temp - POLEMASS_LENGTH * thetaacc* costheta / TOTAL_MASS;
/*** Update the four state variables, using Euler's method. ***/
*x += TAU * *x_dot;
*x_dot += TAU * xacc;
*theta += TAU * *theta_dot;
*theta_dot += TAU * thetaacc;
}
/* ------------------------------------------------------------------ */
/* Double pole balacing */
/* ------------------------------------------------------------------ */
//Perform evolution on double pole balacing, for gens generations
//If velocity is false, then velocity information will be withheld from the
//network population (non-Markov)
Population *pole2_test(int gens,int velocity) {
Population *pop;
Genome *start_genome;
char curword[20];
int id;
ostringstream *fnamebuf;
int gen;
CartPole *thecart;
//Stat collection variables
int highscore;
int record[NEAT::num_runs][1000];
double recordave[1000];
int genesrec[NEAT::num_runs][1000];
double genesave[1000];
int nodesrec[NEAT::num_runs][1000];
double nodesave[1000];
int winnergens[NEAT::num_runs];
int initcount;
int champg, champn, winnernum; //Record number of genes and nodes in champ
int run;
int curtotal; //For averaging
int samples; //For averaging
ofstream oFile("statout",ios::out);
champg=0;
champn=0;
//Initialize the stat recording arrays
for (initcount=0;initcount<200;initcount++) {
recordave[initcount]=0;
genesave[initcount]=0;
nodesave[initcount]=0;
for (run=0;run<NEAT::num_runs;++run) {
record[run][initcount]=0;
genesrec[run][initcount]=0;
nodesrec[run][initcount]=0;
}
}
char *non_markov_starter="pole2startgenes2";
char *markov_starter="pole2startgenes1";
char *startstring;
if (velocity==0) startstring=non_markov_starter;
else if (velocity==1) startstring=markov_starter;
ifstream iFile(startstring,ios::in);
//ifstream iFile("pole2startgenes",ios::in);
cout<<"START DOUBLE POLE BALANCING EVOLUTION"<<endl;
if (!velocity)
cout<<"NO VELOCITY INPUT"<<endl;
cout<<"Reading in the start genome"<<endl;
//Read in the start Genome
iFile>>curword;
iFile>>id;
cout<<"Reading in Genome id "<<id<<endl;
start_genome=new Genome(id,iFile);
iFile.close();
cout<<"Start Genome: "<<start_genome<<endl;
for (run=0;run<NEAT::num_runs;run++) {
cout<<"RUN #"<<run<<endl;
//Spawn the Population from starter gene
cout<<"Spawning Population off Genome"<<endl;
pop=new Population(start_genome,NEAT::pop_size);
//Alternative way to start off of randomly connected genomes
//pop=new Population(pop_size,7,1,10,false,0.3);
cout<<"Verifying Spawned Pop"<<endl;
pop->verify();
//Create the Cart
thecart=new CartPole(true,velocity);
for (gen=1;gen<=gens;gen++) {
cout<<"Epoch "<<gen<<endl;
fnamebuf=new ostringstream();
(*fnamebuf)<<"gen_"<<gen<<ends; //needs end marker
#ifndef NO_SCREEN_OUT
cout<<"name of fname: "<<fnamebuf->str()<<endl;
#endif
char temp[50];
sprintf (temp, "gen_%d", gen);
highscore=pole2_epoch(pop,gen,temp,velocity, thecart,champg,champn,winnernum,oFile);
//highscore=pole2_epoch(pop,gen,fnamebuf->str(),velocity, thecart,champg,champn,winnernum,oFile);
//cout<<"GOT HIGHSCORE FOR GEN "<<gen<<": "<<highscore-1<<endl;
record[run][gen-1]=highscore-1;
genesrec[run][gen-1]=champg;
nodesrec[run][gen-1]=champn;
fnamebuf->clear();
delete fnamebuf;
//Stop right at the winnergen
if (((pop->winnergen)!=0)&&(gen==(pop->winnergen))) {
winnergens[run]=NEAT::pop_size*(gen-1)+winnernum;
gen=gens+1;
}
//In non-MARKOV, stop right at winning (could go beyond if desired)
if ((!(thecart->MARKOV))&&((pop->winnergen)!=0))
gen=gens+1;
#ifndef NO_SCREEN_OUT
cout<<"gen = "<<gen<<" gens = "<<gens<<endl;
#endif
if (gen==(gens-1)) oFile<<"FAIL: Last gen on run "<<run<<endl;
}
if (run<NEAT::num_runs-1) delete pop;
delete thecart;
}
cout<<"Generation highs: "<<endl;
oFile<<"Generation highs: "<<endl;
for(gen=0;gen<=gens-1;gen++) {
curtotal=0;
for (run=0;run<NEAT::num_runs;++run) {
if (record[run][gen]>0) {
cout<<setw(8)<<record[run][gen]<<" ";
oFile<<setw(8)<<record[run][gen]<<" ";
curtotal+=record[run][gen];
}
else {
cout<<" ";
oFile<<" ";
curtotal+=100000;
}
recordave[gen]=(double) curtotal/NEAT::num_runs;
}
cout<<endl;
oFile<<endl;
}
cout<<"Generation genes in champ: "<<endl;
for(gen=0;gen<=gens-1;gen++) {
curtotal=0;
samples=0;
for (run=0;run<NEAT::num_runs;++run) {
if (genesrec[run][gen]>0) {
cout<<setw(4)<<genesrec[run][gen]<<" ";
oFile<<setw(4)<<genesrec[run][gen]<<" ";
curtotal+=genesrec[run][gen];
samples++;
}
else {
cout<<setw(4)<<" ";
oFile<<setw(4)<<" ";
}
}
genesave[gen]=(double) curtotal/samples;
cout<<endl;
oFile<<endl;
}
cout<<"Generation nodes in champ: "<<endl;
oFile<<"Generation nodes in champ: "<<endl;
for(gen=0;gen<=gens-1;gen++) {
curtotal=0;
samples=0;
for (run=0;run<NEAT::num_runs;++run) {
if (nodesrec[run][gen]>0) {
cout<<setw(4)<<nodesrec[run][gen]<<" ";
oFile<<setw(4)<<nodesrec[run][gen]<<" ";
curtotal+=nodesrec[run][gen];
samples++;
}
else {
cout<<setw(4)<<" ";
oFile<<setw(4)<<" ";
}
}
nodesave[gen]=(double) curtotal/samples;
cout<<endl;
oFile<<endl;
}
cout<<"Generational record fitness averages: "<<endl;
oFile<<"Generational record fitness averages: "<<endl;
for(gen=0;gen<gens-1;gen++) {
cout<<recordave[gen]<<endl;
oFile<<recordave[gen]<<endl;
}
cout<<"Generational number of genes in champ averages: "<<endl;
oFile<<"Generational number of genes in champ averages: "<<endl;
for(gen=0;gen<gens-1;gen++) {
cout<<genesave[gen]<<endl;
oFile<<genesave[gen]<<endl;
}
cout<<"Generational number of nodes in champ averages: "<<endl;
oFile<<"Generational number of nodes in champ averages: "<<endl;
for(gen=0;gen<gens-1;gen++) {
cout<<nodesave[gen]<<endl;
oFile<<nodesave[gen]<<endl;
}
cout<<"Winner evals: "<<endl;
oFile<<"Winner evals: "<<endl;
curtotal=0;
samples=0;
for (run=0;run<NEAT::num_runs;++run) {
cout<<winnergens[run]<<endl;
oFile<<winnergens[run]<<endl;
curtotal+=winnergens[run];
samples++;
}
cout<<"Average # evals: "<<((double) curtotal/samples)<<endl;
oFile<<"Average # evals: "<<((double) curtotal/samples)<<endl;
oFile.close();
return pop;
}
//This is used for list sorting of Species by fitness of best organism
//highest fitness first
//Used to choose which organism to test
//bool order_new_species(Species *x, Species *y) {
//
// return (x->compute_max_fitness() >
// y->compute_max_fitness());
//}
int pole2_epoch(Population *pop,int generation,char *filename,bool velocity,
CartPole *thecart,int &champgenes,int &champnodes,
int &winnernum, ofstream &oFile) {
//char cfilename[100];
//strncpy( cfilename, filename.c_str(), 100 );
//ofstream cfilename(filename.c_str());
vector<Organism*>::iterator curorg;
vector<Species*>::iterator curspecies;
vector<Species*> sorted_species; //Species sorted by max fit org in Species
int pause;
bool win=false;
double champ_fitness;
Organism *champ;
//double statevals[5]={-0.9,-0.5,0.0,0.5,0.9};
double statevals[5]={0.05, 0.25, 0.5, 0.75, 0.95};
int s0c,s1c,s2c,s3c;
int score;
thecart->nmarkov_long=false;
thecart->generalization_test=false;
//Evaluate each organism on a test
for(curorg=(pop->organisms).begin();curorg!=(pop->organisms).end();++curorg) {
//shouldn't happen
if (((*curorg)->gnome)==0) {
cout<<"ERROR EMPTY GEMOME!"<<endl;
cin>>pause;
}
if (pole2_evaluate((*curorg),velocity,thecart)) win=true;
}
//Average and max their fitnesses for dumping to file and snapshot
for(curspecies=(pop->species).begin();curspecies!=(pop->species).end();++curspecies) {
//This experiment control routine issues commands to collect ave
//and max fitness, as opposed to having the snapshot do it,
//because this allows flexibility in terms of what time
//to observe fitnesses at
(*curspecies)->compute_average_fitness();
(*curspecies)->compute_max_fitness();
}
//Take a snapshot of the population, so that it can be
//visualized later on
//if ((generation%1)==0)
// pop->snapshot();
//Find the champion in the markov case simply for stat collection purposes
if (thecart->MARKOV) {
champ_fitness=0.0;
for(curorg=(pop->organisms).begin();curorg!=(pop->organisms).end();++curorg) {
if (((*curorg)->fitness)>champ_fitness) {
champ=(*curorg);
champ_fitness=champ->fitness;
champgenes=champ->gnome->genes.size();
champnodes=champ->gnome->nodes.size();
winnernum=champ->gnome->genome_id;
}
}
}
//Check for winner in Non-Markov case
if (!(thecart->MARKOV)) {
cout<<"Non-markov case"<<endl;
//Sort the species
for(curspecies=(pop->species).begin();curspecies!=(pop->species).end();++curspecies) {
sorted_species.push_back(*curspecies);
}
//sorted_species.sort(order_new_species);
std::sort(sorted_species.begin(), sorted_species.end(), NEAT::order_new_species);
//std::sort(sorted_species.begin(), sorted_species.end(), order_species);
cout<<"Number of species sorted: "<<sorted_species.size()<<endl;
//First update what is checked and unchecked
for(curspecies=sorted_species.begin();curspecies!=sorted_species.end();++curspecies) {
if (((*curspecies)->compute_max_fitness())>((*curspecies)->max_fitness_ever))
(*curspecies)->checked=false;
}
//Now find a species that is unchecked
curspecies=sorted_species.begin();
cout<<"Is the first species checked? "<<(*curspecies)->checked<<endl;
while((curspecies!=(sorted_species.end()))&&
((*curspecies)->checked))
{
cout<<"Species #"<<(*curspecies)->id<<" is checked"<<endl;
++curspecies;
}
if (curspecies==(sorted_species.end())) curspecies=sorted_species.begin();
//Remember it was checked
(*curspecies)->checked=true;
cout<<"Is the species now checked? "<<(*curspecies)->checked<<endl;
//Extract the champ
cout<<"Champ chosen from Species "<<(*curspecies)->id<<endl;
champ=(*curspecies)->get_champ();
champ_fitness=champ->fitness;
cout<<"Champ is organism #"<<champ->gnome->genome_id<<endl;
cout<<"Champ fitness: "<<champ_fitness<<endl;
winnernum=champ->gnome->genome_id;
cout<<champ->gnome<<endl;
//Now check to make sure the champ can do 100,000
thecart->nmarkov_long=true;
thecart->generalization_test=false;
//The champ needs tp be flushed here because it may have
//leftover activation from its last test run that could affect
//its recurrent memory
(champ->net)->flush();
//champ->gnome->print_to_filename("tested");
if (pole2_evaluate(champ,velocity,thecart)) {
cout<<"The champ passed the 100,000 test!"<<endl;
thecart->nmarkov_long=false;
//Given that the champ passed, now run it on generalization tests
score=0;
for (s0c=0;s0c<=4;++s0c)
for (s1c=0;s1c<=4;++s1c)
for (s2c=0;s2c<=4;++s2c)
for (s3c=0;s3c<=4;++s3c) {
thecart->state[0] = statevals[s0c] * 4.32 - 2.16;
thecart->state[1] = statevals[s1c] * 2.70 - 1.35;
thecart->state[2] = statevals[s2c] * 0.12566304 - 0.06283152;
/* 0.06283152 = 3.6 degrees */
thecart->state[3] = statevals[s3c] * 0.30019504 - 0.15009752;
/* 00.15009752 = 8.6 degrees */
thecart->state[4]=0.0;
thecart->state[5]=0.0;
cout<<"On combo "<<thecart->state[0]<<" "<<thecart->state[1]<<" "<<thecart->state[2]<<" "<<thecart->state[3]<<endl;
thecart->generalization_test=true;
(champ->net)->flush(); //Reset the champ for each eval
if (pole2_evaluate(champ,velocity,thecart)) {
cout<<"----------------------------The champ passed its "<<score<<"th test"<<endl;
score++;
}
}
if (score>=200) {
cout<<"The champ wins!!! (generalization = "<<score<<" )"<<endl;
oFile<<"(generalization = "<<score<<" )"<<endl;
oFile<<"generation= "<<generation<<endl;
(champ->gnome)->print_to_file(oFile);
champ_fitness=champ->fitness;
champgenes=champ->gnome->genes.size();
champnodes=champ->gnome->nodes.size();
winnernum=champ->gnome->genome_id;
win=true;
}
else {
cout<<"The champ couldn't generalize"<<endl;
champ->fitness=champ_fitness; //Restore the champ's fitness
}
}
else {
cout<<"The champ failed the 100,000 test :("<<endl;
cout<<"made score "<<champ->fitness<<endl;
champ->fitness=champ_fitness; //Restore the champ's fitness
}
}
//Only print to file every print_every generations
if (win||
((generation%(NEAT::print_every))==0)) {
cout<<"printing file: "<<filename<<endl;
pop->print_to_file_by_species(filename);
}
if ((win)&&((pop->winnergen)==0)) pop->winnergen=generation;
//Prints a champion out on each generation
//IMPORTANT: This causes generational file output!
print_Genome_tofile(champ->gnome,"champ");
//Create the next generation
pop->epoch(generation);
return (int) champ_fitness;
}
bool pole2_evaluate(Organism *org,bool velocity, CartPole *thecart) {
Network *net;
int thresh; /* How many visits will be allowed before giving up
(for loop detection) NOW OBSOLETE */
int pause;
net=org->net;
thresh=100; //this is obsolete
//DEBUG : Check flushedness of org
//org->net->flush_check();
//Try to balance a pole now
org->fitness = thecart->evalNet(net,thresh);
#ifndef NO_SCREEN_OUT
if (org->pop_champ_child)
cout<<" <<DUPLICATE OF CHAMPION>> ";
//Output to screen
cout<<"Org "<<(org->gnome)->genome_id<<" fitness: "<<org->fitness;
cout<<" ("<<(org->gnome)->genes.size();
cout<<" / "<<(org->gnome)->nodes.size()<<")";
cout<<" ";
if (org->mut_struct_baby) cout<<" [struct]";
if (org->mate_baby) cout<<" [mate]";
cout<<endl;
#endif
if ((!(thecart->generalization_test))&&(!(thecart->nmarkov_long)))
if (org->pop_champ_child) {
cout<<org->gnome<<endl;
//DEBUG CHECK
if (org->high_fit>org->fitness) {
cout<<"ALERT: ORGANISM DAMAGED"<<endl;
print_Genome_tofile(org->gnome,"failure_champ_genome");
cin>>pause;
}
}
//Decide if its a winner, in Markov Case
if (thecart->MARKOV) {
if (org->fitness>=(thecart->maxFitness-1)) {
org->winner=true;
return true;
}
else {
org->winner=false;
return false;
}
}
//if doing the long test non-markov
else if (thecart->nmarkov_long) {
if (org->fitness>=99999) {
//if (org->fitness>=9000) {
org->winner=true;
return true;
}
else {
org->winner=false;
return false;
}
}
else if (thecart->generalization_test) {
if (org->fitness>=999) {
org->winner=true;
return true;
}
else {
org->winner=false;
return false;
}
}
else {
org->winner=false;
return false; //Winners not decided here in non-Markov
}
}
CartPole::CartPole(bool randomize,bool velocity)
{
maxFitness = 100000;
MARKOV=velocity;
MIN_INC = 0.001;
POLE_INC = 0.05;
MASS_INC = 0.01;
LENGTH_2 = 0.05;
MASSPOLE_2 = 0.01;
// CartPole::reset() which is called here
}
//Faustino Gomez wrote this physics code using the differential equations from
//Alexis Weiland's paper and added the Runge-Kutta himself.
double CartPole::evalNet(Network *net,int thresh)
{
int steps=0;
double input[NUM_INPUTS];
double output;
int nmarkovmax;
double nmarkov_fitness;
double jiggletotal; //total jiggle in last 100
int count; //step counter
//init(randomize); // restart at some point
if (nmarkov_long) nmarkovmax=100000;
else if (generalization_test) nmarkovmax=1000;
else nmarkovmax=1000;
init(0);
if (MARKOV) {
while (steps++ < maxFitness) {
input[0] = state[0] / 4.8;
input[1] = state[1] /2;
input[2] = state[2] / 0.52;
input[3] = state[3] /2;
input[4] = state[4] / 0.52;
input[5] = state[5] /2;
input[6] = .5;
net->load_sensors(input);
//Activate the net
//If it loops, exit returning only fitness of 1 step
if (!(net->activate())) return 1.0;
output=(*(net->outputs.begin()))->activation;
performAction(output,steps);
if (outsideBounds()) // if failure
break; // stop it now
}
return (double) steps;
}
else { //NON MARKOV CASE
while (steps++ < nmarkovmax) {
//Do special parameter summing on last hundred
//if ((steps==900)&&(!nmarkov_long)) last_hundred=true;
/*
input[0] = state[0] / 4.8;
input[1] = 0.0;
input[2] = state[2] / 0.52;
input[3] = 0.0;
input[4] = state[4] / 0.52;
input[5] = 0.0;
input[6] = .5;
*/
//cout<<"nmarkov_long: "<<nmarkov_long<<endl;
//if (nmarkov_long)
//cout<<"step: "<<steps<<endl;
input[0] = state[0] / 4.8;
input[1] = state[2] / 0.52;
input[2] = state[4] / 0.52;
input[3] = .5;
net->load_sensors(input);
//cout<<"inputs: "<<input[0]<<" "<<input[1]<<" "<<input[2]<<" "<<input[3]<<endl;
//Activate the net
//If it loops, exit returning only fitness of 1 step
if (!(net->activate())) return 0.0001;
output=(*(net->outputs.begin()))->activation;
//cout<<"output: "<<output<<endl;
performAction(output,steps);
if (outsideBounds()) // if failure
break; // stop it now
if (nmarkov_long&&(outsideBounds())) // if failure
break; // stop it now
}
//If we are generalizing we just need to balance it a while
if (generalization_test)
return (double) balanced_sum;
//Sum last 100
if ((steps>100)&&(!nmarkov_long)) {
jiggletotal=0;
cout<<"step "<<steps-99-2<<" to step "<<steps-2<<endl;
//Adjust for array bounds and count
for (count=steps-99-2;count<=steps-2;count++)
jiggletotal+=jigglestep[count];
}
if (!nmarkov_long) {
if (balanced_sum>100)
nmarkov_fitness=((0.1*(((double) balanced_sum)/1000.0))+
(0.9*(0.75/(jiggletotal))));
else nmarkov_fitness=(0.1*(((double) balanced_sum)/1000.0));
#ifndef NO_SCREEN_OUTR
cout<<"Balanced: "<<balanced_sum<<" jiggle: "<<jiggletotal<<" ***"<<endl;
#endif
return nmarkov_fitness;
}
else return (double) steps;
}
}
void CartPole::init(bool randomize)
{
static int first_time = 1;
if (!MARKOV) {
//Clear all fitness records
cartpos_sum=0.0;
cartv_sum=0.0;
polepos_sum=0.0;