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
SingletonGenEngine.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 SINGLETONGENENGINE_H_
28 #define SINGLETONGENENGINE_H_
29 
30 //includes
31 #include <vector>
32 #include <map>
33 #include <string>
34 #include <selforg/randomgenerator.h>
35 #include <selforg/inspectableproxy.h>
36 //#include <selforg/storeable.h> not possible because the restore function need more information!!!
37 
38 //forward declaration
39 class Gen;
40 class GenPrototype;
41 class GenContext;
42 class Individual;
43 class Generation;
44 class IMutationStrategy;
46 class ISelectStrategy;
48 class IRandomStrategy;
49 class IValue;
50 class IFitnessStrategy;
53 struct RESTORE_GA_GENE;
54 template<class Typ>struct RESTORE_GA_TEMPLATE;
55 
56 //forward declaration for LPZROBOTS
57 class PlotOptionEngine;
58 
59 /**
60  * This is the engine of the gen. alg.
61  *
62  * Over this is the class as singleton concepted. Only one engine for a run.
63  */
65 public:
66  /**
67  * this function returns a set of all registered GenPrototypes.
68  * @return (vector<GenPrototype*>&) the set
69  */
70  inline const std::vector<GenPrototype*>& getSetOfGenPrototyps(void) const {return m_prototype;}
71 
72  /**
73  * this function return the number of Generation inside.
74  * @return (int) the number of Generation in the storage
75  */
76  inline int getNumGeneration(void) const {return m_generation.size();}
77 
78  /**
79  * this function returns one Generation
80  * @param x (int) the index of the Generation which is searched
81  * @return (Generation*) the searched Generation. If x not a right index, than the result is zero.
82  */
83  inline Generation* getGeneration(int x) {if(x<getNumGeneration())return m_generation[x];return NULL;}
84 
85  /**
86  * returns the actual generation number, on which the alg. work
87  * @return (int) the actual generation number
88  */
89  inline int getActualGenerationNumber(void) const {return m_actualGeneration;}
90 
91  /**
92  * returns the actual Generation.
93  * @return (Generation*) the actual generation
94  */
96 
97  /**
98  * returns the number of individual inside the alg.
99  * @return (int) number of individual
100  */
101  inline int getNumIndividual(void) const {return m_individual.size();}
102 
103  /**
104  * returns one individual
105  * @param x (int) index of the individual which is searched.
106  * @return (Individual*) the searched individual
107  */
108  inline Individual* getIndividual(int x) const {if(x<getNumIndividual())return m_individual[x];return NULL;}
109 
110  /**
111  * registered a GenPrototype in the engine.
112  * @param prototype (GenPrototype*) the prototype which should be registered.
113  */
114  inline void addGenPrototype(GenPrototype* prototype) {m_prototype.push_back(prototype);}
115 
116  /**
117  * registered a Gen in the engine. Normal only used by the alg. self.
118  * @param gen (Gen*) the Gen which should be registered.
119  */
120  inline void addGen(Gen* gen) {m_gen.push_back(gen);}
121 
122  /**
123  * returns one gene
124  * @param x (int) index of the gene which is searched
125  * @return (Gen*) the searched gene
126  */
127  inline Gen* getGen(int x)const {if((unsigned int)x<m_gen.size())return m_gen[x];return NULL;}
128 
129  /**
130  * registered a individual in the engine. Normal only used by the alg. self.
131  * @param individual (Individual*) the individual
132  */
133  inline void addIndividual(Individual* individual) {m_individual.push_back(individual);}
134 
135  /**
136  * add a Generation to the alg. Normal only used by the alg. self.
137  * @param generation (Generation*) the generation
138  */
139  inline void addGeneration(Generation* generation) {m_generation.push_back(generation);}
140 
141  /**
142  * prepare the alg. and create his fisrt generation.
143  * @param startSize (int) Number of individual with which the alg. will be start.
144  * @param numChildren (int) Number of individual which will be created by crossover.
145  * @param random (RandGen*) A random generator
146  * @param withUpdate (bool) if true, than makes this function on the end a update.
147  */
148  void generateFirstGeneration(int startSize, int numChildren, RandGen* random, bool withUpdate = true);
149 
150  /**
151  * prepare the next generation. Mean create the GenContext for every GenPrototype.
152  * @param size (int) size of the next generation
153  * @param numChildren (int) Number of individual which will be created by crossover
154  */
155  void prepareNextGeneration(int size, int numChildren);
156 
157  /**
158  * prepare the first generation with "generateFirstGeneration" and init the measures.
159  * @param startSize (int) Number of individual with which the alg. will be start.
160  * @param numChildren (int) Number of individual which will be created by crossover
161  * @param proxyGeneration (InspectableProxy*&) the generation which is controlled by measures
162  * @param proxyGene (InspectableProxy*&) the proxy for control measure
163  * @param random (RandGen*) A random generator
164  * @param plotEngine (PlotOptionEngine*) logging the data for later control
165  * @param plotEngineGenContext (PlotOptionEngine*) logging the data of the GenContext for later control
166  * @param withUpdate (bool) is needed for the function "generateFirstGeneration"
167  */
168  void prepare(int startSize, int numChildren, InspectableProxy*& proxyGeneration, InspectableProxy*& proxyGene, RandGen* random, PlotOptionEngine* plotEngine = 0, PlotOptionEngine* plotEngineGenContext = 0, bool withUpdate = true);
169 
170  /**
171  * return the size of the next generation.
172  * @return (int) the size
173  */
174  int getNextGenerationSize();
175 
176  /**
177  * makes a step in the measure
178  * @param time (double) the time stamp in the measure
179  * @param proxyGeneration (InspectableProxy*&) the generation which is controlled by measures
180  * @param proxyGene (InspectableProxy*&) the proxy for control measure
181  */
182  void measureStep(double time,InspectableProxy*& proxyGeneration, InspectableProxy*& proxyGene, PlotOptionEngine* plotEngine = 0, PlotOptionEngine* plotEngineGenContext = 0);
183 
184  /**
185  * this function is for a automatically run of the gen. alg.
186  * @param startSize (int) Number of individual with which the alg. will be start.
187  * @param numChildren (int) Number of individual which will be created by crossover.
188  * @param numGeneration (int) Number of generation after this the alg. will end.
189  * @param random (RandGen*) a random generator
190  * @param plotEngine (PlotOptionEngine*) logging the data for later control
191  * @param plotEngineGenContext (PlotOptionEngine*) logging the data of the GenContext for later control
192  */
193  void runGenAlg(int startSize, int numChildren, int numGeneration, RandGen* random, PlotOptionEngine* plotEngine = 0, PlotOptionEngine* plotEngineGenContext = 0);
194 
195  /**
196  * make the select of the actual generation and transfer it to the next generation
197  * @param createNextGeneration (bool) normal=true. should be the next generation prepare
198  */
199  void select(bool createNextGeneration=true);
200 
201  /**
202  * in the actual generation it will generate the children of the living individual
203  * @param random (RanGen*) random generator
204  */
205  void crossover(RandGen* random);
206 
207  /**
208  * this function makes a update on the statistical data of the gen. alg.
209  * @param factor (double) the factor is for the whisker distance
210  */
211  void update(double factor = 1.5);
212 
213  /**
214  * decid the select strategy
215  * @param strategy (ISelectStrategy*) the select strategy of the alg.
216  */
217  inline void setSelectStrategy(ISelectStrategy* strategy) {m_selectStrategy = strategy;}
218 
219  /**
220  * decide the generation size strategy.
221  * @param strategy (IGenerationSizeStrategy*) the generation size strategy of the alg.
222  */
224 
225  /**
226  * decide the fitness strategy.
227  * @param strategy (IFitnessStrategy*) the fitness strategy of the alg.
228  */
229  inline void setFitnessStrategy(IFitnessStrategy* strategy) {m_fitnessStrategy = strategy;}
230 
231  /**
232  * calculate for a individual the fitness value.
233  * @param individual (Individual*) the individual for which the fitness should be calculated
234  * @return (double) the fitness value.
235  */
236  double getFitness(const Individual* individual);
237 
238  /**
239  * returns the best individual (where the fitness is next to zero) which the alg. have found.
240  * @return (Individual*) the best.
241  */
243 
244  /**
245  * returns a complete list of the inheritance of all individual.
246  * @param withMutation (bool) normal=true. if every individual should be marked if it mutated.
247  * @return (string) the list
248  */
249  std::string getIndividualRoot(bool withMutation=true)const;
250 
251  /**
252  * returns a list of all individual and there gens values.
253  * @return (string) the list
254  */
255  std::string getAllIndividualAsString(void)const;
256 
257  /** stores the object to the given file stream (binary).
258  */
259  virtual bool store(FILE* f) const;
260 
261  /** loads the object from the given file stream (binary).
262  */
263  virtual bool restore(FILE* f, InspectableProxy*& proxyGeneration, InspectableProxy*& proxyGene, PlotOptionEngine* plotEngine, PlotOptionEngine* plotEngineGenContext);
264 
265  /**
266  * returns the only existing engine.
267  * @return (SingletonGenEngine*) the engine
268  */
269  inline static SingletonGenEngine* getInstance(void) { if(m_engine==0) m_engine = new SingletonGenEngine();return m_engine; }
270 
271  /**
272  * destroy the only existing engine.
273  */
274  inline static void destroyGenEngine(bool cleanStrategies=false) {getInstance()->m_cleanStrategies=cleanStrategies; if(m_engine!=0){delete m_engine;m_engine=0;}}
275 
276 protected:
277  /**
278  * managment storage for all Genprototypes.
279  */
280  std::vector<GenPrototype*> m_prototype;
281 
282  /**
283  * managment storage for all Generation
284  */
285  std::vector<Generation*> m_generation;
286 
287  /**
288  * management storage for all Individual
289  */
290  std::vector<Individual*> m_individual;
291 
292  /**
293  * management storage for all Gens
294  */
295  std::vector<Gen*> m_gen;
296 
297  /**
298  * the number of the actual generation
299  */
301 
302  /**
303  * the select strategy of the alg.
304  */
306 
307  /**
308  * the fitness strategy of the alg.
309  */
311 
312  /**
313  * the generation size strategy of the alg.
314  */
316 
317  /**
318  * the one and only GenEngine.
319  */
321 
322  /**
323  * flag for clean the seted strategies
324  */
326 
327  /**
328  * Map for restoring the generations from a run before
329  */
330  std::map<int,RESTORE_GA_GENERATION*> m_restoreGeneration;
331 
332  /**
333  * Map for restoring the individuals from a run before
334  */
335  std::map<int,RESTORE_GA_INDIVIDUAL*> m_restoreIndividual;
336 
337  /**
338  * Map for restoring the individual generation link from a run before
339  */
340  std::map<int,std::vector<int> > m_restoreIndividualInGeneration;
341 
342  /**
343  * Map for restoring the genes individual link from a run before
344  */
345  std::map<int,std::vector<int> > m_restoreGeneInIndividual;
346 
347  /**
348  * Map for restoring the names of the individuals
349  */
350  std::map<int,std::string> m_restoreNameOfIndividuals;
351 
352 private:
353  /**
354  * disable the default constructor
355  */
357 
358  /**
359  * disable the default destructor
360  * @return
361  */
362  virtual ~SingletonGenEngine();
363 };
364 
365 #endif /* SINGLETONGENENGINE_H_ */
static void destroyGenEngine(bool cleanStrategies=false)
destroy the only existing engine.
Definition: SingletonGenEngine.h:274
Individual * getBestIndividual(void)
returns the best individual (where the fitness is next to zero) which the alg.
Definition: SingletonGenEngine.cpp:311
void generateFirstGeneration(int startSize, int numChildren, RandGen *random, bool withUpdate=true)
prepare the alg.
Definition: SingletonGenEngine.cpp:114
this interface is for a select strategy of the generation class.
Definition: ISelectStrategy.h:36
std::map< int, std::vector< int > > m_restoreIndividualInGeneration
Map for restoring the individual generation link from a run before.
Definition: SingletonGenEngine.h:340
void prepareNextGeneration(int size, int numChildren)
prepare the next generation.
Definition: SingletonGenEngine.cpp:170
void setSelectStrategy(ISelectStrategy *strategy)
decid the select strategy
Definition: SingletonGenEngine.h:217
void addGenPrototype(GenPrototype *prototype)
registered a GenPrototype in the engine.
Definition: SingletonGenEngine.h:114
Definition: restore.h:54
std::string getIndividualRoot(bool withMutation=true) const
returns a complete list of the inheritance of all individual.
Definition: SingletonGenEngine.cpp:329
std::vector< Gen * > m_gen
management storage for all Gens
Definition: SingletonGenEngine.h:295
This interface gives the structur for the mutation of a gen.
Definition: IMutationStrategy.h:39
void setFitnessStrategy(IFitnessStrategy *strategy)
decide the fitness strategy.
Definition: SingletonGenEngine.h:229
std::vector< Generation * > m_generation
managment storage for all Generation
Definition: SingletonGenEngine.h:285
ISelectStrategy * m_selectStrategy
the select strategy of the alg.
Definition: SingletonGenEngine.h:305
The Gen class.
Definition: Gen.h:51
The GenContext class.
Definition: GenContext.h:51
Gen * getGen(int x) const
returns one gene
Definition: SingletonGenEngine.h:127
Definition: plotoptionengine.h:38
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
void addGeneration(Generation *generation)
add a Generation to the alg.
Definition: SingletonGenEngine.h:139
This is a interface for a strategy, which is used by ValueMutationStrategy.
Definition: IMutationFactorStrategy.h:40
Generation * getGeneration(int x)
this function returns one Generation
Definition: SingletonGenEngine.h:83
void update(double factor=1.5)
this function makes a update on the statistical data of the gen.
Definition: SingletonGenEngine.cpp:299
Individual * getIndividual(int x) const
returns one individual
Definition: SingletonGenEngine.h:108
int getActualGenerationNumber(void) const
returns the actual generation number, on which the alg.
Definition: SingletonGenEngine.h:89
Definition: restore.h:74
The interface for the fitness strategy of an individual.
Definition: IFitnessStrategy.h:36
virtual bool store(FILE *f) const
stores the object to the given file stream (binary).
Definition: SingletonGenEngine.cpp:349
std::map< int, std::string > m_restoreNameOfIndividuals
Map for restoring the names of the individuals.
Definition: SingletonGenEngine.h:350
std::map< int, std::vector< int > > m_restoreGeneInIndividual
Map for restoring the genes individual link from a run before.
Definition: SingletonGenEngine.h:345
Definition: restore.h:30
std::vector< GenPrototype * > m_prototype
managment storage for all Genprototypes.
Definition: SingletonGenEngine.h:280
This is the engine of the gen.
Definition: SingletonGenEngine.h:64
IFitnessStrategy * m_fitnessStrategy
the fitness strategy of the alg.
Definition: SingletonGenEngine.h:310
bool m_cleanStrategies
flag for clean the seted strategies
Definition: SingletonGenEngine.h:325
void prepare(int startSize, int numChildren, InspectableProxy *&proxyGeneration, InspectableProxy *&proxyGene, RandGen *random, PlotOptionEngine *plotEngine=0, PlotOptionEngine *plotEngineGenContext=0, bool withUpdate=true)
prepare the first generation with "generateFirstGeneration" and init the measures.
Definition: SingletonGenEngine.cpp:187
double getFitness(const Individual *individual)
calculate for a individual the fitness value.
Definition: SingletonGenEngine.cpp:307
this interface is to create a random IValue.
Definition: IRandomStrategy.h:36
This interface is to specify how big the next generation should be.
Definition: IGenerationSizeStrategy.h:36
std::vector< Individual * > m_individual
management storage for all Individual
Definition: SingletonGenEngine.h:290
The GenPrototype class.
Definition: GenPrototype.h:55
The Generation class.
Definition: Generation.h:53
void addGen(Gen *gen)
registered a Gen in the engine.
Definition: SingletonGenEngine.h:120
int getNumGeneration(void) const
this function return the number of Generation inside.
Definition: SingletonGenEngine.h:76
static SingletonGenEngine * m_engine
the one and only GenEngine.
Definition: SingletonGenEngine.h:320
int getNextGenerationSize()
return the size of the next generation.
Definition: SingletonGenEngine.cpp:217
IGenerationSizeStrategy * m_generationSizeStrategy
the generation size strategy of the alg.
Definition: SingletonGenEngine.h:315
This class is a interface for a value which is part of a gen.
Definition: IValue.h:38
int m_actualGeneration
the number of the actual generation
Definition: SingletonGenEngine.h:300
int getNumIndividual(void) const
returns the number of individual inside the alg.
Definition: SingletonGenEngine.h:101
virtual bool restore(FILE *f, InspectableProxy *&proxyGeneration, InspectableProxy *&proxyGene, PlotOptionEngine *plotEngine, PlotOptionEngine *plotEngineGenContext)
loads the object from the given file stream (binary).
Definition: SingletonGenEngine.cpp:396
void addIndividual(Individual *individual)
registered a individual in the engine.
Definition: SingletonGenEngine.h:133
Definition: restore.h:87
void crossover(RandGen *random)
in the actual generation it will generate the children of the living individual
Definition: SingletonGenEngine.cpp:295
std::map< int, RESTORE_GA_GENERATION * > m_restoreGeneration
Map for restoring the generations from a run before.
Definition: SingletonGenEngine.h:330
void runGenAlg(int startSize, int numChildren, int numGeneration, RandGen *random, PlotOptionEngine *plotEngine=0, PlotOptionEngine *plotEngineGenContext=0)
this function is for a automatically run of the gen.
Definition: SingletonGenEngine.cpp:242
void setGenerationSizeStrategy(IGenerationSizeStrategy *strategy)
decide the generation size strategy.
Definition: SingletonGenEngine.h:223
void select(bool createNextGeneration=true)
make the select of the actual generation and transfer it to the next generation
Definition: SingletonGenEngine.cpp:264
This class is a proxy for the inspectable class.
Definition: inspectableproxy.h:35
This class represent one individual of the complete gen.
Definition: Individual.h:45
std::string getAllIndividualAsString(void) const
returns a list of all individual and there gens values.
Definition: SingletonGenEngine.cpp:339
Generation * getActualGeneration(void)
returns the actual Generation.
Definition: SingletonGenEngine.h:95
static SingletonGenEngine * getInstance(void)
returns the only existing engine.
Definition: SingletonGenEngine.h:269
void measureStep(double time, InspectableProxy *&proxyGeneration, InspectableProxy *&proxyGene, PlotOptionEngine *plotEngine=0, PlotOptionEngine *plotEngineGenContext=0)
makes a step in the measure
Definition: SingletonGenEngine.cpp:221
const std::vector< GenPrototype * > & getSetOfGenPrototyps(void) const
this function returns a set of all registered GenPrototypes.
Definition: SingletonGenEngine.h:70
std::map< int, RESTORE_GA_INDIVIDUAL * > m_restoreIndividual
Map for restoring the individuals from a run before.
Definition: SingletonGenEngine.h:335