TMVAGAexample2.cxx

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: TMVAGAexample2.cxx 31458 2009-11-30 13:58:20Z stelzer $
00002 /**********************************************************************************
00003  * Project   : TMVA - a Root-integrated toolkit for multivariate data analysis    *
00004  * Package   : TMVA                                                               *
00005  * Exectuable: TMVAGAexample                                                        *
00006  *                                                                                *
00007  * This exectutable gives an example of a very simple use of the genetic algorithm*
00008  * of TMVA                                                                        *
00009  *                                                                                *
00010  **********************************************************************************/
00011 
00012 #include <iostream> // Stream declarations
00013 #include <vector>
00014 
00015 #include "TMVA/GeneticAlgorithm.h"
00016 #include "TMVA/GeneticFitter.h"
00017 #include "TMVA/IFitterTarget.h"
00018 
00019 using namespace std;
00020 
00021 namespace TMVA {
00022 
00023 
00024 class MyFitness : public IFitterTarget {
00025     public:
00026        MyFitness() : IFitterTarget() {
00027        }
00028        
00029        // the fitness-function goes here
00030        // the factors are optimized such that the return-value of this function is minimized
00031        // take care!! the fitness-function must never fail, .. means: you have to prevent
00032        // the function from reaching undefined values (such as x=0 for 1/x or so)
00033        //
00034        // HINT: to use INTEGER variables, it is sufficient to cast the "factor" in the fitness-function
00035        // to (int). In this case the variable-range has to be chosen +1 ( to get 0..5, take Interval(0,6) )
00036        // since the introduction of "Interval" ranges can be defined with a third parameter
00037        // which gives the number of bins within the interval. With that technique discrete values
00038        // can be achieved easier. The random selection out of this discrete numbers is completly uniform.
00039        // 
00040        Double_t EstimatorFunction( std::vector<Double_t> & factors ){
00041            //return (10.- (int)factors.at(0) *factors.at(1) + (int)factors.at(2));
00042            return (10.- factors.at(0) *factors.at(1) + factors.at(2));
00043 
00044            //return 100.- (10 + factors.at(1)) *factors.at(2)* TMath::Abs( TMath::Sin(factors.at(0)) );
00045        }
00046 };
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 void exampleGA(){
00056         std::cout << "\nEXAMPLE" << std::endl;
00057         // define all the parameters by their minimum and maximum value
00058         // in this example 3 parameters are defined. 
00059         vector<Interval*> ranges;
00060         ranges.push_back( new Interval(0,15,30) );
00061         ranges.push_back( new Interval(0,13) );
00062         ranges.push_back( new Interval(0,5,3) );
00063 
00064         for( std::vector<Interval*>::iterator it = ranges.begin(); it != ranges.end(); it++ ){
00065            std::cout << " range: " << (*it)->GetMin() << "   " << (*it)->GetMax() << std::endl;
00066         }
00067 
00068         IFitterTarget* myFitness = new MyFitness();
00069 
00070         // prepare the genetic algorithm with an initial population size of 20
00071         // mind: big population sizes will help in searching the domain space of the solution
00072         // but you have to weight this out to the number of generations
00073         // the extreme case of 1 generation and populationsize n is equal to 
00074         // a Monte Carlo calculation with n tries
00075 
00076         const TString name( "exampleGA" );
00077         const TString opts( "PopSize=100:Steps=30" );
00078 
00079         GeneticFitter mg( *myFitness, name, ranges, opts);
00080        // mg.SetParameters( 4, 30, 200, 10,5, 0.95, 0.001 );
00081 
00082         std::vector<Double_t> result;
00083         Double_t estimator = mg.Run(result);
00084 
00085          int n = 0;
00086          for( std::vector<Double_t>::iterator it = result.begin(); it<result.end(); it++ ){
00087              std::cout << "FACTOR " << n << " : " << (*it) << std::endl;
00088              n++;
00089          }
00090         
00091 }
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 } // namespace TMVA
00100 
00101 int main( int argc, char** argv ) 
00102 {
00103    cout << "Start Test TMVAGAexample" << endl
00104         << "========================" << endl
00105         << endl;
00106 
00107    TMVA::exampleGA();
00108 }

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