GeneticAlgoithm
Implementation of the genetic algorithm
ParametricModelPopulation.cxx
1 #include "ParametricModelPopulation.h"
2 
3 #include "ParametricModel.h"
4 
7 {
8  m_formula = 0;
9  m_mutationSize = 0.1;
10 }
11 
13 {
14 }
15 
16 /**
17  * @param formula Formula to be used for this population.
18  */
20 {
21  m_formula = formula;
22 }
23 
24 /**
25  * @param relativeSize The relative size (sigma) of the gaussian noise applied during mutation.
26  */
28 {
29  m_mutationSize = relativeSize;
30 }
31 
32 /**
33  * Parameters for the individual models are randomly initialized following uniform
34  * distribution in the allowed range as defined in the population's formula.
35  *
36  * @param The desired size of the population.
37  */
39 {
40  clear();
41  for(int i=0; i<n; i++) {
42  ParametricModel *model = new ParametricModel();
43  model->setFormula(m_formula);
44  TF1 *formula = model->getFormula();
45  for(int p=0; p<formula->GetNpar(); p++) {
46  double pmin, pmax;
47  formula->GetParLimits(p, pmin, pmax);
48  if(pmin < pmax) {
49  double par = m_random->Uniform(pmin, pmax);
50  formula->SetParameter(p, par);
51  }
52  }
53  m_individuals.push_back(model);
54  }
55 }
56 
57 /**
58  * Cross-over is implemented such that each parameter is passed from either parents chosen at random.
59  *
60  * @param parents List of parents to be crossed-over.
61  */
62 void ParametricModelPopulation::doCrossOver(const std::vector<std::vector<IModel*> > &parents)
63 {
64 
65  std::vector<std::vector<double> > offspringGenes;
66  for(int i=0; i<size(); i++) {
67  std::vector<double> offspring;
68  if(parents[i].size()==1) {
69  ParametricModel *parent = dynamic_cast<ParametricModel*>(parents[i][0]);
70  if(!parent) {
71  throw std::runtime_error("Given models are not parametric models");
72  }
73  TF1 *formula = parent->getFormula();
74  for(int p=0; p<formula->GetNpar(); p++) {
75  offspring.push_back(formula->GetParameter(p));
76  }
77  }else if(parents[i].size() == 2) {
78  ParametricModel *parent1 = dynamic_cast<ParametricModel*>(parents[i][0]);
79  ParametricModel *parent2 = dynamic_cast<ParametricModel*>(parents[i][1]);
80  if(!parent1 || !parent2) {
81  throw std::runtime_error("Given models are not parametric models");
82  }
83  TF1 *formula1 = parent1->getFormula();
84  TF1 *formula2 = parent2->getFormula();
85  for(int p=0; p<formula1->GetNpar(); p++) {
86  if(m_random->Integer(2)) {
87  offspring.push_back(formula1->GetParameter(p));
88  }else{
89  offspring.push_back(formula2->GetParameter(p));
90  }
91  }
92  }
93  offspringGenes.push_back(offspring);
94  }
95 
96  for(int i=0; i<size(); i++) {
98  for(unsigned int p=0; p<offspringGenes[i].size(); p++) {
99  model->getFormula()->SetParameter(p, offspringGenes[i][p]);
100  }
101  }
102 }
103 
104 /**
105  * Mutation is implemented such that a random parameter is chosen and then modified by adding a gaussian noise component.
106  * The size of the gaussian noise is controlled via setMutationSize().
107  *
108  * @param imodel Model to be mutated.
109  */
111 {
112 
113  ParametricModel *model = dynamic_cast<ParametricModel*>(imodel);
114  if(!model) {
115  throw std::runtime_error("Given models are not parametric models");
116  }
117 
118  TF1 *formula = model->getFormula();
119  int p = m_random->Integer(formula->GetNpar());
120  double pmin, pmax;
121  formula->GetParLimits(p, pmin, pmax);
122  if(pmin < pmax) {
123  double par = formula->GetParameter(p);
124  par += m_random->Gaus(0, par==0?m_mutationSize:par*m_mutationSize);
125  formula->SetParameter(p, par);
126  }
127 }
128 
TRandom3 * m_random
Stores a random number generator.
Definition: IPopulation.h:100
void setFormula(TF1 *formula)
void setMutationSize(double relativeSize)
Abstract class describing a population of models.
Definition: IPopulation.h:27
double m_mutationSize
Stores the relative size (sigma) of the gaussian noise applied during mutation.
virtual void doMutate(IModel *model)
TF1 * m_formula
Stores the formula for this population.
virtual void doCrossOver(const std::vector< std::vector< IModel *> > &parents)
std::vector< IModel * > m_individuals
Stores the individuals of this population.
Definition: IPopulation.h:98
Class representing a model defined by a parametric function.
Abstract class describing the interface for a model.
Definition: IModel.h:13