00001 #ifndef CGENERALPOPULATION_H 00002 #define CGENERALPOPULATION_H 00003 #include "gentypes.h" 00004 #include "GeneralRNG.h" 00005 #include "FatalException.h" 00006 00007 namespace gplib 00008 { 00009 /** \addtogroup gainv Genetic algorithm optimization */ 00010 /* @{ */ 00011 00012 //! The base class for the population of a genetic algorithm, implements storage and access functions 00013 class GeneralPopulation 00014 { 00015 private: 00016 //! The population of the last iteration for elitism 00017 tpopulation OldPopulation; 00018 //! Did we store the last population 00019 bool OldStored; 00020 //! The selection probabilities for each population member 00021 tprobabilityv Probabilities; 00022 //! The crowding distance for each population member 00023 tcrowddistv CrowdingDistances; 00024 protected: 00025 //! Change the population size 00026 void ResizePop(const int popsize, const int genesize); 00027 //! The population of the current iteration 00028 tpopulation Population; 00029 public: 00030 virtual void InitPop() 00031 { 00032 } 00033 ; 00034 const tpopulation& GetOldPopulation() const 00035 { 00036 if (OldStored) 00037 return OldPopulation; 00038 else 00039 throw FatalException("Old Population has not been stored"); 00040 } 00041 ; 00042 void StoreOldPopulation() 00043 { 00044 OldPopulation = Population; 00045 OldStored = true; 00046 } 00047 ; 00048 void PrintPopulation(std::ostream &output) const; 00049 void ReadPopulation(std::istream &input); 00050 void PrintProbabilities(std::ostream &output) const; 00051 void PrintDistances(std::ostream &output) const; 00052 const tpopulation& GetPopulation() const 00053 { 00054 return Population; 00055 } 00056 ; 00057 const tprobabilityv& GetProbabilities() const 00058 { 00059 return Probabilities; 00060 } 00061 ; 00062 const tcrowddistv& GetCrowdingDistances() const 00063 { 00064 return CrowdingDistances; 00065 } 00066 ; 00067 void SetPopulation(const tpopulation &LocalPop) 00068 { 00069 OldPopulation = Population; 00070 Population = LocalPop; 00071 OldStored = true; 00072 } 00073 ; 00074 void SetProbabilities(const tprobabilityv &LocalProb) 00075 { 00076 Probabilities = LocalProb; 00077 } 00078 ; 00079 void SetCrowdingDistances(const tcrowddistv &LocalDist) 00080 { 00081 CrowdingDistances = LocalDist; 00082 } 00083 ; 00084 int GetPopsize() const 00085 { 00086 return Population.size1(); 00087 } 00088 ; 00089 int GetGenesize() const 00090 { 00091 return Population.size2(); 00092 } 00093 ; 00094 GeneralPopulation(const int popsize, const int genesize); 00095 GeneralPopulation(GeneralPopulation &Old); 00096 //! Merge two populations in a new population object 00097 GeneralPopulation(const tpopulation &FirstHalf, 00098 const tpopulation &SecondHalf); 00099 virtual GeneralPopulation& operator=(const GeneralPopulation &source); 00100 virtual ~GeneralPopulation(); 00101 }; 00102 /* @} */ 00103 } 00104 #endif // CGENERALPOPULATION_H
1.5.8