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 used for grouping some individuals which representing * 00027 * one step in the gen. alg. (called generation). For this it save all * 00028 * individual which are part of this generation. Also it have an Number * 00029 * like a ID, which make this generation individual. * 00030 * * 00031 * All Generations inside the gen.alg. are only saved in the GenEngine. * 00032 * * 00033 * $Log: Generation.h,v $ 00034 * Revision 1.12 2009/10/23 10:47:45 robot12 00035 * bugfix in store and restore 00036 * 00037 * Revision 1.11 2009/10/21 14:08:06 robot12 00038 * add restore and store functions to the ga package 00039 * 00040 * Revision 1.10 2009/10/01 13:36:04 robot12 00041 * add a methode to become a vector for all individuals which must be calculated (fitness value) 00042 * 00043 * Revision 1.9 2009/08/11 12:57:38 robot12 00044 * change the genetic algorithm (first crossover, second select) 00045 * 00046 * Revision 1.8 2009/07/21 08:39:01 robot12 00047 * rename "crosover" to crossover 00048 * 00049 * Revision 1.7 2009/06/29 15:30:11 robot12 00050 * finishing Generation and add some comments 00051 * 00052 * Revision 1.6 2009/05/14 15:29:54 robot12 00053 * bugfix: mutation change the oldGen, not the new!!! now fixed 00054 * 00055 * Revision 1.5 2009/05/12 13:29:25 robot12 00056 * some new function 00057 * -> toString methodes 00058 * 00059 * Revision 1.4 2009/05/07 14:47:47 robot12 00060 * some comments 00061 * 00062 * Revision 1.3 2009/05/04 15:27:55 robot12 00063 * rename of some files and moving files to other positions 00064 * - SingletonGenAlgAPI has one error!!! --> is not ready now 00065 * 00066 * Revision 1.5 2009/04/30 14:32:34 robot12 00067 * some implements... Part5 00068 * 00069 * Revision 1.4 2009/04/30 11:35:53 robot12 00070 * some changes: 00071 * - insert a SelectStrategie 00072 * - insert a MutationStrategie 00073 * - reorganisation of the design 00074 * 00075 * Revision 1.3 2009/04/29 11:36:41 robot12 00076 * some implements... Part3 00077 * 00078 * Revision 1.2 2009/04/28 13:23:55 robot12 00079 * some implements... Part2 00080 * 00081 * Revision 1.1 2009/04/27 10:59:33 robot12 00082 * some implements 00083 * 00084 * 00085 ***************************************************************************/ 00086 00087 #ifndef GENERATION_H_ 00088 #define GENERATION_H_ 00089 00090 // standard includes 00091 #include <string> 00092 #include <vector> 00093 #include <map> 00094 #include <selforg/randomgenerator.h> 00095 #include <selforg/inspectable.h> 00096 00097 // forward declarations 00098 class Individual; 00099 struct RESTORE_GA_GENERATION; 00100 00101 // gen. alg. includes 00102 00103 /** 00104 * The Generation class 00105 * 00106 * This class is used for grouping some individuals which representing 00107 * one step in the gen. alg. (called generation). For this it save all 00108 * individual which are part of this generation. Also it have an Number 00109 * like a ID, which make this generation individual. 00110 * 00111 * All Generations inside the gen.alg. are only saved in the GenEngine. 00112 */ 00113 class Generation : public Inspectable { 00114 public: 00115 /** 00116 * constructor to create a Generation. Information which the class need are 00117 * the generation number, the size of it and how many individual don t come 00118 * in the next generation (killRate). 00119 * 00120 * @param generationNumber (int) The ID of the Generation. 00121 * @param size (int) The Size of this Generation. Means how many individual are lives in this generation 00122 * @param numChildren (int) Number of individual which will be created by crossover 00123 */ 00124 Generation(int generationNumber, int size, int numChildren); 00125 00126 /** 00127 * destructor to delete a GenContext. 00128 */ 00129 virtual ~Generation(); 00130 00131 /** 00132 * [inline], [const] 00133 * This function gives the ID (number) of the generation back. 00134 * 00135 * @return (int) The ID 00136 */ 00137 inline int getGenerationNumber(void)const {return m_generationNumber;} 00138 00139 /** 00140 * [inline], [const] 00141 * This function gives the size which is planed for this generation back. 00142 * 00143 * @return (int) The planed size 00144 */ 00145 inline int getSize(void)const {return m_size;} 00146 00147 /** 00148 * [inline], [const] 00149 * This function gives the actual size (number of individuals inside the generation) back. 00150 * 00151 * @return (int) current size 00152 */ 00153 inline int getCurrentSize(void)const {return m_individual.size();} 00154 00155 /** 00156 * [inline], [const] 00157 * This function gives the number of children back, which will be created by crossover. 00158 * 00159 * @return (int) the number of children 00160 */ 00161 inline int getNumChildren(void)const {return m_numChildren;} 00162 00163 /** 00164 * [individual], [const] 00165 * This function gives one individual from this generation back. 00166 * 00167 * @param x (int) the index of the searched individual 00168 * 00169 * @return (Individual*) The individual. If 0, if the param x is not inside the index range 00170 */ 00171 inline Individual* getIndividual(int x)const {if(x<getCurrentSize())return m_individual[x];return NULL;} 00172 00173 /** 00174 * [inline], [const] 00175 * This function gives all individual back. 00176 * 00177 * @return (vector<Individual*>&) all individual inside the generation 00178 */ 00179 inline const std::vector<Individual*>& getAllIndividual(void)const {return m_individual;} 00180 00181 /** 00182 * [inline], [const] 00183 * This function gives all individual back which aren't have the fitness value calculated. 00184 * 00185 * @return (vector<Individual*>&) all individual inside the generation 00186 */ 00187 std::vector<Individual*>* getAllUnCalculatedIndividuals(void)const; 00188 00189 /** 00190 * This function insert an individual in the generation. 00191 * 00192 * @param individual (Individual*) the individual which should be insert in the generation 00193 */ 00194 void addIndividual(Individual* individual); 00195 00196 /** 00197 * This function makes an crossOver whit the existing individuals to become from the current size the planed size. 00198 * 00199 * @param random (RandGen*) a pseudo number generator. 00200 */ 00201 void crossover(RandGen* random); 00202 00203 /** 00204 * returns a string which represent all individual in this generation. 00205 * 00206 * @return (string) the string 00207 */ 00208 std::string getAllIndividualAsString(void)const; 00209 00210 /** 00211 * returns all fitness values from the individuals. 00212 * 00213 * @return (vector<double> the fitness values. 00214 */ 00215 std::vector<double>* getAllFitness(void)const; 00216 00217 /** 00218 * This function updates the statistical values 00219 * @param factor (double) normal 1.5 Is needed for the data analysation 00220 */ 00221 void update(double factor = 1.5); 00222 00223 /** 00224 * store a generation in a file 00225 * @param f (FILE*) the file in which should be stored 00226 * @return (bool) true if all ok 00227 */ 00228 bool store(FILE* f)const; 00229 00230 /** 00231 * restore all generation from a restore structure 00232 * 00233 * remember the individuals must be restored before 00234 * 00235 * @param numberGeneration (int) number of generations which should be restored 00236 * @param generationSet (map<int,RESTORE_GA_GENERATION*>) the structures which should be restored 00237 * @param linkSet (map<int,vector<int>>) the linkings between the generation and the individuals 00238 * @return (bool) true if all ok 00239 */ 00240 static bool restore(int numberGeneration, std::map<int,RESTORE_GA_GENERATION*>& generationSet, std::map<int,std::vector<int> >& linkSet); 00241 00242 protected: 00243 /** 00244 * (int) 00245 * The generation number (ID) 00246 */ 00247 int m_generationNumber; 00248 00249 /** 00250 * (vector<Individual*>) 00251 * The storage for the individuals, which are part of this generation. (NO deleting) 00252 */ 00253 std::vector<Individual*> m_individual; 00254 00255 /** 00256 * (int) 00257 * The planed size of the generation. 00258 */ 00259 int m_size; 00260 00261 /** 00262 * (int) 00263 * The number of children 00264 */ 00265 int m_numChildren; 00266 00267 private: 00268 /** 00269 * disable the default constructor 00270 */ 00271 Generation(); 00272 00273 /** 00274 * the under quartil 00275 */ 00276 double m_q1; 00277 00278 /** 00279 * the upper quartil 00280 */ 00281 double m_q3; 00282 00283 /** 00284 * the min 00285 */ 00286 double m_min; 00287 00288 /** 00289 * the max 00290 */ 00291 double m_max; 00292 00293 /** 00294 * the average 00295 */ 00296 double m_avg; 00297 00298 /** 00299 * the median 00300 */ 00301 double m_med; 00302 00303 /** 00304 * the under whisker 00305 */ 00306 double m_w1; 00307 00308 /** 00309 * the upper whisker 00310 */ 00311 double m_w3; 00312 00313 /** 00314 * the best fitness value inside the generation 00315 */ 00316 double m_best; 00317 00318 /** 00319 * the number of individual inside the generation (will be) 00320 */ 00321 double m_dSize; 00322 00323 /** 00324 * the number of individual which will be created by crossover. 00325 */ 00326 double m_dNumChildren; 00327 }; 00328 00329 #endif /* GENERATION_H_ */