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

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

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);  // remove pointer 'worst' from vector '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             // perform jobSize selections and operations
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             // evaluation-waiting loop
00093             do {
00094                 // if a worker is ready
00095                 if(!evalPool.empty() && comm->messageWaiting()) {
00096                     std::vector<uint> received = comm->recvFitnessVector(*deme, Comm::ANY_PROCESS);
00097                     state->increaseEvaluations((uint) received.size());
00098                     uint iWorker = comm->getLastSource();
00099 
00100                     // send job
00101                     storeGenotypes(evalPool);
00102                     comm->sendIndividuals(evalPool, iWorker);
00103                     evalPool.resize(0);
00104 
00105                     restoreIndividuals(received);
00106                     break;  // continue main loop
00107                 }
00108                 // do something useful in the meantime...
00109                 else {
00110                     evaluate(evalPool.back());
00111                     setConsistency(evalPool.back());
00112                     evalPool.pop_back();
00113 
00114                     IndividualP newInd = performSingleTournament(deme);
00115                     iIter++;
00116                     if(isMember(newInd, evalPool) == false)
00117                         evalPool.push_back(newInd);
00118                 }
00119             } while(iIter < deme->size());
00120         }
00121 
00122     }
00123 
00124     // WORKER
00125     else {
00126         myJob_.resize(0);
00127         comm->sendFitness(myJob_, MASTER);
00128         uint myJobSize = 1;
00129 
00130         // while individuals to evaluate
00131         while(myJobSize != 0) {
00132 
00133             myJobSize = comm->recvReplaceIndividuals(myJob_, MASTER);
00134             for(uint i = 0; i < myJobSize; i++)
00135                 evaluate(myJob_[i]);
00136 
00137             comm->sendFitness(myJob_, MASTER, myJobSize);
00138         }
00139         ECF_LOG(state, 4, "Worker ends...");
00140     }
00141 
00142     return true;
00143 }
00144 
00145 
00146 void AlgAEliGpea::bcastTermination(StateP state)
00147 {
00148     // master: if terminate condition is set, send all workers empty jobs
00149     if(state->getCommunicator()->getCommRank() == 0 && state->getTerminateCond()) {
00150         std::vector<IndividualP> empty;
00151         DemeP myDeme = state->getPopulation()->getLocalDeme();
00152 
00153         for(uint iWorker = 1; iWorker < state->getCommunicator()->getCommSize(); iWorker++) {
00154             if(state->getCommunicator()->messageWaiting(iWorker)) {
00155                 std::vector<uint> received = state->getCommunicator()->recvFitnessVector(*myDeme, Comm::ANY_PROCESS);
00156                 restoreIndividuals(received);
00157             }
00158             state->getCommunicator()->sendIndividuals(empty, iWorker);
00159         }
00160 
00161         // restore individuals to last consistent state
00162         restorePopulation();
00163     }
00164 
00165     // worker: evolution is over
00166     else if(state->getCommunicator()->getCommRank() != 0)
00167         state->setTerminateCond();
00168 }

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