GPLIB++
GeneralGA.h
Go to the documentation of this file.
1 #ifndef CGENERALGA_H
2 #define CGENERALGA_H
3 
4 #include "GeneralPropagation.h"
5 #include "GeneralObjective.h"
6 #include "GeneralPopulation.h"
7 #include "GeneralTranscribe.h"
8 #include "GeneralRNG.h"
9 #include "GeneralSelect.h"
10 #include "UniquePop.h"
11 #include <vector>
12 #include <fstream>
13 #include "VecMat.h"
14 #include <boost/shared_ptr.hpp>
15 namespace gplib
16  {
17  /** \addtogroup gainv Genetic algorithm optimization */
18  /* @{ */
19 
20  //! General genetic algorithm class
21  /*! GeneralGA implements the functionality that is common to all genetic algorithms
22  * it declares a bunch of virtual functions, that are implemented in derived classes and
23  * pointers to several other objects that together make up the genetic algorithm
24  */
25  class GeneralGA
26  {
27  public:
28  //! We provide some typedefs that are used in other parts as well
29  typedef std::vector<gplib::rvec> tparamvector;
30  typedef std::vector<boost::shared_ptr<GeneralObjective> >
32  typedef std::vector<std::vector<int> > tparamindv;
33  private:
34  //! Calculate the misfit for all models, this implements the core functionality for misfit calculations
35  void CalcMisfit(const int iterationnumber);
36  //! The number of threads for parallel calculation, works only with OpenMP
37  int Threads;
38  //the process ID of the main program, used for file identification
39  int Programnum;
40  //! The average summed fit
41  double CombAvgFit;
42  //! The maximum summed fit
43  double CombMaxFit;
44  //! The minimum summed fit
45  double CombMinFit;
46  //! Should the GA be elitist, i.e. ensure that the best models are preserved
47  bool Elitist;
48  //! Holds the summed Fit for each objective function
49  std::vector<double> CombMisFit;
50  std::vector<double> AvgFit;
51  //! Maximum Fit for each objective function
52  std::vector<double> MaxFit;
53  //! Minimum Fit for each objective Function
54  std::vector<double> MinFit;
55  //! The weight for each objective function, the exact meaning will depend on the algorithm, i.e. the derived class
56  std::vector<double> Weights;
57  //! The object holding one copy of each model vector calculated so far
58  UniquePop UniquePopHist;
59  //! For each objective function we store the indices of the complete model vector that each objective function needs for its calculations
60  tparamindv ParameterIndices;
61  protected:
62  gplib::rmat OldMisFit;
63  //! The number of objective functions we're using
64  const unsigned int nobjective;
65  //! A matrix holding the numerical model vectors (transcribed from the genes) for each population member
66  gplib::rmat Transcribed;
67  //! Misfit first index objective function second index population member
68  gplib::rmat MisFit;
69  //! The vector holding the objective functions
71  //! A pointer to an object dealing with propagation
73  //! A pointer to an object holding the population
75  //! A pointer to an object translating genes to model vectors
77  //! default implementation does nothing, this can be overriden @see ParetoGA
78  void virtual Elitism(const int iterationnumber)
79  {
80  }
81  ;
82  public:
83  //! Return the weight for each objecive function
84  const std::vector<double> &GetWeights()
85  {
86  return Weights;
87  }
88  ;
89  //! Copy the appropriate parameter values from the transcribed vector for use with the objective functions
90  void SetupParams(const ttranscribed &member, tparamvector &params);
91  //! Return the average fitness for each objective function
92  const std::vector<double> &GetAvgFit()
93  {
94  return AvgFit;
95  }
96  ;
97  //! Do we want elitist behaviour, effect depends on the GA implementetion in the derived class
98  void SetElitist(const bool IsElitist)
99  {
100  Elitist = IsElitist;
101  }
102  ;
103  //! Set the weights for each objective function
104  void SetWeights(const std::vector<double> &LocalWeights);
105  //! Configure which parts of the complete parameter vector are used in each objective function
106  void SetParameterIndices(const tparamindv &Indices);
107  //! Print Fitness statistics for each objective function to output
108  void PrintFitStat(std::ostream &output);
109  //!Print misfit for each member to output
110  void PrintMisfit(std::ostream &output);
111  //!Print transcribed values for each member to output
112  void PrintTranscribed(std::ostream &output);
113  //! Print each population member exactly once
114  void PrintUniquePop(std::ostream &output)
115  {
116  UniquePopHist.PrintAll(output);
117  }
118  ;
119  //! Print misfit of the best population members
120  void PrintBestMisfit(std::ostream &output);
121  //! This has to be implemented in the derived class to return the number of best models
122  unsigned int virtual GetNBestmodels() = 0;
123  //! Return the indices of the best models
124  std::vector<int> virtual GetBestModelIndices() = 0;
125  //! Do one iteration of the GA
126  void virtual DoIteration(const int iterationnumber, const bool last);
127  //! Calculate the Probabilities
128  void virtual CalcProbabilities(const int iterationnumber,
129  gplib::rmat &LocalMisFit, GeneralPopulation &LocalPopulation) = 0;
130  GeneralGA(GeneralPropagation* const LocalPropagation,
131  GeneralPopulation* const LocalPopulation,
132  GeneralTranscribe* const LocalTranscribe,
133  const tObjectiveVector &IndObjective, const int nthreads = 1);
134  virtual ~GeneralGA();
135  };
136  /* @} */
137  }
138 #endif // CGENERALGA_H
The base class for the population of a genetic algorithm, implements storage and access functions...
ublas::vector< double > ttranscribed
Definition: gentypes.h:21
void PrintUniquePop(std::ostream &output)
Print each population member exactly once.
Definition: GeneralGA.h:114
GeneralPopulation *const Population
A pointer to an object holding the population.
Definition: GeneralGA.h:74
GeneralGA(GeneralPropagation *const LocalPropagation, GeneralPopulation *const LocalPopulation, GeneralTranscribe *const LocalTranscribe, const tObjectiveVector &IndObjective, const int nthreads=1)
Definition: GeneralGA.cpp:54
void PrintTranscribed(std::ostream &output)
Print transcribed values for each member to output.
Definition: GeneralGA.cpp:127
virtual void DoIteration(const int iterationnumber, const bool last)
Do one iteration of the GA.
Definition: GeneralGA.cpp:229
virtual std::vector< int > GetBestModelIndices()=0
Return the indices of the best models.
virtual void Elitism(const int iterationnumber)
default implementation does nothing, this can be overriden
Definition: GeneralGA.h:78
gplib::rmat OldMisFit
Definition: GeneralGA.h:62
const unsigned int nobjective
The number of objective functions we're using.
Definition: GeneralGA.h:64
The base class for genetic algorithm propagation methods.
gplib::rmat MisFit
Misfit first index objective function second index population member.
Definition: GeneralGA.h:68
std::vector< gplib::rvec > tparamvector
We provide some typedefs that are used in other parts as well.
Definition: GeneralGA.h:29
void PrintFitStat(std::ostream &output)
Print Fitness statistics for each objective function to output.
Definition: GeneralGA.cpp:100
void PrintBestMisfit(std::ostream &output)
Print misfit of the best population members.
Definition: GeneralGA.cpp:141
std::vector< std::vector< int > > tparamindv
Definition: GeneralGA.h:32
GeneralTranscribe *const Transcribe
A pointer to an object translating genes to model vectors.
Definition: GeneralGA.h:76
tObjectiveVector Objective
The vector holding the objective functions.
Definition: GeneralGA.h:70
General Transcribe base class for genetic algorithm parameter transcription.
void PrintAll(std::ostream &output)
Definition: UniquePop.cpp:36
void SetWeights(const std::vector< double > &LocalWeights)
Set the weights for each objective function.
Definition: GeneralGA.cpp:87
void SetElitist(const bool IsElitist)
Do we want elitist behaviour, effect depends on the GA implementetion in the derived class...
Definition: GeneralGA.h:98
virtual ~GeneralGA()
Definition: GeneralGA.cpp:72
This class stores a single unique copy of each population member that is added to it...
Definition: UniquePop.h:17
virtual void CalcProbabilities(const int iterationnumber, gplib::rmat &LocalMisFit, GeneralPopulation &LocalPopulation)=0
Calculate the Probabilities.
unsigned virtual int GetNBestmodels()=0
This has to be implemented in the derived class to return the number of best models.
General genetic algorithm class.
Definition: GeneralGA.h:25
GeneralPropagation *const Propagation
A pointer to an object dealing with propagation.
Definition: GeneralGA.h:72
const std::vector< double > & GetAvgFit()
Return the average fitness for each objective function.
Definition: GeneralGA.h:92
const std::vector< double > & GetWeights()
Return the weight for each objecive function.
Definition: GeneralGA.h:84
void SetParameterIndices(const tparamindv &Indices)
Configure which parts of the complete parameter vector are used in each objective function...
Definition: GeneralGA.cpp:95
std::vector< boost::shared_ptr< GeneralObjective > > tObjectiveVector
Definition: GeneralGA.h:31
gplib::rmat Transcribed
A matrix holding the numerical model vectors (transcribed from the genes) for each population member...
Definition: GeneralGA.h:66
void SetupParams(const ttranscribed &member, tparamvector &params)
Copy the appropriate parameter values from the transcribed vector for use with the objective function...
Definition: GeneralGA.cpp:77
void PrintMisfit(std::ostream &output)
Print misfit for each member to output.
Definition: GeneralGA.cpp:111