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