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

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

00001 #include "ECF_base.h"
00002 #include <string.h>
00003 
00004 
00005 Population::Population()
00006 {
00007     hof_ = static_cast<HallOfFameP> (new HallOfFame);
00008     stats_ = static_cast<StatCalcP> (new StatCalc);
00009     
00010     nIndividuals_ = 100;
00011     nDemes_ = 1;
00012     myDemeIndex_ = 0;
00013 }
00014 
00015 
00016 void Population::registerParameters(StateP state)
00017 {
00018     int *sizep = new int(100);
00019     state->getRegistry()->registerEntry("population.size", (voidP) sizep, ECF::UINT);
00020     int *demep = new int(1);
00021     state->getRegistry()->registerEntry("population.demes", (voidP) demep, ECF::UINT);
00022 
00023     stats_->registerParameters(state);
00024 }
00025 
00026 
00027 #ifndef _MPI    // slijedna verzija
00028 
00029 
00033 bool Population::initialize(StateP state)
00034 {
00035     state_ = state;
00036     this->clear();
00037 
00038     hof_ = static_cast<HallOfFameP> (new HallOfFame);
00039     hof_->initialize(state);
00040 
00041     stats_ = static_cast<StatCalcP> (new StatCalc);
00042     stats_->initialize(state);
00043 
00044     voidP sptr = state->getRegistry()->getEntry("population.size");
00045     nIndividuals_ = *((uint*) sptr.get());
00046     sptr = state->getRegistry()->getEntry("population.demes");
00047     nDemes_ = *((uint*) sptr.get());
00048 
00049     for(uint i = 0; i < nDemes_; i++){
00050         this->push_back(static_cast<DemeP> (new Deme));
00051         this->back()->getSize() = nIndividuals_;
00052         this->back()->initialize(state);
00053     }
00054 
00055     return true;
00056 }
00057 
00058 
00063 void Population::read(XMLNode &xPopulation)
00064 {
00065     XMLNode xHof = xPopulation.getChildNode(0);
00066     this->hof_->read(xHof);
00067 
00068     for(uint i = 0; i < this->size(); i++) {
00069         XMLNode xDeme = xPopulation.getChildNode(i + 1);
00070         this->at(i)->read(xDeme);
00071     }
00072 }
00073 
00074 
00078 void Population::write(XMLNode &xPopulation)
00079 {
00080     xPopulation = XMLNode::createXMLTopNode(NODE_POPULATION);
00081     xPopulation.addAttribute("size", uint2str(nDemes_).c_str());    // number of demes
00082 
00083     XMLNode xHoF;
00084     hof_->write(xHoF);
00085     xPopulation.addChild(xHoF);
00086 
00087     for(uint iDeme = 0; iDeme < nDemes_; iDeme++) {
00088         XMLNode xDeme;
00089         this->at(iDeme)->write(xDeme);
00090         xPopulation.addChild(xDeme);
00091     }
00092 }
00093 
00094 
00098 void Population::updateDemeStats()
00099 {   
00100     ECF_LOG(state_, 4, "Population: updating HoF and statistics of all demes");
00101 
00102     // operate statistics on all demes
00103     for(uint iDeme = 0; iDeme < this->size(); iDeme++) {
00104         DemeP deme = this->at(iDeme);
00105         deme->stats_->operate(*(deme));
00106         ECF_LOG(state_, 3, "Deme: " + uint2str(iDeme));
00107         deme->stats_->log();
00108     }
00109 
00110     // copy stats from first deme, update with others
00111     stats_->copyStats(this->at(0)->stats_);
00112 
00113     if(this->nDemes_ > 1) {
00114         for(uint iDeme = 1; iDeme < this->size(); iDeme++) 
00115             stats_->update(this->at(iDeme)->stats_->getStats());
00116         ECF_LOG(state_, 3, "Population:");
00117         stats_->log();
00118     }
00119 
00120     // gather HoF
00121     std::vector<IndividualP> pool;
00122     for(uint iDeme = 0; iDeme < this->size(); iDeme++) {
00123         this->at(iDeme)->hof_->operate(*(this->at(iDeme)));
00124         std::vector<IndividualP> bestOfDeme = this->at(iDeme)->hof_->getBest();
00125         for(uint i = 0; i < bestOfDeme.size(); i++)
00126             pool.push_back(bestOfDeme[i]);
00127         hof_->operate(pool);
00128     }
00129 }
00130 
00131 
00132 #else   // _MPI - paralelna verzija
00133 
00134 
00138 bool Population::initialize(StateP state)
00139 {
00140     this->clear();
00141     state_ = state;
00142 
00143     voidP sptr = state->getRegistry()->getEntry("population.size");
00144     nIndividuals_ = *((uint*) sptr.get());
00145     sptr = state->getRegistry()->getEntry("population.demes");
00146     nDemes_ = *((uint*) sptr.get());
00147 
00148     hof_ = static_cast<HallOfFameP> (new HallOfFame);
00149     hof_->initialize(state);
00150 
00151     stats_ = static_cast<StatCalcP> (new StatCalc);
00152     stats_->initialize(state);
00153 
00154     // single deme:
00155     if(nDemes_ == 1) {
00156         this->push_back(static_cast<DemeP> (new Deme));
00157         this->back()->getSize() = nIndividuals_;
00158         this->back()->initialize(state);
00159         myDemeIndex_ = 0;
00160         return true;
00161     }
00162 
00163     // multiple demes:
00164     myDemeIndex_ = state->getCommunicator()->createDemeCommunicator(nDemes_);
00165     this->push_back(static_cast<DemeP> (new Deme));
00166     this->back()->getSize() = nIndividuals_;
00167     this->back()->initialize(state);
00168 
00169     return true;
00170 }
00171 
00172 
00176 void Population::updateDemeStats()
00177 {
00178     ECF_LOG(state_, 4, "Population: updating HoF and statistics of all demes");
00179     // every process 'processes' its local deme
00180     ECF_LOG(state_, 3, "Deme: " + uint2str(getLocalDemeId()));
00181     this->at(0)->hof_->operate(*(getLocalDeme()));
00182 
00183     // operate hof and stats on local deme
00184     std::vector<IndividualP> bestPool = this->at(0)->hof_->getBest();
00185     hof_->operate(bestPool);
00186 
00187     this->at(0)->stats_->operate(*(getLocalDeme()));
00188     stats_->copyStats(this->at(0)->stats_);
00189     stats_->log();
00190 
00191     // update with remote demes
00192     if(nDemes_ > 1) {       // vise demova - svaki lokalni proces 0 dodje ovdje
00193         // globalni proces 0 prima hof-ove i statistiku od ostalih deme mastera
00194         if(state_->getCommunicator()->getCommGlobalRank() == 0) {
00195 
00196             std::vector<IndividualP> bestPool, received;
00197             bestPool = this->at(0)->hof_->getBest();    // uzmi moj hof za pocetak
00198 
00199             for(uint iDeme = 1; iDeme < nDemes_; iDeme++) { // dodaj ostale
00200                 received = state_->getCommunicator()->recvIndividualsGlobal();
00201                 for(uint ind = 0; ind < received.size(); ind++)
00202                     bestPool.push_back(received[ind]);
00203             }
00204 
00205             hof_->operate(bestPool);    // konacno, odredi globalni hof
00206 
00207             // gather statistics
00208             std::vector<double> statValues;
00209             for(uint iDeme = 1; iDeme < nDemes_; iDeme++) {
00210                 statValues = state_->getCommunicator()->recvValuesGlobal();
00211                 stats_->update(statValues);
00212             }
00213             ECF_LOG(state_, 3, "Population: ");
00214             stats_->log();
00215         }
00216         else {  // saljem globalnom 0
00217             // send HoF
00218             std::vector<IndividualP> myBest = this->at(0)->hof_->getBest();
00219             state_->getCommunicator()->sendIndividualsGlobal(myBest, 0);
00220             // send stats
00221             std::vector<double> myStats = stats_->getStats();
00222             state_->getCommunicator()->sendValuesGlobal(myStats, 0);
00223         }
00224     }
00225 }
00226 
00227 
00231 void Population::write(XMLNode &xPopulation)
00232 {
00233     CommunicatorP comm = state_->getCommunicator();
00234 
00235     // only deme masters continue
00236     if(comm->getCommRank() != 0)
00237         return;
00238 
00239     xPopulation = XMLNode::createXMLTopNode(NODE_POPULATION);
00240     xPopulation.addAttribute("size", uint2str(nDemes_).c_str());
00241 
00242     XMLNode xHoF;
00243     hof_->write(xHoF);
00244     xPopulation.addChild(xHoF);
00245 
00246     // write local deme
00247     XMLNode xDeme;
00248     this->at(0)->write(xDeme);
00249     xPopulation.addChild(xDeme);
00250 
00251     // collect remote demes
00252     if(comm->getCommGlobalRank() == 0) {
00253         for(uint iDeme = 1; iDeme < nDemes_; iDeme++) {
00254             XMLNode xDeme;
00255             voidP msg = comm->recvDataGlobal();
00256             xDeme = XMLNode::parseString((char*) msg.get(), "Deme");
00257             xPopulation.addChild(xDeme);
00258         }
00259     }
00260     else {
00261         char* message = xDeme.createXMLString();
00262         comm->sendDataGlobal((voidP) message, (uint) strlen(message), 0);
00263     }
00264 }
00265 
00266 
00271 void Population::read(XMLNode &xPopulation)
00272 {
00273     CommunicatorP comm = state_->getCommunicator();
00274 
00275     // only deme masters continue
00276     if(comm->getCommRank() != 0)
00277         return;
00278 
00279     XMLNode xHof = xPopulation.getChildNode(0);
00280     this->hof_->read(xHof);
00281 
00282     // read the designated deme
00283     XMLNode xDeme = xPopulation.getChildNode(getLocalDemeId() + 1);
00284     getLocalDeme()->read(xDeme);
00285 }
00286 
00287 
00288 #endif  // _MPI

Generated on Fri Jul 5 2013 09:34:23 for ECF by  doxygen 1.7.1