GeneticAlgoithm
Implementation of the genetic algorithm
IPopulation.h
1 #ifndef IPOPULATION_H
2 #define IPOPULATION_H
3 
4 #include <vector>
5 #include "TRandom3.h"
6 
7 class IModel;
8 class IFigureOfMerit;
9 
10 /**
11  * @brief Abstract class describing a population of models.
12  *
13  * This class should implement the following functionalities:
14  * - Initialize the population: usually this use a random number generator.
15  * The actual implementation is left to the derived classes.
16  * - Selection: decide which members of the population should be cross-overed.
17  * The default behavior is to select parents with a probability that is linear with the rank.
18  * - Cross-over: implements the logic based on which a child is constructed from its parents.
19  * - Mutation: implements the logic based on which a child is altered through random mutations.
20  *
21  * Derive from this class by implementing at least these three methods:
22  * - doInitialize(): performs the initialization of the population
23  * - doCrossOver(): performs the cross-over.
24  * - doMutate(): performs the mutation of a model.
25  * Additionally, one might override the selectParents() method to change its default behavior.
26  */
27 class IPopulation {
28 
29 public:
30 
31  /** Default Constructor */
32  IPopulation();
33 
34  /** Destructor */
35  virtual ~IPopulation();
36 
37  /** Initialize a population of a given size. */
38  void initialize(int n);
39 
40  /** Performs population cross-over. */
41  void crossOver();
42 
43  /** Performs individual mutations. */
44  void mutate();
45 
46  /** Compute the scores for the members of the population. */
47  void score();
48 
49  /** Sets the random seed for the random number generator. */
50  void setRandomSeed(int seed);
51 
52  /** Sets the mutation rate. */
53  void setMutateRate(double rate);
54 
55  /** Sets the figure of merit to be used to calculate scores and perform the ranking. */
57 
58  /** Returns the figure of merit that is used to calculate scores and perform the ranking. */
60 
61  /** Returns the method at a given rank. */
62  IModel *getBestFitted(int rank=0);
63 
64  /** Returns the size of the population. */
65  int size();
66 
67  /** Returns the mean score for the population. */
68  double getScoreMean();
69 
70  /** Returns the RMS of the scores for the population. */
71  double getScoreRMS();
72 
73  /** Resets the population. */
74  void clear();
75 
76 protected:
77 
78  /** Should implement the actual initialization. */
79  virtual void doInitialize(int n)=0;
80 
81  /** Should implement the cross-over. */
82  virtual void doCrossOver(const std::vector<std::vector<IModel*> > &parents)=0;
83 
84  /** Should implement the mutation of a model. */
85  virtual void doMutate(IModel *model)=0;
86 
87  /** Selects parents to be crossed-over */
88  virtual void selectParents(int &p1, int &p2);
89 
90  /** Performs the ranking from the best to the least fitted. */
91  void sort();
92 
93  /** Makes sure an IFigureOfMerit object is assigned to this population. */
94  void checkFigureOfMerit();
95 
96  double m_mutateRate; //!< Stores the mutate rate.
97  bool m_sorted; //!< Stores whether the ranking is valid or needs to be redone.
98  std::vector<IModel*> m_individuals; //!< Stores the individuals of this population.
99  IFigureOfMerit *m_fom; //!< Stores the figure of merit to be used to calculate scores and perform the ranking.
100  TRandom3 *m_random; //!< Stores a random number generator.
101  double m_scoreMean; //!< Stores the mean score for the population.
102  double m_scoreRMS; //!< Stores the score RMS for the population.
103  std::vector<std::vector<IModel*> > m_parents; //!< Stores the list of parents about to be crossed-over.
104 };
105 
106 #endif
TRandom3 * m_random
Stores a random number generator.
Definition: IPopulation.h:100
double m_scoreMean
Stores the mean score for the population.
Definition: IPopulation.h:101
IModel * getBestFitted(int rank=0)
Abstract class describing a population of models.
Definition: IPopulation.h:27
virtual void selectParents(int &p1, int &p2)
void initialize(int n)
Definition: IPopulation.cxx:28
void setMutateRate(double rate)
Definition: IPopulation.cxx:79
void setFigureOfMerit(IFigureOfMerit *fom)
double getScoreMean()
IFigureOfMerit * getFigureOfMerit()
void mutate()
Definition: IPopulation.cxx:56
bool m_sorted
Stores whether the ranking is valid or needs to be redone.
Definition: IPopulation.h:97
double m_mutateRate
Stores the mutate rate.
Definition: IPopulation.h:96
virtual ~IPopulation()
Definition: IPopulation.cxx:20
std::vector< std::vector< IModel * > > m_parents
Stores the list of parents about to be crossed-over.
Definition: IPopulation.h:103
Abstract class representing a figure of merit.
void checkFigureOfMerit()
void score()
Definition: IPopulation.cxx:93
void crossOver()
Definition: IPopulation.cxx:34
virtual void doMutate(IModel *model)=0
IFigureOfMerit * m_fom
Stores the figure of merit to be used to calculate scores and perform the ranking.
Definition: IPopulation.h:99
void setRandomSeed(int seed)
Definition: IPopulation.cxx:70
std::vector< IModel * > m_individuals
Stores the individuals of this population.
Definition: IPopulation.h:98
virtual void doInitialize(int n)=0
virtual void doCrossOver(const std::vector< std::vector< IModel *> > &parents)=0
Abstract class describing the interface for a model.
Definition: IModel.h:13
double getScoreRMS()
double m_scoreRMS
Stores the score RMS for the population.
Definition: IPopulation.h:102