00001 #ifndef CGENERALGA_H 00002 #define CGENERALGA_H 00003 00004 #include "GeneralPropagation.h" 00005 #include "GeneralObjective.h" 00006 #include "GeneralPopulation.h" 00007 #include "GeneralTranscribe.h" 00008 #include "GeneralRNG.h" 00009 #include "GeneralSelect.h" 00010 #include "UniquePop.h" 00011 #include <vector> 00012 #include <fstream> 00013 #include "VecMat.h" 00014 #include <boost/shared_ptr.hpp> 00015 namespace gplib 00016 { 00017 /** \addtogroup gainv Genetic algorithm optimization */ 00018 /* @{ */ 00019 00020 //! General genetic algorithm class 00021 /*! GeneralGA implements the functionality that is common to all genetic algorithms 00022 * it declares a bunch of virtual functions, that are implemented in derived classes and 00023 * pointers to several other objects that together make up the genetic algorithm 00024 */ 00025 class GeneralGA 00026 { 00027 public: 00028 //! We provide some typedefs that are used in other parts as well 00029 typedef std::vector<gplib::rvec> tparamvector; 00030 typedef std::vector<boost::shared_ptr<GeneralObjective> > 00031 tObjectiveVector; 00032 typedef std::vector<std::vector<int> > tparamindv; 00033 private: 00034 //! Calculate the misfit for all models, this implements the core functionality for misfit calculations 00035 void CalcMisfit(const int iterationnumber); 00036 //! The number of threads for parallel calculation, works only with OpenMP 00037 int Threads; 00038 //the process ID of the main program, used for file identification 00039 int Programnum; 00040 //! The average summed fit 00041 double CombAvgFit; 00042 //! The maximum summed fit 00043 double CombMaxFit; 00044 //! The minimum summed fit 00045 double CombMinFit; 00046 //! Should the GA be elitist, i.e. ensure that the best models are preserved 00047 bool Elitist; 00048 //! Holds the summed Fit for each objective function 00049 std::vector<double> CombMisFit; 00050 std::vector<double> AvgFit; 00051 //! Maximum Fit for each objective function 00052 std::vector<double> MaxFit; 00053 //! Minimum Fit for each objective Function 00054 std::vector<double> MinFit; 00055 //! The weight for each objective function, the exact meaning will depend on the algorithm, i.e. the derived class 00056 std::vector<double> Weights; 00057 //! The object holding one copy of each model vector calculated so far 00058 UniquePop UniquePopHist; 00059 //! For each objective function we store the indices of the complete model vector that each objective function needs for its calculations 00060 tparamindv ParameterIndices; 00061 protected: 00062 gplib::rmat OldMisFit; 00063 //! The number of objective functions we're using 00064 const unsigned int nobjective; 00065 //! A matrix holding the numerical model vectors (transcribed from the genes) for each population member 00066 gplib::rmat Transcribed; 00067 //! Misfit first index objective function second index population member 00068 gplib::rmat MisFit; 00069 //! The vector holding the objective functions 00070 tObjectiveVector Objective; 00071 //! A pointer to an object dealing with propagation 00072 GeneralPropagation* const Propagation; 00073 //! A pointer to an object holding the population 00074 GeneralPopulation* const Population; 00075 //! A pointer to an object translating genes to model vectors 00076 GeneralTranscribe* const Transcribe; 00077 //! default implementation does nothing, this can be overriden @see ParetoGA 00078 void virtual Elitism(const int iterationnumber) 00079 { 00080 } 00081 ; 00082 public: 00083 //! Return the weight for each objecive function 00084 const std::vector<double> &GetWeights() 00085 { 00086 return Weights; 00087 } 00088 ; 00089 //! Copy the appropriate parameter values from the transcribed vector for use with the objective functions 00090 void SetupParams(const ttranscribed &member, tparamvector ¶ms); 00091 //! Return the average fitness for each objective function 00092 const std::vector<double> &GetAvgFit() 00093 { 00094 return AvgFit; 00095 } 00096 ; 00097 //! Do we want elitist behaviour, effect depends on the GA implementetion in the derived class 00098 void SetElitist(const bool IsElitist) 00099 { 00100 Elitist = IsElitist; 00101 } 00102 ; 00103 //! Set the weights for each objective function 00104 void SetWeights(const std::vector<double> &LocalWeights); 00105 //! Configure which parts of the complete parameter vector are used in each objective function 00106 void SetParameterIndices(const tparamindv &Indices); 00107 //! Print Fitness statistics for each objective function to output 00108 void PrintFitStat(std::ostream &output); 00109 //!Print misfit for each member to output 00110 void PrintMisfit(std::ostream &output); 00111 //!Print transcribed values for each member to output 00112 void PrintTranscribed(std::ostream &output); 00113 //! Print each population member exactly once 00114 void PrintUniquePop(std::ostream &output) 00115 { 00116 UniquePopHist.PrintAll(output); 00117 } 00118 ; 00119 //! Print misfit of the best population members 00120 void PrintBestMisfit(std::ostream &output); 00121 //! This has to be implemented in the derived class to return the number of best models 00122 unsigned int virtual GetNBestmodels() = 0; 00123 //! Return the indices of the best models 00124 std::vector<int> virtual GetBestModelIndices() = 0; 00125 //! Do one iteration of the GA 00126 void virtual DoIteration(const int iterationnumber, const bool last); 00127 //! Calculate the Probabilities 00128 void virtual CalcProbabilities(const int iterationnumber, 00129 gplib::rmat &LocalMisFit, GeneralPopulation &LocalPopulation) = 0; 00130 GeneralGA(GeneralPropagation* const LocalPropagation, 00131 GeneralPopulation* const LocalPopulation, 00132 GeneralTranscribe* const LocalTranscribe, 00133 const tObjectiveVector &IndObjective, const int nthreads = 1); 00134 virtual ~GeneralGA(); 00135 }; 00136 /* @} */ 00137 } 00138 #endif // CGENERALGA_H
1.5.8