00001 #include "ECF_base.h"
00002 #include "ECF_derived.h"
00003 #include "ECF_macro.h"
00004
00005 #include "AlgAEliGPEA.h"
00006
00007 const int MASTER = 0;
00008 const int RANDOM = 0;
00009 const int WORST = 1;
00010
00011
00012 AlgAEliGpea::AlgAEliGpea()
00013 {
00014 name_ = "AlgAEliGPEA";
00015 selectionOp.push_back(static_cast<SelectionOperatorP> (new SelRandomOp));
00016 selectionOp.push_back(static_cast<SelectionOperatorP> (new SelWorstOp));
00017 }
00018
00019
00020 void AlgAEliGpea::registerParameters(StateP state)
00021 {
00022 int* tsizep = new int(3);
00023 state->getRegistry()->registerEntry(name_ + ".tsize", (voidP) tsizep, ECF::UINT);
00024
00025 uint* jobSize = new uint(10);
00026 state->getRegistry()->registerEntry(name_ + ".jobsize", (voidP) jobSize, ECF::UINT);
00027 }
00028
00029
00030 bool AlgAEliGpea::initialize(StateP state)
00031 {
00032 if(state->getCommunicator()->getCommRank() != MASTER)
00033 myJob_.resize(0);
00034
00035 selectionOp[RANDOM]->initialize(state);
00036 selectionOp[WORST]->initialize(state);
00037
00038 voidP tsizep = state->getRegistry()->getEntry(name_ + ".tsize");
00039 nTournament_ = *((uint*) tsizep.get());
00040
00041 if(nTournament_ < 3) {
00042 ECF_LOG_ERROR(state, "Error: AlgAEliGPEA algorithm requires minimum tournament size of 3!");
00043 throw "";
00044 }
00045
00046 voidP jobSizeP = state->getRegistry()->getEntry(name_ + ".jobsize");
00047 jobSize_ = *((uint*) jobSizeP.get());
00048
00049 return true;
00050 }
00051
00052
00053 IndividualP AlgAEliGpea::performSingleTournament(DemeP deme)
00054 {
00055 std::vector<IndividualP> tournament;
00056 for (uint i = 0; i < nTournament_; ++i) {
00057 tournament.push_back(selectionOp[RANDOM]->select(*deme));
00058 }
00059
00060 IndividualP worst = selectionOp[WORST]->select(tournament);
00061 storeIndividual(worst);
00062
00063 removeFrom(worst, tournament);
00064
00065 mate(tournament[0], tournament[1], worst);
00066
00067 mutate(worst);
00068
00069 return worst;
00070 }
00071
00072
00073 bool AlgAEliGpea::advanceGeneration(StateP state, DemeP deme)
00074 {
00075 CommunicatorP comm = state->getCommunicator();
00076
00077 if(comm->getCommRank() == MASTER) {
00078
00079 std::vector<IndividualP> evalPool;
00080 uint iIter = 0;
00081
00082 while(iIter < deme->size()) {
00083
00084
00085 for(uint ind = 0; ind < jobSize_ && iIter < deme->size(); ind++, iIter++) {
00086 IndividualP newInd = performSingleTournament(deme);
00087
00088 if(isMember(newInd, evalPool) == false)
00089 evalPool.push_back(newInd);
00090 }
00091
00092
00093 do {
00094
00095 if(evalPool.size() > 0 && comm->messageWaiting()) {
00096 std::vector<uint> received = comm->recvFitnessVector(*deme, Comm::ANY_PROCESS);
00097 uint iWorker = comm->getLastSource();
00098
00099
00100 storeGenotypes(evalPool);
00101 comm->sendIndividuals(evalPool, iWorker);
00102 evalPool.resize(0);
00103
00104 restoreIndividuals(received);
00105 break;
00106 }
00107
00108 else {
00109 evaluate(evalPool.back());
00110 setConsistency(evalPool.back());
00111 evalPool.pop_back();
00112
00113 IndividualP newInd = performSingleTournament(deme);
00114 iIter++;
00115 if(isMember(newInd, evalPool) == false)
00116 evalPool.push_back(newInd);
00117 }
00118 } while(iIter < deme->size());
00119 }
00120
00121 }
00122
00123
00124 else {
00125 myJob_.resize(0);
00126 comm->sendFitness(myJob_, MASTER);
00127 uint myJobSize = 1;
00128
00129
00130 while(myJobSize != 0) {
00131
00132 myJobSize = comm->recvReplaceIndividuals(myJob_, MASTER);
00133 for(uint i = 0; i < myJobSize; i++)
00134 evaluate(myJob_[i]);
00135
00136 comm->sendFitness(myJob_, MASTER, myJobSize);
00137 }
00138 ECF_LOG(state, 4, "Worker ends...");
00139 }
00140
00141 return true;
00142 }
00143
00144
00145 void AlgAEliGpea::bcastTermination(StateP state)
00146 {
00147
00148 if(state->getCommunicator()->getCommRank() == 0 && state->getTerminateCond()) {
00149 std::vector<IndividualP> empty;
00150 DemeP myDeme = state->getPopulation()->getLocalDeme();
00151
00152 for(uint iWorker = 1; iWorker < state->getCommunicator()->getCommSize(); iWorker++) {
00153 if(state->getCommunicator()->messageWaiting(iWorker)) {
00154 std::vector<uint> received = state->getCommunicator()->recvFitnessVector(*myDeme, Comm::ANY_PROCESS);
00155 restoreIndividuals(received);
00156 }
00157 state->getCommunicator()->sendIndividuals(empty, iWorker);
00158 }
00159
00160
00161 restorePopulation();
00162 }
00163
00164
00165 else if(state->getCommunicator()->getCommRank() != 0)
00166 state->setTerminateCond();
00167 }