Logo ROOT   6.10/09
Reference Guide
TMVAGAexample.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tmva
3 /// \notebook -nodraw
4 /// This exectutable gives an example of a very simple use of the genetic algorithm
5 /// of TMVA
6 /// - Project : TMVA - a Root-integrated toolkit for multivariate data analysis
7 /// - Package : TMVA
8 /// - Exectuable: TMVAGAexample
9 ///
10 /// \macro_output
11 /// \macro_code
12 /// \author Andreas Hoecker
13 
14 #include <iostream> // Stream declarations
15 #include <vector>
16 
17 #include "TMVA/GeneticAlgorithm.h"
18 #include "TMVA/GeneticFitter.h"
19 #include "TMVA/IFitterTarget.h"
20 
21 using namespace std;
22 
23 using namespace TMVA;
24 
25 class MyFitness : public IFitterTarget {
26  public:
27  MyFitness() : IFitterTarget() {
28  }
29 
30  // the fitness-function goes here
31  // the factors are optimized such that the return-value of this function is minimized
32  // take care!! the fitness-function must never fail, .. means: you have to prevent
33  // the function from reaching undefined values (such as x=0 for 1/x or so)
34  //
35  // HINT: to use INTEGER variables, it is sufficient to cast the "factor" in the fitness-function
36  // to (int). In this case the variable-range has to be chosen +1 ( to get 0..5, take Interval(0,6) )
37  // since the introduction of "Interval" ranges can be defined with a third parameter
38  // which gives the number of bins within the interval. With that technique discrete values
39  // can be achieved easier. The random selection out of this discrete numbers is completly uniform.
40  //
41  Double_t EstimatorFunction( std::vector<Double_t> & factors ){
42  //return (10.- (int)factors.at(0) *factors.at(1) + (int)factors.at(2));
43  return (10.- factors.at(0) *factors.at(1) + factors.at(2));
44 
45  //return 100.- (10 + factors.at(1)) *factors.at(2)* TMath::Abs( TMath::Sin(factors.at(0)) );
46  }
47 };
48 
49 
50 class MyGA2nd : public GeneticAlgorithm {
51  public:
52  MyGA2nd( IFitterTarget& target, Int_t size, vector<Interval*>& ranges ) : GeneticAlgorithm(target,
53  size, ranges ){
54  }
55 
56 
57  // this method has to be activated if one wants to change the behaviour of the evolution
58  // works only with the head version
59  //void Evolution(){
60  // fSexual = true;
61  // if (fSexual) {
62  // fPopulation.MakeCopies( 5 );
63  // fPopulation.MakeChildren();
64  // fPopulation.NextGeneration();
65 
66  // fPopulation.Mutate( 10, 3, kTRUE, fSpread, fMirror );
67  // fPopulation.Mutate( 40, fPopulation.GetPopulationSize()*3/4 );
68  // } else {
69  // fPopulation.MakeCopies( 3 );
70  // fPopulation.MakeMutants(100,true, 0.1, true);
71  // fPopulation.NextGeneration();
72  // }
73  // }
74 };
75 
76 
77 
78 void TMVAGAexample() {
79 
80  std::cout << "Start Test TMVAGAexample" << std::endl
81  << "========================" << std::endl
82  << "\nEXAMPLE" << std::endl;
83  // define all the parameters by their minimum and maximum value
84  // in this example 3 parameters are defined.
85  vector<Interval*> ranges;
86  ranges.push_back( new Interval(0,15,30) );
87  ranges.push_back( new Interval(0,13) );
88  ranges.push_back( new Interval(0,5,3) );
89 
90  for( std::vector<Interval*>::iterator it = ranges.begin(); it != ranges.end(); it++ ){
91  std::cout << " range: " << (*it)->GetMin() << " " << (*it)->GetMax() << std::endl;
92  }
93 
94  IFitterTarget* myFitness = new MyFitness();
95 
96  // prepare the genetic algorithm with an initial population size of 20
97  // mind: big population sizes will help in searching the domain space of the solution
98  // but you have to weight this out to the number of generations
99  // the extreme case of 1 generation and populationsize n is equal to
100  // a Monte Carlo calculation with n tries
101 
102  MyGA2nd mg( *myFitness, 100, ranges );
103  // mg.SetParameters( 4, 30, 200, 10,5, 0.95, 0.001 );
104 
105  #define CONVSTEPS 20
106  #define CONVCRIT 0.0001
107  #define SCSTEPS 10
108  #define SCRATE 5
109  #define SCFACTOR 0.95
110 
111  do {
112  // prepares the new generation and does evolution
113  mg.Init();
114 
115  // assess the quality of the individuals
116  mg.CalculateFitness();
117 
118  mg.GetGeneticPopulation().Print(0);
119  std::cout << "---" << std::endl;
120 
121  // reduce the population size to the initially defined one
122  mg.GetGeneticPopulation().TrimPopulation();
123 
124  // tricky thing: control the speed of how fast the "solution space" is searched through
125  // this function basically influences the sigma of a gaussian around the actual value
126  // of the parameter where the new value will be randomly thrown.
127  // when the number of improvements within the last SCSTEPS
128  // A) smaller than SCRATE: divide the preset sigma by SCFACTOR
129  // B) equal to SCRATE: do nothing
130  // C) greater than SCRATE: multiply the preset sigma by SCFACTOR
131  // if you don't know what to do, leave it unchanged or even delete this function call
132  mg.SpreadControl( SCSTEPS, SCRATE, SCFACTOR );
133 
134  } while (!mg.HasConverged( CONVSTEPS, CONVCRIT )); // converged if: fitness-improvement < CONVCRIT within the last CONVSTEPS loops
135 
136  GeneticGenes* genes = mg.GetGeneticPopulation().GetGenes( 0 );
137  std::vector<Double_t> gvec;
138  gvec = genes->GetFactors();
139  int n = 0;
140  for( std::vector<Double_t>::iterator it = gvec.begin(); it<gvec.end(); it++ ){
141  std::cout << "FACTOR " << n << " : " << (*it) << std::endl;
142  n++;
143  }
144 }
145 
146 
147 int main( int argc, char** argv )
148 {
149  TMVAGAexample();
150 }
int Int_t
Definition: RtypesCore.h:41
STL namespace.
Cut optimisation interface class for genetic algorithm.
Definition: GeneticGenes.h:41
The TMVA::Interval Class.
Definition: Interval.h:61
Base definition for genetic algorithm.
double Double_t
Definition: RtypesCore.h:55
std::vector< Double_t > & GetFactors()
Definition: GeneticGenes.h:49
Abstract ClassifierFactory template that handles arbitrary types.
Interface for a fitter &#39;target&#39;.
Definition: IFitterTarget.h:44
const Int_t n
Definition: legend1.C:16
int main(int argc, char **argv)