Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
NumericalMinimization.py
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
15import ROOT
16import numpy as np
17
18
19def RosenBrock(vecx):
20 x = vecx[0]
21 y = vecx[1]
22 return (y - x**2)**2 + (1 - x)**2
23
24# create minimizer giving a name and a name (optionally) for the specific algorithm
25# possible choices are:
26# minimizerName algoName
27#
28# Minuit Migrad, Simplex,Combined,Scan (default is Migrad)
29# Minuit2 Migrad, BFGS, Simplex,Combined,Scan (default is Migrad)
30# GSLMultiMin ConjugateFR, ConjugatePR, BFGS, BFGS2, SteepestDescent
31# GSLSimAn
32# Genetic
33
34
35def NumericalMinimization(minimizerName="Minuit2",
36 algoName="",
37 randomSeed=-1):
38
39 minimizer = ROOT.Math.Factory.CreateMinimizer(minimizerName, algoName)
40 if (not minimizer):
41 raise RuntimeError(
42 "Cannot create minimizer \"{}\". Maybe the required library was not built?".format(minimizerName))
43
44 # Set tolerance and other minimizer parameters, one can also use default
45 # values
46
47 minimizer.SetMaxFunctionCalls(1000000) # working for Minuit/Minuit2
48 # for GSL minimizers - no effect in Minuit/Minuit2
49 minimizer.SetMaxIterations(10000)
50 minimizer.SetTolerance(0.001)
51 minimizer.SetPrintLevel(1)
52
53 # Create function wrapper for minimizer
54
55 f = ROOT.Math.Functor(RosenBrock, 2)
56
57 # Evaluate function at a point
58 x0 = np.array([-1., 2.])
59 print("f(-1,1.2) = ", f(x0))
60
61 # Starting point
62 variable = [-1., 1.2]
63 step = [0.01, 0.01]
64 if (randomSeed >= 0):
65 r = ROOT.TRandom2(randomSeed)
66 variable[0] = r.Uniform(-20, 20)
67 variable[1] = r.Uniform(-20, 20)
68
69 minimizer.SetFunction(f)
70
71 # Set the free variables to be minimized !
72 minimizer.SetVariable(0, "x", variable[0], step[0])
73 minimizer.SetVariable(1, "y", variable[1], step[1])
74
75 # Do the minimization
76 ret = minimizer.Minimize()
77
78 xs = minimizer.X()
79 print("Minimum: f({} , {}) = {}".format(xs[0],xs[1],minimizer.MinValue()))
80
81 # Real minimum is f(xmin) = 0
82 if (ret and minimizer.MinValue() < 1.E-4):
83 print("Minimizer {} - {} converged to the right minimum!".format(minimizerName, algoName))
84 else:
85 print("Minimizer {} - {} failed to converge !!!".format(minimizerName, algoName))
86 raise RuntimeError("NumericalMinimization failed to converge!")
87
88
89if __name__ == "__main__":
#define f(i)
Definition RSha256.hxx:104
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 Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
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