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
Generation.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 GENERATION_H_
28 #define GENERATION_H_
29 
30 // standard includes
31 #include <string>
32 #include <vector>
33 #include <map>
34 #include <selforg/randomgenerator.h>
35 #include <selforg/inspectable.h>
36 
37 // forward declarations
38 class Individual;
40 
41 // gen. alg. includes
42 
43 /**
44  * The Generation class
45  *
46  * This class is used for grouping some individuals which representing
47  * one step in the gen. alg. (called generation). For this it save all
48  * individual which are part of this generation. Also it have an Number
49  * like a ID, which make this generation individual.
50  *
51  * All Generations inside the gen.alg. are only saved in the GenEngine.
52  */
53 class Generation : public Inspectable {
54 public:
55  /**
56  * constructor to create a Generation. Information which the class need are
57  * the generation number, the size of it and how many individual don t come
58  * in the next generation (killRate).
59  *
60  * @param generationNumber (int) The ID of the Generation.
61  * @param size (int) The Size of this Generation. Means how many individual are lives in this generation
62  * @param numChildren (int) Number of individual which will be created by crossover
63  */
64  Generation(int generationNumber, int size, int numChildren);
65 
66  /**
67  * destructor to delete a GenContext.
68  */
69  virtual ~Generation();
70 
71  /**
72  * [inline], [const]
73  * This function gives the ID (number) of the generation back.
74  *
75  * @return (int) The ID
76  */
77  inline int getGenerationNumber(void)const {return m_generationNumber;}
78 
79  /**
80  * [inline], [const]
81  * This function gives the size which is planed for this generation back.
82  *
83  * @return (int) The planed size
84  */
85  inline int getSize(void)const {return m_size;}
86 
87  /**
88  * [inline], [const]
89  * This function gives the actual size (number of individuals inside the generation) back.
90  *
91  * @return (int) current size
92  */
93  inline int getCurrentSize(void)const {return m_individual.size();}
94 
95  /**
96  * [inline], [const]
97  * This function gives the number of children back, which will be created by crossover.
98  *
99  * @return (int) the number of children
100  */
101  inline int getNumChildren(void)const {return m_numChildren;}
102 
103  /**
104  * [individual], [const]
105  * This function gives one individual from this generation back.
106  *
107  * @param x (int) the index of the searched individual
108  *
109  * @return (Individual*) The individual. If 0, if the param x is not inside the index range
110  */
111  inline Individual* getIndividual(int x)const {if(x<getCurrentSize())return m_individual[x];return NULL;}
112 
113  /**
114  * [inline], [const]
115  * This function gives all individual back.
116  *
117  * @return (vector<Individual*>&) all individual inside the generation
118  */
119  inline const std::vector<Individual*>& getAllIndividual(void)const {return m_individual;}
120 
121  /**
122  * [inline], [const]
123  * This function gives all individual back which aren't have the fitness value calculated.
124  *
125  * @return (vector<Individual*>&) all individual inside the generation
126  */
127  std::vector<Individual*>* getAllUnCalculatedIndividuals(void)const;
128 
129  /**
130  * This function insert an individual in the generation.
131  *
132  * @param individual (Individual*) the individual which should be insert in the generation
133  */
134  void addIndividual(Individual* individual);
135 
136  /**
137  * This function makes an crossOver whit the existing individuals to become from the current size the planed size.
138  *
139  * @param random (RandGen*) a pseudo number generator.
140  */
141  void crossover(RandGen* random);
142 
143  /**
144  * returns a string which represent all individual in this generation.
145  *
146  * @return (string) the string
147  */
148  std::string getAllIndividualAsString(void)const;
149 
150  /**
151  * returns all fitness values from the individuals.
152  *
153  * @return (vector<double> the fitness values.
154  */
155  std::vector<double>* getAllFitness(void)const;
156 
157  /**
158  * This function updates the statistical values
159  * @param factor (double) normal 1.5 Is needed for the data analysation
160  */
161  void update(double factor = 1.5);
162 
163  /**
164  * store a generation in a file
165  * @param f (FILE*) the file in which should be stored
166  * @return (bool) true if all ok
167  */
168  bool store(FILE* f)const;
169 
170  /**
171  * restore all generation from a restore structure
172  *
173  * remember the individuals must be restored before
174  *
175  * @param numberGeneration (int) number of generations which should be restored
176  * @param generationSet (map<int,RESTORE_GA_GENERATION*>) the structures which should be restored
177  * @param linkSet (map<int,vector<int>>) the linkings between the generation and the individuals
178  * @return (bool) true if all ok
179  */
180  static bool restore(int numberGeneration, std::map<int,RESTORE_GA_GENERATION*>& generationSet, std::map<int,std::vector<int> >& linkSet);
181 
182 protected:
183  /**
184  * (int)
185  * The generation number (ID)
186  */
188 
189  /**
190  * (vector<Individual*>)
191  * The storage for the individuals, which are part of this generation. (NO deleting)
192  */
193  std::vector<Individual*> m_individual;
194 
195  /**
196  * (int)
197  * The planed size of the generation.
198  */
199  int m_size;
200 
201  /**
202  * (int)
203  * The number of children
204  */
206 
207 private:
208  /**
209  * disable the default constructor
210  */
211  Generation();
212 
213  /**
214  * the under quartil
215  */
216  double m_q1;
217 
218  /**
219  * the upper quartil
220  */
221  double m_q3;
222 
223  /**
224  * the min
225  */
226  double m_min;
227 
228  /**
229  * the max
230  */
231  double m_max;
232 
233  /**
234  * the average
235  */
236  double m_avg;
237 
238  /**
239  * the median
240  */
241  double m_med;
242 
243  /**
244  * the under whisker
245  */
246  double m_w1;
247 
248  /**
249  * the upper whisker
250  */
251  double m_w3;
252 
253  /**
254  * the best fitness value inside the generation
255  */
256  double m_best;
257 
258  /**
259  * the number of individual inside the generation (will be)
260  */
261  double m_dSize;
262 
263  /**
264  * the number of individual which will be created by crossover.
265  */
266  double m_dNumChildren;
267 };
268 
269 #endif /* GENERATION_H_ */
std::vector< Individual * > m_individual
(vector<Individual*>) The storage for the individuals, which are part of this generation.
Definition: Generation.h:193
static bool restore(int numberGeneration, std::map< int, RESTORE_GA_GENERATION * > &generationSet, std::map< int, std::vector< int > > &linkSet)
restore all generation from a restore structure
Definition: Generation.cpp:176
const std::vector< Individual * > & getAllIndividual(void) const
[inline], [const] This function gives all individual back.
Definition: Generation.h:119
virtual ~Generation()
destructor to delete a GenContext.
Definition: Generation.cpp:57
std::string getAllIndividualAsString(void) const
returns a string which represent all individual in this generation.
Definition: Generation.cpp:83
void crossover(RandGen *random)
This function makes an crossOver whit the existing individuals to become from the current size the pl...
Definition: Generation.cpp:61
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
int m_numChildren
(int) The number of children
Definition: Generation.h:205
int getNumChildren(void) const
[inline], [const] This function gives the number of children back, which will be created by crossover...
Definition: Generation.h:101
void update(double factor=1.5)
This function updates the statistical values.
Definition: Generation.cpp:103
void addIndividual(Individual *individual)
This function insert an individual in the generation.
Definition: Generation.cpp:79
int getGenerationNumber(void) const
[inline], [const] This function gives the ID (number) of the generation back.
Definition: Generation.h:77
int getCurrentSize(void) const
[inline], [const] This function gives the actual size (number of individuals inside the generation) b...
Definition: Generation.h:93
Definition: restore.h:30
int m_size
(int) The planed size of the generation.
Definition: Generation.h:199
std::vector< double > * getAllFitness(void) const
returns all fitness values from the individuals.
Definition: Generation.cpp:93
int getSize(void) const
[inline], [const] This function gives the size which is planed for this generation back...
Definition: Generation.h:85
Interface for inspectable objects.
Definition: inspectable.h:48
int m_generationNumber
(int) The generation number (ID)
Definition: Generation.h:187
The Generation class.
Definition: Generation.h:53
Individual * getIndividual(int x) const
[individual], [const] This function gives one individual from this generation back.
Definition: Generation.h:111
std::vector< Individual * > * getAllUnCalculatedIndividuals(void) const
[inline], [const] This function gives all individual back which aren't have the fitness value calcula...
Definition: Generation.cpp:123
bool store(FILE *f) const
store a generation in a file
Definition: Generation.cpp:134
This class represent one individual of the complete gen.
Definition: Individual.h:45