SingletonGenAlgAPI.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2009 by Robot Group Leipzig                        *
00003  *    martius@informatik.uni-leipzig.de                                    *
00004  *    fhesse@informatik.uni-leipzig.de                                     *
00005  *    der@informatik.uni-leipzig.de                                        *
00006  *    guettler@informatik.uni-leipzig.de                                   *
00007  *    jhoffmann@informatik.uni-leipzig.de                                  *
00008  *    joergweide84@aol.com (robot12)                                       *
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  *   This program is distributed in the hope that it will be useful,       *
00016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00018  *   GNU General Public License for more details.                          *
00019  *                                                                         *
00020  *   You should have received a copy of the GNU General Public License     *
00021  *   along with this program; if not, write to the                         *
00022  *   Free Software Foundation, Inc.,                                       *
00023  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00024  ***************************************************************************
00025  *                                                                         *
00026  *   This class is a facade for the gen. alg. It covert all function to    *
00027  *   work with the alg.                                                    *
00028  *                                                                         *
00029  *   $Log: SingletonGenAlgAPI.h,v $
00030  *   Revision 1.11  2009/10/21 14:08:06  robot12
00031  *   add restore and store functions to the ga package
00032  *
00033  *   Revision 1.10  2009/08/11 12:57:38  robot12
00034  *   change the genetic algorithm (first crossover, second select)
00035  *
00036  *   Revision 1.9  2009/07/28 13:20:40  robot12
00037  *   add some clean ups and add member variable m_cleanStrategies
00038  *
00039  *   Revision 1.8  2009/07/21 08:39:01  robot12
00040  *   rename "crosover" to crossover
00041  *
00042  *   Revision 1.7  2009/07/15 12:53:36  robot12
00043  *   some bugfix's and new functions
00044  *
00045  *   Revision 1.6  2009/07/06 15:06:35  robot12
00046  *   bugfix
00047  *
00048  *   Revision 1.5  2009/07/02 15:24:53  robot12
00049  *   update and add new class InvertedFitnessStrategy
00050  *
00051  *   Revision 1.4  2009/06/30 14:20:49  robot12
00052  *   finishing the gen API and add some comments
00053  *
00054  *   Revision 1.3  2009/05/11 14:08:51  robot12
00055  *   patch some bugfix....
00056  *
00057  *   Revision 1.2  2009/05/06 13:28:22  robot12
00058  *   some implements... Finish
00059  *
00060  *   Revision 1.1  2009/05/04 15:27:55  robot12
00061  *   rename of some files and moving files to other positions
00062  *    - SingletonGenAlgAPI has one error!!! --> is not ready now
00063  *
00064  *   Revision 1.4  2009/04/30 11:35:53  robot12
00065  *   some changes:
00066  *    - insert a SelectStrategie
00067  *    - insert a MutationStrategie
00068  *    - reorganisation of the design
00069  *
00070  *   Revision 1.3  2009/04/29 14:32:29  robot12
00071  *   some implements... Part4
00072  *
00073  *   Revision 1.2  2009/04/29 11:36:41  robot12
00074  *   some implements... Part3
00075  *
00076  *   Revision 1.1  2009/04/27 10:59:34  robot12
00077  *   some implements
00078  *
00079  *
00080  ***************************************************************************/
00081 
00082 #ifndef SINGLETONGENALGAPI_H_
00083 #define SINGLETONGENALGAPI_H_
00084 
00085 //includes
00086 #include <string>
00087 #include <list>
00088 #include <selforg/randomgenerator.h>
00089 
00090 //forward declaration
00091 class Gen;
00092 class GenPrototype;
00093 class GenContext;
00094 class Individual;
00095 class Generation;
00096 class IMutationStrategy;
00097 class IMutationFactorStrategy;
00098 class ISelectStrategy;
00099 class IGenerationSizeStrategy;
00100 class IRandomStrategy;
00101 class IValue;
00102 class IFitnessStrategy;
00103 
00104 //forward declaration for LPZROBOTS
00105 class PlotOptionEngine;
00106 class PlotOption;
00107 
00108 //ga_tools includes
00109 #include "SingletonGenEngine.h"
00110 
00111 /**
00112  * This is a facade for the gen. alg.
00113  *
00114  * Over this is the class as singleton concepted. Only one API for a run.
00115  */
00116 class SingletonGenAlgAPI{
00117 public:
00118         // Action
00119         /**
00120          * starts the selection
00121          * @param createNextGeneration (bool) normal=true should be the next generation be prepare?
00122          */
00123         void select(bool createNextGeneration=true);
00124         /**
00125          * create the children from to fill up the next generation
00126          * @param random (RandGen*) random generator
00127          */
00128         void crossover(RandGen* random);
00129         /**
00130          * update the internal statistical data
00131          * @param factor (double) normal=1.5 is needed for the whisker distance
00132          */
00133         void update(double factor = 1.5);
00134         /**
00135          * prepares the first generation and optional the enabled measure
00136          * @param startSize (int) Number of individual at begin of the gen. alg.
00137          * @param startChildren (int) Number of individual which will be created by crossover
00138          * @param random (RandGen*) A random generator
00139          * @param withUpdate (bool) is needed for "generateFirstGeneration"
00140          */
00141         void prepare(int startSize, int numChildren, RandGen* random, bool withUpdate = true);
00142         /**
00143          * prepares the next generation and optional the enabled measure
00144          */
00145         void prepare();
00146         /**
00147          * makes a step in the measure
00148          * @param time (double) time stamp in the measure
00149          */
00150         void measureStep(double time);
00151         /**
00152          * start the sequenz of select, crossover, update in a automatically loop
00153          * @param startSize (int) Number of individual at begin of the gen. alg.
00154          * @param numChildren (int) Number of individual which will be created by crossover
00155          * @param numGeneration (int) Number of generation which the alg. max. runs
00156          * @param random (RandGen*) random generator
00157          */
00158         void runGenAlg(int startSize, int numChildren, int numGeneration, RandGen* random);
00159 
00160         //measure
00161         /**
00162          * enables data measure with more than one plotOption.
00163          * @param plotOptions (list<PöotOption>&) the list
00164          */
00165         void enableMeasure(std::list<PlotOption>& plotOptions);
00166         /**
00167          * enables da : public Storableta measure.
00168          * @param plotOption (PlotOption&) the plot option
00169          */
00170         void enableMeasure(PlotOption& plotOption);
00171         /**
00172          * returns the active plotOptionEngine for data measure.
00173          * @return (PlotOptionEngine*) the plot option engine
00174          */
00175         inline PlotOptionEngine* getPlotOptionEngine(void)const {return m_plotEngine;}
00176         /**
00177          * enable data measure inside the GenContexts with more than one plotOption
00178          * @param plotOptions (list<PlotOption>&) the list
00179          */
00180         void enableGenContextMeasure(std::list<PlotOption>& plotOptions);
00181         /**
00182          * enable data measure inside the GenContexts.
00183          * @param plotOption (PlotOption&) the plot option
00184          */
00185         void enableGenContextMeasure(PlotOption& plotOption);
00186         /**
00187          * returns the active plotOptionEngine for data measure inside the GenContexts.
00188          * @return (PlotOptionEngine*) the plot option engine.
00189          */
00190         inline PlotOptionEngine* getPlotOptionEngineForGenContext(void)const {return m_plotEngineGenContext;}
00191 
00192         // set static strategies
00193         /**
00194          * set the generation size strategy
00195          * @param strategy (IGenerationSizeStrategy*) the strategy
00196          */
00197         inline void setGenerationSizeStrategy(IGenerationSizeStrategy* strategy) {SingletonGenEngine::getInstance()->setGenerationSizeStrategy(strategy);}
00198         /**
00199          * set the fitness strategy
00200          * @param strategy (IFitnessStrategy*) the strategy
00201          */
00202         inline void setFitnessStrategy(IFitnessStrategy* strategy) {SingletonGenEngine::getInstance()->setFitnessStrategy(strategy);}
00203         /**
00204          * set the select strategy
00205          * @param strategy (ISelectStrategy*) the strategy
00206          */
00207         inline void setSelectStrategy(ISelectStrategy* strategy) {SingletonGenEngine::getInstance()->setSelectStrategy(strategy);}
00208 
00209         // gets
00210         /**
00211          * returns the GenAlgEngine
00212          * @return (SingletonGenEngine*) the engine
00213          */
00214         SingletonGenEngine* getEngine(void)const;
00215 
00216         // default interface creation
00217         /**
00218          * creates a SumFitnessStrategy. This strategy make the sum of all gens with a double value.
00219          * @return (IFitnessStrategy*) the strategy
00220          */
00221         IFitnessStrategy* createSumFitnessStrategy()const;
00222         /**
00223          * creates a EuclidicFitnessStrategy. This strategy calculate the euclidic distance of all gens with a double value.
00224          * @return (IFitnessStrategy*) the strategy
00225          */
00226         IFitnessStrategy* createEuclidicDistanceFitnessStrategy()const;
00227         /**
00228          * creates a TestFitnessStrategy which is the hardest test for a gen. alg. Only a smale area has a fitness. All other gives no information!
00229          * @param fitness (IFitnessStrategy*) a other fitness strat* Over this is the class as singleton concepted. Only one engine for a run.egy which define the smal area.
00230          * @return (IFitnessStrategy*) the strategy
00231          */
00232         IFitnessStrategy* createExtreamTestFitnessStrategy(IFitnessStrategy* fitness)const;
00233         /**
00234          * creates the test function fitness strategy from the papper tp this alg.
00235          * f(x,y) = 10.0 * (x² + 2.5y² - y) * exp(1 - (x² + y²)) + 2.4 + 0.1x² + 0.1y²
00236          * @return (IFitnessStrategy*) the strategy
00237          */
00238         IFitnessStrategy* createTestFitnessStrategy()const;
00239         /**
00240          * returns a fitness strategy which calculate the inverse value from a other strategy.
00241          * @param strategy (IFitnessStrategy*) the other strategy
00242          * @return (IFitnessStrategy*) the resulting strategy
00243          */
00244         IFitnessStrategy* createInvertedFitnessStrategy(IFitnessStrategy* strategy)const;
00245         /**
00246          * creates a random strategy for double values.
00247          * The values will be generated in the intervals [base-epsilon:-epsilon] or [epsilon:factor+base+epsilon]
00248          *
00249          * This means a random value in the interval [0:1] will be mul. with factor => [0:factor]
00250          * After this it will be shifted by base => [base:factor+base]
00251          * And than divided at the point zero and moved away by epsilon from the point zero
00252          * => [base-epsilon:-epsilon], [epsilon:factor+base+epsilon]
00253          *
00254          * For example:
00255          * We want values in the intervals [-10:-5] and [5:10].
00256          * ==> epsilon=5
00257          * ==> [-5:5]
00258          * ==> base=-5
00259          * ==> [0:10]
00260          * ==> factor=10
00261          *
00262          * @param random (RandGen*) random generator
00263          * @param base (double) normal=0.0 moves the ground interval
00264          * @param factor (double) normal=1.0 resize the interval
00265          * @param epsilon (double) normal=0.0 devided the interval at the point zero and move the
00266          *                      values by epsilon away from point zero
00267          * @return (IRandomStrategy*) the strategy
00268          */
00269         IRandomStrategy* createDoubleRandomStrategy(RandGen* random, double base=0.0, double factor=1.0, double epsilon = 0.0)const;
00270         /**
00271          * creates mutation strategy which change the old values by add a other value
00272          * @param strategy (IMutationFactorStrategy*) this strategy define what the other value is.
00273          * @param mutationProbability (int) the probability of mutation in one per tausend (1/1000)
00274          * @return (IMutationStrategy*) the strategy
00275          */
00276         IMutationStrategy* createValueMutationStrategy(IMutationFactorStrategy* strategy, int mutationProbability)const;
00277         /**
00278          * creates a mutation factor strategy with a fix value
00279          * @param value (IValue*) the fix value.
00280          * @return (IMutationFactorStrategy*) the strategy
00281          */
00282         IMutationFactorStrategy* createFixMutationFactorStrategy(IValue* value)const;
00283         /**
00284          * creates a mutation factor strategy with a optimized value (varianz about all existing gens)
00285          * @return (IMutationFactorStrategy*) the strategy
00286          */
00287         IMutationFactorStrategy* createStandartMutationFactorStrategy(void)const;
00288         /**
00289          * creates a generation size strategy with a fix value
00290          * @param value (int) the size of the generation
00291          * @return (IGenerationSizeStrategy*) the strategy
00292          */
00293         IGenerationSizeStrategy* createFixGenerationSizeStrategy(int value)const;
00294         /**
00295          * creates a generation size strategy with a optimized size for the generation (changing by speed of development)
00296          * @param startSize (int) the size at the start time
00297          * @param numGeneration (int) number of generation. (needed for the speed calculation
00298          * @return (IGenerationSizeStrategy*) the strategy
00299          */
00300         IGenerationSizeStrategy* createStandartGenerationSizeStrategy(int startSize, int numGeneration)const;
00301         /**
00302          * creates a elite select strategy
00303          * @return (ISelectStrategy*) the strategy
00304          */
00305         ISelectStrategy* createEliteSelectStrategy(void)const;
00306         /**
00307          * creates a tournament SelctStrategy. By this strategy will two individual fight again
00308          * (the individual with the higher fitness lives longer).
00309          * @param random (RandGen*) random generator
00310          * @return (ISelectStrategy*) the strategy
00311          */
00312         ISelectStrategy* createTournamentSelectStrategy(RandGen* random)const;
00313         /**
00314          * creates a random select strategy. By this strategy will a random selected individual fight again a random number.
00315          * If the random number higher than the fitness from the individual, than it will be die.
00316          * @param random (RandGen*) ranom generator
00317          * @return (ISelectStrategy*) the strategy
00318          */
00319         ISelectStrategy* createRandomSelectStrategy(RandGen* random)const;
00320         /**
00321          * creates a IValue (TemplateValue<double) from type double.
00322          * @param value (double) the value
00323          * @return (IValue*) the new object
00324          */
00325         IValue* createDoubleValue(double value)const;
00326 
00327         //Storable
00328         /** stores the object to the given file stream (binary).
00329          */
00330         bool storeGA(FILE* f) const;
00331 
00332         /** loads the object from the given file stream (binary).
00333          */
00334         bool restoreGA(FILE* f) const;
00335 
00336         // inserts
00337         /**
00338          * add a GenPrototype to the alg.
00339          * @param prototype (GenPrototype*) the prototype
00340          */
00341         void insertGenPrototype(GenPrototype* prototype);
00342 
00343         // object creation
00344         /**
00345          * create a prototype.
00346          * @param name (STRING) THE NAME OF THE PROTOTYPE
00347          * @param randomStrategy (IRandomStrategy*) the random strategy to generate values of the type
00348          * which the prototype represent
00349          * @param mutationStrategy (IMutationStrategy*) the mutation strategy to change a value (Gen)
00350          * @return (GenPrototype*) the prototype
00351          */
00352         GenPrototype* createPrototype(std::string name, IRandomStrategy* randomStrategy, IMutationStrategy* mutationStrategy)const;
00353 
00354         // singleton
00355         /**
00356          * returns the one and only API to the alg.
00357          * @return (SingletonGenAlgAPI*) the api
00358          */
00359         inline static SingletonGenAlgAPI* getInstance(void) {if(m_api==0)m_api = new SingletonGenAlgAPI();return m_api;}
00360         /**
00361          * destroy the api
00362          * @param cleanStrategies (bool) default = false set a flag to clean the strategies, which are seted.
00363          */
00364         inline static void destroyAPI(bool cleanStrategies=false) {getInstance()->m_cleanStrategies=cleanStrategies; if(m_api!=0){delete m_api;m_api=0;}}
00365 
00366         // data access
00367         /**
00368          * returns the best individual which the alg. have found
00369          * @return (Individual*) the best
00370          */
00371         inline Individual* getBestIndividual(void)const {return SingletonGenEngine::getInstance()->getBestIndividual();}
00372 
00373 protected:
00374         /**
00375          * the api
00376          */
00377         static SingletonGenAlgAPI* m_api;
00378 
00379         /**
00380          * plot option engine for data measure
00381          */
00382         PlotOptionEngine* m_plotEngine;
00383 
00384         /**
00385          * plot option engine for gen contexts
00386          */
00387         PlotOptionEngine* m_plotEngineGenContext;
00388 
00389 private:
00390         /**
00391          * disable the default contructor
00392          */
00393         SingletonGenAlgAPI();
00394         /**
00395          * disable the default destructor
00396          */
00397         virtual ~SingletonGenAlgAPI();
00398 
00399         /**
00400          * help declaration for prepare
00401          */
00402         void* m_generation;
00403 
00404         /**
00405          * help declaration for prepare
00406          */
00407         void* m_inspectable;
00408 
00409         /**
00410          * Flag for clean the strategies
00411          */
00412         bool m_cleanStrategies;
00413 };
00414 
00415 #endif /* SINGLETONGENALGAPI_H_ */

Generated on Fri Oct 30 16:29:01 2009 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.4.7