00001 #include "ECF_base.h"
00002 #include<sstream>
00003
00004
00005 Individual::Individual(StateP state)
00006 {
00007 cid = 0;
00008 initialize(state);
00009 }
00010
00011
00012 bool Individual::initialize(StateP state)
00013 {
00014 state_ = state;
00015
00016
00017 for(uint i = 0; i < state->getGenotypes().size(); i++) {
00018 this->push_back(static_cast<GenotypeP> (state->getGenotypes()[i]->copy()));
00019 (*this)[i]->setGenotypeId(i);
00020 }
00021
00022
00023 for(uint i = 0; i < this->size(); i++)
00024 (*this)[i]->initialize(state);
00025
00026 return true;
00027 }
00028
00029
00030 Individual* Individual::copy()
00031 {
00032 Individual *c = new Individual;
00033 c->state_ = state_;
00034 c->cid = cid;
00035 if(fitness)
00036 c->fitness = (FitnessP) this->fitness->copy();
00037 for(uint i = 0; i < this->size(); i++)
00038 c->push_back((GenotypeP) (this->at(i)->copy()));
00039 return c;
00040 }
00041
00042
00043 GenotypeP Individual::getGenotype(uint index)
00044 {
00045 return this->at(index);
00046 }
00047
00048
00055 void Individual::write(XMLNode &xIndividual)
00056 {
00057 xIndividual = XMLNode::createXMLTopNode("Individual");
00058 std::stringstream sValue;
00059 sValue << this->size();
00060 xIndividual.addAttribute("size", sValue.str().c_str());
00061
00062 if (fitness) {
00063 XMLNode xFitness;
00064 fitness->write(xFitness);
00065 xIndividual.addChild(xFitness);
00066 } else {
00067 XMLNode xFitness = XMLNode::createXMLTopNode("Fitness");
00068 xIndividual.addChild(xFitness);
00069 }
00070
00071 XMLNode xGenotype;
00072 for(uint i = 0; i < this->size(); i++) {
00073 this->at(i)->write(xGenotype);
00074 xIndividual.addChild(xGenotype);
00075 }
00076 }
00077
00078
00085 void Individual::read(XMLNode &xIndividual)
00086 {
00087 XMLNode xFit = xIndividual.getChildNode(0);
00088
00089 if(!fitness)
00090 fitness = static_cast<FitnessP> (state_->getFitnessObject()->copy());
00091 this->fitness->read(xFit);
00092
00093 for(uint i = 0; i < this->size(); i++) {
00094 XMLNode xGen = xIndividual.getChildNode(i + 1);
00095 this->at(i)->read(xGen);
00096 }
00097 }