Robot Simulator of the Robotics Group for Self-Organization of Control: SingletonIndividualFactory.h Source File

# SingletonIndividualFactory.h

Go to the documentation of this file.

    00001 /***************************************************************************
    00002  *   Copyright (C) 2008-2011 LpzRobots development team                    *
    00003  *    Joerg Weider   <joergweide84 at aol dot com> (robot12)               *
    00004  *    Georg Martius  <georg dot martius at web dot de>                     *
    00005  *    Frank Guettler <guettler at informatik dot uni-leipzig dot de        *
    00006  *    Frank Hesse    <frank at nld dot ds dot mpg dot de>                  *
    00007  *    Ralf Der       <ralfder at mis dot mpg dot de>                       *
    00008  *    Joern Hoffmann <jhoffmann at informatik dot uni-leipzig dot de       *
    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
    00027 #ifndef SINGLETONINDIVIDUALFACTORY_H_
    00028 #define SINGLETONINDIVIDUALFACTORY_H_
    00029
    00030 //includes
    00031 #include <selforg/randomgenerator.h>
    00032
    00033 //forward declaration
    00034 class Gen;
    00035 class Generation;
    00036
    00037 //ga_tools includes
    00038 #include "Individual.h"
    00039
    00040 /**
    00041  * this is a factory for the individual class. It use the SingletonGenFactory to create new individuals.
    00042  * It have 2 methods to create a individual. (random and recombination)
    00043  *
    00044  * Over this is the class as singleton concepted. Only one Factory for a run.
    00045  */
    00046 class SingletonIndividualFactory {
    00047 public:
    00048         /**
    00049          * this method gives the one and only existing factory back.
    00050          * @return (SingletonIndividualFactory*) the factory
    00051          */
    00052         inline static SingletonIndividualFactory* getInstance(void) {
    00053                 if(m_factory==0)m_factory = new SingletonIndividualFactory;
    00054                 return m_factory;
    00055         }
    00056
    00057         /**
    00058          * destroy the only existing factory
    00059          */
    00060         inline static void destroyFactory(void) {if(m_factory!=0){delete m_factory; m_factory=NULL;}}
    00061
    00062         // 2 methods to create an individual
    00063         /**
    00064          * random creation by random creation of Gen for every GenPrototype.
    00065          * @param name (string) the name of the new individual. Will be automaticly created
    00066          * @return (Individual*) the new individual
    00067          */
    00068         Individual* createIndividual(std::string name=createName())const;                                                                                                                                               // random
    00069         /**
    00070          * create a new individual by recombination of the gens of there parents
    00071          * @param individual1 (Individual*) parent 1
    00072          * @param individual2 (Individual*) parent 2
    00073          * @param random (RandGen*) a random generator
    00074          * @param name (string) the name of the new individual. Will be automaticly created
    00075          * @return (Individual*) the new individual
    00076          */
    00077         Individual* createIndividual(Individual* individual1, Individual* individual2, RandGen* random, std::string name=createName())const;    // recombinate
    00078
    00079         //reset m_number inside restore
    00080         /**
    00081          * set the member variable m_number to number
    00082          * @param number (int) the new value
    00083          */
    00084         inline void setNumber(int number) {m_number=number;}
    00085
    00086 private:
    00087         /**
    00088          * the one and only factory
    00089          */
    00090         static SingletonIndividualFactory* m_factory;
    00091
    00092         /**
    00093          * a counter for individual IDs
    00094          */
    00095         static int m_number;
    00096
    00097         /**
    00098          * disable the default constructor
    00099          * only usable for itself
    00100          */
    00101         SingletonIndividualFactory();
    00102
    00103         /**
    00104          * disable the default destructor
    00105          * only usable for itself
    00106          */
    00107         virtual ~SingletonIndividualFactory();
    00108
    00109         /**
    00110          * methode to generate a name for the new individual automatically
    00111          * @return (string) the name
    00112          */
    00113         inline static std::string createName(void) {std::string s = "Ind ";char buffer[128];sprintf(buffer,"%i",m_number);s+=buffer;return s;}
    00114 };
    00115
    00116 #endif /* SINGLETONINDIVIDUALFACTORY_H_ */