Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ModularFunctionMinimizer.cxx
Go to the documentation of this file.
1// @(#)root/minuit2:$Id$
2// Authors: M. Winkler, F. James, L. Moneta, A. Zsenei, E.G.P. Bos 2003-2017
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
7 * *
8 **********************************************************************/
9
16#include "Minuit2/MinimumSeed.h"
22#include "Minuit2/MnUserFcn.h"
23#include "Minuit2/FCNBase.h"
25#include "Minuit2/MnStrategy.h"
26#include "Minuit2/MnHesse.h"
29#include "Minuit2/MnPrint.h"
30
31namespace ROOT {
32
33namespace Minuit2 {
34
35// #include "Minuit2/MnUserParametersPrint.h"
36
37FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNBase &fcn, const std::vector<double> &par,
38 const std::vector<double> &err, unsigned int stra,
39 unsigned int maxfcn, double toler) const
40{
41 // minimize from FCNBase and std::vector of double's for parameter values and errors (step sizes)
42 MnUserParameterState st(par, err);
43 MnStrategy strategy(stra);
44 return Minimize(fcn, st, strategy, maxfcn, toler);
45}
46
47FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNGradientBase &fcn, const std::vector<double> &par,
48 const std::vector<double> &err, unsigned int stra,
49 unsigned int maxfcn, double toler) const
50{
51 // minimize from FCNGradientBase (use analytical gradient provided in FCN)
52 // and std::vector of double's for parameter values and errors (step sizes)
53 MnUserParameterState st(par, err);
54 MnStrategy strategy(stra);
55 return Minimize(fcn, st, strategy, maxfcn, toler);
56}
57
58// move nrow before cov to avoid ambiguities when using default parameters
59FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNBase &fcn, const std::vector<double> &par,
60 unsigned int nrow, const std::vector<double> &cov, unsigned int stra,
61 unsigned int maxfcn, double toler) const
62{
63 // minimize from FCNBase using std::vector for parameter error and
64 // an std::vector of size n*(n+1)/2 for the covariance matrix and n (rank of cov matrix)
65
66 MnUserParameterState st(par, cov, nrow);
67 MnStrategy strategy(stra);
68 return Minimize(fcn, st, strategy, maxfcn, toler);
69}
70
71FunctionMinimum ModularFunctionMinimizer::Minimize(const FCNGradientBase &fcn, const std::vector<double> &par,
72 unsigned int nrow, const std::vector<double> &cov, unsigned int stra,
73 unsigned int maxfcn, double toler) const
74{
75 // minimize from FCNGradientBase (use analytical gradient provided in FCN)
76 // using std::vector for parameter error and
77 // an std::vector of size n*(n+1)/2 for the covariance matrix and n (rank of cov matrix)
78
79 MnUserParameterState st(par, cov, nrow);
80 MnStrategy strategy(stra);
81 return Minimize(fcn, st, strategy, maxfcn, toler);
82}
83
85 const MnStrategy &strategy, unsigned int maxfcn, double toler) const
86{
87 // minimize from FCNBase and MnUserParameters object
88
89 MnUserParameterState st(upar);
90 return Minimize(fcn, st, strategy, maxfcn, toler);
91}
92
94 const MnStrategy &strategy, unsigned int maxfcn, double toler) const
95{
96 // minimize from FCNGradientBase (use analytical gradient provided in FCN) and MnUserParameters object
97
98 MnUserParameterState st(upar);
99 return Minimize(fcn, st, strategy, maxfcn, toler);
100}
101
103 const MnUserCovariance &cov, const MnStrategy &strategy,
104 unsigned int maxfcn, double toler) const
105{
106 // minimize from FCNBase and MnUserParameters and MnUserCovariance objects
107
108 MnUserParameterState st(upar, cov);
109 return Minimize(fcn, st, strategy, maxfcn, toler);
110}
111
113 const MnUserCovariance &cov, const MnStrategy &strategy,
114 unsigned int maxfcn, double toler) const
115{
116 // minimize from FCNGradientBase (use analytical gradient provided in FCN) and
117 // MnUserParameters MnUserCovariance objects
118
119 MnUserParameterState st(upar, cov);
120 return Minimize(fcn, st, strategy, maxfcn, toler);
121}
122
124 const MnStrategy &strategy, unsigned int maxfcn, double toler) const
125{
126 // minimize from a FCNBase and a MnUserparameterState - interface used by all the previous ones
127 // based on FCNBase. Create in this case a NumericalGradient calculator
128 // Create the minuit FCN wrapper (MnUserFcn) containing the trasformation (int<->ext)
129
130 // neeed MnUsserFcn for difference int-ext parameters
131 MnUserFcn mfcn(fcn, st.Trafo());
132 Numerical2PGradientCalculator gc(mfcn, st.Trafo(), strategy);
133
134 unsigned int npar = st.VariableParameters();
135 if (maxfcn == 0)
136 maxfcn = 200 + 100 * npar + 5 * npar * npar;
137 MinimumSeed mnseeds = SeedGenerator()(mfcn, gc, st, strategy);
138
139 return Minimize(mfcn, gc, mnseeds, strategy, maxfcn, toler);
140}
141
142// use Gradient here
144 const MnStrategy &strategy, unsigned int maxfcn, double toler) const
145{
146 // minimize from a FCNGradientBase and a MnUserparameterState - interface used by all the previous ones
147 // based on FCNGradientBase.
148 // Create in this acase an AnalyticalGradient calculator
149 // Create the minuit FCN wrapper (MnUserFcn) containing the trasformation (int<->ext)
150
151 MnUserFcn mfcn(fcn, st.Trafo());
154 // std::cout << "-- ModularFunctionMinimizer::Minimize: Internal parameter space" << std::endl;
155 gc = new ExternalInternalGradientCalculator(fcn, st.Trafo());
156 } else {
157 // std::cout << "-- ModularFunctionMinimizer::Minimize: External parameter space" << std::endl;
158 gc = new AnalyticalGradientCalculator(fcn, st.Trafo());
159 }
160
161 unsigned int npar = st.VariableParameters();
162 if (maxfcn == 0)
163 maxfcn = 200 + 100 * npar + 5 * npar * npar;
164
165 // use numerical gradient to compute initial derivatives for SeedGenerator
166 Numerical2PGradientCalculator numgc(mfcn, st.Trafo(), strategy);
167 MinimumSeed mnseeds = SeedGenerator()(mfcn, numgc, st, strategy);
168
169 auto minimum = Minimize(mfcn, *gc, mnseeds, strategy, maxfcn, toler);
170
171 delete gc;
172
173 return minimum;
174}
175
177 const MinimumSeed &seed, const MnStrategy &strategy,
178 unsigned int maxfcn, double toler) const
179{
180 // Interface used by all the others for the minimization using the base MinimumBuilder class
181 // According to the contained type of MinimumBuilder the right type will be used
182
183 MnPrint print("ModularFunctionMinimizer");
184
185 const MinimumBuilder &mb = Builder();
186 // std::cout << typeid(&mb).Name() << std::endl;
187 double effective_toler = toler * mfcn.Up(); // scale tolerance with Up()
188 // avoid tolerance too smalls (than limits)
189 double eps = MnMachinePrecision().Eps2();
190 if (effective_toler < eps)
191 effective_toler = eps;
192
193 // check if maxfcn is already exhausted
194 // case already reached call limit
195 if (mfcn.NumOfCalls() >= maxfcn) {
196 print.Warn("Stop before iterating - call limit already exceeded");
197
198 return FunctionMinimum(seed, std::vector<MinimumState>(1, seed.State()), mfcn.Up(),
200 }
201
202 return mb.Minimum(mfcn, gc, seed, strategy, maxfcn, effective_toler);
203}
204
205} // namespace Minuit2
206
207} // namespace ROOT
Similar to the AnalyticalGradientCalculator, the ExternalInternalGradientCalculator supplies Minuit w...
Interface (abstract class) defining the function to be minimized, which has to be implemented by the ...
Definition FCNBase.h:45
virtual GradientParameterSpace gradParameterSpace() const
class holding the full result of the minimization; both internal and external (MnUserParameterState) ...
interface class for gradient calculators
virtual FunctionMinimum Minimum(const MnFcn &, const GradientCalculator &, const MinimumSeed &, const MnStrategy &, unsigned int, double) const =0
const MinimumState & State() const
Definition MinimumSeed.h:28
Wrapper class to FCNBase interface used internally by Minuit.
Definition MnFcn.h:30
double Up() const
Definition MnFcn.cxx:39
unsigned int NumOfCalls() const
Definition MnFcn.h:39
Sets the relative floating point (double) arithmetic precision.
double Eps2() const
eps2 returns 2*sqrt(eps)
void Warn(const Ts &... args)
Definition MnPrint.h:126
API class for defining three levels of strategies: low (0), medium (1), high (>=2); acts on: Migrad (...
Definition MnStrategy.h:27
Class containing the covariance matrix data represented as a vector of size n*(n+1)/2 Used to hide in...
Wrapper used by Minuit of FCN interface containing a reference to the transformation object.
Definition MnUserFcn.h:25
class which holds the external user and/or internal Minuit representation of the parameters and error...
const MnUserTransformation & Trafo() const
API class for the user interaction with the parameters; serves as input to the minimizer as well as o...
virtual const MinimumBuilder & Builder() const =0
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
virtual const MinimumSeedGenerator & SeedGenerator() const =0
class performing the numerical gradient calculation
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...