Individual.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 represent one individual of the complete gen. alg. It have *
00027  *   some gens and a fitness.                                              *
00028  *                                                                         *
00029  *   $Log: Individual.h,v $
00030  *   Revision 1.12  2009/11/05 14:07:41  robot12
00031  *   bugfix for restore and store
00032  *
00033  *   Revision 1.11  2009/10/23 10:47:45  robot12
00034  *   bugfix in store and restore
00035  *
00036  *   Revision 1.10  2009/10/21 14:08:07  robot12
00037  *   add restore and store functions to the ga package
00038  *
00039  *   Revision 1.9  2009/10/01 13:29:42  robot12
00040  *   now the individual save his own fitness value
00041  *
00042  *   Revision 1.8  2009/07/21 08:37:58  robot12
00043  *   add some comments
00044  *
00045  *   Revision 1.7  2009/06/29 14:52:14  robot12
00046  *   finishing Individual and add some comments
00047  *
00048  *   Revision 1.6  2009/05/12 13:29:25  robot12
00049  *   some new function
00050  *   -> toString methodes
00051  *
00052  *   Revision 1.5  2009/05/11 14:08:51  robot12
00053  *   patch some bugfix....
00054  *
00055  *   Revision 1.4  2009/05/04 15:27:56  robot12
00056  *   rename of some files and moving files to other positions
00057  *    - SingletonGenAlgAPI has one error!!! --> is not ready now
00058  *
00059  *   Revision 1.7  2009/04/30 14:32:34  robot12
00060  *   some implements... Part5
00061  *
00062  *   Revision 1.6  2009/04/30 11:35:53  robot12
00063  *   some changes:
00064  *    - insert a SelectStrategie
00065  *    - insert a MutationStrategie
00066  *    - reorganisation of the design
00067  *
00068  *   Revision 1.5  2009/04/28 13:23:55  robot12
00069  *   some implements... Part2
00070  *
00071  *   Revision 1.4  2009/04/27 10:59:34  robot12
00072  *   some implements
00073  *
00074  *
00075  ***************************************************************************/
00076 
00077 #ifndef INDIVIDUAL_H_
00078 #define INDIVIDUAL_H_
00079 
00080 //includes
00081 #include <vector>
00082 #include <string>
00083 #include <map>
00084 
00085 //ga_tools includes
00086 #include "Gen.h"
00087 #include "SingletonGenEngine.h"
00088 
00089 //forward declaration
00090 struct RESTORE_GA_INDIVIDUAL;
00091 
00092 /**
00093  * This class represent one individual of the complete gen. alg. It have some gens and a fitness.
00094  */
00095 class Individual {
00096 public:
00097         /**
00098          * constructor
00099          * Create the individual with the name "name"
00100          * @param name (sting) the name of the new individual
00101          * @param id (int) an unique ID
00102          * @param p1 (Individual*) parent 1 or zero if it random generate
00103          * @param p2 (Individual*) parent 2 or zero if it random generate
00104          */
00105         Individual(std::string name, int id, Individual* p1=0, Individual* p2=0);
00106 
00107         /**
00108          * default destructor
00109          */
00110         virtual ~Individual();
00111 
00112         /**
00113          * return the ID of the individual
00114          * @return (int) the ID
00115          */
00116         inline int getID(void)const {return m_ID;}
00117 
00118         /**
00119          * return the name of the individual
00120          * @return (string) the name
00121          */
00122         inline std::string getName(void)const {return m_name;}
00123 
00124         /**
00125          * return the size of the individual. This mean the number of gens inside the individual.
00126          * @return (int) number of gens
00127          */
00128         inline int getSize(void)const {return m_gene.size();}
00129 
00130         /**
00131          * return a gen of the individual
00132          * @param x (the index of the gen
00133          * @return (Gen*) the searched gen. NULL if the index isn't right
00134          */
00135         inline Gen* getGen(int x)const {if(x<getSize())return m_gene[x];return NULL;}
00136 
00137         /**
00138          * add a gen to the individual.
00139          * @param gen (Gen*) the new gen
00140          */
00141         inline void addGen(Gen* gen) {m_gene.push_back(gen);}
00142 
00143         /**
00144          * returns all gens of the individual
00145          * @return (vector<Gen*>&) all gens
00146          */
00147         inline const std::vector<Gen*>& getGene(void)const {return m_gene;}
00148 
00149         /**
00150          * remove a specified gen from the individual
00151          * @param gen (Gen*) the gen which should removed from the individual
00152          */
00153         void removeGen(Gen* gen);
00154 
00155         /**
00156          * remove a specified gen from the individual
00157          * @param x (the index of the gen, which should b removed.
00158          */
00159         void removeGen(int x);
00160 
00161         /**
00162          * this function calculate the fitness value of the individual
00163          * @return fitness value
00164          */
00165         double getFitness();
00166 
00167   /**
00168    * this function calculate the fitness value of the individual (const)
00169    * @return fitness value
00170    */
00171   double getFitnessC()const;
00172 
00173         /**
00174          * this select the individual as a product of mutation.
00175          */
00176         inline void setMutated(void) {m_mutated=true;}
00177 
00178         /**
00179          * returns parent 1 of the individual
00180          * @return (Individual*) parent 1 (could be zero if individual a random generation)
00181          */
00182         inline const Individual* getParent1(void)const {return m_parent1;}
00183 
00184         /**
00185          * returns parent 2 of the individual
00186          * @return (Individual*) parent 2 (could be zero if individual a random generation)
00187          */
00188         inline const Individual* getParent2(void)const {return m_parent2;}
00189 
00190         /**
00191          * test if the individual a product of mutation
00192          * @return (bool) true if it a product of mutation
00193          */
00194         inline bool isMutated(void)const {return m_mutated;}
00195 
00196         /**
00197          * returns a string, which repesent the individual (for logging)
00198          * @return (string) the string representation of the individual
00199          */
00200         std::string IndividualToString(void)const;
00201 
00202         /**
00203          * returns a string with the parents and a mark for mutation
00204          * @param withMutation (bool) should mutation part of the string
00205          * @return (string) the result
00206          */
00207         std::string RootToString(bool withMutation=true)const;
00208 
00209         /**
00210          * returns the m_fitnessCalculated flag, which represent, that the fitness value was calculated before.
00211          * @return (bool) the flag m_fitnessCalculated
00212          */
00213         inline bool isFitnessCalculated()const {return m_fitnessCalculated;}
00214 
00215         /**
00216          * store the individual in a file
00217          * @param f (FILE) the file to store in
00218          * @return (bool) return true if ok
00219          */
00220         bool store(FILE* f)const;
00221 
00222         /**
00223          * restore all individual from a restore structure
00224          * @param numberIndividuals (int) number of individuals which should be restored
00225          * @param nameSet (map<int,string>) names of the individuals
00226          * @param individualSet (map<int,RESTORE_GA_INDIVIDUAL*> the structures which should be restored
00227          * @param linkSet (map<int,vector<int>>) the linkings between the individual and the genes
00228    * @return (bool) true if all ok
00229          */
00230         static bool restore(int numberIndividuals,std::map<int,std::string>& nameSet,std::map<int,RESTORE_GA_INDIVIDUAL*>& individualSet, std::map<int,std::vector<int> >& linkSet, std::vector<Individual*>& storage);
00231 
00232         /**
00233          * restore the parent links from a restore structure
00234          * @param numberIndividuals (int) number of individuals which should be restored
00235    * @param individualSet (map<int,RESTORE_GA_INDIVIDUAL*> the structures which should be restored
00236    * @return (bool) true if all ok
00237          */
00238         static bool restoreParent(int numberIndividuals,std::map<int,RESTORE_GA_INDIVIDUAL*>& individualSet);
00239 
00240 protected:
00241         /**
00242          * the name of the individual
00243          */
00244         std::string m_name;
00245 
00246         /**
00247          * the ID of the individual
00248          */
00249         int m_ID;
00250 
00251         /**
00252          * the gens inside the individual
00253          */
00254         std::vector<Gen*> m_gene;
00255 
00256         /**
00257          * parent 1
00258          */
00259         Individual* m_parent1;
00260 
00261         /**
00262          * parent 2
00263          */
00264         Individual* m_parent2;
00265 
00266         /**
00267          * remember if the individual a product of mutation
00268          */
00269         bool m_mutated;
00270 
00271         /**
00272          * remember if the fitness value was calculated
00273          */
00274         bool m_fitnessCalculated;
00275 
00276         /**
00277          * save the calculated fitness value
00278          */
00279         double m_fitness;
00280 
00281 private:
00282         /**
00283          * disable the default constructor
00284          */
00285         Individual();
00286 };
00287 
00288 #endif /* INDIVIDUAL_H_ */
Generated on Fri Nov 4 10:59:38 2011 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3