00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef ROOT_Math_Minimizer
00014 #define ROOT_Math_Minimizer
00015
00016 #ifndef ROOT_Math_IFunction
00017 #include "Math/IFunction.h"
00018 #endif
00019
00020 #ifndef ROOT_Math_MinimizerOptions
00021 #include "Math/MinimizerOptions.h"
00022 #endif
00023
00024
00025 #include <vector>
00026 #include <string>
00027
00028 #include <limits>
00029 #include <cmath>
00030
00031
00032 namespace ROOT {
00033
00034
00035 namespace Math {
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 class Minimizer {
00074
00075 public:
00076
00077
00078
00079
00080 Minimizer () :
00081 fValidError(false),
00082 fDebug(MinimizerOptions::DefaultPrintLevel()),
00083 fStrategy(MinimizerOptions::DefaultStrategy()),
00084 fStatus(-1),
00085 fMaxCalls(MinimizerOptions::DefaultMaxFunctionCalls()),
00086 fMaxIter(MinimizerOptions::DefaultMaxIterations()),
00087 fTol(MinimizerOptions::DefaultTolerance()),
00088 fPrec(MinimizerOptions::DefaultPrecision()),
00089 fUp(MinimizerOptions::DefaultErrorDef() )
00090 {}
00091
00092
00093
00094
00095 virtual ~Minimizer () {}
00096
00097
00098
00099
00100 private:
00101
00102
00103
00104
00105
00106 Minimizer(const Minimizer &) {}
00107
00108
00109
00110
00111 Minimizer & operator = (const Minimizer & rhs) {
00112 if (this == &rhs) return *this;
00113 return *this;
00114 }
00115
00116 public:
00117
00118
00119 virtual void Clear() {}
00120
00121
00122 virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func) = 0;
00123
00124
00125 virtual void SetFunction(const ROOT::Math::IMultiGradFunction & func)
00126 {
00127 SetFunction(static_cast<const ::ROOT::Math::IMultiGenFunction &> (func));
00128 }
00129
00130
00131
00132 template<class VariableIterator>
00133 int SetVariables(const VariableIterator & begin, const VariableIterator & end) {
00134 unsigned int ivar = 0;
00135 for ( VariableIterator vitr = begin; vitr != end; ++vitr) {
00136 bool iret = false;
00137 if (vitr->IsFixed() )
00138 iret = SetFixedVariable(ivar, vitr->Name(), vitr->Value() );
00139 else if (vitr->IsDoubleBound() )
00140 iret = SetLimitedVariable(ivar, vitr->Name(), vitr->Value(), vitr->StepSize(), vitr->LowerLimit(), vitr->UpperLimit() );
00141 else if (vitr->HasLowerLimit() )
00142 iret = SetLowerLimitedVariable(ivar, vitr->Name(), vitr->Value(), vitr->StepSize(), vitr->LowerLimit() );
00143 else if (vitr->HasUpperLimit() )
00144 iret = SetUpperLimitedVariable(ivar, vitr->Name(), vitr->Value(), vitr->StepSize(), vitr->UpperLimit() );
00145 else
00146 iret = SetVariable( ivar, vitr->Name(), vitr->Value(), vitr->StepSize() );
00147
00148 if (iret) ivar++;
00149
00150
00151 }
00152 return ivar;
00153 }
00154
00155 virtual bool SetVariable(unsigned int ivar, const std::string & name, double val, double step) = 0;
00156
00157 virtual bool SetLowerLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double lower ) {
00158 return SetLimitedVariable(ivar, name, val, step, lower, std::numeric_limits<double>::infinity() );
00159 }
00160
00161 virtual bool SetUpperLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double upper ) {
00162 return SetLimitedVariable(ivar, name, val, step, - std::numeric_limits<double>::infinity(), upper );
00163 }
00164
00165 virtual bool SetLimitedVariable(unsigned int , const std::string & , double , double , double , double ) {
00166 return false;
00167 }
00168
00169 virtual bool SetFixedVariable(unsigned int , const std::string & , double ) {
00170 return false;
00171 }
00172
00173 virtual bool SetVariableValue(unsigned int , double ) { return false; }
00174
00175 virtual bool SetVariableValues(const double * x) {
00176 bool ret = true;
00177 unsigned int i = 0;
00178 while ( i <= NDim() && ret) {
00179 SetVariableValue(i,x[i] ); i++;
00180 }
00181 return ret;
00182 }
00183
00184
00185 virtual bool Minimize() = 0;
00186
00187
00188 virtual double MinValue() const = 0;
00189
00190
00191 virtual double Edm() const = 0;
00192
00193
00194 virtual const double * X() const = 0;
00195
00196
00197 virtual const double * MinGradient() const = 0;
00198
00199
00200 virtual unsigned int NCalls() const = 0;
00201
00202
00203
00204 virtual unsigned int NDim() const = 0;
00205
00206
00207
00208 virtual unsigned int NFree() const = 0;
00209
00210
00211 virtual bool ProvidesError() const = 0;
00212
00213
00214 virtual const double * Errors() const = 0;
00215
00216
00217
00218
00219
00220 virtual double CovMatrix(unsigned int i, unsigned int j) const = 0;
00221
00222
00223
00224
00225 virtual int CovMatrixStatus() const { return 0; }
00226
00227
00228
00229
00230
00231 virtual double Correlation(unsigned int i, unsigned int j ) const {
00232 double tmp = CovMatrix(i,i) * CovMatrix(j,j);
00233 return ( tmp < 0) ? 0 : CovMatrix(i,j) / std::sqrt( tmp );
00234 }
00235
00236
00237
00238
00239
00240
00241
00242
00243 virtual double GlobalCC(unsigned int ) const { return -1; }
00244
00245
00246
00247
00248
00249
00250
00251 virtual bool GetMinosError(unsigned int , double & errLow, double & errUp, int = 0) {
00252 errLow = 0; errUp = 0;
00253 return false;
00254 }
00255
00256
00257
00258
00259 virtual bool Hesse() { return false; }
00260
00261
00262
00263
00264
00265 virtual bool Scan(unsigned int , unsigned int & , double * , double * ,
00266 double = 0, double = 0) {
00267 return false;
00268 }
00269
00270
00271
00272
00273
00274 virtual bool Contour(unsigned int , unsigned int , unsigned int &,
00275 double * , double * ) {
00276 return false;
00277 }
00278
00279
00280
00281
00282
00283 virtual void PrintResults() {}
00284
00285
00286
00287 virtual std::string VariableName(unsigned int ) const { return std::string();}
00288
00289
00290
00291 virtual int VariableIndex(const std::string &) const { return -1; }
00292
00293
00294
00295
00296 int PrintLevel() const { return fDebug; }
00297
00298
00299 unsigned int MaxFunctionCalls() const { return fMaxCalls; }
00300
00301
00302 unsigned int MaxIterations() const { return fMaxIter; }
00303
00304
00305 double Tolerance() const { return fTol; }
00306
00307
00308
00309 double Precision() const { return fPrec; }
00310
00311
00312 int Strategy() const { return fStrategy; }
00313
00314
00315 int Status() const { return fStatus; }
00316
00317
00318
00319 double ErrorDef() const { return fUp; }
00320
00321
00322 bool IsValidError() const { return fValidError; }
00323
00324
00325 virtual MinimizerOptions Options() const {
00326 MinimizerOptions opt;
00327 opt.SetPrintLevel(fDebug);
00328 opt.SetStrategy(fStrategy);
00329 opt.SetMaxFunctionCalls(fMaxCalls);
00330 opt.SetMaxIterations(fMaxIter);
00331 opt.SetTolerance(fTol);
00332 opt.SetPrecision(fPrec);
00333 opt.SetErrorDef(fUp);
00334 return opt;
00335 }
00336
00337
00338 void SetPrintLevel(int level) { fDebug = level; }
00339
00340
00341 void SetMaxFunctionCalls(unsigned int maxfcn) { if (maxfcn > 0) fMaxCalls = maxfcn; }
00342
00343
00344 void SetMaxIterations(unsigned int maxiter) { if (maxiter > 0) fMaxIter = maxiter; }
00345
00346
00347 void SetTolerance(double tol) { fTol = tol; }
00348
00349
00350
00351 void SetPrecision(double prec) { fPrec = prec; }
00352
00353
00354 void SetStrategy(int strategyLevel) { fStrategy = strategyLevel; }
00355
00356
00357 void SetErrorDef(double up) { fUp = up; }
00358
00359
00360 void SetValidError(bool on) { fValidError = on; }
00361
00362
00363 void SetOptions(const MinimizerOptions & opt) {
00364 fDebug = opt.PrintLevel();
00365 fStrategy = opt.Strategy();
00366 fMaxCalls = opt.MaxFunctionCalls();
00367 fMaxIter = opt.MaxIterations();
00368 fTol = opt.Tolerance();
00369 fPrec = opt.Precision();
00370 fUp = opt.ErrorDef();
00371 }
00372
00373
00374 void SetDefaultOptions() {
00375 fDebug = MinimizerOptions::DefaultPrintLevel();
00376 fStrategy = MinimizerOptions::DefaultStrategy();
00377 fMaxCalls = MinimizerOptions::DefaultMaxFunctionCalls();
00378 fMaxIter = MinimizerOptions::DefaultMaxIterations();
00379 fTol = MinimizerOptions::DefaultTolerance();
00380 fPrec = MinimizerOptions::DefaultPrecision();
00381 fUp = MinimizerOptions::DefaultErrorDef();
00382 }
00383
00384 protected:
00385
00386
00387
00388
00389
00390
00391
00392
00393 bool fValidError;
00394 int fDebug;
00395 int fStrategy;
00396 int fStatus;
00397 unsigned int fMaxCalls;
00398 unsigned int fMaxIter;
00399 double fTol;
00400 double fPrec;
00401 double fUp;
00402
00403 };
00404
00405 }
00406
00407 }
00408
00409
00410 #endif