2#include "ECF_derived.h"
5#include "AlgSGenGPEA.h"
10AlgSGenGpea::AlgSGenGpea()
12 name_ =
"AlgSGenGPEA";
14 selRandomOp =
static_cast<SelRandomOpP
> (
new SelRandomOp);
15 selBestOp =
static_cast<SelBestOpP
> (
new SelBestOp);
21 double* crxRate =
new double(0.5);
22 state->getRegistry()->registerEntry(
name_ +
".crxprob", (voidP) crxRate, ECF::DOUBLE);
24 double* selPress =
new double(2);
25 state->getRegistry()->registerEntry(
name_ +
".selpressure", (voidP) selPress, ECF::DOUBLE);
27 uint* jobSize =
new uint(10);
28 state->getRegistry()->registerEntry(
name_ +
".jobsize", (voidP) jobSize, ECF::UINT);
34 selFitPropOp->initialize(state);
35 selRandomOp->initialize(state);
36 selBestOp->initialize(state);
38 voidP crRateP = state->getRegistry()->getEntry(
name_ +
".crxprob");
39 crxRate = *((
double*) crRateP.get());
41 voidP selPressP = state->getRegistry()->getEntry(
name_ +
".selpressure");
42 selPressure = *((
double*) selPressP.get());
43 selFitPropOp->setSelPressure(selPressure);
45 voidP jobSizeP = state->getRegistry()->getEntry(
name_ +
".jobsize");
46 jobSize = *((uint*) jobSizeP.get());
48 if(state->getCommunicator()->getCommRank() != MASTER) {
58 CommunicatorP comm = state->getCommunicator();
60 if(comm->getCommRank() == MASTER) {
63 IndividualP best = selBestOp->select(*deme);
66 std::vector<IndividualP> wheel;
67 wheel = selFitPropOp->selectMany(*deme, (uint) deme->size());
69 for(uint i = 0; i < wheel.size(); ++i)
70 wheel[i] =
copy(wheel[i]);
73 for(uint i = 0; i < deme->size(); i++)
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()));
81 uint noCrx = (int)(deme->size() * crxRate /2);
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);
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));
105 uint current = 0, remaining = (uint) pool.size();
106 while(current < pool.size()) {
108 if(comm->messageWaiting()) {
110 uint received = comm->recvDemeFitness(*deme, Comm::ANY_PROCESS);
111 state->increaseEvaluations(received);
112 remaining -= received;
114 uint iWorker = comm->getLastSource();
116 for(uint i = 0; i < jobSize && current < pool.size(); i++, current++)
117 job.push_back(pool[current]);
118 comm->sendIndividuals(job, iWorker);
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;
136 uint iWorker = comm->getLastSource();
137 comm->sendIndividuals(job, iWorker);
142 IndividualP random = selRandomOp->select(*deme);
143 if(best->fitness->isBetterThan(random->fitness))
146 for(uint i = 0; i < deme->size(); i++){
147 ECF_LOG(state, 5,
"deme[" + uint2str(i) +
"]: " + dbl2str(deme->at(i)->fitness->getValue()));
153 std::vector<IndividualP> empty;
155 comm->sendFitness(empty, MASTER);
158 myJobSize = comm->recvReplaceIndividuals(myJob, MASTER);
161 while(myJobSize > 0) {
162 for(uint i = 0; i < myJobSize; i++)
165 comm->sendFitness(myJob, MASTER, myJobSize);
167 myJobSize = comm->recvReplaceIndividuals(myJob, MASTER);
void registerParameters(StateP state)
Register algorithm's parameters (if any).
bool initialize(StateP state)
Initialize the algorithm, read parameters from the system, do a sanity check.
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
uint mutate(const std::vector< IndividualP > &pool)
Helper function: send a vector of individuals to mutation.
IndividualP copy(IndividualP source)
Helper function: make a copy of an individual.
std::string name_
algorithm name
bool mate(IndividualP p1, IndividualP p2, IndividualP child)
Helper function: crossover two individuals.
void replaceWith(IndividualP oldInd, IndividualP newInd)
Helper function: replace an individual in current deme.
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
Best individual selection operator.
Fitness proportional (and inverse proportional) individual selection operator.
Random individual selection operator.