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 GENERATION_H_ 00028 #define GENERATION_H_ 00029 00030 // standard includes 00031 #include <string> 00032 #include <vector> 00033 #include <map> 00034 #include <selforg/randomgenerator.h> 00035 #include <selforg/inspectable.h> 00036 00037 // forward declarations 00038 class Individual; 00039 struct RESTORE_GA_GENERATION; 00040 00041 // gen. alg. includes 00042 00043 /** 00044 * The Generation class 00045 * 00046 * This class is used for grouping some individuals which representing 00047 * one step in the gen. alg. (called generation). For this it save all 00048 * individual which are part of this generation. Also it have an Number 00049 * like a ID, which make this generation individual. 00050 * 00051 * All Generations inside the gen.alg. are only saved in the GenEngine. 00052 */ 00053 class Generation : public Inspectable { 00054 public: 00055 /** 00056 * constructor to create a Generation. Information which the class need are 00057 * the generation number, the size of it and how many individual don t come 00058 * in the next generation (killRate). 00059 * 00060 * @param generationNumber (int) The ID of the Generation. 00061 * @param size (int) The Size of this Generation. Means how many individual are lives in this generation 00062 * @param numChildren (int) Number of individual which will be created by crossover 00063 */ 00064 Generation(int generationNumber, int size, int numChildren); 00065 00066 /** 00067 * destructor to delete a GenContext. 00068 */ 00069 virtual ~Generation(); 00070 00071 /** 00072 * [inline], [const] 00073 * This function gives the ID (number) of the generation back. 00074 * 00075 * @return (int) The ID 00076 */ 00077 inline int getGenerationNumber(void)const {return m_generationNumber;} 00078 00079 /** 00080 * [inline], [const] 00081 * This function gives the size which is planed for this generation back. 00082 * 00083 * @return (int) The planed size 00084 */ 00085 inline int getSize(void)const {return m_size;} 00086 00087 /** 00088 * [inline], [const] 00089 * This function gives the actual size (number of individuals inside the generation) back. 00090 * 00091 * @return (int) current size 00092 */ 00093 inline int getCurrentSize(void)const {return m_individual.size();} 00094 00095 /** 00096 * [inline], [const] 00097 * This function gives the number of children back, which will be created by crossover. 00098 * 00099 * @return (int) the number of children 00100 */ 00101 inline int getNumChildren(void)const {return m_numChildren;} 00102 00103 /** 00104 * [individual], [const] 00105 * This function gives one individual from this generation back. 00106 * 00107 * @param x (int) the index of the searched individual 00108 * 00109 * @return (Individual*) The individual. If 0, if the param x is not inside the index range 00110 */ 00111 inline Individual* getIndividual(int x)const {if(x<getCurrentSize())return m_individual[x];return NULL;} 00112 00113 /** 00114 * [inline], [const] 00115 * This function gives all individual back. 00116 * 00117 * @return (vector<Individual*>&) all individual inside the generation 00118 */ 00119 inline const std::vector<Individual*>& getAllIndividual(void)const {return m_individual;} 00120 00121 /** 00122 * [inline], [const] 00123 * This function gives all individual back which aren't have the fitness value calculated. 00124 * 00125 * @return (vector<Individual*>&) all individual inside the generation 00126 */ 00127 std::vector<Individual*>* getAllUnCalculatedIndividuals(void)const; 00128 00129 /** 00130 * This function insert an individual in the generation. 00131 * 00132 * @param individual (Individual*) the individual which should be insert in the generation 00133 */ 00134 void addIndividual(Individual* individual); 00135 00136 /** 00137 * This function makes an crossOver whit the existing individuals to become from the current size the planed size. 00138 * 00139 * @param random (RandGen*) a pseudo number generator. 00140 */ 00141 void crossover(RandGen* random); 00142 00143 /** 00144 * returns a string which represent all individual in this generation. 00145 * 00146 * @return (string) the string 00147 */ 00148 std::string getAllIndividualAsString(void)const; 00149 00150 /** 00151 * returns all fitness values from the individuals. 00152 * 00153 * @return (vector<double> the fitness values. 00154 */ 00155 std::vector<double>* getAllFitness(void)const; 00156 00157 /** 00158 * This function updates the statistical values 00159 * @param factor (double) normal 1.5 Is needed for the data analysation 00160 */ 00161 void update(double factor = 1.5); 00162 00163 /** 00164 * store a generation in a file 00165 * @param f (FILE*) the file in which should be stored 00166 * @return (bool) true if all ok 00167 */ 00168 bool store(FILE* f)const; 00169 00170 /** 00171 * restore all generation from a restore structure 00172 * 00173 * remember the individuals must be restored before 00174 * 00175 * @param numberGeneration (int) number of generations which should be restored 00176 * @param generationSet (map<int,RESTORE_GA_GENERATION*>) the structures which should be restored 00177 * @param linkSet (map<int,vector<int>>) the linkings between the generation and the individuals 00178 * @return (bool) true if all ok 00179 */ 00180 static bool restore(int numberGeneration, std::map<int,RESTORE_GA_GENERATION*>& generationSet, std::map<int,std::vector<int> >& linkSet); 00181 00182 protected: 00183 /** 00184 * (int) 00185 * The generation number (ID) 00186 */ 00187 int m_generationNumber; 00188 00189 /** 00190 * (vector<Individual*>) 00191 * The storage for the individuals, which are part of this generation. (NO deleting) 00192 */ 00193 std::vector<Individual*> m_individual; 00194 00195 /** 00196 * (int) 00197 * The planed size of the generation. 00198 */ 00199 int m_size; 00200 00201 /** 00202 * (int) 00203 * The number of children 00204 */ 00205 int m_numChildren; 00206 00207 private: 00208 /** 00209 * disable the default constructor 00210 */ 00211 Generation(); 00212 00213 /** 00214 * the under quartil 00215 */ 00216 double m_q1; 00217 00218 /** 00219 * the upper quartil 00220 */ 00221 double m_q3; 00222 00223 /** 00224 * the min 00225 */ 00226 double m_min; 00227 00228 /** 00229 * the max 00230 */ 00231 double m_max; 00232 00233 /** 00234 * the average 00235 */ 00236 double m_avg; 00237 00238 /** 00239 * the median 00240 */ 00241 double m_med; 00242 00243 /** 00244 * the under whisker 00245 */ 00246 double m_w1; 00247 00248 /** 00249 * the upper whisker 00250 */ 00251 double m_w3; 00252 00253 /** 00254 * the best fitness value inside the generation 00255 */ 00256 double m_best; 00257 00258 /** 00259 * the number of individual inside the generation (will be) 00260 */ 00261 double m_dSize; 00262 00263 /** 00264 * the number of individual which will be created by crossover. 00265 */ 00266 double m_dNumChildren; 00267 }; 00268 00269 #endif /* GENERATION_H_ */