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}
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);
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}
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
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 transformation (int<->ext)
129
130 // need MnUserFcn 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 -
147 // interface based on FCNGradientBase (external/analytical gradients)
148 // Create in this case an AnalyticalGradient calculator
149 // Create the minuit FCN wrapper (MnUserFcn) containing the transformation (int<->ext)
150
151 MnUserFcn mfcn(fcn, st.Trafo());
152 std::unique_ptr<AnalyticalGradientCalculator> gc;
154 // std::cout << "-- ModularFunctionMinimizer::Minimize: Internal parameter space" << std::endl;
155 gc = std::unique_ptr<AnalyticalGradientCalculator>(new ExternalInternalGradientCalculator(fcn, st.Trafo()));
156 } else {
157 // std::cout << "-- ModularFunctionMinimizer::Minimize: External parameter space" << std::endl;
158 gc = std::make_unique<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 // compute seed (will use internally numerical gradient in case calculator does not implement g2 computations)
166 MinimumSeed mnseeds = SeedGenerator()(mfcn, *gc, st, strategy);
167 auto minimum = Minimize(mfcn, *gc, mnseeds, strategy, maxfcn, toler);
168
169 return minimum;
170}
171
173 const MinimumSeed &seed, const MnStrategy &strategy,
174 unsigned int maxfcn, double toler) const
175{
176 // Interface used by all the others for the minimization using the base MinimumBuilder class
177 // According to the contained type of MinimumBuilder the right type will be used
178
179 MnPrint print("ModularFunctionMinimizer");
180
181 const MinimumBuilder &mb = Builder();
182 // std::cout << typeid(&mb).Name() << std::endl;
183 double effective_toler = toler * mfcn.Up(); // scale tolerance with Up()
184 // avoid tolerance too smalls (than limits)
185 double eps = MnMachinePrecision().Eps2();
186 if (effective_toler < eps)
187 effective_toler = eps;
188
189 // check if maxfcn is already exhausted
190 // case already reached call limit
191 if (mfcn.NumOfCalls() >= maxfcn) {
192 print.Warn("Stop before iterating - call limit already exceeded");
193
194 return FunctionMinimum(seed, std::vector<MinimumState>(1, seed.State()), mfcn.Up(),
196 }
197
198 return mb.Minimum(mfcn, gc, seed, strategy, maxfcn, effective_toler);
199}
200
201} // namespace Minuit2
202
203} // namespace ROOT
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
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:135
API class for defining four levels of strategies: low (0), medium (1), high (2), very high (>=3); act...
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
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 override
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...