00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <iostream>
00031
00032 #include "TMVA/GeneticFitter.h"
00033 #include "TMVA/GeneticAlgorithm.h"
00034 #include "TMVA/Interval.h"
00035 #include "TMVA/Timer.h"
00036
00037 ClassImp(TMVA::GeneticFitter)
00038
00039
00040 TMVA::GeneticFitter::GeneticFitter( IFitterTarget& target,
00041 const TString& name,
00042 const std::vector<TMVA::Interval*>& ranges,
00043 const TString& theOption )
00044 : FitterBase( target, name, ranges, theOption )
00045 {
00046
00047
00048
00049 DeclareOptions();
00050 ParseOptions();
00051 }
00052
00053
00054 void TMVA::GeneticFitter::DeclareOptions()
00055 {
00056
00057
00058 DeclareOptionRef( fPopSize=300, "PopSize", "Population size for GA" );
00059 DeclareOptionRef( fNsteps=40, "Steps", "Number of steps for convergence" );
00060 DeclareOptionRef( fCycles=3, "Cycles", "Independent cycles of GA fitting" );
00061 DeclareOptionRef( fSC_steps=10, "SC_steps", "Spread control, steps" );
00062 DeclareOptionRef( fSC_rate=5, "SC_rate", "Spread control, rate: factor is changed depending on the rate" );
00063 DeclareOptionRef( fSC_factor=0.95, "SC_factor", "Spread control, factor" );
00064 DeclareOptionRef( fConvCrit=0.001, "ConvCrit", "Convergence criteria" );
00065
00066 DeclareOptionRef( fSaveBestFromGeneration=1, "SaveBestGen",
00067 "Saves the best n results from each generation. They are included in the last cycle" );
00068 DeclareOptionRef( fSaveBestFromCycle=10, "SaveBestCycle",
00069 "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" );
00070
00071 DeclareOptionRef( fTrim=kFALSE, "Trim",
00072 "Trim the population to PopSize after assessing the fitness of each individual" );
00073 DeclareOptionRef( fSeed=100, "Seed", "Set seed of random generator (0 gives random seeds)" );
00074 }
00075
00076
00077 void TMVA::GeneticFitter::SetParameters( Int_t cycles,
00078 Int_t nsteps,
00079 Int_t popSize,
00080 Int_t SC_steps,
00081 Int_t SC_rate,
00082 Double_t SC_factor,
00083 Double_t convCrit)
00084 {
00085
00086 fNsteps = nsteps;
00087 fCycles = cycles;
00088 fPopSize = popSize;
00089 fSC_steps = SC_steps;
00090 fSC_rate = SC_rate;
00091 fSC_factor = SC_factor;
00092 fConvCrit = convCrit;
00093 }
00094
00095
00096 Double_t TMVA::GeneticFitter::Run( std::vector<Double_t>& pars )
00097 {
00098
00099 Log() << kINFO << "<GeneticFitter> Optimisation, please be patient "
00100 << "... (inaccurate progress timing for GA)" << Endl;
00101
00102 GetFitterTarget().ProgressNotifier( "GA", "init" );
00103
00104 GeneticAlgorithm gstore( GetFitterTarget(), fPopSize, fRanges);
00105
00106
00107
00108 Timer timer( 100*(fCycles), GetName() );
00109 timer.DrawProgressBar( 0 );
00110
00111 Double_t progress = 0.;
00112
00113 for (Int_t cycle = 0; cycle < fCycles; cycle++) {
00114 GetFitterTarget().ProgressNotifier( "GA", "cycle" );
00115
00116
00117
00118 GeneticAlgorithm ga( GetFitterTarget(), fPopSize, fRanges, fSeed );
00119
00120
00121 if ( pars.size() == fRanges.size() ){
00122 ga.GetGeneticPopulation().GiveHint( pars, 0.0 );
00123 }
00124 if (cycle==fCycles-1) {
00125 GetFitterTarget().ProgressNotifier( "GA", "last" );
00126 ga.GetGeneticPopulation().AddPopulation( gstore.GetGeneticPopulation() );
00127 }
00128
00129 GetFitterTarget().ProgressNotifier( "GA", "iteration" );
00130
00131 ga.CalculateFitness();
00132 ga.GetGeneticPopulation().TrimPopulation();
00133
00134 Double_t n=0.;
00135 do {
00136 GetFitterTarget().ProgressNotifier( "GA", "iteration" );
00137 ga.Init();
00138 ga.CalculateFitness();
00139 if ( fTrim ) ga.GetGeneticPopulation().TrimPopulation();
00140 ga.SpreadControl( fSC_steps, fSC_rate, fSC_factor );
00141
00142
00143 if (ga.fConvCounter > n) n = Double_t(ga.fConvCounter);
00144 progress = 100*((Double_t)cycle) + 100*(n/Double_t(fNsteps));
00145
00146 timer.DrawProgressBar( (Int_t)progress );
00147
00148
00149 ga.GetGeneticPopulation().Sort();
00150 for ( Int_t i = 0; i<fSaveBestFromGeneration && i<fPopSize; i++ ) {
00151 gstore.GetGeneticPopulation().GiveHint( ga.GetGeneticPopulation().GetGenes(i)->GetFactors(),
00152 ga.GetGeneticPopulation().GetGenes(i)->GetFitness() );
00153 }
00154 } while (!ga.HasConverged( fNsteps, fConvCrit ));
00155
00156 timer.DrawProgressBar( 100*(cycle+1) );
00157
00158 ga.GetGeneticPopulation().Sort();
00159 for ( Int_t i = 0; i<fSaveBestFromGeneration && i<fPopSize; i++ ) {
00160 gstore.GetGeneticPopulation().GiveHint( ga.GetGeneticPopulation().GetGenes(i)->GetFactors(),
00161 ga.GetGeneticPopulation().GetGenes(i)->GetFitness() );
00162 }
00163 }
00164
00165
00166 Log() << kINFO << "Elapsed time: " << timer.GetElapsedTime()
00167 << " " << Endl;
00168
00169 Double_t fitness = gstore.CalculateFitness();
00170 gstore.GetGeneticPopulation().Sort();
00171 pars.swap( gstore.GetGeneticPopulation().GetGenes(0)->GetFactors() );
00172
00173 GetFitterTarget().ProgressNotifier( "GA", "stop" );
00174 return fitness;
00175 }