-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenetic.cpp
More file actions
134 lines (103 loc) · 3.25 KB
/
Copy pathgenetic.cpp
File metadata and controls
134 lines (103 loc) · 3.25 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
//Comment this line below if not running in Visual Studio
#include "stdafx.h"
using namespace std;
#include "genetic-algorithm.h"
#include "mutation-uniform.h"
#include "mutation-gaussian.h"
#include "mutation-vector.h"
#include "crossover-uniform-bitwise.h"
#include "examples.h"
#define FITNESS_CLOSENESS 0.99
#define OPT_MODE MODE_MAXIMIZE
#define OPTIMAL_FITNESS -38499.8
#define POPULATION_SIZE 250
#define ITERATION_SHOW 1000
#define ELITE_SIZE 25
#define CROSSOVER_PROB 0.7f
#define MUTATION_PROB 0.001f
#define ES_ENABLED true
#define ES_NOFFSPRING 50
#define ES_ELITEONLY false
#define IT_STOP_CROSSOVER 700
int main(){
GeneticAlgorithm ga(getGenotype7());
ga.setFitnessFunction(fitnessFunction7);
ga.setOptimizationMode(OPT_MODE);
ga.setElitism(true);
ga.setEliteSize(ELITE_SIZE);
CrossoverUniformBitwise cross;
ga.setCrossoverOperator(&cross);
ga.setCrossoverProbability(CROSSOVER_PROB);
MutationGaussian mutG(0.0, 0.01);
MutationUniform mutU;
vector<MutationVectorized *> mutvec;
mutvec.push_back(&mutU);
mutvec.push_back(&mutU);
mutvec.push_back(&mutU);
mutvec.push_back(&mutG);
mutvec.push_back(&mutG);
mutvec.push_back(&mutG);
mutvec.push_back(&mutG);
mutvec.push_back(&mutG);
mutvec.push_back(&mutG);
mutvec.push_back(&mutG);
MutationVector mut(mutvec);
ga.setMutationOperator(&mutU);
ga.enableMutation(true);
ga.setMutationProbability(MUTATION_PROB);
ga.initializePopulation(POPULATION_SIZE);
clock_t timeBegin = clock(); //Starting time
Chromosome *lastChm = new Chromosome(ga.getFittestChromosome());
uint64_t i, countEqual = 0;
bool eliteOnly = ES_ELITEONLY;
for (i = 0; i < 0xFFFFFFFF; i++) {
ga.calculateFitness();
if (i % ITERATION_SHOW == 0) {
cout << "Iteration " << i << endl;
cout << "Fittest chromosome:" << endl;
ga.printFittestChromosome();
cout << "Std Dev: " << mutG.getStdDev() << endl;
}
if (ga.getFittestChromosome()->getFitness() / OPTIMAL_FITNESS >= FITNESS_CLOSENESS
&&
ga.getFittestChromosome()->getFitness() / OPTIMAL_FITNESS <= 1.0 + (1.0 - FITNESS_CLOSENESS))
break;
//Last chromosome is not equals the best one
if (!ga.getFittestChromosome()->equals(lastChm)) {
delete lastChm;
lastChm = new Chromosome(ga.getFittestChromosome());
countEqual = 0;
}
else {
countEqual++;
}
if (countEqual >= 200){
countEqual = 0;
if(mutG.getStdDev()>=0.001)
mutG.setStdDev(mutG.getStdDev()/10.0);
}
if (i < IT_STOP_CROSSOVER) { //Stop crossover and let ES guide the population
ga.selectionRoulette();
ga.generateRouletteMatingPool();
ga.setMutationOperator(&mutU);
ga.crossOver();
}
if (ES_ENABLED) {
ga.setMutationOperator(&mut);
ga.evolutionStrategy(ES_NOFFSPRING, eliteOnly);
}
if (i == IT_STOP_CROSSOVER) {
ga.setPopulationSurvivalSize(ELITE_SIZE);
}
}
clock_t timeEnd = clock(); //Ending time
double timeSpent = (double)(timeEnd - timeBegin) / CLOCKS_PER_SEC;
cout << "-------------------------------" << endl << endl;
cout << endl << "Finished at iteration " << i << endl;
cout << "Elapsed time: " << timeSpent << "s" << endl;
cout << endl << "Final fittest chromosome: " << endl;
ga.calculateFitness();
ga.printFittestChromosome();
cout << endl;
return (int)ga.getFittestChromosome()->getFitness();
}