ECF 1.5
Algorithm.h
1#ifndef Algorithm_h
2#define Algorithm_h
3
4#include "State.h"
5#include "SelectionOperator.h"
6#include "Crossover.h"
7#include "Mutation.h"
8#include "EvaluateOp.h"
9#include<vector>
10
20{
21protected:
22 DemeP activeDeme_;
23 std::string name_;
25 std::vector<SelectionOperatorP> selectionOp;
26
35 bool registerParameter(StateP state, std::string name, voidP value, enum ECF::type T, std::string description = "")
36 {
37 return state->getRegistry()->registerEntry(name_ + "." + name, value, T, description);
38 }
39
46 voidP getParameterValue(StateP state, std::string name)
47 {
48 return state->getRegistry()->getEntry(name_ + "." + name);
49 }
50
51#ifdef _MPI
52 std::vector<IndividualP> requests_, stored_;
53 std::vector<uint> requestIds_;
54 std::vector<IndividualP> demeCopy_;
55 std::vector<IndividualP> myJob_;
56 CommunicatorP comm_;
57 uint jobSize_;
58 uint totalEvaluations_, wastedEvaluations_;
62 SelectionOperatorP selBestOp;
63 std::vector<IndividualP> requestsMut_, receivedMut_;
64 std::vector<uint> requestMutIds_;
65 IndividualP currentBest_;
66
67 std::vector<IndividualP> storedInds_; //<! last consistent version of an individual
68 std::vector< std::vector<IndividualP> > sentInds_;
69 std::vector<bool> isConsistent_;
70
71 void storeIndividual(IndividualP);
72 void storeGenotypes(std::vector<IndividualP>&);
73 void setConsistency(IndividualP);
74 void restoreIndividuals(std::vector<uint>);
75 void restorePopulation();
76#endif
77
78
79public:
80 CrossoverP crossover_;
81 MutationP mutation_;
82 EvaluateOpP evalOp_;
83 StateP state_;
84
85 Algorithm()
86 { bImplicitParallel_ = false; }
87
88 virtual ~Algorithm()
89 { }
90
92 std::string getName()
93 { return name_; }
94
96 virtual bool isParallel()
97 { return false; }
98
101 { return bImplicitParallel_; }
102
108 virtual bool advanceGeneration(StateP, DemeP) = 0;
109
115 virtual bool initialize(StateP)
116 { return true; }
117
123 virtual void registerParameters(StateP) {}
124
128 virtual bool initializePopulation(StateP);
129
130 virtual void read() {}
131
132#ifndef _MPI
133
139 virtual bool advanceGeneration(StateP state)
140 {
141 ECF_LOG(state, 4, name_ + ": starting generation");
142
143 bool bResult = true;
144 for(uint iDeme = 0; iDeme < state->getPopulation()->size(); iDeme++) {
145 activeDeme_ = state->getPopulation()->at(iDeme);
146 bResult |= advanceGeneration(state, state->getPopulation()->at(iDeme));
147 }
148
149 ECF_LOG(state, 4, name_ + ": ending generation");
150
151 return bResult;
152 }
153
157 void evaluate(IndividualP ind)
158 {
159 state_->getContext()->evaluatedIndividual = ind;
160 ECF_LOG(state_, 5, "Evaluating individual: " + ind->toString());
161 ind->fitness = evalOp_->evaluate(ind);
162 ECF_LOG(state_, 5, "New fitness: " + ind->fitness->toString());
163 state_->increaseEvaluations();
164 }
165
169 uint mutate(const std::vector<IndividualP>& pool)
170 { return mutation_->mutation(pool); }
171
175 uint mutate(const IndividualP victim)
176 {
177 std::vector<IndividualP> pool;
178 pool.push_back(victim);
179 return mutation_->mutation(pool);
180 }
181
187 void replaceWith(IndividualP oldInd, IndividualP newInd)
188 { activeDeme_->replace(oldInd->index, newInd); }
189
195 void replaceWith(uint oldIndId, IndividualP newInd)
196 { activeDeme_->replace(oldIndId, newInd); }
197
199 void registerParallelParameters(StateP state)
200 { }
201
203 bool initializeParallel(StateP state)
204 { return true; }
205
206#else // implicit parallel version
207
209 virtual bool advanceGeneration(StateP state);
210
212 virtual void bcastTermination(StateP state);
213
215 void registerParallelParameters(StateP state);
216
218 bool initializeParallel(StateP state);
219
221 void initializeImplicit(StateP state);
222
224 bool implicitParallelOperate(StateP state);
225
227 void evaluate(IndividualP ind)
228 {
229 // implicit evaluation
231 implicitEvaluate(ind);
232 // if implicit mutation is active, check is evaluation needed
233 else if(bImplicitMutation_ && ind->fitness->isValid())
234 return;
235 // else, evaluate the individual
236 state_->getContext()->evaluatedIndividual = ind;
237 ECF_LOG(state_, 5, "Evaluating individual: " + ind->toString());
238 ind->fitness = evalOp_->evaluate(ind);
239 ECF_LOG(state_, 5, "New fitness: " + ind->fitness->toString());
240 state_->increaseEvaluations();
241 }
242
244 void implicitEvaluate(IndividualP ind);
245
247 uint mutate(const IndividualP victim)
248 {
249 std::vector<IndividualP> pool;
250 pool.push_back(victim);
251 return mutate(pool);
252 }
253
255 uint mutate(const std::vector<IndividualP>& pool)
256 {
258 uint nMutations = 0;
259 for(uint i = 0; i < pool.size(); i++) {
260 nMutations += implicitMutate(pool[i]);
261 }
262 return nMutations;
263 }
264 else
265 return mutation_->mutation(pool);
266 }
267
269 uint implicitMutate(IndividualP ind);
270
272 void replaceWith(IndividualP oldInd, IndividualP newInd);
273
275 void replaceWith(uint oldIndId, IndividualP newInd)
276 { replaceWith(state_->getPopulation()->getLocalDeme()->at(oldIndId), newInd); }
277
278#endif
279
280// common functions (serial and parallel)
281
285 bool mate(IndividualP p1, IndividualP p2, IndividualP child)
286 { return crossover_->mate(p1, p2, child); }
287
291 IndividualP copy(IndividualP source)
292 { return (IndividualP) source->copy(); }
293
297 bool removeFrom(IndividualP victim, std::vector<IndividualP> &pool)
298 {
299 uint iWorst = 0;
300 while(iWorst < pool.size() && pool[iWorst] != victim)
301 iWorst++;
302 if(iWorst == pool.size())
303 return false;
304 pool.erase(pool.begin() + iWorst);
305 return true;
306 }
307
311 bool isMember(IndividualP single, std::vector<IndividualP> &pool)
312 {
313 uint index = 0;
314 while(index < pool.size() && pool[index] != single)
315 index++;
316 if(index == pool.size())
317 return false;
318 return true;
319 }
320};
321typedef boost::shared_ptr<Algorithm> AlgorithmP;
322
323#endif // Algorithm_h
324
Algorithm base class.
Definition: Algorithm.h:20
std::vector< IndividualP > stored_
individual vectors for implicit evaluation
Definition: Algorithm.h:52
virtual bool advanceGeneration(StateP state)
Perform one generation of the algorithm on the whole population.
Definition: Algorithm.h:139
bool implicitParallelOperate(StateP state)
Parallel ECF: Worker processes in implicit parallel algorithm.
Definition: Algorithm.cpp:239
uint mutate(const std::vector< IndividualP > &pool)
Helper function: send a vector of individuals to mutation.
Definition: Algorithm.h:169
IndividualP copy(IndividualP source)
Helper function: make a copy of an individual.
Definition: Algorithm.h:291
virtual bool advanceGeneration(StateP state)
Parallel ECF: Perform one generation of the algorithm.
bool bSynchronized_
is implicit paralelization synchronous
Definition: Algorithm.h:61
void initializeImplicit(StateP state)
Parallel ECF: Initialize implicit parallel mode.
Definition: Algorithm.cpp:92
std::vector< uint > requestMutIds_
individual indexes for implicit mutation
Definition: Algorithm.h:64
bool bImplicitParallel_
implicit parallel flag
Definition: Algorithm.h:24
virtual bool advanceGeneration(StateP, DemeP)=0
Perform a single generation on a single deme.
std::vector< SelectionOperatorP > selectionOp
sel. operators used by algorithm
Definition: Algorithm.h:25
EvaluateOpP evalOp_
sptr to evaluation operator (set by the system)
Definition: Algorithm.h:82
void storeIndividual(IndividualP)
stores the individual (if it is consistent), resets consistency flag
Definition: Algorithm.cpp:414
std::string name_
algorithm name
Definition: Algorithm.h:23
virtual bool initialize(StateP)
Initialize the algorithm, read parameters from the system, do a sanity check.
Definition: Algorithm.h:115
std::vector< IndividualP > myJob_
worker's individual vector
Definition: Algorithm.h:55
uint mutate(const IndividualP victim)
Helper function: send a single individual to mutation.
Definition: Algorithm.h:175
bool registerParameter(StateP state, std::string name, voidP value, enum ECF::type T, std::string description="")
Helper function: register a single parameter with the system.
Definition: Algorithm.h:35
CrossoverP crossover_
sptr to container of crossover operators (set by the system)
Definition: Algorithm.h:80
voidP getParameterValue(StateP state, std::string name)
Helper function: get parameter value from the system.
Definition: Algorithm.h:46
virtual bool initializePopulation(StateP)
Evaluate initial population (called by State::run before evolution starts).
Definition: Algorithm.cpp:338
bool isMember(IndividualP single, std::vector< IndividualP > &pool)
Helper function: check if individual is in the pool.
Definition: Algorithm.h:311
bool mate(IndividualP p1, IndividualP p2, IndividualP child)
Helper function: crossover two individuals.
Definition: Algorithm.h:285
void setConsistency(IndividualP)
denotes current individual as consistent
Definition: Algorithm.cpp:424
void replaceWith(IndividualP oldInd, IndividualP newInd)
Helper function: replace an individual in current deme.
Definition: Algorithm.h:187
bool isImplicitParallel()
Is algorithm run in implicit parallel mode (in development, see tutorial).
Definition: Algorithm.h:100
bool removeFrom(IndividualP victim, std::vector< IndividualP > &pool)
Helper function: remove victim from pool of individual pointers.
Definition: Algorithm.h:297
MutationP mutation_
sptr to container of mutation operators (set by the system)
Definition: Algorithm.h:81
uint implicitMutate(IndividualP ind)
Parallel ECF: implicitly mutate an individual (store for later mutation in implicit parallel version)...
Definition: Algorithm.cpp:176
void registerParallelParameters(StateP state)
used only in parallel ECF
Definition: Algorithm.h:199
std::string getName()
Get algorithm name. Each algorithm is uniquely identified with its name.
Definition: Algorithm.h:92
std::vector< bool > isConsistent_
is individual (genotype-fitness pair) consistent
Definition: Algorithm.h:69
void restorePopulation()
restores inconsistent individuals to last consistent state
Definition: Algorithm.cpp:481
bool bImplicitMutation_
implicit mutation flag
Definition: Algorithm.h:60
bool initializeParallel(StateP state)
used only in parallel ECF
Definition: Algorithm.h:203
virtual void bcastTermination(StateP state)
Parallel ECF: broadcast termination to worker processes.
Definition: Algorithm.cpp:290
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
Definition: Algorithm.h:157
void implicitEvaluate(IndividualP ind)
Parallel ECF: implicitly evaluate an individual (store for later evaluation in implicit parallel vers...
Definition: Algorithm.cpp:117
void replaceWith(uint oldIndId, IndividualP newInd)
Helper function: replace an individual at given position in current deme.
Definition: Algorithm.h:195
bool bImplicitEvaluation_
implicit evaluation flag
Definition: Algorithm.h:59
std::vector< std::vector< IndividualP > > sentInds_
individuals sent for evaluation
Definition: Algorithm.h:68
void restoreIndividuals(std::vector< uint >)
restores individuals whose fitness is received
Definition: Algorithm.cpp:455
void storeGenotypes(std::vector< IndividualP > &)
adds genotypes of individuals to 'sent' repository
Definition: Algorithm.cpp:432
std::vector< IndividualP > receivedMut_
individual vectors for implicit mutation
Definition: Algorithm.h:63
virtual void registerParameters(StateP)
Register algorithm's parameters (if any).
Definition: Algorithm.h:123
std::vector< uint > requestIds_
individual indexes for implicit evaluation
Definition: Algorithm.h:53
virtual bool isParallel()
Is algorithm parallel (false by default for all algorithms not inheriting ParallelAlgorithm class).
Definition: Algorithm.h:96
type
Data types used for configuration file parameters.
Definition: Registry.h:16