Logo ROOT  
Reference Guide
GeneticFitter.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Peter Speckmayer
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : GeneticFitter *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation *
12  * *
13  * Authors (alphabetical): *
14  * Peter Speckmayer <speckmay@mail.cern.ch> - CERN, Switzerland *
15  * *
16  * Copyright (c) 2005: *
17  * CERN, Switzerland *
18  * MPI-K Heidelberg, Germany *
19  * *
20  * Redistribution and use in source and binary forms, with or without *
21  * modification, are permitted according to the terms listed in LICENSE *
22  * (http://tmva.sourceforge.net/LICENSE) *
23  **********************************************************************************/
24 
25 /*! \class TMVA::GeneticFitter
26 \ingroup TMVA
27 
28 Fitter using a Genetic Algorithm.
29 
30 */
31 
32 #include "TMVA/GeneticFitter.h"
33 
34 #include "TMVA/Configurable.h"
35 #include "TMVA/GeneticAlgorithm.h"
36 #include "TMVA/Interval.h"
37 #include "TMVA/FitterBase.h"
38 #include "TMVA/MsgLogger.h"
39 #include "TMVA/Timer.h"
40 #include "TMVA/Types.h"
41 
42 #include "Rtypes.h"
43 #include "TString.h"
44 
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 /// constructor
49 
51  const TString& name,
52  const std::vector<TMVA::Interval*>& ranges,
53  const TString& theOption )
54 : FitterBase( target, name, ranges, theOption )
55 {
56  // default parameters settings for Genetic Algorithm
58  ParseOptions();
59 }
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// declare GA options
63 
65 {
66  DeclareOptionRef( fPopSize=300, "PopSize", "Population size for GA" );
67  DeclareOptionRef( fNsteps=40, "Steps", "Number of steps for convergence" );
68  DeclareOptionRef( fCycles=3, "Cycles", "Independent cycles of GA fitting" );
69  DeclareOptionRef( fSC_steps=10, "SC_steps", "Spread control, steps" );
70  DeclareOptionRef( fSC_rate=5, "SC_rate", "Spread control, rate: factor is changed depending on the rate" );
71  DeclareOptionRef( fSC_factor=0.95, "SC_factor", "Spread control, factor" );
72  DeclareOptionRef( fConvCrit=0.001, "ConvCrit", "Convergence criteria" );
73 
74  DeclareOptionRef( fSaveBestFromGeneration=1, "SaveBestGen",
75  "Saves the best n results from each generation. They are included in the last cycle" );
76  DeclareOptionRef( fSaveBestFromCycle=10, "SaveBestCycle",
77  "Saves the best n results from each cycle. They are included in the last cycle. The value should be set to at least 1.0" );
78 
79  DeclareOptionRef( fTrim=kFALSE, "Trim",
80  "Trim the population to PopSize after assessing the fitness of each individual" );
81  DeclareOptionRef( fSeed=100, "Seed", "Set seed of random generator (0 gives random seeds)" );
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// set GA configuration parameters
86 
88  Int_t nsteps,
89  Int_t popSize,
90  Int_t SC_steps,
91  Int_t SC_rate,
92  Double_t SC_factor,
93  Double_t convCrit)
94 {
95  fNsteps = nsteps;
96  fCycles = cycles;
97  fPopSize = popSize;
98  fSC_steps = SC_steps;
99  fSC_rate = SC_rate;
100  fSC_factor = SC_factor;
101  fConvCrit = convCrit;
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// Execute fitting
106 
107 Double_t TMVA::GeneticFitter::Run( std::vector<Double_t>& pars )
108 {
109  Log() << kHEADER << "<GeneticFitter> Optimisation, please be patient "
110  << "... (inaccurate progress timing for GA)" << Endl;
111 
112  GetFitterTarget().ProgressNotifier( "GA", "init" );
113 
114  GeneticAlgorithm gstore( GetFitterTarget(), fPopSize, fRanges);
115  // gstore.SetMakeCopies(kTRUE); // commented out, because it reduces speed
116 
117  // timing of GA
118  Timer timer( 100*(fCycles), GetName() );
119  if (fIPyMaxIter) *fIPyMaxIter = 100*(fCycles);
120  timer.DrawProgressBar( 0 );
121 
122  Double_t progress = 0.;
123 
124  for (Int_t cycle = 0; cycle < fCycles; cycle++) {
125  if (fIPyCurrentIter) *fIPyCurrentIter = 100*(cycle);
126  if (fExitFromTraining && *fExitFromTraining) break;
127  GetFitterTarget().ProgressNotifier( "GA", "cycle" );
128  // ---- perform series of fits to achieve best convergence
129 
130  // "m_ga_spread" times the number of variables
131  GeneticAlgorithm ga( GetFitterTarget(), fPopSize, fRanges, fSeed );
132  // ga.SetMakeCopies(kTRUE); // commented out, because it reduces speed
133 
134  if ( pars.size() == fRanges.size() ){
135  ga.GetGeneticPopulation().GiveHint( pars, 0.0 );
136  }
137  if (cycle==fCycles-1) {
138  GetFitterTarget().ProgressNotifier( "GA", "last" );
140  }
141 
142  GetFitterTarget().ProgressNotifier( "GA", "iteration" );
143 
144  ga.CalculateFitness();
146 
147  Double_t n=0.;
148  do {
149  GetFitterTarget().ProgressNotifier( "GA", "iteration" );
150  ga.Init();
151  ga.CalculateFitness();
152  if ( fTrim ) ga.GetGeneticPopulation().TrimPopulation();
153  ga.SpreadControl( fSC_steps, fSC_rate, fSC_factor );
154 
155  // monitor progrss
156  if (ga.fConvCounter > n) n = Double_t(ga.fConvCounter);
157  progress = 100*((Double_t)cycle) + 100*(n/Double_t(fNsteps));
158 
159  timer.DrawProgressBar( (Int_t)progress );
160 
161  // Copy the best genes of the generation
162  ga.GetGeneticPopulation().Sort();
163  for ( Int_t i = 0; i<fSaveBestFromGeneration && i<fPopSize; i++ ) {
166  }
167  } while (!ga.HasConverged( fNsteps, fConvCrit ));
168 
169  timer.DrawProgressBar( 100*(cycle+1) );
170 
171  ga.GetGeneticPopulation().Sort();
172  for ( Int_t i = 0; i<fSaveBestFromGeneration && i<fPopSize; i++ ) {
175  }
176  }
177 
178  // get elapsed time
179  Log() << kINFO << "Elapsed time: " << timer.GetElapsedTime()
180  << " " << Endl;
181 
182  Double_t fitness = gstore.CalculateFitness();
183  gstore.GetGeneticPopulation().Sort();
184  pars.swap( gstore.GetGeneticPopulation().GetGenes(0)->GetFactors() );
185 
186  GetFitterTarget().ProgressNotifier( "GA", "stop" );
187  return fitness;
188 }
TMVA::GeneticPopulation::AddPopulation
void AddPopulation(GeneticPopulation *strangers)
add another population (strangers) to the one of this GeneticPopulation
Definition: GeneticPopulation.cxx:280
n
const Int_t n
Definition: legend1.C:16
TMVA::GeneticFitter::GeneticFitter
GeneticFitter(IFitterTarget &target, const TString &name, const std::vector< TMVA::Interval * > &ranges, const TString &theOption)
constructor
Definition: GeneticFitter.cxx:50
TMVA::GeneticFitter
Fitter using a Genetic Algorithm.
Definition: GeneticFitter.h:44
TMVA::GeneticAlgorithm::SpreadControl
virtual Double_t SpreadControl(Int_t steps, Int_t ofSteps, Double_t factor)
this function provides the ability to change the stepSize of a mutation according to the success of t...
Definition: GeneticAlgorithm.cxx:217
TMVA::GeneticPopulation::TrimPopulation
void TrimPopulation()
trim the population to the predefined size
Definition: GeneticPopulation.cxx:299
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TMath::Log
Double_t Log(Double_t x)
Definition: TMath.h:760
TMVA::GeneticAlgorithm
Base definition for genetic algorithm.
Definition: GeneticAlgorithm.h:50
TMVA::Timer::DrawProgressBar
void DrawProgressBar(Int_t, const TString &comment="")
draws progress bar in color or B&W caution:
Definition: Timer.cxx:202
TString
Basic string class.
Definition: TString.h:136
TMVA::GeneticAlgorithm::Init
void Init()
calls evolution, but if it is not the first time.
Definition: GeneticAlgorithm.cxx:101
TString.h
TMVA::GeneticFitter::SetParameters
void SetParameters(Int_t cycles, Int_t nsteps, Int_t popSize, Int_t SC_steps, Int_t SC_rate, Double_t SC_factor, Double_t convCrit)
set GA configuration parameters
Definition: GeneticFitter.cxx:87
TMVA::GeneticGenes::GetFitness
Double_t GetFitness() const
Definition: GeneticGenes.h:52
TMVA::Timer::GetElapsedTime
TString GetElapsedTime(Bool_t Scientific=kTRUE)
returns pretty string with elapsed time
Definition: Timer.cxx:146
TMVA::FitterBase::Run
Double_t Run()
estimator function interface for fitting
Definition: FitterBase.cxx:74
MsgLogger.h
TMVA::GeneticPopulation::GiveHint
void GiveHint(std::vector< Double_t > &hint, Double_t fitness=0)
add an individual (a set of variables) to the population if there is a set of variables which is know...
Definition: GeneticPopulation.cxx:310
Timer.h
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
GeneticFitter.h
TMVA::GeneticAlgorithm::HasConverged
virtual Bool_t HasConverged(Int_t steps=10, Double_t ratio=0.1)
gives back true if the last "steps" steps have lead to an improvement of the "fitness" of the "indivi...
Definition: GeneticAlgorithm.cxx:260
Types.h
Configurable.h
TMVA::GeneticGenes::GetFactors
std::vector< Double_t > & GetFactors()
Definition: GeneticGenes.h:49
TMVA::Endl
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
TMVA::Timer
Timing information for training and evaluation of MVA methods.
Definition: Timer.h:58
Double_t
double Double_t
Definition: RtypesCore.h:59
TMVA::GeneticAlgorithm::CalculateFitness
virtual Double_t CalculateFitness()
starts the evaluation of the fitness of all different individuals of the population.
Definition: GeneticAlgorithm.cxx:135
TMVA::GeneticPopulation::Sort
void Sort()
sort the genepool according to the fitness of the individuals
Definition: GeneticPopulation.cxx:321
TMVA::GeneticAlgorithm::GetGeneticPopulation
GeneticPopulation & GetGeneticPopulation()
Definition: GeneticAlgorithm.h:67
TMVA::GeneticAlgorithm::fConvCounter
Int_t fConvCounter
Definition: GeneticAlgorithm.h:75
name
char name[80]
Definition: TGX11.cxx:110
GeneticAlgorithm.h
TMVA::IFitterTarget
Interface for a fitter 'target'.
Definition: IFitterTarget.h:44
TMVA::GeneticPopulation::GetGenes
GeneticGenes * GetGenes(Int_t index)
gives back the "Genes" of the population with the given index.
Definition: GeneticPopulation.cxx:194
Rtypes.h
TMVA::Configurable::ParseOptions
virtual void ParseOptions()
options parser
Definition: Configurable.cxx:124
FitterBase.h
TMVA::GeneticFitter::DeclareOptions
void DeclareOptions()
declare GA options
Definition: GeneticFitter.cxx:64
Interval.h
int
TMVA::FitterBase
Base class for TMVA fitters.
Definition: FitterBase.h:51