• Main Page
  • Modules
  • Classes
  • Files
  • File List

D:/Projekt/ECF_trunk/ECF/AlgAEliGPEA2.cpp

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             // perform jobSize selections and operations
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);  // remove pointer 'worst' from vector '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             // if a worker is ready, send individuals to evaluate
00076             //if(comm->messageWaiting(MPI::ANY_SOURCE))
00077 
00078             // OR, just wait for a worker to be ready
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         // this is redundant IF the MASTER waits for a WORKER (above)
00087         //std::vector<IndividualP> job;
00088         //int remaining = (int) evalPool.size() - 1;
00089         //while(remaining >= 0) {
00090         //  comm->recvDemeFitness(*deme, MPI::ANY_SOURCE);
00091         //  uint iWorker = comm->getLastSource();
00092 
00093         //  job.resize(0);
00094         //  int iInd;
00095         //  for(iInd = remaining; iInd >= 0 && remaining - iInd < (int) jobSize; iInd--)
00096         //      job.push_back(evalPool[iInd]);
00097         //  remaining = iInd;
00098         //  comm->sendIndividuals(job, iWorker);
00099         //}
00100         //evalPool.resize(0);
00101 
00102         // while all individuals not evaluated
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 {  // WORKER
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         // while individuals to evaluate
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 }

Generated on Tue Nov 4 2014 13:04:30 for ECF by  doxygen 1.7.1