Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Individual.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2008-2011 LpzRobots development team *
3  * Joerg Weider <joergweide84 at aol dot com> (robot12) *
4  * Georg Martius <georg dot martius at web dot de> *
5  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
6  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
7  * Ralf Der <ralfder at mis dot mpg dot de> *
8  * Joern Hoffmann <jhoffmann at informatik dot uni-leipzig dot de *
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  * This program is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18  * GNU General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU General Public License *
21  * along with this program; if not, write to the *
22  * Free Software Foundation, Inc., *
23  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
24  * *
25  ***************************************************************************/
26 
27 #ifndef INDIVIDUAL_H_
28 #define INDIVIDUAL_H_
29 
30 //includes
31 #include <vector>
32 #include <string>
33 #include <map>
34 
35 //ga_tools includes
36 #include "Gen.h"
37 #include "SingletonGenEngine.h"
38 
39 //forward declaration
41 
42 /**
43  * This class represent one individual of the complete gen. alg. It have some gens and a fitness.
44  */
45 class Individual {
46 public:
47  /**
48  * constructor
49  * Create the individual with the name "name"
50  * @param name (sting) the name of the new individual
51  * @param id (int) an unique ID
52  * @param p1 (Individual*) parent 1 or zero if it random generate
53  * @param p2 (Individual*) parent 2 or zero if it random generate
54  */
55  Individual(std::string name, int id, Individual* p1=0, Individual* p2=0);
56 
57  /**
58  * default destructor
59  */
60  virtual ~Individual();
61 
62  /**
63  * return the ID of the individual
64  * @return (int) the ID
65  */
66  inline int getID(void)const {return m_ID;}
67 
68  /**
69  * return the name of the individual
70  * @return (string) the name
71  */
72  inline std::string getName(void)const {return m_name;}
73 
74  /**
75  * return the size of the individual. This mean the number of gens inside the individual.
76  * @return (int) number of gens
77  */
78  inline int getSize(void)const {return m_gene.size();}
79 
80  /**
81  * return a gen of the individual
82  * @param x (the index of the gen
83  * @return (Gen*) the searched gen. NULL if the index isn't right
84  */
85  inline Gen* getGen(int x)const {if(x<getSize())return m_gene[x];return NULL;}
86 
87  /**
88  * add a gen to the individual.
89  * @param gen (Gen*) the new gen
90  */
91  inline void addGen(Gen* gen) {m_gene.push_back(gen);}
92 
93  /**
94  * returns all gens of the individual
95  * @return (vector<Gen*>&) all gens
96  */
97  inline const std::vector<Gen*>& getGene(void)const {return m_gene;}
98 
99  /**
100  * remove a specified gen from the individual
101  * @param gen (Gen*) the gen which should removed from the individual
102  */
103  void removeGen(Gen* gen);
104 
105  /**
106  * remove a specified gen from the individual
107  * @param x (the index of the gen, which should b removed.
108  */
109  void removeGen(int x);
110 
111  /**
112  * this function calculate the fitness value of the individual
113  * @return fitness value
114  */
115  double getFitness();
116 
117  /**
118  * this function calculate the fitness value of the individual (const)
119  * @return fitness value
120  */
121  double getFitnessC()const;
122 
123  /**
124  * this select the individual as a product of mutation.
125  */
126  inline void setMutated(void) {m_mutated=true;}
127 
128  /**
129  * returns parent 1 of the individual
130  * @return (Individual*) parent 1 (could be zero if individual a random generation)
131  */
132  inline const Individual* getParent1(void)const {return m_parent1;}
133 
134  /**
135  * returns parent 2 of the individual
136  * @return (Individual*) parent 2 (could be zero if individual a random generation)
137  */
138  inline const Individual* getParent2(void)const {return m_parent2;}
139 
140  /**
141  * test if the individual a product of mutation
142  * @return (bool) true if it a product of mutation
143  */
144  inline bool isMutated(void)const {return m_mutated;}
145 
146  /**
147  * returns a string, which repesent the individual (for logging)
148  * @return (string) the string representation of the individual
149  */
150  std::string IndividualToString(void)const;
151 
152  /**
153  * returns a string with the parents and a mark for mutation
154  * @param withMutation (bool) should mutation part of the string
155  * @return (string) the result
156  */
157  std::string RootToString(bool withMutation=true)const;
158 
159  /**
160  * returns the m_fitnessCalculated flag, which represent, that the fitness value was calculated before.
161  * @return (bool) the flag m_fitnessCalculated
162  */
163  inline bool isFitnessCalculated()const {return m_fitnessCalculated;}
164 
165  /**
166  * store the individual in a file
167  * @param f (FILE) the file to store in
168  * @return (bool) return true if ok
169  */
170  bool store(FILE* f)const;
171 
172  /**
173  * restore all individual from a restore structure
174  * @param numberIndividuals (int) number of individuals which should be restored
175  * @param nameSet (map<int,string>) names of the individuals
176  * @param individualSet (map<int,RESTORE_GA_INDIVIDUAL*> the structures which should be restored
177  * @param linkSet (map<int,vector<int>>) the linkings between the individual and the genes
178  * @return (bool) true if all ok
179  */
180  static bool restore(int numberIndividuals,std::map<int,std::string>& nameSet,std::map<int,RESTORE_GA_INDIVIDUAL*>& individualSet, std::map<int,std::vector<int> >& linkSet, std::vector<Individual*>& storage);
181 
182  /**
183  * restore the parent links from a restore structure
184  * @param numberIndividuals (int) number of individuals which should be restored
185  * @param individualSet (map<int,RESTORE_GA_INDIVIDUAL*> the structures which should be restored
186  * @return (bool) true if all ok
187  */
188  static bool restoreParent(int numberIndividuals,std::map<int,RESTORE_GA_INDIVIDUAL*>& individualSet);
189 
190 protected:
191  /**
192  * the name of the individual
193  */
194  std::string m_name;
195 
196  /**
197  * the ID of the individual
198  */
199  int m_ID;
200 
201  /**
202  * the gens inside the individual
203  */
204  std::vector<Gen*> m_gene;
205 
206  /**
207  * parent 1
208  */
210 
211  /**
212  * parent 2
213  */
215 
216  /**
217  * remember if the individual a product of mutation
218  */
219  bool m_mutated;
220 
221  /**
222  * remember if the fitness value was calculated
223  */
225 
226  /**
227  * save the calculated fitness value
228  */
229  double m_fitness;
230 
231 private:
232  /**
233  * disable the default constructor
234  */
235  Individual();
236 };
237 
238 #endif /* INDIVIDUAL_H_ */
Gen * getGen(int x) const
return a gen of the individual
Definition: Individual.h:85
Definition: restore.h:54
double getFitnessC() const
this function calculate the fitness value of the individual (const)
Definition: Individual.cpp:57
bool isMutated(void) const
test if the individual a product of mutation
Definition: Individual.h:144
const Individual * getParent1(void) const
returns parent 1 of the individual
Definition: Individual.h:132
int getSize(void) const
return the size of the individual.
Definition: Individual.h:78
The Gen class.
Definition: Gen.h:51
int m_ID
the ID of the individual
Definition: Individual.h:199
Individual * m_parent1
parent 1
Definition: Individual.h:209
Individual * m_parent2
parent 2
Definition: Individual.h:214
virtual ~Individual()
default destructor
Definition: Individual.cpp:44
bool store(FILE *f) const
store the individual in a file
Definition: Individual.cpp:108
void removeGen(Gen *gen)
remove a specified gen from the individual
Definition: Individual.cpp:65
double getFitness()
this function calculate the fitness value of the individual
Definition: Individual.cpp:48
double m_fitness
save the calculated fitness value
Definition: Individual.h:229
static bool restore(int numberIndividuals, std::map< int, std::string > &nameSet, std::map< int, RESTORE_GA_INDIVIDUAL * > &individualSet, std::map< int, std::vector< int > > &linkSet, std::vector< Individual * > &storage)
restore all individual from a restore structure
Definition: Individual.cpp:155
bool isFitnessCalculated() const
returns the m_fitnessCalculated flag, which represent, that the fitness value was calculated before...
Definition: Individual.h:163
std::string m_name
the name of the individual
Definition: Individual.h:194
static bool restoreParent(int numberIndividuals, std::map< int, RESTORE_GA_INDIVIDUAL * > &individualSet)
restore the parent links from a restore structure
Definition: Individual.cpp:186
bool m_fitnessCalculated
remember if the fitness value was calculated
Definition: Individual.h:224
const std::vector< Gen * > & getGene(void) const
returns all gens of the individual
Definition: Individual.h:97
bool m_mutated
remember if the individual a product of mutation
Definition: Individual.h:219
void setMutated(void)
this select the individual as a product of mutation.
Definition: Individual.h:126
std::string RootToString(bool withMutation=true) const
returns a string with the parents and a mark for mutation
Definition: Individual.cpp:87
void addGen(Gen *gen)
add a gen to the individual.
Definition: Individual.h:91
int getID(void) const
return the ID of the individual
Definition: Individual.h:66
This class represent one individual of the complete gen.
Definition: Individual.h:45
const Individual * getParent2(void) const
returns parent 2 of the individual
Definition: Individual.h:138
std::vector< Gen * > m_gene
the gens inside the individual
Definition: Individual.h:204
std::string IndividualToString(void) const
returns a string, which repesent the individual (for logging)
Definition: Individual.cpp:73
std::string getName(void) const
return the name of the individual
Definition: Individual.h:72