-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneticAlgorithm.cpp
177 lines (144 loc) · 4.37 KB
/
GeneticAlgorithm.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
/**
* GeneticAlgorithm.cpp
*
* Created on: Jul 31, 2011
* Author: Jeroen Vlek
* Email: jeroenvlek@gmail.com
* Website: www.perceptivebits.com
* License: Free Beer (Feel free to use it in every
* way possible and if you like it, make
* sure to give me credit and buy me a drink
* if we ever meet ;) )
*/
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <utility>
#include <boost/bind.hpp>
#include "GeneticAlgorithm.h"
#include "ImageComputation.h"
#include "TargetImage.h"
GeneticAlgorithm::GeneticAlgorithm(GUI& aGUI) :
m_gui(aGUI), m_doEvolution(false) {
m_gui.loadTargetImage();
Image& targetImage = *TargetImage::Instance();
Organism::reserve(Config::GetPopulationSize() * 2);
Organism::setGrowthPolicy(memory::DOUBLE);
for (unsigned int i = 0; i < Config::GetPopulationSize(); ++i) {
Organism* organism = new Organism(Config::GetGenomeSize());
m_population.push_back(organism);
double score = averagePixelDistance(targetImage,
organism->getPhenotype());
organism->setScore(score);
}
displayPhenoTypes();
}
GeneticAlgorithm::~GeneticAlgorithm() {
stop();
for (PopulationIter it = m_population.begin(); it != m_population.end();
++it) {
delete *it;
}
}
void GeneticAlgorithm::start() {
if (m_doEvolution) {
return;
}
m_doEvolution = true;
m_thread = boost::thread(boost::bind(&GeneticAlgorithm::evolve, this));
}
void GeneticAlgorithm::stop() {
if (!m_doEvolution) {
return;
}
m_doEvolution = false;
m_thread.join();
}
void GeneticAlgorithm::fillNewPopulation(bool doMutation) {
Population newPopulation;
std::vector<boost::thread*> threads;
for (unsigned int i = 0; i < Config::GetNumWorkerThreads(); ++i) {
threads.push_back(
new boost::thread(
boost::bind(&GeneticAlgorithm::createOffspring, this, _1, _2),
boost::ref(newPopulation),
doMutation
)
);
}
std::vector<boost::thread*>::iterator it;
for(it = threads.begin(); it != threads.end(); ++it) {
(*it)->join();
delete *it;
}
if (m_population.size() == 1) {
Organism* leftOver = m_population[0];
newPopulation.push_back(leftOver);
}
m_population = newPopulation;
}
void GeneticAlgorithm::createOffspring(Population& newPopulation,
bool doMutation) {
Image& targetImage = *TargetImage::Instance();
Population tmpResult;
while (m_population.size() > 1) {
m_inputLock.lock();
if (m_population.size() <= 1) {
m_inputLock.unlock();
break;
}
std::pair<Organism*, Organism*> couple =
m_pairGenerator.removeRandomPair(m_population);
m_inputLock.unlock();
Organism* child = new Organism(*couple.first, *couple.second, doMutation);
double score = averagePixelDistance(targetImage, child->getPhenotype());
child->setScore(score);
tmpResult.push_back(child);
tmpResult.push_back(couple.first);
tmpResult.push_back(couple.second);
}
m_outputLock.lock();
newPopulation.insert(newPopulation.end(), tmpResult.begin(),
tmpResult.end());
m_outputLock.unlock();
}
/**
* Natural selection by selecting the organisms that are
* the closest to the target image (i.e. fit the best to the environment)
* and remove the others.
*/
void GeneticAlgorithm::doNaturalSelection() {
std::sort(m_population.begin(), m_population.end(), Organism::compareScores);
while (m_population.size() > Config::GetPopulationSize()) {
delete m_population.back();
m_population.pop_back();
}
}
/**
* Updates the GUI to display the phenotypes
*/
void GeneticAlgorithm::displayPhenoTypes() {
for (unsigned int i = 0; i < Config::GetDisplaySize(); ++i) {
PhenotypeImage& phenotypeImage = m_population[i]->getPhenotype();
m_gui.displayPhenotypeImage(i, phenotypeImage);
}
}
void GeneticAlgorithm::evolve() {
std::cout << "[ GeneticAlgorithm::evolve() ] Entering evolution loop"
<< std::endl;
unsigned int numIterations = 0;
while (m_doEvolution) {
bool doMutation = (numIterations % Config::GetMutationInterval()) == 0;
fillNewPopulation(doMutation);
doNaturalSelection();
displayPhenoTypes();
++numIterations;
// if((numIterations % Config::GetReportingInterval()) == 0) {
// std::cout << "[ GeneticAlgorithm::evolve() ] Iteration: " << numIterations <<
// ", smallest distance: " << m_populationScores.begin()->first << std::endl;
// }
}
std::cout << "[ GeneticAlgorithm::evolve() ] Total number of iterations: "
<< numIterations << std::endl;
}