NumericalMinimization.C: Example on how to use the new Minimizer class in ROOT
// Example on how to use the new Minimizer class in ROOT
// Show usage with all the possible minimizers.
// Minimize the Rosenbrock function (a 2D -function)
// This example is described also in
// http://root.cern.ch/drupal/content/numerical-minimization#multidim_minim
// input : minimizer name + algorithm name
// randomSeed: = <0 : fixed value: 0 random with seed 0; >0 random with given seed
//
//Author: L. Moneta Dec 2010
#include "Math/Minimizer.h"
#include "Math/Factory.h"
#include "Math/Functor.h"
#include "TRandom2.h"
#include "TError.h"
#include <iostream>
double RosenBrock(const double *xx )
{
const Double_t x = xx[0];
const Double_t y = xx[1];
const Double_t tmp1 = y-x*x;
const Double_t tmp2 = 1-x;
return 100*tmp1*tmp1+tmp2*tmp2;
}
int NumericalMinimization(const char * minName = "Minuit2",
const char *algoName = "" ,
int randomSeed = -1)
{
// create minimizer giving a name and a name (optionally) for the specific
// algorithm
// possible choices are:
// minName algoName
// Minuit /Minuit2 Migrad, Simplex,Combined,Scan (default is Migrad)
// Minuit2 Fumili2
// Fumili
// GSLMultiMin ConjugateFR, ConjugatePR, BFGS,
// BFGS2, SteepestDescent
// GSLMultiFit
// GSLSimAn
// Genetic
ROOT::Math::Minimizer* min =
ROOT::Math::Factory::CreateMinimizer(minName, algoName);
// set tolerance , etc...
min->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2
min->SetMaxIterations(10000); // for GSL
min->SetTolerance(0.001);
min->SetPrintLevel(1);
// create funciton wrapper for minmizer
// a IMultiGenFunction type
ROOT::Math::Functor f(&RosenBrock,2);
double step[2] = {0.01,0.01};
// starting point
double variable[2] = { -1.,1.2};
if (randomSeed >= 0) {
TRandom2 r(randomSeed);
variable[0] = r.Uniform(-20,20);
variable[1] = r.Uniform(-20,20);
}
min->SetFunction(f);
// Set the free variables to be minimized!
min->SetVariable(0,"x",variable[0], step[0]);
min->SetVariable(1,"y",variable[1], step[1]);
// do the minimization
min->Minimize();
const double *xs = min->X();
std::cout << "Minimum: f(" << xs[0] << "," << xs[1] << "): "
<< min->MinValue() << std::endl;
// expected minimum is 0
if ( min->MinValue() < 1.E-4 && f(xs) < 1.E-4)
std::cout << "Minimizer " << minName << " - " << algoName
<< " converged to the right minimum" << std::endl;
else {
std::cout << "Minimizer " << minName << " - " << algoName
<< " failed to converge !!!" << std::endl;
Error("NumericalMinimization","fail to converge");
}
return 0;
}
NumericalMinimization.C:1 NumericalMinimization.C:2 NumericalMinimization.C:3 NumericalMinimization.C:4 NumericalMinimization.C:5 NumericalMinimization.C:6 NumericalMinimization.C:7 NumericalMinimization.C:8 NumericalMinimization.C:9 NumericalMinimization.C:10 NumericalMinimization.C:11 NumericalMinimization.C:12 NumericalMinimization.C:13 NumericalMinimization.C:14 NumericalMinimization.C:15 NumericalMinimization.C:16 NumericalMinimization.C:17 NumericalMinimization.C:18 NumericalMinimization.C:19 NumericalMinimization.C:20 NumericalMinimization.C:21 NumericalMinimization.C:22 NumericalMinimization.C:23 NumericalMinimization.C:24 NumericalMinimization.C:25 NumericalMinimization.C:26 NumericalMinimization.C:27 NumericalMinimization.C:28 NumericalMinimization.C:29 NumericalMinimization.C:30 NumericalMinimization.C:31 NumericalMinimization.C:32 NumericalMinimization.C:33 NumericalMinimization.C:34 NumericalMinimization.C:35 NumericalMinimization.C:36 NumericalMinimization.C:37 NumericalMinimization.C:38 NumericalMinimization.C:39 NumericalMinimization.C:40 NumericalMinimization.C:41 NumericalMinimization.C:42 NumericalMinimization.C:43 NumericalMinimization.C:44 NumericalMinimization.C:45 NumericalMinimization.C:46 NumericalMinimization.C:47 NumericalMinimization.C:48 NumericalMinimization.C:49 NumericalMinimization.C:50 NumericalMinimization.C:51 NumericalMinimization.C:52 NumericalMinimization.C:53 NumericalMinimization.C:54 NumericalMinimization.C:55 NumericalMinimization.C:56 NumericalMinimization.C:57 NumericalMinimization.C:58 NumericalMinimization.C:59 NumericalMinimization.C:60 NumericalMinimization.C:61 NumericalMinimization.C:62 NumericalMinimization.C:63 NumericalMinimization.C:64 NumericalMinimization.C:65 NumericalMinimization.C:66 NumericalMinimization.C:67 NumericalMinimization.C:68 NumericalMinimization.C:69 NumericalMinimization.C:70 NumericalMinimization.C:71 NumericalMinimization.C:72 NumericalMinimization.C:73 NumericalMinimization.C:74 NumericalMinimization.C:75 NumericalMinimization.C:76 NumericalMinimization.C:77 NumericalMinimization.C:78 NumericalMinimization.C:79 NumericalMinimization.C:80 NumericalMinimization.C:81 NumericalMinimization.C:82 NumericalMinimization.C:83 NumericalMinimization.C:84 NumericalMinimization.C:85 NumericalMinimization.C:86 NumericalMinimization.C:87 NumericalMinimization.C:88 NumericalMinimization.C:89 NumericalMinimization.C:90