Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
BasicMinimizer.h
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Author: L. Moneta Oct 2012
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 
12 // Header file for class BasicMinimizer
13 
14 #ifndef ROOT_Math_BasicMinimizer
15 #define ROOT_Math_BasicMinimizer
16 
17 #include "Math/Minimizer.h"
18 
19 
20 #include "Math/IFunctionfwd.h"
21 
22 #include "Math/IParamFunctionfwd.h"
23 
25 
26 
27 #include <vector>
28 #include <map>
29 #include <string>
30 
31 
32 
33 namespace ROOT {
34 
35 namespace Math {
36 
37  class MinimTransformFunction;
38 
39 
40 
41 //_______________________________________________________________________________
42 /**
43  Base Minimizer class, which defines the basic funcionality of various minimizer
44  implementations (apart from Minuit and Minuit2)
45  It provides support for storing parameter values, step size,
46  parameter transofrmation etc.. in case real minimizer impelmentations do not provide
47  such functionality.
48  This is an internal class and should not be used directly by the user
49 
50  @ingroup MultiMin
51 */
52 
53 
54 class BasicMinimizer : public ROOT::Math::Minimizer {
55 
56 public:
57 
58  /**
59  Default constructor
60  */
61  BasicMinimizer ( );
62 
63 
64  /**
65  Destructor
66  */
67  virtual ~BasicMinimizer ();
68 
69 private:
70  // usually copying is non trivial, so we make this unaccessible
71 
72  /**
73  Copy constructor
74  */
75  BasicMinimizer(const BasicMinimizer &) : Minimizer() {}
76 
77  /**
78  Assignment operator
79  */
80  BasicMinimizer & operator = (const BasicMinimizer & rhs) {
81  if (this == &rhs) return *this; // time saving self-test
82  return *this;
83  }
84 
85 public:
86 
87  /// set the function to minimize
88  virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func);
89 
90  /// set gradient the function to minimize
91  virtual void SetFunction(const ROOT::Math::IMultiGradFunction & func);
92 
93  /// set free variable
94  virtual bool SetVariable(unsigned int ivar, const std::string & name, double val, double step);
95 
96 
97  /// set lower limit variable (override if minimizer supports them )
98  virtual bool SetLowerLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double lower );
99  /// set upper limit variable (override if minimizer supports them )
100  virtual bool SetUpperLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double upper );
101  /// set upper/lower limited variable (override if minimizer supports them )
102  virtual bool SetLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double /* lower */, double /* upper */);
103  /// set fixed variable (override if minimizer supports them )
104  virtual bool SetFixedVariable(unsigned int /* ivar */, const std::string & /* name */, double /* val */);
105  /// set the value of an existing variable
106  virtual bool SetVariableValue(unsigned int ivar, double val );
107  /// set the values of all existing variables (array must be dimensioned to the size of existing parameters)
108  virtual bool SetVariableValues(const double * x);
109  /// set the step size of an already existing variable
110  virtual bool SetVariableStepSize(unsigned int ivar, double step );
111  /// set the lower-limit of an already existing variable
112  virtual bool SetVariableLowerLimit(unsigned int ivar, double lower);
113  /// set the upper-limit of an already existing variable
114  virtual bool SetVariableUpperLimit(unsigned int ivar, double upper);
115  /// set the limits of an already existing variable
116  virtual bool SetVariableLimits(unsigned int ivar, double lower, double upper);
117  /// fix an existing variable
118  virtual bool FixVariable(unsigned int ivar);
119  /// release an existing variable
120  virtual bool ReleaseVariable(unsigned int ivar);
121  /// query if an existing variable is fixed (i.e. considered constant in the minimization)
122  /// note that by default all variables are not fixed
123  virtual bool IsFixedVariable(unsigned int ivar) const;
124  /// get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
125  virtual bool GetVariableSettings(unsigned int ivar, ROOT::Fit::ParameterSettings & varObj) const;
126  /// get name of variables (override if minimizer support storing of variable names)
127  virtual std::string VariableName(unsigned int ivar) const;
128  /// get index of variable given a variable given a name
129  /// return -1 if variable is not found
130  virtual int VariableIndex(const std::string & name) const;
131 
132  /// method to perform the minimization
133  virtual bool Minimize();
134 
135  /// return minimum function value
136  virtual double MinValue() const { return fMinVal; }
137 
138  /// return pointer to X values at the minimum
139  virtual const double * X() const { return &fValues.front(); }
140 
141  /// number of dimensions
142  virtual unsigned int NDim() const { return fDim; }
143 
144  /// number of free variables (real dimension of the problem)
145  virtual unsigned int NFree() const;
146 
147  /// total number of parameter defined
148  virtual unsigned int NPar() const { return fValues.size(); }
149 
150  /// return pointer to used objective function
151  const ROOT::Math::IMultiGenFunction * ObjFunction() const { return fObjFunc; }
152 
153  /// return pointer to used gradient object function (NULL if gradient is not supported)
154  const ROOT::Math::IMultiGradFunction * GradObjFunction() const;
155 
156  /// return transformation function (NULL if not having a transformation)
157  const ROOT::Math::MinimTransformFunction * TransformFunction() const;
158 
159  /// print result of minimization
160  void PrintResult() const;
161 
162  /// accessor methods
163  virtual const double * StepSizes() const { return &fSteps.front(); }
164 
165 protected:
166 
167  bool CheckDimension() const;
168 
169  bool CheckObjFunction() const;
170 
171  MinimTransformFunction * CreateTransformation(std::vector<double> & startValues, const ROOT::Math::IMultiGradFunction * func = 0);
172 
173  void SetFinalValues(const double * x);
174 
175  void SetMinValue(double val) { fMinVal = val; }
176 
177 private:
178 
179  // dimension of the function to be minimized
180  unsigned int fDim;
181 
182  const ROOT::Math::IMultiGenFunction * fObjFunc;
183 
184  double fMinVal;
185  std::vector<double> fValues;
186  std::vector<double> fSteps;
187  std::vector<std::string> fNames;
188  std::vector<ROOT::Math::EMinimVariableType> fVarTypes; // vector specifyng the type of variables
189  std::map< unsigned int, std::pair<double, double> > fBounds; // map specifying the bound using as key the parameter index
190 
191 };
192 
193  } // end namespace Fit
194 
195 } // end namespace ROOT
196 
197 
198 
199 #endif /* ROOT_Math_BasicMinimizer */