GeneticFitter.cxx

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: GeneticFitter.cxx 29195 2009-06-24 10:39:49Z brun $ 
00002 // Author: Peter Speckmayer
00003 
00004 /**********************************************************************************
00005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
00006  * Package: TMVA                                                                  *
00007  * Class  : GeneticFitter                                                         *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      Implementation                                                            *
00012  *                                                                                *
00013  * Authors (alphabetical):                                                        *
00014  *      Peter Speckmayer <speckmay@mail.cern.ch> - CERN, Switzerland              *
00015  *                                                                                *
00016  * Copyright (c) 2005:                                                            *
00017  *      CERN, Switzerland                                                         * 
00018  *      MPI-K Heidelberg, Germany                                                 * 
00019  *                                                                                *
00020  * Redistribution and use in source and binary forms, with or without             *
00021  * modification, are permitted according to the terms listed in LICENSE           *
00022  * (http://tmva.sourceforge.net/LICENSE)                                          *
00023  **********************************************************************************/
00024 
00025 //_______________________________________________________________________
00026 //                                                                      
00027 // Fitter using a Genetic Algorithm
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    // constructor
00047 
00048    // default parameters settings for Genetic Algorithm
00049    DeclareOptions();
00050    ParseOptions();
00051 }            
00052 
00053 //_______________________________________________________________________
00054 void TMVA::GeneticFitter::DeclareOptions() 
00055 {
00056    // declare GA options
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    // set GA configuration parameters
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    // Execute fitting
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    //   gstore.SetMakeCopies(kTRUE);  // commented out, because it reduces speed
00106 
00107    // timing of GA
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       // ---- perform series of fits to achieve best convergence
00116          
00117       // "m_ga_spread" times the number of variables
00118       GeneticAlgorithm ga( GetFitterTarget(), fPopSize, fRanges, fSeed ); 
00119       //      ga.SetMakeCopies(kTRUE);  // commented out, because it reduces speed
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          // monitor progrss
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          // Copy the best genes of the generation
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    // get elapsed time   
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 }

Generated on Tue Jul 5 15:25:00 2011 for ROOT_528-00b_version by  doxygen 1.5.1