-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryGASolver.cpp
More file actions
371 lines (310 loc) · 11.1 KB
/
BinaryGASolver.cpp
File metadata and controls
371 lines (310 loc) · 11.1 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
#include "BinaryGASolver.h"
#include <algorithm>
#include <unordered_set>
#include "Common.h"
namespace BinaryGA
{
template<typename T>
struct Chromosome
{
std::vector<T> genes;
uint32_t age = 0;
double fitness = 0.0;
};
template<typename T>
using Population = std::vector<Chromosome<T>>;
template<typename T>
void Mutate(Chromosome<T> & chromosome, double probability, MutationType type, const CustomMutation<T> & custom);
template<typename T>
Population<T> InitializePopulation(const Definition<T> & definition)
{
std::vector<Chromosome<T>> population;
for (size_t i = 0; i < definition.populationSize; ++i)
{
Chromosome<T> chromosome;
if (definition.initializationCustomCallback)
{
chromosome.genes = definition.initializationCustomCallback(i);
}
else
{
chromosome.genes.resize(definition.numberOfGenes);
Mutate(chromosome, 0.5, definition.mutation, definition.mutationCustomCallback);
}
population.push_back(std::move(chromosome));
}
return population;
}
template<typename T>
bool CheckTerminationCondition(uint32_t generationNumber, const Definition<T> & definition, const Population<T> & population, std::vector<T> & ret)
{
for (const auto & chromosome : population)
{
if (definition.evaluate(generationNumber, chromosome.genes) == EvaluationResult::ObjectiveReached)
{
ret = chromosome.genes;
return true;
}
}
if (generationNumber >= definition.maxNumberOfGenerations)
return true;
return false;
}
// SELECTION //////////////////////////////////////////////////////////////
template<class T>
std::vector<const T*> Select(const std::vector<T> & population, size_t number, std::function<double(const T&)> fitness)
{
// http://www.obitko.com/tutorials/genetic-algorithms/selection.php
// [Sum] Calculate sum of all chromosome fitnesses in population - sum S.
// [Select] Generate random number from interval(0, S) - r.
// [Loop] Go through the population and sum fitnesses from 0 - sum s.
// When the sum s is greater then r, stop and return the chromosome where you are.
double sumOfFitnesses = 0.0;
for (const T & chromosome : population)
sumOfFitnesses += fitness(chromosome);
std::vector<const T*> ret;
for (size_t i = 0; i < number; ++i)
{
double partialSum = Common::Frand(0.0, sumOfFitnesses);
double fitnessSum = 0.0;
for (const T & chromosome : population)
{
fitnessSum += fitness(chromosome);
if (fitnessSum >= partialSum)
{
ret.push_back(&chromosome);
break;
}
}
}
return ret;
}
template<typename T>
std::vector<const Chromosome<T>*> RouletteWheelSelection(const std::vector<Chromosome<T>> & population, size_t number)
{
return Select<Chromosome<T>>(population, number, [](const Chromosome<T> & c) { return c.fitness; });
}
template<typename T>
std::vector<const Chromosome<T>*> RankSelection(const std::vector<Chromosome<T>> & population, size_t number)
{
// rank
std::vector<std::pair<size_t, const Chromosome<T>*>> rankedPopulation;
for (const auto & chromosome : population)
rankedPopulation.push_back({ 0, &chromosome });
std::sort(std::begin(rankedPopulation), std::end(rankedPopulation),
[](const auto & a, const auto & b) { return a < b; });
for (size_t i = 0; i < rankedPopulation.size(); ++i)
rankedPopulation[i].first = i + 1;
// shuffle
std::random_shuffle(std::begin(rankedPopulation), std::end(rankedPopulation));
// TODO sum of fitnesses is equal to sum of first population.size() numbers
std::vector<const std::pair<size_t, const Chromosome<T>*>*> selectedChromosomes;
selectedChromosomes = Select<std::pair<size_t, const Chromosome<T>*>>(rankedPopulation, number,
[](const std::pair<size_t, const Chromosome<T>*> & c) { return c.first; });
std::vector<const Chromosome<T>*> ret;
for (const auto & chromosome : selectedChromosomes)
ret.push_back(chromosome->second);
return ret;
}
template<typename T>
std::vector<const Chromosome<T>*> SelectParent(size_t crossoverSize, Population<T> & population, const Definition<T> & definition)
{
switch (definition.parentSelection)
{
case ParentSelectionType::Ranked:
return RankSelection(population, crossoverSize);
case ParentSelectionType::RouletteWheel:
return RouletteWheelSelection(population, crossoverSize);
}
return std::vector<const Chromosome<T>*>();
}
///////////////////////////////////////////////////////////////////////////
// CROSSOVER //////////////////////////////////////////////////////////////
template<typename T>
std::vector<Chromosome<T>> OnePointCrossover(const Chromosome<T> & first, const Chromosome<T> & second)
{
const size_t numberOfGenes = first.genes.size();
size_t point = rand() % numberOfGenes;
std::vector<Chromosome<T>> ret(2);
for (size_t i = 0; i < numberOfGenes; ++i)
{
ret[0].genes.push_back(i < point ? first.genes[i] : second.genes[i]);
ret[1].genes.push_back(i < point ? second.genes[i] : first.genes[i]);
}
return ret;
}
template<typename T>
Chromosome<T> OrderedCrossoverCreateChild(size_t point, const Chromosome<T> & first, const Chromosome<T> & second)
{
const size_t numberOfGenes = first.genes.size();
std::unordered_set<T> inserted;
Chromosome<T> ret;
for (size_t i = 0; i < point; ++i)
{
ret.genes.push_back(first.genes[i]);
inserted.insert(first.genes[i]);
}
for (size_t i = 0; i < numberOfGenes; ++i)
{
if (inserted.find(second.genes[i]) == std::end(inserted))
ret.genes.push_back(second.genes[i]);
}
return ret;
}
template<typename T>
std::vector<Chromosome<T>> OrderedCrossover(const Chromosome<T> & first, const Chromosome<T> & second)
{
const size_t numberOfGenes = first.genes.size();
size_t point = rand() % numberOfGenes;
std::vector<Chromosome<T>> ret{
OrderedCrossoverCreateChild(point, first, second),
OrderedCrossoverCreateChild(point, first, second)
};
return ret;
}
template<typename T>
std::vector<Chromosome<T>> Crossover(const Chromosome<T> & first, const Chromosome<T> & second, const Definition<T> & definition)
{
if (definition.crossover == CrossoverType::None)
return { first, second };
switch (definition.crossover)
{
case CrossoverType::OnePoint:
return OnePointCrossover(first, second);
case CrossoverType::Ordered:
return OrderedCrossover(first, second);
}
return std::vector<Chromosome<T>>();
}
///////////////////////////////////////////////////////////////////////////
// MUTATION ///////////////////////////////////////////////////////////////
template<typename T>
void MutationToggle(Chromosome<T> & chromosome, double mutationProbability)
{
for (size_t i = 0; i < chromosome.genes.size(); ++i)
{
double mutate = (double)rand() / (double)RAND_MAX;
if (mutate < mutationProbability)
chromosome.genes[i] = !chromosome.genes[i];
}
}
template<typename T>
void MutationSwap(Chromosome<T> & chromosome, double mutationProbability)
{
for (size_t i = 0; i < chromosome.genes.size(); ++i)
{
double mutate = (double)rand() / (double)RAND_MAX;
if (mutate < mutationProbability)
{
size_t swapIndex = rand() % chromosome.genes.size();
std::swap(*(std::begin(chromosome.genes) + i), *(std::begin(chromosome.genes) + swapIndex));
}
}
}
template<typename T>
void MutationCustom(Chromosome<T> & chromosome, double mutationProbability, const CustomMutation<T> & custom)
{
for (size_t i = 0; i < chromosome.genes.size(); ++i)
{
double mutate = (double)rand() / (double)RAND_MAX;
if (mutate < mutationProbability)
{
chromosome.genes[i] = custom(chromosome.genes[i], i);
}
}
}
template<typename T>
void Mutate(Chromosome<T> & chromosome, double probability, MutationType type, const CustomMutation<T> & custom)
{
switch (type)
{
case MutationType::Toggle:
MutationToggle(chromosome, probability);
break;
case MutationType::Swap:
MutationSwap(chromosome, probability);
break;
case MutationType::Custom:
MutationCustom(chromosome, probability, custom);
break;
}
}
template<typename T>
void Mutation(std::vector<Chromosome<T>> & childs, const Definition<T> & definition)
{
if (definition.mutation == MutationType::None)
return;
for (Chromosome<T> & c : childs)
Mutate(c, definition.mutationProbability, definition.mutation, definition.mutationCustomCallback);
}
///////////////////////////////////////////////////////////////////////////
template<typename T>
void SurvivorSelection(Population<T> & population, uint32_t populationSize, std::vector<Chromosome<T>> && childs)
{
// add childs to population
std::move(std::begin(childs), std::end(childs), std::back_inserter(population));
if (population.size() > populationSize)
{
// if population is too big, sort it with ration fitness over age
std::sort(std::begin(population), std::end(population),
[](const Chromosome<T> & a, const Chromosome<T> & b)
{
double ac = a.age == 0 ? (double)a.fitness : (double)a.fitness / (double)a.age;
double bc = b.age == 0 ? (double)b.fitness : (double)b.fitness / (double)b.age;
return ac < bc;
});
// remove unlucky ones
size_t toRemove = population.size() - populationSize;
for (size_t i = 0; i < toRemove; ++i)
population.erase(population.begin());
}
}
template<typename T>
void ValidateDefinition(const Definition<T> & definition)
{
if (definition.mutation == MutationType::Custom)
{
if (!definition.mutationCustomCallback)
throw std::runtime_error("custom mutation callback not provided");
}
}
template<typename T>
std::vector<T> Solve(const Definition<T> & definition)
{
ValidateDefinition(definition);
Population<T> population = InitializePopulation(definition);
std::vector<T> ret;
uint32_t generationNumber = 0;
while (!CheckTerminationCondition(generationNumber, definition, population, ret))
{
// increment age
std::for_each(std::begin(population), std::end(population), [](Chromosome<T> & c) { c.age++; });
// recompute
std::for_each(std::begin(population), std::end(population),
[&definition](Chromosome<T> & c) { c.fitness = definition.computeFitness(c.genes); });
// number of crossovers is computed with crossover factor
size_t crossoverSize = (size_t)((double)population.size() * definition.crossoverFactor);
crossoverSize = crossoverSize % 2 ? crossoverSize - 1 : crossoverSize;
// select parents
std::vector<const Chromosome<T>*> parents = SelectParent(crossoverSize, population, definition);
// iterate over pairs of parents
std::vector<Chromosome<T>> childs;
for (size_t i = 0; i < parents.size(); i = i + 2)
{
// crossover - generate childs
std::vector<Chromosome<T>> tmpChilds = Crossover(*parents[i], *parents[i + 1], definition);
// mutation
Mutation(tmpChilds, definition);
// evaluate childs
std::for_each(std::begin(tmpChilds), std::end(tmpChilds),
[&definition](Chromosome<T> & c) { c.fitness = definition.computeFitness(c.genes); });
for (auto && c : tmpChilds)
childs.push_back(std::move(c));
}
// survivor selection - merge parents childs - keep constant population size
SurvivorSelection(population, definition.populationSize, std::move(childs));
generationNumber++;
}
return ret;
}
}