ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
NumericalMinimization.C
Go to the documentation of this file.
1 // Example on how to use the new Minimizer class in ROOT
2 // Show usage with all the possible minimizers.
3 // Minimize the Rosenbrock function (a 2D -function)
4 // This example is described also in
5 // http://root.cern.ch/drupal/content/numerical-minimization#multidim_minim
6 // input : minimizer name + algorithm name
7 // randomSeed: = <0 : fixed value: 0 random with seed 0; >0 random with given seed
8 //
9 //Author: L. Moneta Dec 2010
10 
11 #include "Math/Minimizer.h"
12 #include "Math/Factory.h"
13 #include "Math/Functor.h"
14 #include "TRandom2.h"
15 #include "TError.h"
16 #include <iostream>
17 
18 double RosenBrock(const double *xx )
19 {
20  const Double_t x = xx[0];
21  const Double_t y = xx[1];
22  const Double_t tmp1 = y-x*x;
23  const Double_t tmp2 = 1-x;
24  return 100*tmp1*tmp1+tmp2*tmp2;
25 }
26 
27 int NumericalMinimization(const char * minName = "Minuit2",
28  const char *algoName = "" ,
29  int randomSeed = -1)
30 {
31  // create minimizer giving a name and a name (optionally) for the specific
32  // algorithm
33  // possible choices are:
34  // minName algoName
35  // Minuit /Minuit2 Migrad, Simplex,Combined,Scan (default is Migrad)
36  // Minuit2 Fumili2
37  // Fumili
38  // GSLMultiMin ConjugateFR, ConjugatePR, BFGS,
39  // BFGS2, SteepestDescent
40  // GSLMultiFit
41  // GSLSimAn
42  // Genetic
44  ROOT::Math::Factory::CreateMinimizer(minName, algoName);
45 
46  // set tolerance , etc...
47  min->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2
48  min->SetMaxIterations(10000); // for GSL
49  min->SetTolerance(0.001);
50  min->SetPrintLevel(1);
51 
52  // create funciton wrapper for minmizer
53  // a IMultiGenFunction type
55  double step[2] = {0.01,0.01};
56  // starting point
57 
58  double variable[2] = { -1.,1.2};
59  if (randomSeed >= 0) {
61  variable[0] = r.Uniform(-20,20);
62  variable[1] = r.Uniform(-20,20);
63  }
64 
65  min->SetFunction(f);
66 
67  // Set the free variables to be minimized!
68  min->SetVariable(0,"x",variable[0], step[0]);
69  min->SetVariable(1,"y",variable[1], step[1]);
70 
71  // do the minimization
72  min->Minimize();
73 
74  const double *xs = min->X();
75  std::cout << "Minimum: f(" << xs[0] << "," << xs[1] << "): "
76  << min->MinValue() << std::endl;
77 
78  // expected minimum is 0
79  if ( min->MinValue() < 1.E-4 && f(xs) < 1.E-4)
80  std::cout << "Minimizer " << minName << " - " << algoName
81  << " converged to the right minimum" << std::endl;
82  else {
83  std::cout << "Minimizer " << minName << " - " << algoName
84  << " failed to converge !!!" << std::endl;
85  Error("NumericalMinimization","fail to converge");
86  }
87 
88  return 0;
89 }
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
Definition: Minimizer.h:459
int randomSeed
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
Definition: vector.h:433
int NumericalMinimization(const char *minName="Minuit2", const char *algoName="", int randomSeed=-1)
Documentation for class Functor class.
Definition: Functor.h:394
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition: TRandom2.h:29
static ROOT::Math::Minimizer * CreateMinimizer(const std::string &minimizerType="", const std::string &algoType="")
static method to create the corrisponding Minimizer given the string Supported Minimizers types are: ...
Definition: Factory.cxx:63
TFile * f
double RosenBrock(const double *xx)
Double_t x[n]
Definition: legend1.C:17
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2, Minuit, GSL, etc..) Plug-in's exist in ROOT to be able to instantiate the derived classes like ROOT::Math::GSLMinimizer or ROOT::Math::Minuit2Minimizer via the plug-in manager.
Definition: Minimizer.h:86
virtual double MinValue() const =0
return minimum function value
virtual bool Minimize()=0
method to perform the minimization
virtual const double * X() const =0
return pointer to X values at the minimum
void Error(const char *location, const char *msgfmt,...)
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)=0
set the function to minimize
ROOT::R::TRInterface & r
Definition: Object.C:4
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
Definition: Minimizer.h:456
double Double_t
Definition: RtypesCore.h:55
void SetTolerance(double tol)
set the tolerance
Definition: Minimizer.h:462
Double_t y[n]
Definition: legend1.C:17
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
virtual bool SetVariable(unsigned int ivar, const std::string &name, double val, double step)=0
set a new free variable
void SetPrintLevel(int level)
set print level
Definition: Minimizer.h:453