• Main Page
  • Classes
  • Files
  • File List

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

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

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