GeneticAlgoithm
Implementation of the genetic algorithm
GeneticAlgorithm.cxx
1 #include "GeneticAlgorithm.h"
2 #include "IModel.h"
3 #include "IFigureOfMerit.h"
4 #include "IPopulation.h"
5 
7 {
8  m_generationsMax = 10000;
9  m_populationSize = 100;
10 }
11 
13 {
14 }
15 
16 /**
17  * @param population Population of models to optimize.
18  * @return Best fitted model after optimization.
19  */
21 {
22 
23  initialize(population);
24 
25  while(nextGeneration());
26 
27  return population->getBestFitted();
28 }
29 
30 /**
31  * This function is provided so that the user have the option to control the optimization loop
32  * and possibly execute some code before/after each iteration. This can be useful for example
33  * to implement tests that need to monitor the progress of the optimization.
34  *
35  * @param population Population of models to optimize.
36  */
38 
39  population->initialize(m_populationSize);
40  population->score();
41 
43  m_population = population;
44 }
45 
46 /**
47  * This function is provided so that the user have the option to control the optimization loop
48  * and possibly execute some code before/after each iteration. This can be useful for example
49  * to implement tests that need to monitor the progress of the optimization.
50  *
51  * @return `true` if more generations are needed, `false` if optimal solution has been reached.
52  */
54 
56  return false;
57  }
58 
60  return false;
61  }
62 
64 
68 
69  return true;
70 }
71 
72 /**
73  * @return Number of the current generation.
74  */
76 {
77  return m_currentGeneration;
78 }
79 
80 /**
81  * @param Desired maximum number of iterations.
82  */
83 void GeneticAlgorithm::setNGenerationsMax(int generationsMax)
84 {
85  m_generationsMax = generationsMax;
86 }
87 
88 
89 /**
90  * @param Desired population size.
91  */
92 void GeneticAlgorithm::setPopulationSize(int populationSize)
93 {
94  m_populationSize = populationSize;
95 }
virtual bool accept(IModel *model) const
IModel * getBestFitted(int rank=0)
Abstract class describing a population of models.
Definition: IPopulation.h:27
void initialize(int n)
Definition: IPopulation.cxx:28
int m_generationsMax
Stores the maximum number of generations.
int m_populationSize
Stores the desired population size.
IFigureOfMerit * getFigureOfMerit()
void initialize(IPopulation *population)
void mutate()
Definition: IPopulation.cxx:56
void setNGenerationsMax(int generationsMax)
void score()
Definition: IPopulation.cxx:93
void crossOver()
Definition: IPopulation.cxx:34
IModel * optimize(IPopulation *population)
IPopulation * m_population
Stores a pointer to the population being optimized.
void setPopulationSize(int populationSize)
int m_currentGeneration
Stores the number of the current generation.
Abstract class describing the interface for a model.
Definition: IModel.h:13