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 "number of individuals (default: 100)");
00021 int *demep = new int(1);
00022 state->getRegistry()->registerEntry("population.demes", (voidP) demep, ECF::UINT,
00023 "number of demes (default: 1)");
00024
00025 stats_->registerParameters(state);
00026 }
00027
00028
00029 #ifndef _MPI // slijedna verzija
00030
00031
00035 bool Population::initialize(StateP state)
00036 {
00037 state_ = state;
00038 this->clear();
00039
00040 hof_ = static_cast<HallOfFameP> (new HallOfFame);
00041 hof_->initialize(state);
00042
00043 stats_ = static_cast<StatCalcP> (new StatCalc);
00044 stats_->initialize(state);
00045
00046 voidP sptr = state->getRegistry()->getEntry("population.size");
00047 nIndividuals_ = *((uint*) sptr.get());
00048 sptr = state->getRegistry()->getEntry("population.demes");
00049 nDemes_ = *((uint*) sptr.get());
00050
00051 for(uint i = 0; i < nDemes_; i++){
00052 this->push_back(static_cast<DemeP> (new Deme));
00053 this->back()->getSize() = nIndividuals_;
00054 this->back()->initialize(state);
00055 }
00056
00057 return true;
00058 }
00059
00060
00065 void Population::read(XMLNode &xPopulation)
00066 {
00067 XMLNode xHof = xPopulation.getChildNode(0);
00068 this->hof_->read(xHof);
00069
00070 for(uint i = 0; i < this->size(); i++) {
00071 XMLNode xDeme = xPopulation.getChildNode(i + 1);
00072 this->at(i)->read(xDeme);
00073 }
00074 }
00075
00076
00080 void Population::write(XMLNode &xPopulation)
00081 {
00082 xPopulation = XMLNode::createXMLTopNode(NODE_POPULATION);
00083 xPopulation.addAttribute("size", uint2str(nDemes_).c_str());
00084
00085 XMLNode xHoF;
00086 hof_->write(xHoF);
00087 xPopulation.addChild(xHoF);
00088
00089 for(uint iDeme = 0; iDeme < nDemes_; iDeme++) {
00090 XMLNode xDeme;
00091 this->at(iDeme)->write(xDeme);
00092 xPopulation.addChild(xDeme);
00093 }
00094 }
00095
00096
00100 void Population::updateDemeStats()
00101 {
00102 ECF_LOG(state_, 4, "Population: updating HoF and statistics of all demes");
00103
00104
00105 for(uint iDeme = 0; iDeme < this->size(); iDeme++) {
00106 DemeP deme = this->at(iDeme);
00107 deme->stats_->operate(*(deme));
00108 ECF_LOG(state_, 3, "Deme: " + uint2str(iDeme));
00109 deme->stats_->log();
00110 }
00111
00112
00113 stats_->copyStats(this->at(0)->stats_);
00114
00115 if(this->nDemes_ > 1) {
00116 for(uint iDeme = 1; iDeme < this->size(); iDeme++)
00117 stats_->update(this->at(iDeme)->stats_->getStats());
00118 ECF_LOG(state_, 3, "Population:");
00119 stats_->log();
00120 }
00121
00122
00123 std::vector<IndividualP> pool;
00124 for(uint iDeme = 0; iDeme < this->size(); iDeme++) {
00125 this->at(iDeme)->hof_->operate(*(this->at(iDeme)));
00126 std::vector<IndividualP> bestOfDeme = this->at(iDeme)->hof_->getBest();
00127 for(uint i = 0; i < bestOfDeme.size(); i++)
00128 pool.push_back(bestOfDeme[i]);
00129 hof_->operate(pool);
00130 }
00131 }
00132
00133
00134 #else // _MPI - paralelna verzija
00135
00136
00140 bool Population::initialize(StateP state)
00141 {
00142 this->clear();
00143 state_ = state;
00144
00145 voidP sptr = state->getRegistry()->getEntry("population.size");
00146 nIndividuals_ = *((uint*) sptr.get());
00147 sptr = state->getRegistry()->getEntry("population.demes");
00148 nDemes_ = *((uint*) sptr.get());
00149
00150 hof_ = static_cast<HallOfFameP> (new HallOfFame);
00151 hof_->initialize(state);
00152
00153 stats_ = static_cast<StatCalcP> (new StatCalc);
00154 stats_->initialize(state);
00155
00156
00157 if(nDemes_ == 1) {
00158 this->push_back(static_cast<DemeP> (new Deme));
00159 this->back()->getSize() = nIndividuals_;
00160 this->back()->initialize(state);
00161 myDemeIndex_ = 0;
00162 return true;
00163 }
00164
00165
00166 myDemeIndex_ = state->getCommunicator()->createDemeCommunicator(nDemes_);
00167 this->push_back(static_cast<DemeP> (new Deme));
00168 this->back()->getSize() = nIndividuals_;
00169 this->back()->initialize(state);
00170
00171 return true;
00172 }
00173
00174
00178 void Population::updateDemeStats()
00179 {
00180 ECF_LOG(state_, 4, "Population: updating HoF and statistics of all demes");
00181
00182 ECF_LOG(state_, 3, "Deme: " + uint2str(getLocalDemeId()));
00183 this->at(0)->hof_->operate(*(getLocalDeme()));
00184
00185
00186 std::vector<IndividualP> bestPool = this->at(0)->hof_->getBest();
00187 hof_->operate(bestPool);
00188
00189 this->at(0)->stats_->operate(*(getLocalDeme()));
00190 stats_->copyStats(this->at(0)->stats_);
00191 stats_->log();
00192
00193
00194 if(nDemes_ > 1) {
00195
00196 if(state_->getCommunicator()->getCommGlobalRank() == 0) {
00197
00198 std::vector<IndividualP> bestPool, received;
00199 bestPool = this->at(0)->hof_->getBest();
00200
00201 for(uint iDeme = 1; iDeme < nDemes_; iDeme++) {
00202 received = state_->getCommunicator()->recvIndividualsGlobal();
00203 for(uint ind = 0; ind < received.size(); ind++)
00204 bestPool.push_back(received[ind]);
00205 }
00206
00207 hof_->operate(bestPool);
00208
00209
00210 std::vector<double> statValues;
00211 for(uint iDeme = 1; iDeme < nDemes_; iDeme++) {
00212 statValues = state_->getCommunicator()->recvValuesGlobal();
00213 stats_->update(statValues);
00214 }
00215 ECF_LOG(state_, 3, "Population: ");
00216 stats_->log();
00217 }
00218 else {
00219
00220 std::vector<IndividualP> myBest = this->at(0)->hof_->getBest();
00221 state_->getCommunicator()->sendIndividualsGlobal(myBest, 0);
00222
00223 std::vector<double> myStats = stats_->getStats();
00224 state_->getCommunicator()->sendValuesGlobal(myStats, 0);
00225 }
00226 }
00227 }
00228
00229
00233 void Population::write(XMLNode &xPopulation)
00234 {
00235 CommunicatorP comm = state_->getCommunicator();
00236
00237
00238 if(comm->getCommRank() != 0)
00239 return;
00240
00241 xPopulation = XMLNode::createXMLTopNode(NODE_POPULATION);
00242 xPopulation.addAttribute("size", uint2str(nDemes_).c_str());
00243
00244 XMLNode xHoF;
00245 hof_->write(xHoF);
00246 xPopulation.addChild(xHoF);
00247
00248
00249 XMLNode xDeme;
00250 this->at(0)->write(xDeme);
00251 xPopulation.addChild(xDeme);
00252
00253
00254 if(comm->getCommGlobalRank() == 0) {
00255 for(uint iDeme = 1; iDeme < nDemes_; iDeme++) {
00256 XMLNode xDeme;
00257 voidP msg = comm->recvDataGlobal();
00258 xDeme = XMLNode::parseString((char*) msg.get(), "Deme");
00259 xPopulation.addChild(xDeme);
00260 }
00261 }
00262 else {
00263 char* message = xDeme.createXMLString();
00264 comm->sendDataGlobal((voidP) message, (uint) strlen(message), 0);
00265 }
00266 }
00267
00268
00273 void Population::read(XMLNode &xPopulation)
00274 {
00275 CommunicatorP comm = state_->getCommunicator();
00276
00277
00278 if(comm->getCommRank() != 0)
00279 return;
00280
00281 XMLNode xHof = xPopulation.getChildNode(0);
00282 this->hof_->read(xHof);
00283
00284
00285 XMLNode xDeme = xPopulation.getChildNode(getLocalDemeId() + 1);
00286 getLocalDeme()->read(xDeme);
00287 }
00288
00289
00290 #endif // _MPI