ECF 1.5
HallOfFame.cpp
1#include "ECF_base.h"
2#include "HallOfFame.h"
3#include <string>
4#include <sstream>
5
6
7HallOfFame::HallOfFame()
8{
9 this->selectBest_ = static_cast<SelBestOpP> (new SelBestOp);
10 hofSize_ = 1;
11 bEmpty_ = true;
13}
14
15
16bool HallOfFame::initialize(StateP state)
17{
18 state_ = state;
19 bestIndividuals_.clear();
21 bestGenerations_.clear();
22 bestGenerations_.resize(hofSize_);
23 bEmpty_ = true;
25
26 return true;
27}
28
29
33bool HallOfFame::operate(StateP state)
34{
35 PopulationP population = state->getPopulation();
36
37 // iterate for all demes
38 for(uint i = 0; i < population->size(); i++) {
39 operate(*(population->at(i)));
40 }
41 return true;
42}
43
44
48bool HallOfFame::operate(const std::vector<IndividualP>& individuals)
49{
50 uint ind = 0;
51 IndividualP best;
52
53 while (ind < hofSize_) {
54 best = selectBest_->select(individuals);
55
56 // TODO: reimplement for hofSize_ > 1
57 if(bEmpty_ || best->fitness->isBetterThan(bestIndividuals_[ind]->fitness)) {
58 // copy individual to HoF
59 bestIndividuals_[ind] = (IndividualP) best->copy();
60 bestGenerations_[ind] = state_->getGenerationNo();
61 bEmpty_ = false;
62 lastChangeGen_ = state_->getGenerationNo();
63 }
64 ++ind;
65 }
66
67 return true;
68}
69
70
71void HallOfFame::write(XMLNode& xHoF)
72{
73 xHoF = XMLNode::createXMLTopNode("HallOfFame");
74 std::stringstream sValue;
75 sValue << this->hofSize_;
76 xHoF.addAttribute("size", uint2str(hofSize_).c_str());
77
78 XMLNode xInd;
79 for(uint i = 0; i < hofSize_; i++) {
80 bestIndividuals_[i]->write(xInd);
81 xInd.addAttribute("gen", uint2str(bestGenerations_[i]).c_str());
82 xHoF.addChild(xInd);
83 }
84}
85
86
87void HallOfFame::read(XMLNode& xHof)
88{
89 std::stringstream ss;
90 ss << xHof.getAttribute("size");
91 ss >> hofSize_;
92 ss.clear(); ss.str("");
93
95 bestGenerations_.resize(hofSize_);
96 bEmpty_ = false;
98
99 XMLNode xInd;
100 for(uint i = 0; i < hofSize_; i++) {
101 xInd = xHof.getChildNode((int)i);
102 ss << xInd.getAttribute("gen");
103 ss >> bestGenerations_[i];
104 ss.clear(); ss.str("");
105 bestIndividuals_[i] = (IndividualP) state_->getIndividualObject()->copy();
106 bestIndividuals_[i]->read(xInd);
107
108 if(lastChangeGen_ < bestGenerations_[i])
109 lastChangeGen_ = bestGenerations_[i];
110 }
111}
uint hofSize_
no. of individuals in HoF
Definition: HallOfFame.h:19
void read(XMLNode &)
Read operator state from XMLNode or the Registry. Called after Operator::initialize.
Definition: HallOfFame.cpp:87
bool bEmpty_
is HoF empty
Definition: HallOfFame.h:16
void write(XMLNode &)
Write operator state to XMLNode or the Registry. Called after Operator::initialize.
Definition: HallOfFame.cpp:71
std::vector< IndividualP > bestIndividuals_
vector of individuals in HoF
Definition: HallOfFame.h:20
uint lastChangeGen_
generation of last update
Definition: HallOfFame.h:18
bool initialize(StateP)
Perform initialization. Called before Operator::operate. By default, if the return value is false,...
Definition: HallOfFame.cpp:16
bool operate(StateP)
Collect best individuals of the whole Population.
Definition: HallOfFame.cpp:33
Best individual selection operator.
Definition: SelBestOp.h:10