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

Generated on Thu Oct 6 2011 13:41:01 for ECF by  doxygen 1.7.1