47 ClassImp(TMVA::GeneticFitter);
52 TMVA::GeneticFitter::GeneticFitter( IFitterTarget& target,
54 const std::vector<TMVA::Interval*>& ranges,
55 const TString& theOption )
56 : FitterBase( target, name, ranges, theOption )
66 void TMVA::GeneticFitter::DeclareOptions()
68 DeclareOptionRef( fPopSize=300,
"PopSize",
"Population size for GA" );
69 DeclareOptionRef( fNsteps=40,
"Steps",
"Number of steps for convergence" );
70 DeclareOptionRef( fCycles=3,
"Cycles",
"Independent cycles of GA fitting" );
71 DeclareOptionRef( fSC_steps=10,
"SC_steps",
"Spread control, steps" );
72 DeclareOptionRef( fSC_rate=5,
"SC_rate",
"Spread control, rate: factor is changed depending on the rate" );
73 DeclareOptionRef( fSC_factor=0.95,
"SC_factor",
"Spread control, factor" );
74 DeclareOptionRef( fConvCrit=0.001,
"ConvCrit",
"Convergence criteria" );
76 DeclareOptionRef( fSaveBestFromGeneration=1,
"SaveBestGen",
77 "Saves the best n results from each generation. They are included in the last cycle" );
78 DeclareOptionRef( fSaveBestFromCycle=10,
"SaveBestCycle",
79 "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" );
81 DeclareOptionRef( fTrim=kFALSE,
"Trim",
82 "Trim the population to PopSize after assessing the fitness of each individual" );
83 DeclareOptionRef( fSeed=100,
"Seed",
"Set seed of random generator (0 gives random seeds)" );
89 void TMVA::GeneticFitter::SetParameters( Int_t cycles,
100 fSC_steps = SC_steps;
102 fSC_factor = SC_factor;
103 fConvCrit = convCrit;
109 Double_t TMVA::GeneticFitter::Run( std::vector<Double_t>& pars )
111 Log() << kHEADER <<
"<GeneticFitter> Optimisation, please be patient "
112 <<
"... (inaccurate progress timing for GA)" << Endl;
114 GetFitterTarget().ProgressNotifier(
"GA",
"init" );
116 GeneticAlgorithm gstore( GetFitterTarget(), fPopSize, fRanges);
120 Timer timer( 100*(fCycles), GetName() );
121 if (fIPyMaxIter) *fIPyMaxIter = 100*(fCycles);
122 timer.DrawProgressBar( 0 );
124 Double_t progress = 0.;
126 for (Int_t cycle = 0; cycle < fCycles; cycle++) {
127 if (fIPyCurrentIter) *fIPyCurrentIter = 100*(cycle);
128 if (fExitFromTraining && *fExitFromTraining)
break;
129 GetFitterTarget().ProgressNotifier(
"GA",
"cycle" );
133 GeneticAlgorithm ga( GetFitterTarget(), fPopSize, fRanges, fSeed );
136 if ( pars.size() == fRanges.size() ){
137 ga.GetGeneticPopulation().GiveHint( pars, 0.0 );
139 if (cycle==fCycles-1) {
140 GetFitterTarget().ProgressNotifier(
"GA",
"last" );
141 ga.GetGeneticPopulation().AddPopulation( gstore.GetGeneticPopulation() );
144 GetFitterTarget().ProgressNotifier(
"GA",
"iteration" );
146 ga.CalculateFitness();
147 ga.GetGeneticPopulation().TrimPopulation();
151 GetFitterTarget().ProgressNotifier(
"GA",
"iteration" );
153 ga.CalculateFitness();
154 if ( fTrim ) ga.GetGeneticPopulation().TrimPopulation();
155 ga.SpreadControl( fSC_steps, fSC_rate, fSC_factor );
158 if (ga.fConvCounter > n) n = Double_t(ga.fConvCounter);
159 progress = 100*((Double_t)cycle) + 100*(n/Double_t(fNsteps));
161 timer.DrawProgressBar( (Int_t)progress );
164 ga.GetGeneticPopulation().Sort();
165 for ( Int_t i = 0; i<fSaveBestFromGeneration && i<fPopSize; i++ ) {
166 gstore.GetGeneticPopulation().GiveHint( ga.GetGeneticPopulation().GetGenes(i)->GetFactors(),
167 ga.GetGeneticPopulation().GetGenes(i)->GetFitness() );
169 }
while (!ga.HasConverged( fNsteps, fConvCrit ));
171 timer.DrawProgressBar( 100*(cycle+1) );
173 ga.GetGeneticPopulation().Sort();
174 for ( Int_t i = 0; i<fSaveBestFromGeneration && i<fPopSize; i++ ) {
175 gstore.GetGeneticPopulation().GiveHint( ga.GetGeneticPopulation().GetGenes(i)->GetFactors(),
176 ga.GetGeneticPopulation().GetGenes(i)->GetFitness() );
181 Log() << kINFO <<
"Elapsed time: " << timer.GetElapsedTime()
184 Double_t fitness = gstore.CalculateFitness();
185 gstore.GetGeneticPopulation().Sort();
186 pars.swap( gstore.GetGeneticPopulation().GetGenes(0)->GetFactors() );
188 GetFitterTarget().ProgressNotifier(
"GA",
"stop" );