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

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

00001 #include "ECF_base.h"
00002 #include "HallOfFame.h"
00003 #include <string>
00004 #include <sstream>
00005 
00006 
00007 HallOfFame::HallOfFame() 
00008 {
00009     this->selectBest_ = static_cast<SelBestOpP> (new SelBestOp);
00010     hofSize_ = 1;
00011     bEmpty_ = true;
00012     lastChangeGen_ = 0;
00013 }
00014 
00015 
00016 bool HallOfFame::initialize(StateP state)
00017 {
00018     state_ = state;
00019     bestIndividuals_.clear();
00020     bestIndividuals_.resize(hofSize_);
00021     bestGenerations_.clear();
00022     bestGenerations_.resize(hofSize_);
00023     bEmpty_ = true;
00024     lastChangeGen_ = 0;
00025 
00026     return true;
00027 }
00028 
00029 
00033 bool HallOfFame::operate(StateP state)
00034 {
00035     PopulationP population = state->getPopulation();
00036 
00037     // iterate for all demes
00038     for(uint i = 0; i < population->size(); i++) {
00039         operate(*(population->at(i)));
00040     }
00041     return true;
00042 }
00043 
00044 
00048 bool HallOfFame::operate(const std::vector<IndividualP>& individuals)
00049 {
00050     uint ind = 0;
00051     IndividualP best;
00052 
00053     while (ind < hofSize_) {
00054         best = selectBest_->select(individuals);
00055 
00056         // TODO: reimplement for hofSize_ > 1
00057         if(bEmpty_ || best->fitness->isBetterThan(bestIndividuals_[ind]->fitness)) {    
00058             // copy individual to HoF
00059             bestIndividuals_[ind] = (IndividualP) best->copy();
00060             bestGenerations_[ind] = state_->getGenerationNo();
00061             bEmpty_ = false;
00062             lastChangeGen_ = state_->getGenerationNo();
00063         }
00064         ++ind;
00065     }
00066 
00067     return true;
00068 }
00069 
00070 
00071 void HallOfFame::write(XMLNode& xHoF)
00072 {
00073     xHoF = XMLNode::createXMLTopNode("HallOfFame");
00074     std::stringstream sValue;
00075     sValue << this->hofSize_;
00076     xHoF.addAttribute("size", uint2str(hofSize_).c_str());
00077 
00078     XMLNode xInd;
00079     for(uint i = 0; i < hofSize_; i++) {
00080         bestIndividuals_[i]->write(xInd);
00081         xInd.addAttribute("gen", uint2str(bestGenerations_[i]).c_str());
00082         xHoF.addChild(xInd);
00083     }
00084 }
00085 
00086 
00087 void HallOfFame::read(XMLNode& xHof)
00088 {
00089     std::stringstream ss;
00090     ss << xHof.getAttribute("size");
00091     ss >> hofSize_;
00092     ss.clear(); ss.str("");
00093 
00094     bestIndividuals_.resize(hofSize_);
00095     bestGenerations_.resize(hofSize_);
00096     bEmpty_ = false;
00097     lastChangeGen_ = 0;
00098 
00099     XMLNode xInd;
00100     for(uint i = 0; i < hofSize_; i++) {
00101         xInd = xHof.getChildNode(i);
00102         ss << xInd.getAttribute("gen");
00103         ss >> bestGenerations_[i];
00104         ss.clear(); ss.str("");
00105         bestIndividuals_[i] = (IndividualP) state_->getIndividualObject()->copy();
00106         bestIndividuals_[i]->read(xInd);
00107 
00108         if(lastChangeGen_ < bestGenerations_[i])
00109             lastChangeGen_ = bestGenerations_[i];
00110     }
00111 }

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