Logo ROOT   6.08/07
Reference Guide
demoMinimizer.cxx
Go to the documentation of this file.
1 // @(#)root/minuit2:$Id$
2 // Author: L. Moneta 12/2005
3 /**
4  test of a pure minimization passing a user function
5  This is an example of running Minuit2 using the
6  Minuit2Minimizer class (via the Minimizer interface)
7 
8 */
9 #include "Math/IFunction.h"
11 #include "Math/Functor.h"
12 #include <string>
13 #include <iostream>
14 
15 double Rosenbrock (const double * x) {
16  // Rosebrock function
17  double tmp1 = (x[1] - x[0]*x[0]); // y -x^2
18  double tmp2 = (1.-x[0]); // 1-x
19  return 100.*tmp1*tmp1 + tmp2*tmp2;
20 }
21 
22 int demoMinimizer(const char * algoName, int printlevel) {
23 
25 
26  // set tolerance , etc...
27  min->SetMaxFunctionCalls(1000000);
28  min->SetTolerance(0.001);
29  min->SetPrintLevel(printlevel);
30 
31  // create funciton wrapper for minmizer
32  // a IMultiGenFunction type
34 
35  // starting point
36  double variable[2] = { -1.,1.2};
37  //initial spet sizes
38  double step[2] = {0.01,0.01};
39 
40  min->SetFunction(f);
41 
42  // Set the free variables to be minimized!
43  min->SetVariable(0,"x",variable[0], step[0]);
44  min->SetVariable(1,"y",variable[1], step[1]);
45 
46  // do the minimization
47  min->Minimize();
48 
49  const double *xs = min->X();
50  std::cout << "Minimum: f(" << xs[0] << "," << xs[1] << "): "
51  << min->MinValue() << std::endl;
52 
53  // expected minimum is 0
54  if ( min->MinValue() < 1.E-4 && f(xs) < 1.E-4) {
55  std::cout << "Minuit2 - " << algoName
56  << " converged to the right minimum" << std::endl;
57  return 0;
58  }
59  else
60  std::cerr << "ERROR: Minuit2 - " << algoName
61  << " failed to converge !!!" << std::endl;
62 
63  return -1;
64 }
65 
66 int main(int argc, const char *argv[]) {
67 
68  int printLevel = 0;
69  std::string algoName = ""; // use default (i.e. migrad)
70 
71  // Parse command line arguments
72  for (int i = 1 ; i < argc ; i++) {
73  std::string arg = argv[i] ;
74 
75  if (arg == "-v") {
76  printLevel = 1;
77  }
78  else if (arg == "-vv") {
79  printLevel = 2;
80  }
81  else if (arg == "-vvv") {
82  printLevel = 3;
83  }
84  else if (arg == "-n") {
85  std::cout << "using method " << argv[i+1] << " to minimize" << std::endl;
86  algoName = argv[++i];
87  }
88  else if (arg == "-h") {
89  std::cout << "usage: demoMinimize [ options ] " << std::endl;
90  std::cout << "" << std::endl;
91  std::cout << " -n <algorithm> : use given algorithm (possible names: simplex, minimize, scan, fumili)" << std::endl;
92  std::cout << " -v : set minimul verbose mode to show final result" << std::endl;
93  std::cout << " -vv : set medium verbose mode: show function value and edm at each minimization step" << std::endl;
94  std::cout << " -vvv : set very verbose mode: show full result at each minimization step" << std::endl;
95  return 0;
96  }
97  }
98 
99  int iret = demoMinimizer(algoName.c_str(), printLevel);
100  return iret;
101 }
int main(int argc, const char *argv[])
double Rosenbrock(const double *x)
test of a pure minimization passing a user function This is an example of running Minuit2 using the M...
Documentation for class Functor class.
Definition: Functor.h:394
int demoMinimizer(const char *algoName, int printlevel)
Double_t x[n]
Definition: legend1.C:17
int printlevel
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2, Minuit, GSL, etc..) Plug-in&#39;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
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)=0
set the function to minimize
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
Definition: Minimizer.h:456
double f(double x)
Minuit2Minimizer class implementing the ROOT::Math::Minimizer interface for Minuit2 minimization algo...
void SetTolerance(double tol)
set the tolerance
Definition: Minimizer.h:462
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