Individual.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 INDIVIDUAL_H_
00028 #define INDIVIDUAL_H_
00029 
00030 //includes
00031 #include <vector>
00032 #include <string>
00033 #include <map>
00034 
00035 //ga_tools includes
00036 #include "Gen.h"
00037 #include "SingletonGenEngine.h"
00038 
00039 //forward declaration
00040 struct RESTORE_GA_INDIVIDUAL;
00041 
00042 /**
00043  * This class represent one individual of the complete gen. alg. It have some gens and a fitness.
00044  */
00045 class Individual {
00046 public:
00047         /**
00048          * constructor
00049          * Create the individual with the name "name"
00050          * @param name (sting) the name of the new individual
00051          * @param id (int) an unique ID
00052          * @param p1 (Individual*) parent 1 or zero if it random generate
00053          * @param p2 (Individual*) parent 2 or zero if it random generate
00054          */
00055         Individual(std::string name, int id, Individual* p1=0, Individual* p2=0);
00056 
00057         /**
00058          * default destructor
00059          */
00060         virtual ~Individual();
00061 
00062         /**
00063          * return the ID of the individual
00064          * @return (int) the ID
00065          */
00066         inline int getID(void)const {return m_ID;}
00067 
00068         /**
00069          * return the name of the individual
00070          * @return (string) the name
00071          */
00072         inline std::string getName(void)const {return m_name;}
00073 
00074         /**
00075          * return the size of the individual. This mean the number of gens inside the individual.
00076          * @return (int) number of gens
00077          */
00078         inline int getSize(void)const {return m_gene.size();}
00079 
00080         /**
00081          * return a gen of the individual
00082          * @param x (the index of the gen
00083          * @return (Gen*) the searched gen. NULL if the index isn't right
00084          */
00085         inline Gen* getGen(int x)const {if(x<getSize())return m_gene[x];return NULL;}
00086 
00087         /**
00088          * add a gen to the individual.
00089          * @param gen (Gen*) the new gen
00090          */
00091         inline void addGen(Gen* gen) {m_gene.push_back(gen);}
00092 
00093         /**
00094          * returns all gens of the individual
00095          * @return (vector<Gen*>&) all gens
00096          */
00097         inline const std::vector<Gen*>& getGene(void)const {return m_gene;}
00098 
00099         /**
00100          * remove a specified gen from the individual
00101          * @param gen (Gen*) the gen which should removed from the individual
00102          */
00103         void removeGen(Gen* gen);
00104 
00105         /**
00106          * remove a specified gen from the individual
00107          * @param x (the index of the gen, which should b removed.
00108          */
00109         void removeGen(int x);
00110 
00111         /**
00112          * this function calculate the fitness value of the individual
00113          * @return fitness value
00114          */
00115         double getFitness();
00116 
00117   /**
00118    * this function calculate the fitness value of the individual (const)
00119    * @return fitness value
00120    */
00121   double getFitnessC()const;
00122 
00123         /**
00124          * this select the individual as a product of mutation.
00125          */
00126         inline void setMutated(void) {m_mutated=true;}
00127 
00128         /**
00129          * returns parent 1 of the individual
00130          * @return (Individual*) parent 1 (could be zero if individual a random generation)
00131          */
00132         inline const Individual* getParent1(void)const {return m_parent1;}
00133 
00134         /**
00135          * returns parent 2 of the individual
00136          * @return (Individual*) parent 2 (could be zero if individual a random generation)
00137          */
00138         inline const Individual* getParent2(void)const {return m_parent2;}
00139 
00140         /**
00141          * test if the individual a product of mutation
00142          * @return (bool) true if it a product of mutation
00143          */
00144         inline bool isMutated(void)const {return m_mutated;}
00145 
00146         /**
00147          * returns a string, which repesent the individual (for logging)
00148          * @return (string) the string representation of the individual
00149          */
00150         std::string IndividualToString(void)const;
00151 
00152         /**
00153          * returns a string with the parents and a mark for mutation
00154          * @param withMutation (bool) should mutation part of the string
00155          * @return (string) the result
00156          */
00157         std::string RootToString(bool withMutation=true)const;
00158 
00159         /**
00160          * returns the m_fitnessCalculated flag, which represent, that the fitness value was calculated before.
00161          * @return (bool) the flag m_fitnessCalculated
00162          */
00163         inline bool isFitnessCalculated()const {return m_fitnessCalculated;}
00164 
00165         /**
00166          * store the individual in a file
00167          * @param f (FILE) the file to store in
00168          * @return (bool) return true if ok
00169          */
00170         bool store(FILE* f)const;
00171 
00172         /**
00173          * restore all individual from a restore structure
00174          * @param numberIndividuals (int) number of individuals which should be restored
00175          * @param nameSet (map<int,string>) names of the individuals
00176          * @param individualSet (map<int,RESTORE_GA_INDIVIDUAL*> the structures which should be restored
00177          * @param linkSet (map<int,vector<int>>) the linkings between the individual and the genes
00178    * @return (bool) true if all ok
00179          */
00180         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);
00181 
00182         /**
00183          * restore the parent links from a restore structure
00184          * @param numberIndividuals (int) number of individuals which should be restored
00185    * @param individualSet (map<int,RESTORE_GA_INDIVIDUAL*> the structures which should be restored
00186    * @return (bool) true if all ok
00187          */
00188         static bool restoreParent(int numberIndividuals,std::map<int,RESTORE_GA_INDIVIDUAL*>& individualSet);
00189 
00190 protected:
00191         /**
00192          * the name of the individual
00193          */
00194         std::string m_name;
00195 
00196         /**
00197          * the ID of the individual
00198          */
00199         int m_ID;
00200 
00201         /**
00202          * the gens inside the individual
00203          */
00204         std::vector<Gen*> m_gene;
00205 
00206         /**
00207          * parent 1
00208          */
00209         Individual* m_parent1;
00210 
00211         /**
00212          * parent 2
00213          */
00214         Individual* m_parent2;
00215 
00216         /**
00217          * remember if the individual a product of mutation
00218          */
00219         bool m_mutated;
00220 
00221         /**
00222          * remember if the fitness value was calculated
00223          */
00224         bool m_fitnessCalculated;
00225 
00226         /**
00227          * save the calculated fitness value
00228          */
00229         double m_fitness;
00230 
00231 private:
00232         /**
00233          * disable the default constructor
00234          */
00235         Individual();
00236 };
00237 
00238 #endif /* INDIVIDUAL_H_ */
Generated on Thu Jun 28 14:45:36 2012 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3