Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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 * *
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 * (see tmva/doc/LICENSE) *
23 **********************************************************************************/
24
25/*! \class TMVA::GeneticFitter
26\ingroup TMVA
27
28Fitter using a Genetic Algorithm.
29
30*/
31
32#include "TMVA/GeneticFitter.h"
33
34#include "TMVA/Configurable.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
45
46////////////////////////////////////////////////////////////////////////////////
47/// constructor
48
50 const TString& name,
51 const std::vector<TMVA::Interval*>& ranges,
52 const TString& theOption )
53: FitterBase( target, name, ranges, theOption )
54{
55 // default parameters settings for Genetic Algorithm
58}
59
60////////////////////////////////////////////////////////////////////////////////
61/// declare GA options
62
64{
65 DeclareOptionRef( fPopSize=300, "PopSize", "Population size for GA" );
66 DeclareOptionRef( fNsteps=40, "Steps", "Number of steps for convergence" );
67 DeclareOptionRef( fCycles=3, "Cycles", "Independent cycles of GA fitting" );
68 DeclareOptionRef( fSC_steps=10, "SC_steps", "Spread control, steps" );
69 DeclareOptionRef( fSC_rate=5, "SC_rate", "Spread control, rate: factor is changed depending on the rate" );
70 DeclareOptionRef( fSC_factor=0.95, "SC_factor", "Spread control, factor" );
71 DeclareOptionRef( fConvCrit=0.001, "ConvCrit", "Convergence criteria" );
72
74 "Saves the best n results from each generation. They are included in the last cycle" );
75 DeclareOptionRef( fSaveBestFromCycle=10, "SaveBestCycle",
76 "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" );
77
79 "Trim the population to PopSize after assessing the fitness of each individual" );
80 DeclareOptionRef( fSeed=100, "Seed", "Set seed of random generator (0 gives random seeds)" );
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// set GA configuration parameters
85
87 Int_t nsteps,
88 Int_t popSize,
89 Int_t SC_steps,
90 Int_t SC_rate,
91 Double_t SC_factor,
92 Double_t convCrit)
93{
94 fNsteps = nsteps;
95 fCycles = cycles;
96 fPopSize = popSize;
97 fSC_steps = SC_steps;
98 fSC_rate = SC_rate;
99 fSC_factor = SC_factor;
100 fConvCrit = convCrit;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Execute fitting
105
106Double_t TMVA::GeneticFitter::Run( std::vector<Double_t>& pars )
107{
108 Log() << kHEADER << "<GeneticFitter> Optimisation, please be patient "
109 << "... (inaccurate progress timing for GA)" << Endl;
110
111 GetFitterTarget().ProgressNotifier( "GA", "init" );
112
114 // gstore.SetMakeCopies(kTRUE); // commented out, because it reduces speed
115
116 // timing of GA
117 Timer timer( 100*(fCycles), GetName() );
118 if (fIPyMaxIter) *fIPyMaxIter = 100*(fCycles);
119 timer.DrawProgressBar( 0 );
120
121 Double_t progress = 0.;
122
123 for (Int_t cycle = 0; cycle < fCycles; cycle++) {
124 if (fIPyCurrentIter) *fIPyCurrentIter = 100*(cycle);
126 GetFitterTarget().ProgressNotifier( "GA", "cycle" );
127 // ---- perform series of fits to achieve best convergence
128
129 // "m_ga_spread" times the number of variables
131 // ga.SetMakeCopies(kTRUE); // commented out, because it reduces speed
132
133 if ( pars.size() == fRanges.size() ){
134 ga.GetGeneticPopulation().GiveHint( pars, 0.0 );
135 }
136 if (cycle==fCycles-1) {
137 GetFitterTarget().ProgressNotifier( "GA", "last" );
139 }
140
141 GetFitterTarget().ProgressNotifier( "GA", "iteration" );
142
143 ga.CalculateFitness();
145
146 Double_t n=0.;
147 do {
148 GetFitterTarget().ProgressNotifier( "GA", "iteration" );
149 ga.Init();
150 ga.CalculateFitness();
153
154 // monitor progrss
155 if (ga.fConvCounter > n) n = Double_t(ga.fConvCounter);
156 progress = 100*((Double_t)cycle) + 100*(n/Double_t(fNsteps));
157
158 timer.DrawProgressBar( (Int_t)progress );
159
160 // Copy the best genes of the generation
162 for ( Int_t i = 0; i<fSaveBestFromGeneration && i<fPopSize; i++ ) {
165 }
166 } while (!ga.HasConverged( fNsteps, fConvCrit ));
167
168 timer.DrawProgressBar( 100*(cycle+1) );
169
171 for ( Int_t i = 0; i<fSaveBestFromGeneration && i<fPopSize; i++ ) {
174 }
175 }
176
177 // get elapsed time
178 Log() << kINFO << "Elapsed time: " << timer.GetElapsedTime()
179 << " " << Endl;
180
181 Double_t fitness = gstore.CalculateFitness();
182 gstore.GetGeneticPopulation().Sort();
183 pars.swap( gstore.GetGeneticPopulation().GetGenes(0)->GetFactors() );
184
185 GetFitterTarget().ProgressNotifier( "GA", "stop" );
186 return fitness;
187}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
char name[80]
Definition TGX11.cxx:148
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
virtual void ParseOptions()
options parser
UInt_t * fIPyCurrentIter
Definition FitterBase.h:94
FitterBase(IFitterTarget &target, const TString &name, const std::vector< TMVA::Interval * > ranges, const TString &theOption)
constructor
IFitterTarget & GetFitterTarget() const
Definition FitterBase.h:64
bool * fExitFromTraining
Definition FitterBase.h:95
const char * GetName() const override
Returns name of object.
Definition FitterBase.h:70
MsgLogger & Log() const
Definition FitterBase.h:89
UInt_t * fIPyMaxIter
Definition FitterBase.h:94
Double_t Run()
estimator function interface for fitting
const std::vector< TMVA::Interval * > fRanges
Definition FitterBase.h:85
Base definition for genetic algorithm.
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...
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...
GeneticPopulation & GetGeneticPopulation()
void Init()
calls evolution, but if it is not the first time.
virtual Double_t CalculateFitness()
starts the evaluation of the fitness of all different individuals of the population.
Double_t fSC_factor
... with fSC_factor; if there were less improvements: divide by that factor; if there were exactly fS...
Double_t fConvCrit
improvements bigger than fConvCrit are counted as "improvement"
Int_t fSC_rate
... fSC_rate improvements, than multiply the sigma of the gaussian which defines how the random numbe...
Int_t fNsteps
convergence criteria: if no improvements > fConvCrit was achieved within the last fNsteps: cycle has ...
Int_t fCycles
number of (nearly) independent calculation cycles
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
Int_t fSC_steps
regulates how strong the mutations for the coordinates are: if within fSC_steps there were more than....
Int_t fSaveBestFromGeneration
store the best individuals from one generation (these are included as "hints" in the last cycle of GA...
Bool_t fTrim
take care, that the number of individuals is less fPopSize (trimming is done after the fitness of the...
UInt_t fSeed
Seed for the random generator (0 takes random seeds).
Int_t fSaveBestFromCycle
store the best individuals from one cycle (these are included as "hints" in the last cycle of GA calc...
Int_t fPopSize
number of individuals to start with
GeneticFitter(IFitterTarget &target, const TString &name, const std::vector< TMVA::Interval * > &ranges, const TString &theOption)
constructor
void DeclareOptions() override
declare GA options
std::vector< Double_t > & GetFactors()
Double_t GetFitness() const
void Sort()
sort the genepool according to the fitness of the individuals
void TrimPopulation()
trim the population to the predefined size
GeneticGenes * GetGenes(Int_t index)
gives back the "Genes" of the population with the given index.
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...
void AddPopulation(GeneticPopulation *strangers)
add another population (strangers) to the one of this GeneticPopulation
Interface for a fitter 'target'.
Timing information for training and evaluation of MVA methods.
Definition Timer.h:58
TString GetElapsedTime(Bool_t Scientific=kTRUE)
returns pretty string with elapsed time
Definition Timer.cxx:145
void DrawProgressBar(Int_t, const TString &comment="")
draws progress bar in color or B&W caution:
Definition Timer.cxx:201
Basic string class.
Definition TString.h:138
const Int_t n
Definition legend1.C:16
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148