SingletonIndividualFactory.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 factory for the class individual. It use the          *
00027  *   GenFactory to create a new individual, because the individual is a    *
00028  *   combination of Gens.                                                  *
00029  *                                                                         *
00030  *   $Log: SingletonIndividualFactory.h,v $
00031  *   Revision 1.6  2009/11/05 14:07:41  robot12
00032  *   bugfix for restore and store
00033  *
00034  *   Revision 1.5  2009/07/28 09:38:23  robot12
00035  *   update the Singleton::destroy
00036  *
00037  *   Revision 1.4  2009/07/21 08:37:58  robot12
00038  *   add some comments
00039  *
00040  *   Revision 1.3  2009/06/29 15:20:25  robot12
00041  *   finishing Individual Factory and add some comments
00042  *
00043  *   Revision 1.2  2009/05/11 14:08:52  robot12
00044  *   patch some bugfix....
00045  *
00046  *   Revision 1.1  2009/05/04 15:27:55  robot12
00047  *   rename of some files and moving files to other positions
00048  *    - SingletonGenAlgAPI has one error!!! --> is not ready now
00049  *
00050  *   Revision 1.5  2009/04/30 11:35:53  robot12
00051  *   some changes:
00052  *    - insert a SelectStrategie
00053  *    - insert a MutationStrategie
00054  *    - reorganisation of the design
00055  *
00056  *   Revision 1.4  2009/04/27 10:59:33  robot12
00057  *   some implements
00058  *
00059  *
00060  ***************************************************************************/
00061 
00062 #ifndef SINGLETONINDIVIDUALFACTORY_H_
00063 #define SINGLETONINDIVIDUALFACTORY_H_
00064 
00065 //includes
00066 #include <selforg/randomgenerator.h>
00067 
00068 //forward declaration
00069 class Gen;
00070 class Generation;
00071 
00072 //ga_tools includes
00073 #include "Individual.h"
00074 
00075 /**
00076  * this is a factory for the individual class. It use the SingletonGenFactory to create new individuals.
00077  * It have 2 methods to create a individual. (random and recombination)
00078  *
00079  * Over this is the class as singleton concepted. Only one Factory for a run.
00080  */
00081 class SingletonIndividualFactory {
00082 public:
00083         /**
00084          * this method gives the one and only existing factory back.
00085          * @return (SingletonIndividualFactory*) the factory
00086          */
00087         inline static SingletonIndividualFactory* getInstance(void) {
00088                 if(m_factory==0)m_factory = new SingletonIndividualFactory;
00089                 return m_factory;
00090         }
00091 
00092         /**
00093          * destroy the only existing factory
00094          */
00095         inline static void destroyFactory(void) {if(m_factory!=0){delete m_factory; m_factory=NULL;}}
00096 
00097         // 2 methods to create an individual
00098         /**
00099          * random creation by random creation of Gen for every GenPrototype.
00100          * @param name (string) the name of the new individual. Will be automaticly created
00101          * @return (Individual*) the new individual
00102          */
00103         Individual* createIndividual(std::string name=createName())const;                                                                                                                                               // random
00104         /**
00105          * create a new individual by recombination of the gens of there parents
00106          * @param individual1 (Individual*) parent 1
00107          * @param individual2 (Individual*) parent 2
00108          * @param random (RandGen*) a random generator
00109          * @param name (string) the name of the new individual. Will be automaticly created
00110          * @return (Individual*) the new individual
00111          */
00112         Individual* createIndividual(Individual* individual1, Individual* individual2, RandGen* random, std::string name=createName())const;    // recombinate
00113 
00114         //reset m_number inside restore
00115         /**
00116          * set the member variable m_number to number
00117          * @param number (int) the new value
00118          */
00119         inline void setNumber(int number) {m_number=number;}
00120 
00121 private:
00122         /**
00123          * the one and only factory
00124          */
00125         static SingletonIndividualFactory* m_factory;
00126 
00127         /**
00128          * a counter for individual IDs
00129          */
00130         static int m_number;
00131 
00132         /**
00133          * disable the default constructor
00134          * only usable for itself
00135          */
00136         SingletonIndividualFactory();
00137 
00138         /**
00139          * disable the default destructor
00140          * only usable for itself
00141          */
00142         virtual ~SingletonIndividualFactory();
00143 
00144         /**
00145          * methode to generate a name for the new individual automatically
00146          * @return (string) the name
00147          */
00148         inline static std::string createName(void) {std::string s = "Ind ";char buffer[128];sprintf(buffer,"%i",m_number);s+=buffer;return s;}
00149 };
00150 
00151 #endif /* SINGLETONINDIVIDUALFACTORY_H_ */
Generated on Fri Nov 4 10:59:39 2011 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3