NumericalMinimization.C

Go to the documentation of this file.
00001 // Example on how to use the new Minimizer class in ROOT
00002 //  Show usage with all the possible minimizers. 
00003 // Minimize the Rosenbrock function (a 2D -function)
00004 // This example is described also in 
00005 // http://root.cern.ch/drupal/content/numerical-minimization#multidim_minim
00006 // input : minimizer name + algorithm name
00007 // randomSeed: = <0 : fixed value: 0 random with seed 0; >0 random with given seed 
00008 //
00009 //Author: L. Moneta Dec 2010
00010 
00011 #include "Math/Minimizer.h"
00012 #include "Math/Factory.h"
00013 #include "Math/Functor.h"
00014 #include "TRandom2.h"
00015 #include "TError.h"
00016 #include <iostream>
00017  
00018 double RosenBrock(const double *xx )
00019 {
00020   const Double_t x = xx[0];
00021   const Double_t y = xx[1];
00022   const Double_t tmp1 = y-x*x;
00023   const Double_t tmp2 = 1-x;
00024   return 100*tmp1*tmp1+tmp2*tmp2;
00025 }
00026  
00027 int NumericalMinimization(const char * minName = "Minuit2",
00028                           const char *algoName = "" , 
00029                           int randomSeed = -1)
00030 {
00031    // create minimizer giving a name and a name (optionally) for the specific
00032    // algorithm
00033    // possible choices are: 
00034    //     minName                  algoName
00035    // Minuit /Minuit2             Migrad, Simplex,Combined,Scan  (default is Migrad)
00036    //  Minuit2                     Fumili2
00037    //  Fumili
00038    //  GSLMultiMin                ConjugateFR, ConjugatePR, BFGS, 
00039    //                              BFGS2, SteepestDescent
00040    //  GSLMultiFit
00041    //   GSLSimAn
00042    //   Genetic
00043    ROOT::Math::Minimizer* min = 
00044       ROOT::Math::Factory::CreateMinimizer(minName, algoName);
00045 
00046    // set tolerance , etc...
00047    min->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2 
00048    min->SetMaxIterations(10000);  // for GSL 
00049    min->SetTolerance(0.001);
00050    min->SetPrintLevel(1);
00051 
00052    // create funciton wrapper for minmizer
00053    // a IMultiGenFunction type 
00054    ROOT::Math::Functor f(&RosenBrock,2); 
00055    double step[2] = {0.01,0.01};
00056    // starting point
00057     
00058    double variable[2] = { -1.,1.2};
00059    if (randomSeed >= 0) { 
00060       TRandom2 r(randomSeed);
00061       variable[0] = r.Uniform(-20,20);
00062       variable[1] = r.Uniform(-20,20);
00063    }
00064  
00065    min->SetFunction(f);
00066  
00067    // Set the free variables to be minimized!
00068    min->SetVariable(0,"x",variable[0], step[0]);
00069    min->SetVariable(1,"y",variable[1], step[1]);
00070  
00071    // do the minimization
00072    min->Minimize(); 
00073  
00074    const double *xs = min->X();
00075    std::cout << "Minimum: f(" << xs[0] << "," << xs[1] << "): " 
00076              << min->MinValue()  << std::endl;
00077 
00078    // expected minimum is 0
00079    if ( min->MinValue()  < 1.E-4  && f(xs) < 1.E-4) 
00080       std::cout << "Minimizer " << minName << " - " << algoName 
00081                 << "   converged to the right minimum" << std::endl;
00082    else {
00083       std::cout << "Minimizer " << minName << " - " << algoName 
00084                 << "   failed to converge !!!" << std::endl;
00085       Error("NumericalMinimization","fail to converge");
00086    }
00087  
00088    return 0;
00089 }

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