Logo ROOT   6.13/01
Reference Guide
ModularFunctionMinimizer.cxx
Go to the documentation of this file.
1 // @(#)root/minuit2:$Id$
2 // Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
7  * *
8  **********************************************************************/
9 
14 #include "Minuit2/MinimumBuilder.h"
15 #include "Minuit2/MinimumSeed.h"
21 #include "Minuit2/MnUserFcn.h"
22 #include "Minuit2/FCNBase.h"
24 #include "Minuit2/MnStrategy.h"
25 #include "Minuit2/MnHesse.h"
26 #include "Minuit2/MnLineSearch.h"
28 
29 #if defined(DEBUG) || defined(WARNINGMSG)
30 #include "Minuit2/MnPrint.h"
31 #endif
32 
33 
34 namespace ROOT {
35 
36  namespace Minuit2 {
37 
38 
39 // #include "Minuit2/MnUserParametersPrint.h"
40 
41 FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNBase& fcn, const std::vector<double>& par, const std::vector<double>& err, unsigned int stra, unsigned int maxfcn, double toler) const {
42  // minimize from FCNBase and std::vector of double's for parameter values and errors (step sizes)
43  MnUserParameterState st(par, err);
44  MnStrategy strategy(stra);
45  return Minimize(fcn, st, strategy, maxfcn, toler);
46 }
47 
48 FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNGradientBase& fcn, const std::vector<double>& par, const std::vector<double>& err, unsigned int stra, unsigned int maxfcn, double toler) const {
49  // minimize from FCNGradientBase (use analytical gradient provided in FCN)
50  // and std::vector of double's for parameter values and errors (step sizes)
51  MnUserParameterState st(par, err);
52  MnStrategy strategy(stra);
53  return Minimize(fcn, st, strategy, maxfcn, toler);
54 }
55 
56 // move nrow before cov to avoid ambiguities when using default parameters
57 FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNBase& fcn, const std::vector<double>& par, unsigned int nrow, const std::vector<double>& cov, unsigned int stra, unsigned int maxfcn, double toler) const {
58  // minimize from FCNBase using std::vector for parameter error and
59  // an std::vector of size n*(n+1)/2 for the covariance matrix and n (rank of cov matrix)
60 
61  MnUserParameterState st(par, cov, nrow);
62  MnStrategy strategy(stra);
63  return Minimize(fcn, st, strategy, maxfcn, toler);
64 }
65 
66 FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNGradientBase& fcn, const std::vector<double>& par, unsigned int nrow, const std::vector<double>& cov, unsigned int stra, unsigned int maxfcn, double toler) const {
67  // minimize from FCNGradientBase (use analytical gradient provided in FCN)
68  // using std::vector for parameter error and
69  // an std::vector of size n*(n+1)/2 for the covariance matrix and n (rank of cov matrix)
70 
71  MnUserParameterState st(par, cov, nrow);
72  MnStrategy strategy(stra);
73  return Minimize(fcn, st, strategy, maxfcn, toler);
74 }
75 
76 FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNBase& fcn, const MnUserParameters& upar, const MnStrategy& strategy, unsigned int maxfcn, double toler) const {
77  // minimize from FCNBase and MnUserParameters object
78 
79  MnUserParameterState st(upar);
80  return Minimize(fcn, st, strategy, maxfcn, toler);
81 }
82 
83 FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNGradientBase& fcn, const MnUserParameters& upar, const MnStrategy& strategy, unsigned int maxfcn, double toler) const {
84  // minimize from FCNGradientBase (use analytical gradient provided in FCN) and MnUserParameters object
85 
86  MnUserParameterState st(upar);
87  return Minimize(fcn, st, strategy, maxfcn, toler);
88 }
89 
90 FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNBase& fcn, const MnUserParameters& upar, const MnUserCovariance& cov, const MnStrategy& strategy, unsigned int maxfcn, double toler) const {
91  // minimize from FCNBase and MnUserParameters and MnUserCovariance objects
92 
93  MnUserParameterState st(upar, cov);
94  return Minimize(fcn, st, strategy, maxfcn, toler);
95 }
96 
97 FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNGradientBase& fcn, const MnUserParameters& upar, const MnUserCovariance& cov, const MnStrategy& strategy, unsigned int maxfcn, double toler) const {
98  // minimize from FCNGradientBase (use analytical gradient provided in FCN) and
99  // MnUserParameters MnUserCovariance objects
100 
101  MnUserParameterState st(upar, cov);
102  return Minimize(fcn, st, strategy, maxfcn, toler);
103 }
104 
105 
106 
107 FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNBase& fcn, const MnUserParameterState& st, const MnStrategy& strategy, unsigned int maxfcn, double toler) const {
108  // minimize from a FCNBase and a MnUserparameterState - interface used by all the previous ones
109  // based on FCNBase. Create in this case a NumericalGradient calculator
110  // Create the minuit FCN wrapper (MnUserFcn) containing the trasformation (int<->ext)
111 
112  // neeed MnUsserFcn for difference int-ext parameters
113  MnUserFcn mfcn(fcn, st.Trafo() );
114  Numerical2PGradientCalculator gc(mfcn, st.Trafo(), strategy);
115 
116  unsigned int npar = st.VariableParameters();
117  if(maxfcn == 0) maxfcn = 200 + 100*npar + 5*npar*npar;
118  MinimumSeed mnseeds = SeedGenerator()(mfcn, gc, st, strategy);
119 
120  return Minimize(mfcn, gc, mnseeds, strategy, maxfcn, toler);
121 }
122 
123 
124 // use Gradient here
125 FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNGradientBase& fcn, const MnUserParameterState& st, const MnStrategy& strategy, unsigned int maxfcn, double toler) const {
126  // minimize from a FCNGradientBase and a MnUserparameterState - interface used by all the previous ones
127  // based on FCNGradientBase.
128  // Create in this acase an AnalyticalGradient calculator
129  // Create the minuit FCN wrapper (MnUserFcn) containing the trasformation (int<->ext)
130 
131  MnUserFcn mfcn(fcn, st.Trafo());
132  AnalyticalGradientCalculator gc(fcn, st.Trafo());
133 
134  unsigned int npar = st.VariableParameters();
135  if(maxfcn == 0) maxfcn = 200 + 100*npar + 5*npar*npar;
136 
137  MinimumSeed mnseeds = SeedGenerator()(mfcn, gc, st, strategy);
138 
139  return Minimize(mfcn, gc, mnseeds, strategy, maxfcn, toler);
140 }
141 
142 
143 FunctionMinimum ModularFunctionMinimizer::Minimize(const MnFcn& mfcn, const GradientCalculator& gc, const MinimumSeed& seed, const MnStrategy& strategy, unsigned int maxfcn, double toler) const {
144  // Interface used by all the others for the minimization using the base MinimumBuilder class
145  // According to the contained type of MinimumBuilder the right type will be used
146 
147  const MinimumBuilder & mb = Builder();
148  //std::cout << typeid(&mb).Name() << std::endl;
149  double effective_toler = toler * mfcn.Up(); // scale tolerance with Up()
150  // avoid tolerance too smalls (than limits)
151  double eps = MnMachinePrecision().Eps2();
152  if (effective_toler < eps) effective_toler = eps;
153 
154  // check if maxfcn is already exhausted
155  // case already reached call limit
156  if(mfcn.NumOfCalls() >= maxfcn) {
157 #ifdef WARNINGMSG
158  MN_INFO_MSG("ModularFunctionMinimizer: Stop before iterating - call limit already exceeded");
159 #endif
160  return FunctionMinimum(seed, std::vector<MinimumState>(1, seed.State()), mfcn.Up(), FunctionMinimum::MnReachedCallLimit());
161  }
162 
163 
164 
165 
166  return mb.Minimum(mfcn, gc, seed, strategy, maxfcn, effective_toler);
167 }
168 
169 
170 
171 
172  } // namespace Minuit2
173 
174 } // namespace ROOT
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
MinimumSeed contains the starting values for the minimization produced by the SeedGenerator.
Definition: MinimumSeed.h:31
double Up() const
Definition: MnFcn.cxx:35
determines the relative floating point arithmetic precision.
unsigned int NumOfCalls() const
Definition: MnFcn.h:43
#define MN_INFO_MSG(str)
Definition: MnPrint.h:110
const MinimumState & State() const
Definition: MinimumSeed.h:46
class performing the numerical gradient calculation
virtual const MinimumSeedGenerator & SeedGenerator() const =0
Extension of the FCNBase for providing the analytical Gradient of the function.
class holding the full result of the minimization; both internal and external (MnUserParameterState) ...
Wrapper class to FCNBase interface used internally by Minuit.
Definition: MnFcn.h:33
const MnUserTransformation & Trafo() const
Interface (abstract class) defining the function to be minimized, which has to be implemented by the ...
Definition: FCNBase.h:47
class which holds the external user and/or internal Minuit representation of the parameters and error...
Wrapper used by Minuit of FCN interface containing a reference to the transformation object...
Definition: MnUserFcn.h:26
API class for the user interaction with the parameters; serves as input to the minimizer as well as o...
virtual FunctionMinimum Minimize(const FCNBase &, const std::vector< double > &, const std::vector< double > &, unsigned int stra=1, unsigned int maxfcn=0, double toler=0.1) const
double Eps2() const
eps2 returns 2*sqrt(eps)
virtual FunctionMinimum Minimum(const MnFcn &, const GradientCalculator &, const MinimumSeed &, const MnStrategy &, unsigned int, double) const =0
API class for defining three levels of strategies: low (0), medium (1), high (>=2); acts on: Migrad (...
Definition: MnStrategy.h:27
interface class for gradient calculators
virtual const MinimumBuilder & Builder() const =0
Class containing the covariance matrix data represented as a vector of size n*(n+1)/2 Used to hide in...