ECF 1.5
AlgAEliGPEA2.cpp
1#include "ECF_base.h"
2#include "ECF_derived.h"
3#include "ECF_macro.h"
4
5#include "AlgAEliGPEA2.h"
6
7const int MASTER = 0;
8const int RANDOM = 0;
9const int WORST = 1;
10
11AlgAEliGpea2::AlgAEliGpea2()
12{
13 name_ = "AlgAEliGPEA2";
14 selectionOp.push_back(static_cast<SelectionOperatorP> (new SelRandomOp));
15 selectionOp.push_back(static_cast<SelectionOperatorP> (new SelWorstOp));
16}
17
19{
20 int* tsizep = new int(3);
21 state->getRegistry()->registerEntry(name_ + ".tsize", (voidP) tsizep, ECF::UINT);
22
23 uint* jobSize = new uint(10);
24 state->getRegistry()->registerEntry(name_ + ".jobsize", (voidP) jobSize, ECF::UINT);
25}
26
27bool AlgAEliGpea2::initialize(StateP state)
28{
29 if(state->getCommunicator()->getCommRank() != MASTER)
30 myJob.resize(0);
31
32 selectionOp[RANDOM]->initialize(state);
33 selectionOp[WORST]->initialize(state);
34
35 voidP tsizep = state->getRegistry()->getEntry(name_ + ".tsize");
36 nTournament = *((uint*) tsizep.get());
37
38 voidP jobSizeP = state->getRegistry()->getEntry(name_ + ".jobsize");
39 jobSize = *((uint*) jobSizeP.get());
40
41 return true;
42}
43
44bool AlgAEliGpea2::advanceGeneration(StateP state, DemeP deme)
45{
46 CommunicatorP comm = state->getCommunicator();
47
48 if(comm->getCommRank() == MASTER) {
49
50 std::vector<IndividualP> evalPool;
51 uint iIter = 0;
52
53 while(iIter < deme->size()) {
54
55 // perform jobSize selections and operations
56 for(uint ind = 0; ind < jobSize && iIter < deme->size(); ind++, iIter++) {
57 std::vector<IndividualP> tournament;
58 for (uint i = 0; i < nTournament; ++i) {
59 tournament.push_back(selectionOp[RANDOM]->select(*deme));
60 }
61
62 IndividualP worst = selectionOp[WORST]->select(tournament);
63 removeFrom(worst, tournament); // remove pointer 'worst' from vector 'tournament'
64
65 crossover_->mate(tournament[0], tournament[1], worst);
66
67 std::vector<IndividualP> mutationPool;
68 mutationPool.push_back(worst);
69 mutation_->mutation(mutationPool);
70
71 if(isMember(worst, evalPool) == false)
72 evalPool.push_back(worst);
73 }
74
75 // if a worker is ready, send individuals to evaluate
76 //if(comm->messageWaiting(MPI::ANY_SOURCE))
77
78 // OR, just wait for a worker to be ready
79 comm->recvDemeFitness(*deme, Comm::ANY_PROCESS);
80 uint iWorker = comm->getLastSource();
81
82 comm->sendIndividuals(evalPool, iWorker);
83 evalPool.resize(0);
84 }
85
86 // this is redundant IF the MASTER waits for a WORKER (above)
87 //std::vector<IndividualP> job;
88 //int remaining = (int) evalPool.size() - 1;
89 //while(remaining >= 0) {
90 // comm->recvDemeFitness(*deme, MPI::ANY_SOURCE);
91 // uint iWorker = comm->getLastSource();
92
93 // job.resize(0);
94 // int iInd;
95 // for(iInd = remaining; iInd >= 0 && remaining - iInd < (int) jobSize; iInd--)
96 // job.push_back(evalPool[iInd]);
97 // remaining = iInd;
98 // comm->sendIndividuals(job, iWorker);
99 //}
100 //evalPool.resize(0);
101
102 // while all individuals not evaluated
103 uint remainingWorkers = comm->getCommSize() - 1;
104 while(remainingWorkers > 0) {
105 comm->recvDemeFitness(*deme, Comm::ANY_PROCESS);
106 uint iWorker = comm->getLastSource();
107
108 comm->sendIndividuals(evalPool, iWorker);
109 remainingWorkers--;
110 }
111 }
112
113 else { // WORKER
114 std::vector<IndividualP> empty;
115 empty.resize(0);
116 comm->sendFitness(empty, MASTER);
117
118 uint myJobSize;
119 myJobSize = comm->recvReplaceIndividuals(myJob, MASTER);
120
121 // while individuals to evaluate
122 while(myJobSize > 0) {
123 for(uint i = 0; i < myJobSize; i++)
124 evaluate(myJob[i]);
125
126 comm->sendFitness(myJob, MASTER, myJobSize);
127
128 myJobSize = comm->recvReplaceIndividuals(myJob, MASTER);
129 }
130 }
131
132 return true;
133}
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
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.
std::vector< SelectionOperatorP > selectionOp
sel. operators used by algorithm
Definition: Algorithm.h:25
std::string name_
algorithm name
Definition: Algorithm.h:23
CrossoverP crossover_
sptr to container of crossover operators (set by the system)
Definition: Algorithm.h:80
bool isMember(IndividualP single, std::vector< IndividualP > &pool)
Helper function: check if individual is in the pool.
Definition: Algorithm.h:311
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
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
Definition: Algorithm.h:157
Random individual selection operator.
Definition: SelRandomOp.h:12
Worst individual selection operator.
Definition: SelWorstOp.h:11