Logo ROOT   6.10/09
Reference Guide
GSLSimAnMinimizer.cxx
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Author: L. Moneta Wed Dec 20 17:16:32 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Implementation file for class GSLSimAnMinimizer
12 
13 #include "Math/GSLSimAnMinimizer.h"
15 #include "Math/Error.h"
16 
18 #include "Math/MultiNumGradFunction.h" // needed to use transformation function
19 #include "Math/FitMethodFunction.h"
20 
21 #include <iostream>
22 #include <cassert>
23 
24 namespace ROOT {
25 
26  namespace Math {
27 
28 
29 
30 // GSLSimAnMinimizer implementation
31 
32 GSLSimAnMinimizer::GSLSimAnMinimizer( int /* ROOT::Math::EGSLSimAnMinimizerType type */ ) :
34 {
35  // Constructor implementation : create GSLMultiFit wrapper object
36 
37  SetMaxIterations(100);
38  SetPrintLevel(0);
39 }
40 
42 }
43 
44 
46  // set initial parameters of the minimizer
47  int debugLevel = PrintLevel();
48 
49  if (debugLevel >=1 ) std::cout <<"Minimize using GSLSimAnMinimizer " << std::endl;
50 
51  const ROOT::Math::IMultiGenFunction * function = ObjFunction();
52  if (function == 0) {
53  MATH_ERROR_MSG("GSLSimAnMinimizer::Minimize","Function has not been set");
54  return false;
55  }
56 
57  // vector of internal values (copied by default)
58  unsigned int npar = NPar();
59  std::vector<double> xvar;
60  std::vector<double> steps(StepSizes(),StepSizes()+npar);
61 
62  // needed for the transformation
63  MultiNumGradFunction * gradFunc = new MultiNumGradFunction( *function );
64  gradFunc->SetOwnership();
65 
66  MinimTransformFunction * trFunc = CreateTransformation(xvar, gradFunc );
67  // ObjFunction() will return now the new transformed function
68 
69  if (trFunc) {
70  // transform also the step sizes
71  trFunc->InvStepTransformation(X(), StepSizes(), &steps[0]);
72  steps.resize( trFunc->NDim() );
73  }
74 
75  assert (xvar.size() == steps.size() );
76 
77 
78 #ifdef DEBUG
79  for (unsigned int i = 0; i < npar ; ++i) {
80  std::cout << "x = " << xvar[i] << " steps " << steps[i] << " x " << X()[i] << std::endl;
81  }
82  std::cout << "f(x) = " << (*ObjFunction())(&xvar.front() ) << std::endl;
83  std::cout << "f(x) not transf = " << (*function)( X() ) << std::endl;
84  if (trFunc) std::cout << "ftrans(x) = " << (*trFunc) (&xvar.front() ) << std::endl;
85 #endif
86 
87  // output vector
88  std::vector<double> xmin(xvar.size() );
89 
90 
91  int iret = fSolver.Solve(*ObjFunction(), &xvar.front(), &steps.front(), &xmin[0], (debugLevel > 1) );
92 
93  SetMinValue( (*ObjFunction())(&xmin.front() ) );
94 
95  SetFinalValues(&xmin.front());
96 
97 
98  if (debugLevel >=1 ) {
99  if (iret == 0)
100  std::cout << "GSLSimAnMinimizer: Minimum Found" << std::endl;
101  else
102  std::cout << "GSLSimAnMinimizer: Error in solving" << std::endl;
103 
104  int pr = std::cout.precision(18);
105  std::cout << "FVAL = " << MinValue() << std::endl;
106  std::cout.precision(pr);
107  for (unsigned int i = 0; i < NDim(); ++i)
108  std::cout << VariableName(i) << "\t = " << X()[i] << std::endl;
109  }
110 
111 
112  return ( iret == 0) ? true : false;
113 }
114 
115 
116 unsigned int GSLSimAnMinimizer::NCalls() const {
117  // return number of function calls
120  if (tfunc) f = dynamic_cast<const ROOT::Math::MultiNumGradFunction *>(tfunc->OriginalFunction());
121  else
122  f = dynamic_cast<const ROOT::Math::MultiNumGradFunction *>(ObjFunction());
123  if (f) return f->NCalls();
124  return 0;
125 }
126 
127 
128  } // end namespace Math
129 
130 } // end namespace ROOT
131 
virtual const double * X() const
return pointer to X values at the minimum
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
Definition: Minimizer.h:451
float xmin
Definition: THbookFile.cxx:93
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
MinimTransformFunction class to perform a transformations on the variables to deal with fixed or limi...
Base Minimizer class, which defines the basic funcionality of various minimizer implementations (apar...
MinimTransformFunction * CreateTransformation(std::vector< double > &startValues, const ROOT::Math::IMultiGradFunction *func=0)
virtual double MinValue() const
return minimum function value
virtual bool Minimize()
method to perform the minimization
virtual std::string VariableName(unsigned int ivar) const
get name of variables (override if minimizer support storing of variable names)
int PrintLevel() const
minimizer configuration parameters
Definition: Minimizer.h:411
virtual const double * StepSizes() const
accessor methods
int Solve(const ROOT::Math::IMultiGenFunction &func, const double *x0, const double *scale, double *xmin, bool debug=false)
solve the simulated annealing given a multi-dim function, the initial vector parameters and a vector ...
void SetMinValue(double val)
virtual unsigned int NPar() const
total number of parameter defined
unsigned int NDim() const
Retrieve the dimension of the function.
virtual unsigned int NDim() const
number of dimensions
const ROOT::Math::IMultiGenFunction * ObjFunction() const
return pointer to used objective function
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:50
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
void SetFinalValues(const double *x)
const IMultiGradFunction * OriginalFunction() const
MultiNumGradFunction class to wrap a normal function in a gradient function using numerical gradient ...
GSLSimAnMinimizer(int type=0)
Default constructor.
double f(double x)
void InvStepTransformation(const double *x, const double *sext, double *sint) const
inverse transformation for steps (external -> internal) at external point x
virtual ~GSLSimAnMinimizer()
Destructor (no operations)
Namespace for new Math classes and functions.
ROOT::Math::GSLSimAnnealing fSolver
unsigned int NCalls() const
number of function calls to reach the minimum
void SetPrintLevel(int level)
set print level
Definition: Minimizer.h:445