Logo ROOT   6.12/07
Reference Guide
TMVAGAexample2.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 
15 #include <iostream> // Stream declarations
16 #include <vector>
17 
18 #include "TMVA/GeneticAlgorithm.h"
19 #include "TMVA/GeneticFitter.h"
20 #include "TMVA/IFitterTarget.h"
21 
22 using namespace std;
23 
24 namespace TMVA {
25 
26 
27 class MyFitness : public IFitterTarget {
28  public:
29  MyFitness() : IFitterTarget() {
30  }
31 
32  // the fitness-function goes here
33  // the factors are optimized such that the return-value of this function is minimized
34  // take care!! the fitness-function must never fail, .. means: you have to prevent
35  // the function from reaching undefined values (such as x=0 for 1/x or so)
36  //
37  // HINT: to use INTEGER variables, it is sufficient to cast the "factor" in the fitness-function
38  // to (int). In this case the variable-range has to be chosen +1 ( to get 0..5, take Interval(0,6) )
39  // since the introduction of "Interval" ranges can be defined with a third parameter
40  // which gives the number of bins within the interval. With that technique discrete values
41  // can be achieved easier. The random selection out of this discrete numbers is completly uniform.
42  //
43  Double_t EstimatorFunction( std::vector<Double_t> & factors ){
44  //return (10.- (int)factors.at(0) *factors.at(1) + (int)factors.at(2));
45  return (10.- factors.at(0) *factors.at(1) + factors.at(2));
46 
47  //return 100.- (10 + factors.at(1)) *factors.at(2)* TMath::Abs( TMath::Sin(factors.at(0)) );
48  }
49 };
50 
51 
52 
53 
54 
55 
56 
57 
58 void exampleGA(){
59  std::cout << "\nEXAMPLE" << std::endl;
60  // define all the parameters by their minimum and maximum value
61  // in this example 3 parameters are defined.
62  vector<Interval*> ranges;
63  ranges.push_back( new Interval(0,15,30) );
64  ranges.push_back( new Interval(0,13) );
65  ranges.push_back( new Interval(0,5,3) );
66 
67  for( std::vector<Interval*>::iterator it = ranges.begin(); it != ranges.end(); it++ ){
68  std::cout << " range: " << (*it)->GetMin() << " " << (*it)->GetMax() << std::endl;
69  }
70 
71  IFitterTarget* myFitness = new MyFitness();
72 
73  // prepare the genetic algorithm with an initial population size of 20
74  // mind: big population sizes will help in searching the domain space of the solution
75  // but you have to weight this out to the number of generations
76  // the extreme case of 1 generation and populationsize n is equal to
77  // a Monte Carlo calculation with n tries
78 
79  const TString name( "exampleGA" );
80  const TString opts( "PopSize=100:Steps=30" );
81 
82  GeneticFitter mg( *myFitness, name, ranges, opts);
83  // mg.SetParameters( 4, 30, 200, 10,5, 0.95, 0.001 );
84 
85  std::vector<Double_t> result;
86  Double_t estimator = mg.Run(result);
87 
88  int n = 0;
89  for( std::vector<Double_t>::iterator it = result.begin(); it<result.end(); it++ ){
90  std::cout << "FACTOR " << n << " : " << (*it) << std::endl;
91  n++;
92  }
93 
94 }
95 
96 
97 
98 } // namespace TMVA
99 
100 void TMVAGAexample2() {
101  cout << "Start Test TMVAGAexample" << endl
102  << "========================" << endl
103  << endl;
104 
105  TMVA::exampleGA();
106 
107 }
108 
109 
110 int main( int argc, char** argv )
111 {
112  TMVAGAexample2();
113  return 0;
114 }
STL namespace.
static constexpr double mg
int main(int argc, char **argv)
double Double_t
Definition: RtypesCore.h:55
Abstract ClassifierFactory template that handles arbitrary types.
const Int_t n
Definition: legend1.C:16
char name[80]
Definition: TGX11.cxx:109