ROOT   Reference Guide
Searching...
No Matches
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///
8/// input : minimizer name + algorithm name
9/// randomSeed: = <0 : fixed value: 0 random with seed 0; >0 random with given seed
10///
11/// \macro_code
12///
13/// \author Lorenzo Moneta
14
15#include "Math/Minimizer.h"
16#include "Math/Factory.h"
17#include "Math/Functor.h"
18#include "TRandom2.h"
19#include "TError.h"
20#include <iostream>
21
22double RosenBrock(const double *xx )
23{
24 const double x = xx[0];
25 const double y = xx[1];
26 const double tmp1 = y-x*x;
27 const double tmp2 = 1-x;
28 return 100*tmp1*tmp1+tmp2*tmp2;
29}
30
31int NumericalMinimization(const char * minName = "Minuit2",
32 const char *algoName = "" ,
33 int randomSeed = -1)
34{
35 // create minimizer giving a name and a name (optionally) for the specific
36 // algorithm
37 // possible choices are:
38 // minName algoName
40 // Minuit2 Fumili2
41 // Fumili
42 // GSLMultiMin ConjugateFR, ConjugatePR, BFGS,
43 // BFGS2, SteepestDescent
44 // GSLMultiFit
45 // GSLSimAn
46 // Genetic
47 ROOT::Math::Minimizer* minimum =
48 ROOT::Math::Factory::CreateMinimizer(minName, algoName);
49 if (!minimum) {
50 std::cerr << "Error: cannot create minimizer \"" << minName
51 << "\". Maybe the required library was not built?" << std::endl;
52 return 1;
53 }
54
55 // set tolerance , etc...
56 minimum->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2
57 minimum->SetMaxIterations(10000); // for GSL
58 minimum->SetTolerance(0.001);
59 minimum->SetPrintLevel(1);
60
61 // create function wrapper for minimizer
62 // a IMultiGenFunction type
63 ROOT::Math::Functor f(&RosenBrock,2);
64 double step[2] = {0.01,0.01};
65 // starting point
66
67 double variable[2] = { -1.,1.2};
68 if (randomSeed >= 0) {
69 TRandom2 r(randomSeed);
70 variable[0] = r.Uniform(-20,20);
71 variable[1] = r.Uniform(-20,20);
72 }
73
74 minimum->SetFunction(f);
75
76 // Set the free variables to be minimized !
77 minimum->SetVariable(0,"x",variable[0], step[0]);
78 minimum->SetVariable(1,"y",variable[1], step[1]);
79
80 // do the minimization
81 minimum->Minimize();
82
83 const double *xs = minimum->X();
84 std::cout << "Minimum: f(" << xs[0] << "," << xs[1] << "): "
85 << minimum->MinValue() << std::endl;
86
87 // expected minimum is 0
88 if ( minimum->MinValue() < 1.E-4 )
89 std::cout << "Minimizer " << minName << " - " << algoName
90 << " converged to the right minimum" << std::endl;
91 else {
92 std::cout << "Minimizer " << minName << " - " << algoName
93 << " failed to converge !!!" << std::endl;
94 Error("NumericalMinimization","fail to converge");
95 }
96
97 return 0;
98}
#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:185
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:47
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2,...
Definition Minimizer.h:119
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:351
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)=0
set the function to minimize
void SetTolerance(double tol)
set the tolerance
Definition Minimizer.h:354
virtual bool Minimize()=0
method to perform the minimization
void SetPrintLevel(int level)
set print level
Definition Minimizer.h:345
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:348
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