-
Notifications
You must be signed in to change notification settings - Fork 23
/
GeneticAlgorithm.h
325 lines (264 loc) · 7.42 KB
/
GeneticAlgorithm.h
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
#ifndef GENETICALGORITHM_H_INCLUDED
#define GENETICALGORITHM_H_INCLUDED
#include "Chromosome.h"
class GeneticAlgorithm {
private:
std::vector<Chromosome> population;
int resultIndex;
int firstIndex;
int secondIndex;
int bestIndex;
int worstIndex;
void selectRandom() {
do {
resultIndex = rand() % population.size();
firstIndex = rand() % population.size();
secondIndex = rand() % population.size();
} while(resultIndex==firstIndex || resultIndex==secondIndex || (resultIndex == bestIndex && KEEP_ELITE==true));
}
friend std::ostream& operator<< (std::ostream &out, const GeneticAlgorithm &ga);
public:
static const bool KEEP_ELITE = true;
public:
GeneticAlgorithm(int populationSize=0) {
if(populationSize < 0) {
populationSize = 0;
}
population.resize(populationSize);
resultIndex = 0;
firstIndex = 0;
secondIndex = 0;
bestIndex = 0;
worstIndex = 0;
}
GeneticAlgorithm(const GeneticAlgorithm &ga) {
(*this) = ga;
}
int getResultIndex() {
return( resultIndex );
}
int getBestIndex() {
return( bestIndex );
}
void setChromosome(Chromosome chromosome, int index=-1) {
if(index < -1) {
return;
}
if(index == -1) {
population.push_back( chromosome );
index = population.size() - 1;
} else if(index < population.size()) {
population[index] = chromosome;
}
if(population[index].fitness < population[bestIndex].fitness) {
bestIndex = index;
}
if(population[index].fitness > population[worstIndex].fitness) {
worstIndex = index;
}
}
const Chromosome& getChromosome(int index) const {
if(population.size() <= index || index <= -1) {
//TODO Handle exception.
}
return( population[index] );
}
const Chromosome& getBestChromosome() const {
return( population[bestIndex] );
}
const Chromosome& getRandomChromosome() const {
return( population[rand()%population.size()] );
}
const Chromosome& getWorstChromosome() const {
return( population[worstIndex] );
}
void replaceWorst(const Chromosome& chromosome) {
population[worstIndex] = chromosome;
bestIndex = 0;
worstIndex = 0;
for(int i=0; i<population.size(); i++) {
if(population[i].fitness < population[bestIndex].fitness) {
bestIndex = i;
}
if(population[i].fitness > population[worstIndex].fitness) {
worstIndex = i;
}
}
}
void setFitness(double fitness, int index=-1) {
if(index == -1) {
index = population.size()-1;
}
if(population.size() <= index) {
//TODO Handle exception.
return;
}
population[index].fitness = fitness;
if(fitness < population[bestIndex].fitness) {
bestIndex = index;
}
if(fitness > population[worstIndex].fitness) {
worstIndex = index;
}
}
double getFitness(int index) {
if(population.size() <= index || index <= -1) {
//TODO Handle exception.
return( INVALID_FITNESS_VALUE );
}
return( population[index].fitness );
}
double getBestFitness() const {
return( population[bestIndex].fitness );
}
int size() {
return( population.size() );
}
void subset(GeneticAlgorithm &ga, const int size) const {
if(population.size() <= 0) {
return;
}
for(int i=0; i<size; i++) {
ga.setChromosome( population[ rand()%population.size() ] );
}
}
void selection() {
static const int CROSSOVER_RESULT_INTO_BEST_PERCENT = 1;
static const int CROSSOVER_RESULT_INTO_MIDDLE_PERCENT = 9;
static const int CROSSOVER_RESULT_INTO_WORST_PERCENT = 90;
static int percent = -1;
percent = rand()
% (CROSSOVER_RESULT_INTO_WORST_PERCENT
+ CROSSOVER_RESULT_INTO_MIDDLE_PERCENT
+ CROSSOVER_RESULT_INTO_BEST_PERCENT);
if (percent < CROSSOVER_RESULT_INTO_WORST_PERCENT) {
do {
selectRandom();
} while (population[resultIndex].fitness < population[firstIndex].fitness
|| population[resultIndex].fitness < population[secondIndex].fitness);
} else if (percent
< (CROSSOVER_RESULT_INTO_WORST_PERCENT
+ CROSSOVER_RESULT_INTO_MIDDLE_PERCENT)) {
do {
selectRandom();
} while (population[resultIndex].fitness < population[firstIndex].fitness
|| population[resultIndex].fitness > population[secondIndex].fitness);
} else if (percent
< (CROSSOVER_RESULT_INTO_WORST_PERCENT
+ CROSSOVER_RESULT_INTO_MIDDLE_PERCENT
+ CROSSOVER_RESULT_INTO_BEST_PERCENT)) {
do {
selectRandom();
} while (population[resultIndex].fitness > population[firstIndex].fitness
|| population[resultIndex].fitness > population[secondIndex].fitness);
}
}
void crossover() {
std::vector<std::vector<int> > &a = population[firstIndex].reels;
std::vector<std::vector<int> > &b = population[secondIndex].reels;
std::vector<std::vector<int> > &c = population[resultIndex].reels;
for(int i=0; i<a.size() && i<b.size() && i<c.size(); i++) {
for(int j=0; j<a[i].size() && j<b[i].size() && i<c[i].size(); j++) {
if(rand() % 2 == 0) {
c[i][j] = a[i][j];
} else {
c[i][j] = b[i][j];
}
}
}
population[resultIndex].fitness = INVALID_FITNESS_VALUE;
}
void mutation() {
int index = rand() % population.size();
int i = rand() % population[resultIndex].reels.size();
int j = rand() % population[resultIndex].reels[i].size();
population[resultIndex].reels[i][j] = population[index].reels[i][j];
population[resultIndex].fitness = INVALID_FITNESS_VALUE;
}
const std::string& toString() {
static std::string result;
result = "";
/*
* Keep population size.
*/
result += std::to_string(population.size());
result += " ";
for(int p=0; p<population.size(); p++) {
result += std::to_string(population[p].fitness);
result += " ";
result += std::to_string(population[p].reels.size());
result += " ";
for(int i=0; i<population[p].reels.size(); i++) {
result +=std::to_string( population[p].reels[i].size());
result += " ";
for(int j=0; j<population[p].reels[i].size(); j++) {
result += std::to_string(population[p].reels[i][j]);
result += " ";
}
}
result += " ";
}
/*
* Trim spaces.
*/
result.erase(result.size()-1, 1);
result += '\0';
return result;
}
void fromString(const char text[]) {
std::string buffer(text);
std::istringstream in(buffer);
population.clear();
bestIndex = 0;
worstIndex = 0;
int size = 0;
in >> size;
for(int p=0; p<size; p++) {
double fitness;
in >> fitness;
int width, height;
std::vector<std::vector<int> > reels;
in >> width;
reels.resize(width);
for(int i=0; i<width; i++) {
in >> height;
reels[i].resize(height);
for(int j=0; j<height; j++) {
in >> reels[i][j];
}
}
setChromosome(Chromosome(reels,fitness));
if(population[bestIndex].fitness > population[p].fitness) {
bestIndex = p;
}
if(population[worstIndex].fitness < population[p].fitness) {
worstIndex = p;
}
}
}
void operator=(const GeneticAlgorithm &ga) {
this->population.clear();
this->population = ga.population;
this->resultIndex = ga.resultIndex;
this->firstIndex = ga.firstIndex;
this->secondIndex = ga.secondIndex;
this->bestIndex = ga.bestIndex;
this->worstIndex = ga.worstIndex;
}
};
std::ostream& operator<< (std::ostream &out, const GeneticAlgorithm &ga) {
for(int p=0; p<ga.population.size(); p++) {
out << ga.population[p].fitness;
out << std::endl;
for(int i=0; i<ga.population[p].reels.size(); i++) {
for(int j=0; j<ga.population[p].reels[i].size(); j++) {
out << ga.population[p].reels[i][j];
out << " ";
}
out << std::endl;
}
out << std::endl;
}
return out;
}
#endif