File indexing completed on 2025-01-18 10:10:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef ROOT_Math_Minimizer
0014 #define ROOT_Math_Minimizer
0015
0016 #include "Math/IFunction.h"
0017 #include "Math/MinimizerOptions.h"
0018
0019 #include <string>
0020 #include <limits>
0021 #include <cmath>
0022 #include <vector>
0023 #include <functional>
0024
0025
0026
0027 namespace ROOT {
0028
0029 namespace Fit {
0030 class ParameterSettings;
0031 }
0032
0033
0034 namespace Math {
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117 class Minimizer {
0118
0119 public:
0120
0121
0122
0123
0124 Minimizer () :
0125 fValidError(false),
0126 fStatus(-1)
0127 {}
0128
0129
0130
0131
0132 virtual ~Minimizer () {}
0133
0134
0135
0136
0137 private:
0138
0139
0140
0141
0142
0143 Minimizer(const Minimizer &) {}
0144
0145
0146
0147
0148 Minimizer & operator = (const Minimizer & rhs) {
0149 if (this == &rhs) return *this;
0150 return *this;
0151 }
0152
0153 public:
0154
0155
0156 virtual void Clear() {}
0157
0158
0159 virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func) = 0;
0160
0161
0162 virtual void SetHessianFunction(std::function<bool(const std::vector<double> &, double *)> ) {}
0163
0164
0165 template<class VariableIterator>
0166 int SetVariables(const VariableIterator & begin, const VariableIterator & end) {
0167 unsigned int ivar = 0;
0168 for ( VariableIterator vitr = begin; vitr != end; ++vitr) {
0169 bool iret = false;
0170 if (vitr->IsFixed() )
0171 iret = SetFixedVariable(ivar, vitr->Name(), vitr->Value() );
0172 else if (vitr->IsDoubleBound() )
0173 iret = SetLimitedVariable(ivar, vitr->Name(), vitr->Value(), vitr->StepSize(), vitr->LowerLimit(), vitr->UpperLimit() );
0174 else if (vitr->HasLowerLimit() )
0175 iret = SetLowerLimitedVariable(ivar, vitr->Name(), vitr->Value(), vitr->StepSize(), vitr->LowerLimit() );
0176 else if (vitr->HasUpperLimit() )
0177 iret = SetUpperLimitedVariable(ivar, vitr->Name(), vitr->Value(), vitr->StepSize(), vitr->UpperLimit() );
0178 else
0179 iret = SetVariable( ivar, vitr->Name(), vitr->Value(), vitr->StepSize() );
0180
0181 if (iret) ivar++;
0182
0183
0184 }
0185 return ivar;
0186 }
0187
0188 virtual bool SetVariable(unsigned int ivar, const std::string & name, double val, double step) = 0;
0189
0190 virtual bool SetLowerLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double lower ) {
0191 return SetLimitedVariable(ivar, name, val, step, lower, std::numeric_limits<double>::infinity() );
0192 }
0193
0194 virtual bool SetUpperLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double upper ) {
0195 return SetLimitedVariable(ivar, name, val, step, - std::numeric_limits<double>::infinity(), upper );
0196 }
0197 virtual bool SetLimitedVariable(unsigned int ivar , const std::string & name , double val , double step ,
0198 double lower , double upper );
0199 virtual bool SetFixedVariable(unsigned int ivar , const std::string & name , double val );
0200 virtual bool SetVariableValue(unsigned int ivar , double value);
0201
0202 virtual bool SetVariableValues(const double * x) {
0203 bool ret = true;
0204 unsigned int i = 0;
0205 while ( i <= NDim() && ret) {
0206 ret &= SetVariableValue(i,x[i] ); i++;
0207 }
0208 return ret;
0209 }
0210 virtual bool SetVariableStepSize(unsigned int ivar, double value );
0211 virtual bool SetVariableLowerLimit(unsigned int ivar, double lower);
0212 virtual bool SetVariableUpperLimit(unsigned int ivar, double upper);
0213
0214 virtual bool SetVariableLimits(unsigned int ivar, double lower, double upper) {
0215 return SetVariableLowerLimit(ivar,lower) && SetVariableUpperLimit(ivar,upper);
0216 }
0217 virtual bool FixVariable(unsigned int ivar);
0218 virtual bool ReleaseVariable(unsigned int ivar);
0219 virtual bool IsFixedVariable(unsigned int ivar) const;
0220 virtual bool GetVariableSettings(unsigned int ivar, ROOT::Fit::ParameterSettings & pars) const;
0221
0222
0223 virtual bool SetVariableInitialRange(unsigned int , double , double ) {
0224 return false;
0225 }
0226
0227
0228 virtual bool Minimize() = 0;
0229
0230
0231 virtual double MinValue() const = 0;
0232
0233
0234 virtual const double * X() const = 0;
0235
0236
0237 virtual double Edm() const { return -1; }
0238
0239
0240 virtual const double * MinGradient() const { return nullptr; }
0241
0242
0243 virtual unsigned int NCalls() const { return 0; }
0244
0245
0246 virtual unsigned int NIterations() const { return NCalls(); }
0247
0248
0249
0250 virtual unsigned int NDim() const = 0;
0251
0252
0253
0254
0255 virtual unsigned int NFree() const { return NDim(); }
0256
0257
0258 virtual bool ProvidesError() const { return false; }
0259
0260
0261 virtual const double * Errors() const { return nullptr; }
0262
0263 virtual double CovMatrix(unsigned int ivar , unsigned int jvar ) const;
0264 virtual bool GetCovMatrix(double * covMat) const;
0265 virtual bool GetHessianMatrix(double * hMat) const;
0266
0267
0268
0269
0270
0271 virtual int CovMatrixStatus() const {
0272 return 0;
0273 }
0274
0275
0276
0277
0278
0279 virtual double Correlation(unsigned int i, unsigned int j ) const {
0280 double tmp = CovMatrix(i,i) * CovMatrix(j,j);
0281 return ( tmp < 0) ? 0 : CovMatrix(i,j) / std::sqrt( tmp );
0282 }
0283
0284 virtual double GlobalCC(unsigned int ivar) const;
0285
0286 virtual bool GetMinosError(unsigned int ivar , double & errLow, double & errUp, int option = 0);
0287 virtual bool Hesse();
0288 virtual bool Scan(unsigned int ivar , unsigned int & nstep , double * x , double * y ,
0289 double xmin = 0, double xmax = 0);
0290 virtual bool Contour(unsigned int ivar , unsigned int jvar, unsigned int & npoints,
0291 double * xi , double * xj );
0292
0293
0294
0295
0296
0297 virtual void PrintResults() {}
0298
0299 virtual std::string VariableName(unsigned int ivar) const;
0300
0301 virtual int VariableIndex(const std::string & name) const;
0302
0303
0304
0305
0306 int PrintLevel() const { return fOptions.PrintLevel(); }
0307
0308
0309 unsigned int MaxFunctionCalls() const { return fOptions.MaxFunctionCalls(); }
0310
0311
0312 unsigned int MaxIterations() const { return fOptions.MaxIterations(); }
0313
0314
0315 double Tolerance() const { return fOptions.Tolerance(); }
0316
0317
0318
0319 double Precision() const { return fOptions.Precision(); }
0320
0321
0322 int Strategy() const { return fOptions.Strategy(); }
0323
0324
0325 int Status() const { return fStatus; }
0326
0327
0328 virtual int MinosStatus() const { return -1; }
0329
0330
0331
0332 double ErrorDef() const { return fOptions.ErrorDef(); }
0333
0334
0335 bool IsValidError() const { return fValidError; }
0336
0337
0338 virtual MinimizerOptions Options() const {
0339 return fOptions;
0340 }
0341
0342
0343 void SetPrintLevel(int level) { fOptions.SetPrintLevel(level); }
0344
0345
0346 void SetMaxFunctionCalls(unsigned int maxfcn) { if (maxfcn > 0) fOptions.SetMaxFunctionCalls(maxfcn); }
0347
0348
0349 void SetMaxIterations(unsigned int maxiter) { if (maxiter > 0) fOptions.SetMaxIterations(maxiter); }
0350
0351
0352 void SetTolerance(double tol) { fOptions.SetTolerance(tol); }
0353
0354
0355
0356 void SetPrecision(double prec) { fOptions.SetPrecision(prec); }
0357
0358
0359 void SetStrategy(int strategyLevel) { fOptions.SetStrategy(strategyLevel); }
0360
0361
0362 void SetErrorDef(double up) { fOptions.SetErrorDef(up); }
0363
0364
0365 void SetValidError(bool on) { fValidError = on; }
0366
0367
0368 void SetOptions(const MinimizerOptions & opt) {
0369 fOptions = opt;
0370 }
0371
0372
0373 void SetExtraOptions(const IOptions & extraOptions) { fOptions.SetExtraOptions(extraOptions); }
0374
0375
0376 void SetDefaultOptions() {
0377 fOptions.ResetToDefaultOptions();
0378 }
0379
0380 protected:
0381
0382
0383
0384
0385
0386
0387
0388
0389 bool fValidError;
0390 MinimizerOptions fOptions;
0391 int fStatus;
0392 };
0393
0394 }
0395
0396 }
0397
0398
0399 #endif