ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 //_______________________________________________________________________
26 //
27 // Fitter using a Genetic Algorithm
28 //_______________________________________________________________________
29 
30 #include <iostream>
31 
32 #include "TMVA/GeneticFitter.h"
33 #include "TMVA/GeneticAlgorithm.h"
34 #include "TMVA/Interval.h"
35 #include "TMVA/MsgLogger.h"
36 #include "TMVA/Timer.h"
37 #include "TMVA/Types.h"
38 
39 #include "Rtypes.h"
40 #include "TString.h"
41 
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// constructor
46 
47 TMVA::GeneticFitter::GeneticFitter( IFitterTarget& target,
48  const TString& name,
49  const std::vector<TMVA::Interval*>& ranges,
50  const TString& theOption )
51  : FitterBase( target, name, ranges, theOption )
52 {
53  // default parameters settings for Genetic Algorithm
54  DeclareOptions();
55  ParseOptions();
56 }
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// declare GA options
60 
62 {
63  DeclareOptionRef( fPopSize=300, "PopSize", "Population size for GA" );
64  DeclareOptionRef( fNsteps=40, "Steps", "Number of steps for convergence" );
65  DeclareOptionRef( fCycles=3, "Cycles", "Independent cycles of GA fitting" );
66  DeclareOptionRef( fSC_steps=10, "SC_steps", "Spread control, steps" );
67  DeclareOptionRef( fSC_rate=5, "SC_rate", "Spread control, rate: factor is changed depending on the rate" );
68  DeclareOptionRef( fSC_factor=0.95, "SC_factor", "Spread control, factor" );
69  DeclareOptionRef( fConvCrit=0.001, "ConvCrit", "Convergence criteria" );
70 
71  DeclareOptionRef( fSaveBestFromGeneration=1, "SaveBestGen",
72  "Saves the best n results from each generation. They are included in the last cycle" );
73  DeclareOptionRef( fSaveBestFromCycle=10, "SaveBestCycle",
74  "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" );
75 
76  DeclareOptionRef( fTrim=kFALSE, "Trim",
77  "Trim the population to PopSize after assessing the fitness of each individual" );
78  DeclareOptionRef( fSeed=100, "Seed", "Set seed of random generator (0 gives random seeds)" );
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// set GA configuration parameters
83 
85  Int_t nsteps,
86  Int_t popSize,
87  Int_t SC_steps,
88  Int_t SC_rate,
89  Double_t SC_factor,
90  Double_t convCrit)
91 {
92  fNsteps = nsteps;
93  fCycles = cycles;
94  fPopSize = popSize;
95  fSC_steps = SC_steps;
96  fSC_rate = SC_rate;
97  fSC_factor = SC_factor;
98  fConvCrit = convCrit;
99 }
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 /// Execute fitting
103 
104 Double_t TMVA::GeneticFitter::Run( std::vector<Double_t>& pars )
105 {
106  Log() << kINFO << "<GeneticFitter> Optimisation, please be patient "
107  << "... (inaccurate progress timing for GA)" << Endl;
108 
109  GetFitterTarget().ProgressNotifier( "GA", "init" );
110 
111  GeneticAlgorithm gstore( GetFitterTarget(), fPopSize, fRanges);
112  // gstore.SetMakeCopies(kTRUE); // commented out, because it reduces speed
113 
114  // timing of GA
115  Timer timer( 100*(fCycles), GetName() );
116  timer.DrawProgressBar( 0 );
117 
118  Double_t progress = 0.;
119 
120  for (Int_t cycle = 0; cycle < fCycles; cycle++) {
121  GetFitterTarget().ProgressNotifier( "GA", "cycle" );
122  // ---- perform series of fits to achieve best convergence
123 
124  // "m_ga_spread" times the number of variables
125  GeneticAlgorithm ga( GetFitterTarget(), fPopSize, fRanges, fSeed );
126  // ga.SetMakeCopies(kTRUE); // commented out, because it reduces speed
127 
128  if ( pars.size() == fRanges.size() ){
129  ga.GetGeneticPopulation().GiveHint( pars, 0.0 );
130  }
131  if (cycle==fCycles-1) {
132  GetFitterTarget().ProgressNotifier( "GA", "last" );
134  }
135 
136  GetFitterTarget().ProgressNotifier( "GA", "iteration" );
137 
138  ga.CalculateFitness();
140 
141  Double_t n=0.;
142  do {
143  GetFitterTarget().ProgressNotifier( "GA", "iteration" );
144  ga.Init();
145  ga.CalculateFitness();
146  if ( fTrim ) ga.GetGeneticPopulation().TrimPopulation();
147  ga.SpreadControl( fSC_steps, fSC_rate, fSC_factor );
148 
149  // monitor progrss
150  if (ga.fConvCounter > n) n = Double_t(ga.fConvCounter);
151  progress = 100*((Double_t)cycle) + 100*(n/Double_t(fNsteps));
152 
153  timer.DrawProgressBar( (Int_t)progress );
154 
155  // Copy the best genes of the generation
156  ga.GetGeneticPopulation().Sort();
157  for ( Int_t i = 0; i<fSaveBestFromGeneration && i<fPopSize; i++ ) {
160  }
161  } while (!ga.HasConverged( fNsteps, fConvCrit ));
162 
163  timer.DrawProgressBar( 100*(cycle+1) );
164 
165  ga.GetGeneticPopulation().Sort();
166  for ( Int_t i = 0; i<fSaveBestFromGeneration && i<fPopSize; i++ ) {
169  }
170  }
171 
172  // get elapsed time
173  Log() << kINFO << "Elapsed time: " << timer.GetElapsedTime()
174  << " " << Endl;
175 
176  Double_t fitness = gstore.CalculateFitness();
177  gstore.GetGeneticPopulation().Sort();
178  pars.swap( gstore.GetGeneticPopulation().GetGenes(0)->GetFactors() );
179 
180  GetFitterTarget().ProgressNotifier( "GA", "stop" );
181  return fitness;
182 }
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
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...
virtual Double_t CalculateFitness()
starts the evaluation of the fitness of all different individuals of the population.
GeneticPopulation & GetGeneticPopulation()
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...
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
ClassImp(TMVA::GeneticFitter) TMVA
constructor
void AddPopulation(GeneticPopulation *strangers)
add another population (strangers) to the one of this GeneticPopulation
TStopwatch timer
Definition: pirndm.C:37
Double_t Run()
estimator function interface for fitting
Definition: FitterBase.cxx:79
TString GetElapsedTime(Bool_t Scientific=kTRUE)
Definition: Timer.cxx:131
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.
double Double_t
Definition: RtypesCore.h:55
void Sort()
sort the genepool according to the fitness of the individuals
Double_t GetFitness() const
Definition: GeneticGenes.h:54
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...
std::vector< Double_t > & GetFactors()
Definition: GeneticGenes.h:51
#define name(a, b)
Definition: linkTestLib0.cpp:5
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
void DeclareOptions()
declare GA options
void DrawProgressBar(Int_t, const TString &comment="")
draws progress bar in color or B&W caution:
Definition: Timer.cxx:183
void Init()
calls evolution, but if it is not the first time.
const Int_t n
Definition: legend1.C:16
Definition: math.cpp:60