• Main Page
  • Classes
  • Files
  • File List

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

Generated on Wed Sep 1 2010 14:31:21 for ECF by  doxygen 1.7.1