ECF 1.5
AlgSGenGPEA.cpp
1#include "ECF_base.h"
2#include "ECF_derived.h"
3#include "ECF_macro.h"
4
5#include "AlgSGenGPEA.h"
6
7const int MASTER = 0;
8
9
10AlgSGenGpea::AlgSGenGpea()
11{
12 name_ = "AlgSGenGPEA";
13 selFitPropOp = static_cast<SelFitnessProportionalOpP> (new SelFitnessProportionalOp);
14 selRandomOp = static_cast<SelRandomOpP> (new SelRandomOp);
15 selBestOp = static_cast<SelBestOpP> (new SelBestOp);
16}
17
18
20{
21 double* crxRate = new double(0.5);
22 state->getRegistry()->registerEntry(name_ + ".crxprob", (voidP) crxRate, ECF::DOUBLE);
23
24 double* selPress = new double(2);
25 state->getRegistry()->registerEntry(name_ + ".selpressure", (voidP) selPress, ECF::DOUBLE);
26
27 uint* jobSize = new uint(10);
28 state->getRegistry()->registerEntry(name_ + ".jobsize", (voidP) jobSize, ECF::UINT);
29}
30
31
32bool AlgSGenGpea::initialize(StateP state)
33{
34 selFitPropOp->initialize(state);
35 selRandomOp->initialize(state);
36 selBestOp->initialize(state);
37
38 voidP crRateP = state->getRegistry()->getEntry(name_ + ".crxprob");
39 crxRate = *((double*) crRateP.get());
40
41 voidP selPressP = state->getRegistry()->getEntry(name_ + ".selpressure");
42 selPressure = *((double*) selPressP.get());
43 selFitPropOp->setSelPressure(selPressure);
44
45 voidP jobSizeP = state->getRegistry()->getEntry(name_ + ".jobsize");
46 jobSize = *((uint*) jobSizeP.get());
47
48 if(state->getCommunicator()->getCommRank() != MASTER) {
49 myJob.resize(0);
50 }
51
52 return true;
53}
54
55
56bool AlgSGenGpea::advanceGeneration(StateP state, DemeP deme)
57{
58 CommunicatorP comm = state->getCommunicator();
59
60 if(comm->getCommRank() == MASTER) {
61
62 // elitism
63 IndividualP best = selBestOp->select(*deme);
64 best = copy(best);
65
66 std::vector<IndividualP> wheel;
67 wheel = selFitPropOp->selectMany(*deme, (uint) deme->size());
68
69 for(uint i = 0; i < wheel.size(); ++i)
70 wheel[i] = copy(wheel[i]);
71
72 // replace population
73 for(uint i = 0; i < deme->size(); i++)
74 replaceWith(deme->at(i), wheel[i]);
75
76 ECF_LOG(state, 5, "Selected individuals:");
77 for(uint i = 0; i < deme->size(); i++){
78 ECF_LOG(state, 5, dbl2str(deme->at(i)->fitness->getValue()));
79 }
80
81 uint noCrx = (int)(deme->size() * crxRate /2);
82
83 for(uint i = 0; i < noCrx; i++){
84 IndividualP parent1 = selRandomOp->select(*deme);
85 IndividualP parent2 = selRandomOp->select(*deme);
86 ECF_LOG(state, 5, "Parents: " + dbl2str(parent1->fitness->getValue()) + ", " + dbl2str(parent2->fitness->getValue()));
87 IndividualP child1 = copy(parent1);
88 IndividualP child2 = copy(parent2);
89 mate(parent1, parent2, child1);
90 mate(parent1, parent2, child2);
91 replaceWith(parent1, child1);
92 replaceWith(parent2, child2);
93 }
94
95 // perform mutation on whole population
96 mutate(*deme);
97
98 // prepare vector of individuals to evaluate
99 std::vector<IndividualP> pool, job;
100 for(uint i = 0; i < deme->size(); i++)
101 if(!deme->at(i)->fitness->isValid())
102 pool.push_back(deme->at(i));
103
104 // while all individuals not sent
105 uint current = 0, remaining = (uint) pool.size();
106 while(current < pool.size()) {
107 // if a worker is ready, receive and send new job
108 if(comm->messageWaiting()) {
109 // receive fitness objects, increase total number of evaluations
110 uint received = comm->recvDemeFitness(*deme, Comm::ANY_PROCESS);
111 state->increaseEvaluations(received);
112 remaining -= received;
113
114 uint iWorker = comm->getLastSource();
115 job.resize(0);
116 for(uint i = 0; i < jobSize && current < pool.size(); i++, current++)
117 job.push_back(pool[current]);
118 comm->sendIndividuals(job, iWorker);
119 }
120 // otherwise, do something useful
121 else {
122 evaluate(pool[current]);
123 current++;
124 remaining--;
125 }
126 }
127
128 // while all individuals not received
129 job.resize(0);
130 uint remainingWorkers = comm->getCommSize() - 1;
131 while(remaining > 0 || remainingWorkers > 0) {
132 uint received = comm->recvDemeFitness(*deme, Comm::ANY_PROCESS);
133 state->increaseEvaluations(received);
134 remaining -= received;
135
136 uint iWorker = comm->getLastSource();
137 comm->sendIndividuals(job, iWorker);
138 remainingWorkers--;
139 }
140
141 // elitism
142 IndividualP random = selRandomOp->select(*deme);
143 if(best->fitness->isBetterThan(random->fitness))
144 replaceWith(random, best);
145
146 for(uint i = 0; i < deme->size(); i++){
147 ECF_LOG(state, 5, "deme[" + uint2str(i) + "]: " + dbl2str(deme->at(i)->fitness->getValue()));
148 }
149 }
150
151 // WORKER
152 else {
153 std::vector<IndividualP> empty;
154 empty.resize(0);
155 comm->sendFitness(empty, MASTER);
156
157 uint myJobSize;
158 myJobSize = comm->recvReplaceIndividuals(myJob, MASTER);
159
160 // while individuals to evaluate
161 while(myJobSize > 0) {
162 for(uint i = 0; i < myJobSize; i++)
163 evaluate(myJob[i]);
164
165 comm->sendFitness(myJob, MASTER, myJobSize);
166
167 myJobSize = comm->recvReplaceIndividuals(myJob, MASTER);
168 }
169 }
170
171 return true;
172}
void registerParameters(StateP state)
Register algorithm's parameters (if any).
Definition: AlgSGenGPEA.cpp:19
bool initialize(StateP state)
Initialize the algorithm, read parameters from the system, do a sanity check.
Definition: AlgSGenGPEA.cpp:32
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
Definition: AlgSGenGPEA.cpp:56
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
std::string name_
algorithm name
Definition: Algorithm.h:23
bool mate(IndividualP p1, IndividualP p2, IndividualP child)
Helper function: crossover two individuals.
Definition: Algorithm.h:285
void replaceWith(IndividualP oldInd, IndividualP newInd)
Helper function: replace an individual in current deme.
Definition: Algorithm.h:187
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
Definition: Algorithm.h:157
Best individual selection operator.
Definition: SelBestOp.h:10
Fitness proportional (and inverse proportional) individual selection operator.
Random individual selection operator.
Definition: SelRandomOp.h:12