00001 #include "ECF_base.h"
00002 #include "ECF_derived.h"
00003 #include "ECF_macro.h"
00004
00005 #include "AlgAEliGPEA2.h"
00006
00007 const int MASTER = 0;
00008 const int RANDOM = 0;
00009 const int WORST = 1;
00010
00011 AlgAEliGpea2::AlgAEliGpea2()
00012 {
00013 name_ = "AlgAEliGPEA2";
00014 selectionOp.push_back(static_cast<SelectionOperatorP> (new SelRandomOp));
00015 selectionOp.push_back(static_cast<SelectionOperatorP> (new SelWorstOp));
00016 }
00017
00018 void AlgAEliGpea2::registerParameters(StateP state)
00019 {
00020 int* tsizep = new int(3);
00021 state->getRegistry()->registerEntry(name_ + ".tsize", (voidP) tsizep, ECF::UINT);
00022
00023 uint* jobSize = new uint(10);
00024 state->getRegistry()->registerEntry(name_ + ".jobsize", (voidP) jobSize, ECF::UINT);
00025 }
00026
00027 bool AlgAEliGpea2::initialize(StateP state)
00028 {
00029 if(state->getCommunicator()->getCommRank() != MASTER)
00030 myJob.resize(0);
00031
00032 selectionOp[RANDOM]->initialize(state);
00033 selectionOp[WORST]->initialize(state);
00034
00035 voidP tsizep = state->getRegistry()->getEntry(name_ + ".tsize");
00036 nTournament = *((uint*) tsizep.get());
00037
00038 voidP jobSizeP = state->getRegistry()->getEntry(name_ + ".jobsize");
00039 jobSize = *((uint*) jobSizeP.get());
00040
00041 return true;
00042 }
00043
00044 bool AlgAEliGpea2::advanceGeneration(StateP state, DemeP deme)
00045 {
00046 CommunicatorP comm = state->getCommunicator();
00047
00048 if(comm->getCommRank() == MASTER) {
00049
00050 std::vector<IndividualP> evalPool;
00051 uint iIter = 0;
00052
00053 while(iIter < deme->size()) {
00054
00055
00056 for(uint ind = 0; ind < jobSize && iIter < deme->size(); ind++, iIter++) {
00057 std::vector<IndividualP> tournament;
00058 for (uint i = 0; i < nTournament; ++i) {
00059 tournament.push_back(selectionOp[RANDOM]->select(*deme));
00060 }
00061
00062 IndividualP worst = selectionOp[WORST]->select(tournament);
00063 removeFrom(worst, tournament);
00064
00065 crossover_->mate(tournament[0], tournament[1], worst);
00066
00067 std::vector<IndividualP> mutationPool;
00068 mutationPool.push_back(worst);
00069 mutation_->mutation(mutationPool);
00070
00071 if(isMember(worst, evalPool) == false)
00072 evalPool.push_back(worst);
00073 }
00074
00075
00076
00077
00078
00079 comm->recvDemeFitness(*deme, Comm::ANY_PROCESS);
00080 uint iWorker = comm->getLastSource();
00081
00082 comm->sendIndividuals(evalPool, iWorker);
00083 evalPool.resize(0);
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 uint remainingWorkers = comm->getCommSize() - 1;
00104 while(remainingWorkers > 0) {
00105 comm->recvDemeFitness(*deme, Comm::ANY_PROCESS);
00106 uint iWorker = comm->getLastSource();
00107
00108 comm->sendIndividuals(evalPool, iWorker);
00109 remainingWorkers--;
00110 }
00111 }
00112
00113 else {
00114 std::vector<IndividualP> empty;
00115 empty.resize(0);
00116 comm->sendFitness(empty, MASTER);
00117
00118 uint myJobSize;
00119 myJobSize = comm->recvReplaceIndividuals(myJob, MASTER);
00120
00121
00122 while(myJobSize > 0) {
00123 for(uint i = 0; i < myJobSize; i++)
00124 evaluate(myJob[i]);
00125
00126 comm->sendFitness(myJob, MASTER, myJobSize);
00127
00128 myJobSize = comm->recvReplaceIndividuals(myJob, MASTER);
00129 }
00130 }
00131
00132 return true;
00133 }