 ROOT   Reference Guide NumericalMinimization.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_fit
3/// \notebook -nodraw
4/// Example on how to use the new Minimizer class in ROOT
5/// Show usage with all the possible minimizers.
6/// Minimize the Rosenbrock function (a 2D -function)
7/// This example is described also in
8/// http://root.cern.ch/drupal/content/numerical-minimization#multidim_minim
9/// input : minimizer name + algorithm name
10/// randomSeed: = <0 : fixed value: 0 random with seed 0; >0 random with given seed
11///
12/// \macro_code
13///
14/// \author Lorenzo Moneta
15
16#include "Math/Minimizer.h"
17#include "Math/Factory.h"
18#include "Math/Functor.h"
19#include "TRandom2.h"
20#include "TError.h"
21#include <iostream>
22
23double RosenBrock(const double *xx )
24{
25 const double x = xx;
26 const double y = xx;
27 const double tmp1 = y-x*x;
28 const double tmp2 = 1-x;
29 return 100*tmp1*tmp1+tmp2*tmp2;
30}
31
32int NumericalMinimization(const char * minName = "Minuit2",
33 const char *algoName = "" ,
34 int randomSeed = -1)
35{
36 // create minimizer giving a name and a name (optionally) for the specific
37 // algorithm
38 // possible choices are:
39 // minName algoName
40 // Minuit /Minuit2 Migrad, Simplex,Combined,Scan (default is Migrad)
41 // Minuit2 Fumili2
42 // Fumili
43 // GSLMultiMin ConjugateFR, ConjugatePR, BFGS,
44 // BFGS2, SteepestDescent
45 // GSLMultiFit
46 // GSLSimAn
47 // Genetic
48 ROOT::Math::Minimizer* minimum =
49 ROOT::Math::Factory::CreateMinimizer(minName, algoName);
50 if (!minimum) {
51 std::cerr << "Error: cannot create minimizer \"" << minName
52 << "\". Maybe the required library was not built?" << std::endl;
53 return 1;
54 }
55
56 // set tolerance , etc...
57 minimum->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2
58 minimum->SetMaxIterations(10000); // for GSL
59 minimum->SetTolerance(0.001);
60 minimum->SetPrintLevel(1);
61
62 // create function wrapper for minimizer
63 // a IMultiGenFunction type
64 ROOT::Math::Functor f(&RosenBrock,2);
65 double step = {0.01,0.01};
66 // starting point
67
68 double variable = { -1.,1.2};
69 if (randomSeed >= 0) {
70 TRandom2 r(randomSeed);
71 variable = r.Uniform(-20,20);
72 variable = r.Uniform(-20,20);
73 }
74
75 minimum->SetFunction(f);
76
77 // Set the free variables to be minimized !
78 minimum->SetVariable(0,"x",variable, step);
79 minimum->SetVariable(1,"y",variable, step);
80
81 // do the minimization
82 minimum->Minimize();
83
84 const double *xs = minimum->X();
85 std::cout << "Minimum: f(" << xs << "," << xs << "): "
86 << minimum->MinValue() << std::endl;
87
88 // expected minimum is 0
89 if ( minimum->MinValue() < 1.E-4 && f(xs) < 1.E-4)
90 std::cout << "Minimizer " << minName << " - " << algoName
91 << " converged to the right minimum" << std::endl;
92 else {
93 std::cout << "Minimizer " << minName << " - " << algoName
94 << " failed to converge !!!" << std::endl;
95 Error("NumericalMinimization","fail to converge");
96 }
97
98 return 0;
99}
#define f(i)
Definition: RSha256.hxx:104
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
static ROOT::Math::Minimizer * CreateMinimizer(const std::string &minimizerType="", const std::string &algoType="")
static method to create the corresponding Minimizer given the string Supported Minimizers types are: ...
Definition: Factory.cxx:63
Documentation for class Functor class.
Definition: Functor.h:401
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2,...
Definition: Minimizer.h:75
virtual const double * X() const =0
return pointer to X values at the minimum
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
Definition: Minimizer.h:450
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)=0
set the function to minimize
void SetTolerance(double tol)
set the tolerance
Definition: Minimizer.h:453
virtual bool Minimize()=0
method to perform the minimization
void SetPrintLevel(int level)
set print level
Definition: Minimizer.h:444
virtual bool SetVariable(unsigned int ivar, const std::string &name, double val, double step)=0
set a new free variable
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
Definition: Minimizer.h:447
virtual double MinValue() const =0
return minimum function value
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition: TRandom2.h:27
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17