-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1593 lines (1330 loc) · 50.5 KB
/
main.cpp
File metadata and controls
1593 lines (1330 loc) · 50.5 KB
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 <iostream>
#include <errno.h>
#include <sys/types.h>
#include <fstream>
#include <unistd.h>
#include <sys/stat.h>
//random
#include "random.h"
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <sys/time.h>
//renders
#include "render.h"
#include "nullrender.h"
#include "simpledrawstuffrender.h"
//experiments
#include "experiment.h"
#include "testwheelsexp.h"
#include "testlightexp.h"
#include "testbluelightexp.h"
#include "testproxexp.h"
#include "testcontactexp.h"
#include "braitenbergvehicle2experiment.h"
#include "testevoexp.h"
#include "iri1exp.h"
#include "iri2exp.h"
#include "iri3exp.h"
#include "testgroundexp.h"
#include "testbatteryexp.h"
#include "testneuronexp.h"
#include "subsumptiongarbageexp.h"
#include "subsumptionlightexp.h"
#include "testcomexp.h"
#include "testswitchlightexp.h"
#include "testredlightexp.h"
#include "testswitchlightexp.h"
#include "testencoderexp.h"
#include "testcompassexp.h"
#include "ctrnnexp.h"
//genetic algorithms
#include "population.h"
#include "standardgapopulation.h"
#include "individual.h"
//fitnessfunctions
#include "fitnessfunction.h"
#include "avoidcollisionsfitnessfunction.h"
#include "garbagefitnessfunction.h"
#include "loadfitnessfunction.h"
#include "irifitnessfunction.h"
#include "lightfitnessfunction.h"
using namespace std;
/******************************************************************************/
/******************************************************************************/
/* EXPERIMENTS */
#define WHEELS_EXP 1
#define CONTACT_EXP 2
#define PROX_EXP 3
#define LIGHT_EXP 4
#define BLUE_LIGHT_EXP 5
#define RED_LIGHT_EXP 6
#define TEST_SWITCH_LIGHT 7
#define GROUND_EXP 8
#define BATTERY_EXP 9
#define ENCODER_EXP 10
#define COMPASS_EXP 11
//#define TEST_COM 10
#define BRAITENBERG_VEHICLE2 20
#define NEURON_EXP 21
#define SUBSUMPTION_LIGHT 22
#define SUBSUMPTION_GARBAGE 23
#define CTRNN_EXP 24
//#define TEST_EVO 22
#define IRI1 30
#define IRI2 31
#define IRI3 32
/******************************************************************************/
/******************************************************************************/
#define POPULATION_STANDARD 0
#define FITNESSFUNCTION_NONE 0
#define FITNESSFUNCTION_AVOID 1
#define FITNESSFUNCTION_GARBAGE 2
#define FITNESSFUNCTION_LOAD 3
#define FITNESSFUNCTION_IRI 4
#define FITNESSFUNCTION_LIGHT 5
#define AVERAGE_FITNESS 0
#define BEST_FITNESS 1
#define WORST_FITNESS 2
//filenames
#define FILENAME_MAXGENERATION "maxgeneration"
#define FILENAME_GENERATION "generation%d.log"
#define FILENAME_BESTCHROMOSOME "best%d.log"
#define FILENAME_CURRENTBESTCHROMOSOME "currentbest"
#define FILENAME_FITNESSLOG "fitness.log"
/******************************************************************************/
/******************************************************************************/
/* FUNCTIONS */
void SetUpExperiment ( unsigned int exp ); // Sets up the experiment
void usage ( void ); //Prints simulator command line options
void printExpId ( void ); //Prints the experiment identifiers
void init_rng ( gsl_rng** RNG ); // Init random chain
void del_rng ( gsl_rng* RNG ); // Del random chain
void set_seed_rng ( gsl_rng* RNG , long int seed ); // Set random seed
void RunEvolutionaryAlgorithm ( void ); // Run evolution
CPopulation* GetPopulation ( void ); // Get Population
unsigned int GetNumberOfLastGeneration ( void ); // Get last Generation
void LoadGeneration(CPopulation* pc_population, unsigned int un_generation); // Load Generation
double EvaluateIndividual(double* pf_chromosome, unsigned int un_chromosome_length); // Evaluate an individual
CFitnessFunction* GetFitnessFunction(int n_number, CSimulator* pc_simulator); // Get fitness function
void GetGeneticParameters ( const char* param_file );
/******************************************************************************/
/******************************************************************************/
/* GLOBAL VARIABLES */
char* filename = NULL; // Input Filename
char* chromosomefilename = NULL; // Chromosome Input Filename
unsigned int g_unExperiment = 0; // Experiment
gsl_rng* rng; // Random variable
long int rngSeed; // Random seed
CRender* g_pcRender = NULL; // Render object
CExperiment* g_pcExperiment = NULL; // Experiment object
CSimulator* g_pcSimulator = NULL; // Simulator object
/* Flags */
bool g_bProduceFrames = false; // Write frames on HD flagi
/* Evolutionary */
unsigned int g_unFitnessFunction = FITNESSFUNCTION_NONE; // No Fitness function
unsigned int g_unUseFitnessFromSample = AVERAGE_FITNESS; // Fitness to use (default AVERAGE)
float g_fFitnessLimit = 0; // Max fitness allowed
int g_nFitnessStagnationLimit = 0; // Number of generations allowed without improvement (0 -> All, other --> number)
unsigned int g_unFitnessLimitGenerations = 0; // Max number of generations with max fitness allowed
unsigned int g_unPopulation = POPULATION_STANDARD; // Population
int g_nRestartFromGeneration = -1; // Number to generation to be restarted. -1 None, any other number the generation
unsigned int g_unNumberOfSamplesPerChromosome = 1; // Number of evaluations per Chromosome (default to 1)
unsigned int g_unDeleteSomePopulations = 25; //each g_unDeleteSomePopulations generations the data of current one are written to file (default 25)
char g_pchDirectory[256] = "geneticDataFiles"; // Genetic dir
unsigned int g_unRandomPositionOrientation = 0; // if 1, it Orders the experiment to create random position and orientation
unsigned int g_unChromosomeLength = 0; // Lenght of a Chromosome. Should be given by the experiment. ( default 0)
unsigned int g_unChromosomes = 100; // Number of Individuals (default to 100)
unsigned int g_unGenerations = 100000; // Number of generations (default to 100)
double g_fEvaluationTime = 100; // Evaluation time limit (default to 100)
double g_fRunTime = 10000; // Evaluation time limit (default to 100)
bool g_bIsCrossoverOn = true; // Crossover flag
int g_nNumberOfCrossovers = 1; // Number of Crossovers
int g_nCrossoverDistance = 1; // Crossover Distance
double g_fMutationRate = 0.05; // Mutation rate
int g_nNumberOfElites = 5; // Number of Elites
double g_fUpperBounds = 1.0; // Upper Weight Limit
double g_fLowerBounds = 0.0; // Lower Weight Limit
bool g_bEvolutionaryExperiment = false; // Evolutionary experiment flag
bool g_bRestartEvolution = false; // Restart evolution flag
bool g_bRandomizeFirstGeneration = false; // First Generation random flag
bool g_bWriteToFiles = true; // Write to files flag
bool g_bNNParameters = false;
bool g_bVisual = true;
/******************************************************************************/
/******************************************************************************/
int main(int argc, char** argv)
{
//srand(1357911);
for(int i=1; i<argc; i++) {
if(argv[i][0] != '-') {
usage();
exit(-1);
}
switch(argv[i][1]) {
/* Evolutionary Experiment */
case 'e':
g_bEvolutionaryExperiment=true;
printf("Evolutionary mode selected\n");
break;
/* Set seed */
case 's':
++i; // go to seed value
Random::seed = atoi(argv[i]);
printf("Set the seed: %d\n",Random::seed);
break;
/* Produce frames */
case 'f':
g_bProduceFrames=true;
printf("Frames will be produced in frames-directory\n");
break;
/* Set Experiment */
case 'E':
if(i==argc-1){
printExpId();
exit(0);
}
i++;
if(argv[i][0] == '-'){
printExpId();
exit(0);
}
g_unExperiment=atoi(argv[i]);
printf("EXPERIMENT: %d CHOOSEN.\n",g_unExperiment);
sleep(1);
break;
/* Get Input File */
case 'p':
++i;
filename = argv[i];
printf("filename: %s CHOOSEN.\n",filename);
break;
/* Get Input File */
case 'c':
++i;
chromosomefilename = argv[i];
printf("chromosomefilename: %s CHOOSEN.\n",chromosomefilename);
break;
case 'l':
g_bNNParameters = true;
printf("Create NN Parameters\n");
break;
case 'v':
g_bVisual = false;
printf("No visual\n");
break;
/* Print Help */
case 'h':
usage();
exit(0);
break;
}//switch on current argument
}//loop on command line arguments
/* Evolutionary Experiment */
if ( g_bEvolutionaryExperiment )
{
/* Set up experiment */
SetUpExperiment(g_unExperiment);
/* Run evolution */
RunEvolutionaryAlgorithm();
}
/* Standard Experiment */
else
{
/* Set up experiment */
SetUpExperiment(g_unExperiment);
/* Select Arena */
int ArenaType = g_pcExperiment->GetSimulator()->GetArena()->GetArenaType();
/* Set if frames to be produce */
g_pcRender->SetProduceFrames(g_bProduceFrames);
/* Start simulation */
g_pcRender->Start();
}
}
/******************************************************************************/
/******************************************************************************/
void SetUpExperiment(unsigned int exp){
switch (exp)
{
case WHEELS_EXP:
printf("TEST WHEELS EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CTestWheelsExp("Wheels Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case CONTACT_EXP:
printf("TEST CONTACT EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CTestContactExp("Contact Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case LIGHT_EXP:
printf("TEST LIGHT EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CTestLightExp("Light Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case BLUE_LIGHT_EXP:
printf("TEST BLUE EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CTestBlueLightExp("Blue Light Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case RED_LIGHT_EXP:
printf("TEST RED EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CTestRedLightExp("Red Light Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case PROX_EXP:
printf("TEST PROX EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CTestProxExp("Prox Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case ENCODER_EXP:
printf("TEST ENCODER EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CTestEncoderExp("Encoder Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case COMPASS_EXP:
printf("TEST COMPASS EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CTestCompassExp("Compass Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case BRAITENBERG_VEHICLE2:
printf("BRAITENBERG VEHICLE 2 \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CBraitenbergVehicle2Experiment("Braitenberg Vehicle 2", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case GROUND_EXP:
printf("TEST GROUND EXPERIMENT STARTED\n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CTestGroundExp("Ground Exp", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case BATTERY_EXP:
printf("TEST BATTERY EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CTestBatteryExp("Battery Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
//case TEST_EVO:
//printf("TEST EVOLUTIONARY ROBOTICS \n");
//init_rng(&rng);
///* Evolutionary Part */
//g_unChromosomeLength = 18;
//g_unFitnessFunction = FITNESSFUNCTION_LIGHT;
///* Create new experiment */
//g_pcExperiment = new CTestEvoExp("Evo Exp", filename);
///* Create new simulation */
//g_pcSimulator = g_pcExperiment->CreateSimulator();
///* Add experiment to simulation */
//g_pcSimulator->AddChild(g_pcExperiment);
///* If evolutionary, evol */
//if(g_bEvolutionaryExperiment){
//g_pcRender=new CNullRender(g_pcSimulator);
//g_pcSimulator->SetTimeLimit(g_fEvaluationTime);
//}
///* If not, load weights and create render */
//else{
//char inputFile[128];
///* If not chromosome file given */
//if ( chromosomefilename == NULL)
//{
//sprintf(inputFile, "%s/" FILENAME_CURRENTBESTCHROMOSOME, g_pchDirectory);
////sprintf(inputFile, "%s/best0.log", g_pchDirectory);
//}
///* If chromosome file given */
//else
//{
//sprintf(inputFile,"%s",chromosomefilename);
//}
//ifstream in(inputFile);
//in >> g_unChromosomeLength;
//double* pfChromosome;
//pfChromosome = new double[g_unChromosomeLength];
//for ( int i = 0 ; i < g_unChromosomeLength ; i++ )
//{
//in >> pfChromosome[i];
//}
//g_pcExperiment->SetChromosome(pfChromosome, g_unChromosomeLength);
//g_pcExperiment->Reset();
//g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
//}
//break;
case NEURON_EXP:
printf("TEST NEURON EXP \n");
init_rng(&rng);
/* Get genetic Parameters */
GetGeneticParameters ( filename );
/* Create new experiment */
g_pcExperiment = new CTestNeuronExp("Neuron Exp", filename, g_unChromosomeLength, g_unFitnessFunction, g_fEvaluationTime, g_fUpperBounds, g_fLowerBounds, g_bEvolutionaryExperiment, g_bNNParameters);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* If evolutionary, evol */
if(g_bEvolutionaryExperiment)
{
//g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
g_pcRender=new CNullRender(g_pcSimulator);
g_pcSimulator->SetTimeLimit(g_fEvaluationTime);
}
/* If not genetic and nor learning, load weights and create render */
else
{
//g_pcSimulator->SetTimeLimit(g_fRunTime);
//if ( g_bNNParameters )
//{
//FILE* fileLearning = fopen("learningFiles/initFile","a");
//fprintf(fileLearning,"%d ",g_unChromosomeLength);
//fclose(fileLearning);
//printf("HOLA\n");
//fflush(stdout);
//}
char inputFile[128];
/* If not chromosome file given */
if ( chromosomefilename == NULL)
{
sprintf(inputFile, "%s/" FILENAME_CURRENTBESTCHROMOSOME, g_pchDirectory);
//sprintf(inputFile, "%s/best0.log", g_pchDirectory);
}
/* If chromosome file given */
else
{
sprintf(inputFile,"%s",chromosomefilename);
}
ifstream in(inputFile);
in >> g_unChromosomeLength;
double* pfChromosome;
pfChromosome = new double[g_unChromosomeLength];
for ( int i = 0 ; i < g_unChromosomeLength ; i++ )
{
in >> pfChromosome[i];
}
g_pcExperiment->SetChromosome(pfChromosome, g_unChromosomeLength);
g_pcExperiment->Reset();
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
}
break;
case CTRNN_EXP:
printf("TEST NEURON EXP \n");
init_rng(&rng);
/* Get genetic Parameters */
GetGeneticParameters ( filename );
/* Create new experiment */
g_pcExperiment = new CCTRNNExp("Neuron Exp", filename, g_unChromosomeLength, g_unFitnessFunction, g_fEvaluationTime, g_fUpperBounds, g_fLowerBounds, g_bEvolutionaryExperiment, g_bNNParameters);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* If evolutionary, evol */
if(g_bEvolutionaryExperiment)
{
//g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
g_pcRender=new CNullRender(g_pcSimulator);
g_pcSimulator->SetTimeLimit(g_fEvaluationTime);
}
/* If not genetic and nor learning, load weights and create render */
else
{
//g_pcSimulator->SetTimeLimit(g_fRunTime);
//if ( g_bNNParameters )
//{
//FILE* fileLearning = fopen("learningFiles/initFile","a");
//fprintf(fileLearning,"%d ",g_unChromosomeLength);
//fclose(fileLearning);
//printf("HOLA\n");
//fflush(stdout);
//}
char inputFile[128];
/* If not chromosome file given */
if ( chromosomefilename == NULL)
{
sprintf(inputFile, "%s/" FILENAME_CURRENTBESTCHROMOSOME, g_pchDirectory);
//sprintf(inputFile, "%s/best0.log", g_pchDirectory);
}
/* If chromosome file given */
else
{
sprintf(inputFile,"%s",chromosomefilename);
}
ifstream in(inputFile);
in >> g_unChromosomeLength;
double* pfChromosome;
pfChromosome = new double[g_unChromosomeLength];
for ( int i = 0 ; i < g_unChromosomeLength ; i++ )
{
in >> pfChromosome[i];
}
g_pcExperiment->SetChromosome(pfChromosome, g_unChromosomeLength);
g_pcExperiment->Reset();
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
}
break;
case IRI1:
printf("TEST IRI 1 EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CIri1Exp("Iri1 Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case IRI2:
printf("TEST IRI 2 EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CIri2Exp("Iri2 Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case IRI3:
printf("TEST IRI 3 EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CIri3Exp("Iri3 Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case SUBSUMPTION_GARBAGE:
printf("SUBSUMPTION GARBAGE EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CSubsumptionGarbageExp("Subsumption Garbage Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
case SUBSUMPTION_LIGHT:
printf("SUBSUMPTION LIGHT EXPERIMENT STARTED \n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CSubsumptionLightExp("Subsumption Light Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
//case TEST_COM:
//printf("TEST COM EXPERIMENT STARTED\n");
//init_rng(&rng);
///* Create new experiment */
//g_pcExperiment = new CTestComExp("Test Com Experiment", filename);
///* Create new simulation */
//g_pcSimulator = g_pcExperiment->CreateSimulator();
///* Add experiment to simulation */
//g_pcSimulator->AddChild(g_pcExperiment);
///* Create render */
//if ( g_bVisual == true )
//g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
//else
//g_pcRender=new CNullRender(g_pcSimulator);
//break;
case TEST_SWITCH_LIGHT:
printf("TEST SWITCH LIGHT EXPERIMENT STARTED\n");
init_rng(&rng);
/* Create new experiment */
g_pcExperiment = new CTestSwitchLightExp("Test Switch Light Experiment", filename);
/* Create new simulation */
g_pcSimulator = g_pcExperiment->CreateSimulator();
/* Add experiment to simulation */
g_pcSimulator->AddChild(g_pcExperiment);
/* Create render */
if ( g_bVisual == true )
g_pcRender=new CSimpleDrawStuffRender(g_pcExperiment->GetSimulator(),0,NULL);
else
g_pcRender=new CNullRender(g_pcSimulator);
break;
/* No valid experiment */
default:
printf("SIMULATOR ERROR, UNKNOWN EXPERIMENT: %d\n",exp);
exit(1);
break;
}
}
/******************************************************************************/
/******************************************************************************/
void RunEvolutionaryAlgorithm()
{
time_t tStartTime;
time(&tStartTime);
unsigned int unGenerationWithBestFitness = 0;
double fBestFitnessRecorded = -10000000;
unsigned int unEvaluations = 0;
char pchTerminationReason[1024] = "Error or user interruption";
if (g_unFitnessFunction == FITNESSFUNCTION_NONE)
{
printf("You are trying to run an evolution without using a fitness function\n");
fflush(stdout);
exit(-1);
}
double fGlobalFitness = 0;
double fBestFitness = 0;
double fWorstFitness = 0;
double fAverageFitness = 0;
int nStartGeneration = 0;
CPopulation* pcPopulation = GetPopulation();
if (g_bRestartEvolution)
{
g_nRestartFromGeneration = GetNumberOfLastGeneration();
}
// Load a generation from file if necessary:
if (g_nRestartFromGeneration >= 0)
{
LoadGeneration(pcPopulation, g_nRestartFromGeneration);
pcPopulation->GetGenerationStats(&fWorstFitness, &fAverageFitness, &fBestFitness);
fGlobalFitness = fBestFitness;
nStartGeneration = g_nRestartFromGeneration + 1;
}
//if (g_bWriteToFiles)
//{
//WriteGNUPlotFile();
//}
unsigned int unConsecutiveFitnessLimitGenerations = 0;
unsigned int unCurrentGeneration;
double* pfChromosome;
unsigned int unChromosomeLength;
void* pvIndividual;
for (unCurrentGeneration = nStartGeneration;
unCurrentGeneration < g_unGenerations;
unCurrentGeneration++)
{
/* Check if GENERATION is STAGNATED */
if (g_nFitnessStagnationLimit > 0 && unCurrentGeneration != nStartGeneration &&
unGenerationWithBestFitness + g_nFitnessStagnationLimit < unCurrentGeneration)
{
sprintf(pchTerminationReason, "Fitness stagnated and has not improved for %d generations. Stopping evolution.\n", g_nFitnessStagnationLimit);
break;
}
/* Check if FITNESS reached its LIMIT */
if (fGlobalFitness >= g_fFitnessLimit )
{
unConsecutiveFitnessLimitGenerations++;
if (unConsecutiveFitnessLimitGenerations >= g_unFitnessLimitGenerations && g_unFitnessLimitGenerations > 0)
{
sprintf(pchTerminationReason, "Fitness limit (%.5f) exceeded %.5f by generation %d", g_fFitnessLimit, fGlobalFitness, unCurrentGeneration-1);
break;
}
}
else {
unConsecutiveFitnessLimitGenerations = 0;
}
/* Check if GENERATION must be RANDOMIZED */
if (unCurrentGeneration == 0 || (g_bRandomizeFirstGeneration && unCurrentGeneration == nStartGeneration))
{
printf("Creating random generation...\n");
pcPopulation->CreateRandomPopulation(g_unChromosomes);
}
else {
pcPopulation->CreateNextGeneration();
}
/* reset POPULATION */
pfChromosome = NULL;
unChromosomeLength = 0;
pvIndividual = NULL;
// Let the experiment know the current generation and fitness
// such that the arena, sensors, etc. can be modified accordingly.
// Useful if the experiment relies on incremental evolution:
g_pcExperiment->SetGeneration(unCurrentGeneration);
g_pcExperiment->SetFitness(fGlobalFitness);
//printf("Calling initialize generation method, evolutionary algorithm already started\n");
g_pcExperiment->InitializeGeneration();
int unCurrentChromosome = 0;
while (pcPopulation->GetNextChromosomeToEvaluate(&pvIndividual,
&pfChromosome,
&unChromosomeLength))
{
time_t tTimeUsed;
time(&tTimeUsed);
tTimeUsed -= tStartTime;
// CoolPrompt(unCurrentGeneration, g_unGenerations, unCurrentChromosome, pcPopulation->GetNumberOfChromosomes(), unEvaluations, tTimeUsed);
double fAccumulatedFitness = 0;
double fBestSampleFitness = -1e10;
double fWorstSampleFitness = 1e10;
for (int nSample = 0; nSample < g_unNumberOfSamplesPerChromosome; nSample++)
{
//printf("SAMPLE: %d\n", nSample);
/* if (g_bUseMultipleExperiments)
{
SetNextExperimentInMultipleExperimentList(nSample);
g_pcExperiment->SetGeneration(unCurrentGeneration);
g_pcExperiment->SetFitness(fGlobalFitness);
}
*/
// printf("Setting experiment sample number: %d\n",nSample);
g_pcExperiment->SetSampleNumber(nSample);
//printf("NEW SAMPLE NEW SAMPLE NEW SAMPLE: %d ###########################################\n",nSample);
//fflush(stdout);
//printf("Evaluating chromosome: %d\n",unCurrentChromosome);
double fThisSampleFitness = EvaluateIndividual(pfChromosome, unChromosomeLength);
//printf("IN MAIN Eval result: %f ############################################\n",fThisSampleFitness);
// sleep(1);
fAccumulatedFitness += fThisSampleFitness;
if (fThisSampleFitness < fWorstSampleFitness){
fWorstSampleFitness = fThisSampleFitness;
}
if (fThisSampleFitness > fBestSampleFitness){
fBestSampleFitness = fThisSampleFitness;
}
}
//printf("Number of samples for this chromosome has reached max, next one\n");
// Set the result depending on if we should use the best, average or worst fitess among the samples:
if (g_unUseFitnessFromSample == AVERAGE_FITNESS)
pcPopulation->SetEvaluationResult(pvIndividual, fAccumulatedFitness / g_unNumberOfSamplesPerChromosome);
else if (g_unUseFitnessFromSample == BEST_FITNESS)
pcPopulation->SetEvaluationResult(pvIndividual, fBestSampleFitness);
else
pcPopulation->SetEvaluationResult(pvIndividual, fWorstSampleFitness);
unCurrentChromosome++;
unEvaluations++;
//printf("End evaluation, fitness obtained: %f\n",fAccumulatedFitness/g_unNumberOfSamplesPerChromosome);
//sleep(2);
}
double fTemp;
double fPreviousGenerationsFitness = fGlobalFitness;
pcPopulation->GetGenerationStats(&fWorstFitness, &fAverageFitness, &fBestFitness);
fGlobalFitness = fBestFitness;
printf("Generation %d - best fitness: %f, average: %f, worst: %f \n",
unCurrentGeneration, fBestFitness, fAverageFitness, fWorstFitness);
if (fGlobalFitness > fBestFitnessRecorded)
{
unGenerationWithBestFitness = unCurrentGeneration;
fBestFitnessRecorded = fGlobalFitness;
}