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