ECF 1.5
Individual.cpp
1#include "ECF_base.h"
2#include<sstream>
3
4
5Individual::Individual(StateP state)
6{
7 cid = 0;
8 initialize(state);
9}
10
11
12bool Individual::initialize(StateP state)
13{
14 state_ = state;
15 this->clear();
16
17 // copy genotypes from State
18 for(uint i = 0; i < state->getGenotypes().size(); i++) {
19 this->push_back(static_cast<GenotypeP> (state->getGenotypes()[i]->copy()));
20 (*this)[i]->setGenotypeId(i);
21 }
22
23 // init genotypes
24 for(uint i = 0; i < this->size(); i++)
25 (*this)[i]->initialize(state);
26
27 return true;
28}
29
30
32{
33 Individual *c = new Individual;
34 c->state_ = state_;
35 c->cid = cid;
36 if(fitness)
37 c->fitness = (FitnessP) this->fitness->copy();
38 for(uint i = 0; i < this->size(); i++)
39 c->push_back((GenotypeP) (this->at(i)->copy()));
40 return c;
41}
42
43
44GenotypeP Individual::getGenotype(uint index)
45{
46 return this->at(index);
47}
48
49
51{
52 return fitness;
53}
54
55
62void Individual::write(XMLNode &xIndividual)
63{
64 xIndividual = XMLNode::createXMLTopNode("Individual");
65 std::stringstream sValue;
66 sValue << this->size();
67 xIndividual.addAttribute("size", sValue.str().c_str());
68
69 if (fitness) {
70 XMLNode xFitness;
71 fitness->write(xFitness);
72 xIndividual.addChild(xFitness);
73 } else {
74 XMLNode xFitness = XMLNode::createXMLTopNode("Fitness");
75 xIndividual.addChild(xFitness);
76 }
77
78 XMLNode xGenotype;
79 for(uint i = 0; i < this->size(); i++) {
80 this->at(i)->write(xGenotype);
81 xIndividual.addChild(xGenotype);
82 }
83}
84
85
87{
88 XMLNode xInd;
89 write(xInd);
90 char *s = xInd.createXMLString();
91 std::string out(s);
92 freeXMLString(s);
93 return out;
94}
95
96
103void Individual::read(XMLNode &xIndividual)
104{
105 XMLNode xFit = xIndividual.getChildNode(0);
106
107 if(!fitness)
108 fitness = static_cast<FitnessP> (state_->getFitnessObject()->copy());
109 this->fitness->read(xFit);
110
111 for(uint i = 0; i < this->size(); i++) {
112 XMLNode xGen = xIndividual.getChildNode((int)i + 1);
113 this->at(i)->read(xGen);
114 }
115}
Individual class - inherits a vector of Genotype objects.
Definition: Individual.h:12
FitnessP getFitness()
return sptr to individual's fitness object
Definition: Individual.cpp:50
uint cid
coherence index, used in asynchronous parallel algoritmhs
Definition: Individual.h:42
GenotypeP getGenotype(uint index=0)
return genotype with given index
Definition: Individual.cpp:44
void write(XMLNode &)
write individual to XML node
Definition: Individual.cpp:62
void read(XMLNode &)
read individual from XML node
Definition: Individual.cpp:103
bool initialize(StateP state)
initialize individual
Definition: Individual.cpp:12
std::string toString()
output individual to string
Definition: Individual.cpp:86
FitnessP fitness
sptr to individual's fitness object
Definition: Individual.h:38
uint index
individual's index in Deme
Definition: Individual.h:40
Individual * copy()
create a copy of the individual
Definition: Individual.cpp:31