GPLIB++
GeneralPopulation.h
Go to the documentation of this file.
1 #ifndef CGENERALPOPULATION_H
2 #define CGENERALPOPULATION_H
3 #include "gentypes.h"
4 #include "GeneralRNG.h"
5 #include "FatalException.h"
6 
7 namespace gplib
8  {
9  /** \addtogroup gainv Genetic algorithm optimization */
10  /* @{ */
11 
12  //! The base class for the population of a genetic algorithm, implements storage and access functions
14  {
15  private:
16  //! The population of the last iteration for elitism
17  tpopulation OldPopulation;
18  //! Did we store the last population
19  bool OldStored;
20  //! The selection probabilities for each population member
21  tprobabilityv Probabilities;
22  //! The crowding distance for each population member
23  tcrowddistv CrowdingDistances;
24  protected:
25  //! Change the population size
26  void ResizePop(const int popsize, const int genesize);
27  //! The population of the current iteration
29  public:
30  virtual void InitPop()
31  {
32  }
33  ;
35  {
36  if (OldStored)
37  return OldPopulation;
38  else
39  throw FatalException("Old Population has not been stored");
40  }
41  ;
43  {
44  OldPopulation = Population;
45  OldStored = true;
46  }
47  ;
48  void PrintPopulation(std::ostream &output) const;
49  void ReadPopulation(std::istream &input);
50  void PrintProbabilities(std::ostream &output) const;
51  void PrintDistances(std::ostream &output) const;
52  const tpopulation& GetPopulation() const
53  {
54  return Population;
55  }
56  ;
58  {
59  return Probabilities;
60  }
61  ;
63  {
64  return CrowdingDistances;
65  }
66  ;
67  void SetPopulation(const tpopulation &LocalPop)
68  {
69  OldPopulation = Population;
70  Population = LocalPop;
71  OldStored = true;
72  }
73  ;
74  void SetProbabilities(const tprobabilityv &LocalProb)
75  {
76  Probabilities = LocalProb;
77  }
78  ;
79  void SetCrowdingDistances(const tcrowddistv &LocalDist)
80  {
81  CrowdingDistances = LocalDist;
82  }
83  ;
84  int GetPopsize() const
85  {
86  return Population.size1();
87  }
88  ;
89  int GetGenesize() const
90  {
91  return Population.size2();
92  }
93  ;
94  GeneralPopulation(const int popsize, const int genesize);
96  //! Merge two populations in a new population object
97  GeneralPopulation(const tpopulation &FirstHalf,
98  const tpopulation &SecondHalf);
99  virtual GeneralPopulation& operator=(const GeneralPopulation &source);
100  virtual ~GeneralPopulation();
101  };
102  /* @} */
103  }
104 #endif // CGENERALPOPULATION_H
The base class for the population of a genetic algorithm, implements storage and access functions...
tpopulation Population
The population of the current iteration.
ublas::vector< double > tprobabilityv
Definition: gentypes.h:17
void PrintProbabilities(std::ostream &output) const
void SetProbabilities(const tprobabilityv &LocalProb)
GeneralPopulation(const int popsize, const int genesize)
void PrintDistances(std::ostream &output) const
const tcrowddistv & GetCrowdingDistances() const
void ReadPopulation(std::istream &input)
const tpopulation & GetOldPopulation() const
void SetPopulation(const tpopulation &LocalPop)
virtual GeneralPopulation & operator=(const GeneralPopulation &source)
void PrintPopulation(std::ostream &output) const
gplib::rmat tpopulation
Definition: gentypes.h:25
const tpopulation & GetPopulation() const
const tprobabilityv & GetProbabilities() const
void ResizePop(const int popsize, const int genesize)
Change the population size.
void SetCrowdingDistances(const tcrowddistv &LocalDist)
The basic exception class for all errors that arise in gplib.
ublas::vector< double > tcrowddistv
Definition: gentypes.h:20