ECF 1.5
AlgAEliGPEA.cpp
1#include "ECF_base.h"
2#include "ECF_derived.h"
3#include "ECF_macro.h"
4
5#include "AlgAEliGPEA.h"
6
7const int MASTER = 0;
8const int RANDOM = 0;
9const int WORST = 1;
10
11
12AlgAEliGpea::AlgAEliGpea()
13{
14 name_ = "AlgAEliGPEA";
15 selectionOp.push_back(static_cast<SelectionOperatorP> (new SelRandomOp));
16 selectionOp.push_back(static_cast<SelectionOperatorP> (new SelWorstOp));
17}
18
19
21{
22 int* tsizep = new int(3);
23 state->getRegistry()->registerEntry(name_ + ".tsize", (voidP) tsizep, ECF::UINT);
24
25 uint* jobSize = new uint(10);
26 state->getRegistry()->registerEntry(name_ + ".jobsize", (voidP) jobSize, ECF::UINT);
27}
28
29
30bool AlgAEliGpea::initialize(StateP state)
31{
32 if(state->getCommunicator()->getCommRank() != MASTER)
33 myJob_.resize(0);
34
35 selectionOp[RANDOM]->initialize(state);
36 selectionOp[WORST]->initialize(state);
37
38 voidP tsizep = state->getRegistry()->getEntry(name_ + ".tsize");
39 nTournament_ = *((uint*) tsizep.get());
40
41 if(nTournament_ < 3) {
42 ECF_LOG_ERROR(state, "Error: AlgAEliGPEA algorithm requires minimum tournament size of 3!");
43 throw "";
44 }
45
46 voidP jobSizeP = state->getRegistry()->getEntry(name_ + ".jobsize");
47 jobSize_ = *((uint*) jobSizeP.get());
48
49 return true;
50}
51
52
53IndividualP AlgAEliGpea::performSingleTournament(DemeP deme)
54{
55 std::vector<IndividualP> tournament;
56 for (uint i = 0; i < nTournament_; ++i) {
57 tournament.push_back(selectionOp[RANDOM]->select(*deme));
58 }
59
60 IndividualP worst = selectionOp[WORST]->select(tournament);
61 storeIndividual(worst);
62
63 removeFrom(worst, tournament); // remove pointer 'worst' from vector 'tournament'
64
65 mate(tournament[0], tournament[1], worst);
66
67 mutate(worst);
68
69 return worst;
70}
71
72
73bool AlgAEliGpea::advanceGeneration(StateP state, DemeP deme)
74{
75 CommunicatorP comm = state->getCommunicator();
76
77 if(comm->getCommRank() == MASTER) {
78
79 std::vector<IndividualP> evalPool;
80 uint iIter = 0;
81
82 while(iIter < deme->size()) {
83
84 // perform jobSize selections and operations
85 for(uint ind = 0; ind < jobSize_ && iIter < deme->size(); ind++, iIter++) {
86 IndividualP newInd = performSingleTournament(deme);
87
88 if(isMember(newInd, evalPool) == false)
89 evalPool.push_back(newInd);
90 }
91
92 // evaluation-waiting loop
93 do {
94 // if a worker is ready
95 if(!evalPool.empty() && comm->messageWaiting()) {
96 std::vector<uint> received = comm->recvFitnessVector(*deme, Comm::ANY_PROCESS);
97 state->increaseEvaluations((uint) received.size());
98 uint iWorker = comm->getLastSource();
99
100 // send job
101 storeGenotypes(evalPool);
102 comm->sendIndividuals(evalPool, iWorker);
103 evalPool.resize(0);
104
105 restoreIndividuals(received);
106 break; // continue main loop
107 }
108 // do something useful in the meantime...
109 else {
110 evaluate(evalPool.back());
111 setConsistency(evalPool.back());
112 evalPool.pop_back();
113
114 IndividualP newInd = performSingleTournament(deme);
115 iIter++;
116 if(isMember(newInd, evalPool) == false)
117 evalPool.push_back(newInd);
118 }
119 } while(iIter < deme->size());
120 }
121
122 }
123
124 // WORKER
125 else {
126 myJob_.resize(0);
127 comm->sendFitness(myJob_, MASTER);
128 uint myJobSize = 1;
129
130 // while individuals to evaluate
131 while(myJobSize != 0) {
132
133 myJobSize = comm->recvReplaceIndividuals(myJob_, MASTER);
134 for(uint i = 0; i < myJobSize; i++)
135 evaluate(myJob_[i]);
136
137 comm->sendFitness(myJob_, MASTER, myJobSize);
138 }
139 ECF_LOG(state, 4, "Worker ends...");
140 }
141
142 return true;
143}
144
145
147{
148 // master: if terminate condition is set, send all workers empty jobs
149 if(state->getCommunicator()->getCommRank() == 0 && state->getTerminateCond()) {
150 std::vector<IndividualP> empty;
151 DemeP myDeme = state->getPopulation()->getLocalDeme();
152
153 for(uint iWorker = 1; iWorker < state->getCommunicator()->getCommSize(); iWorker++) {
154 if(state->getCommunicator()->messageWaiting(iWorker)) {
155 std::vector<uint> received = state->getCommunicator()->recvFitnessVector(*myDeme, Comm::ANY_PROCESS);
156 restoreIndividuals(received);
157 }
158 state->getCommunicator()->sendIndividuals(empty, iWorker);
159 }
160
161 // restore individuals to last consistent state
163 }
164
165 // worker: evolution is over
166 else if(state->getCommunicator()->getCommRank() != 0)
167 state->setTerminateCond();
168}
void registerParameters(StateP state)
Register algorithm's parameters (if any).
Definition: AlgAEliGPEA.cpp:20
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
Definition: AlgAEliGPEA.cpp:73
void bcastTermination(StateP)
Parallel ECF: broadcast termination to worker processes.
bool initialize(StateP state)
Initialize the algorithm, read parameters from the system, do a sanity check.
Definition: AlgAEliGPEA.cpp:30
uint mutate(const std::vector< IndividualP > &pool)
Helper function: send a vector of individuals to mutation.
Definition: Algorithm.h:169
std::vector< SelectionOperatorP > selectionOp
sel. operators used by algorithm
Definition: Algorithm.h:25
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
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
bool removeFrom(IndividualP victim, std::vector< IndividualP > &pool)
Helper function: remove victim from pool of individual pointers.
Definition: Algorithm.h:297
void restorePopulation()
restores inconsistent individuals to last consistent state
Definition: Algorithm.cpp:481
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
Definition: Algorithm.h:157
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
Random individual selection operator.
Definition: SelRandomOp.h:12
Worst individual selection operator.
Definition: SelWorstOp.h:11