Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMVAGAexample.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tmva
3/// \notebook -nodraw
4/// This executable 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/// - Executable: TMVAGAexample
9///
10/// \macro_output
11/// \macro_code
12/// \author Andreas Hoecker
13
14#include <iostream> // Stream declarations
15#include <vector>
16
18#include "TMVA/GeneticFitter.h"
19#include "TMVA/IFitterTarget.h"
20
21using namespace std;
22
23using namespace TMVA;
24
25class 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 completely 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
50class 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
78void 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
147int main( int argc, char** argv )
148{
149 TMVAGAexample();
150}
int main()
Definition Prototype.cxx:12
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
Base definition for genetic algorithm.
Cut optimisation interface class for genetic algorithm.
std::vector< Double_t > & GetFactors()
Interface for a fitter 'target'.
The TMVA::Interval Class.
Definition Interval.h:61
const Int_t n
Definition legend1.C:16
create variable transformations