Logo ROOT  
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 #include "Math/GenAlgoOptions.h"
21 
22 #include <iostream>
23 #include <cassert>
24 
25 namespace ROOT {
26 
27  namespace Math {
28 
29 
30 
31 // GSLSimAnMinimizer implementation
32 
33 GSLSimAnMinimizer::GSLSimAnMinimizer( int /* ROOT::Math::EGSLSimAnMinimizerType type */ ) :
34  BasicMinimizer()
35 {
36  // Constructor implementation : create GSLMultiFit wrapper object
37 
38  SetMaxIterations(100);
39  SetPrintLevel(0);
40 }
41 
43 }
44 
45 
47  // set initial parameters of the minimizer
48  int debugLevel = PrintLevel();
49 
50  if (debugLevel >=1 ) std::cout <<"Minimize using GSLSimAnMinimizer " << std::endl;
51 
53  if (function == 0) {
54  MATH_ERROR_MSG("GSLSimAnMinimizer::Minimize","Function has not been set");
55  return false;
56  }
57 
58  // vector of internal values (copied by default)
59  unsigned int npar = NPar();
60  std::vector<double> xvar;
61  std::vector<double> steps(StepSizes(),StepSizes()+npar);
62 
63  // needed for the transformation
64  MultiNumGradFunction * gradFunc = new MultiNumGradFunction( *function );
65  gradFunc->SetOwnership();
66 
67  MinimTransformFunction * trFunc = CreateTransformation(xvar, gradFunc );
68  // ObjFunction() will return now the new transformed function
69 
70  if (trFunc) {
71  // transform also the step sizes
72  trFunc->InvStepTransformation(X(), StepSizes(), &steps[0]);
73  steps.resize( trFunc->NDim() );
74  }
75 
76  assert (xvar.size() == steps.size() );
77 
78 
79 #ifdef DEBUG
80  for (unsigned int i = 0; i < npar ; ++i) {
81  std::cout << "x = " << xvar[i] << " steps " << steps[i] << " x " << X()[i] << std::endl;
82  }
83  std::cout << "f(x) = " << (*ObjFunction())(&xvar.front() ) << std::endl;
84  std::cout << "f(x) not transf = " << (*function)( X() ) << std::endl;
85  if (trFunc) std::cout << "ftrans(x) = " << (*trFunc) (&xvar.front() ) << std::endl;
86 #endif
87 
88  // output vector
89  std::vector<double> xmin(xvar.size() );
90 
91 
92  int iret = fSolver.Solve(*ObjFunction(), &xvar.front(), &steps.front(), &xmin[0], (debugLevel > 1) );
93 
94  SetMinValue( (*ObjFunction())(&xmin.front() ) );
95 
96  SetFinalValues(&xmin.front());
97 
98 
99  if (debugLevel >=1 ) {
100  if (iret == 0)
101  std::cout << "GSLSimAnMinimizer: Minimum Found" << std::endl;
102  else
103  std::cout << "GSLSimAnMinimizer: Error in solving" << std::endl;
104 
105  int pr = std::cout.precision(18);
106  std::cout << "FVAL = " << MinValue() << std::endl;
107  std::cout.precision(pr);
108  for (unsigned int i = 0; i < NDim(); ++i)
109  std::cout << VariableName(i) << "\t = " << X()[i] << std::endl;
110  }
111 
112 
113  return ( iret == 0) ? true : false;
114 }
115 
116 
117 unsigned int GSLSimAnMinimizer::NCalls() const {
118  // return number of function calls
121  if (tfunc) f = dynamic_cast<const ROOT::Math::MultiNumGradFunction *>(tfunc->OriginalFunction());
122  else
123  f = dynamic_cast<const ROOT::Math::MultiNumGradFunction *>(ObjFunction());
124  if (f) return f->NCalls();
125  return 0;
126 }
127 
130  opt.SetMinimizerType("GSLSimAn");
131  // set dummy values since those are not used
132  opt.SetTolerance(-1);
133  opt.SetPrintLevel(0);
134  opt.SetMaxIterations(-1);
135  opt.SetMaxFunctionCalls(0);
136  opt.SetStrategy(-1);
137  opt.SetErrorDef(0);
138  opt.SetPrecision(0);
139  opt.SetMinimizerAlgorithm("");
140 
141  const GSLSimAnParams & params = MinimizerParameters();
142 
144  simanOpt.SetValue("n_tries",params.n_tries);
145  simanOpt.SetValue("iters_fixed_T",params.iters_fixed_T);
146  simanOpt.SetValue("step_size",params.step_size);
147  simanOpt.SetValue("k",params.k);
148  simanOpt.SetValue("t_initial",params.t_initial);
149  simanOpt.SetValue("mu_t",params.mu_t);
150  simanOpt.SetValue("t_min",params.t_min);
151 
152  opt.SetExtraOptions(simanOpt);
153  return opt;
154 }
155 
157 
158  // get the specific siman options
159  const ROOT::Math::IOptions * simanOpt = opt.ExtraOptions();
160  if (!simanOpt) {
161  MATH_WARN_MSG("GSLSimAnMinimizer::SetOptions", "No specific sim. annealing minimizer options are provided. No options are set");
162  return;
163  }
164  GSLSimAnParams params;
165  simanOpt->GetValue("n_tries",params.n_tries);
166  simanOpt->GetValue("iters_fixed_T",params.iters_fixed_T);
167  simanOpt->GetValue("step_size",params.step_size);
168  simanOpt->GetValue("k",params.k);
169  simanOpt->GetValue("t_initial",params.t_initial);
170  simanOpt->GetValue("mu_t",params.mu_t);
171  simanOpt->GetValue("t_min",params.t_min);
172 
173  SetParameters(params);
174 }
175 
176 
177  } // end namespace Math
178 
179 } // end namespace ROOT
180 
ROOT::Math::IOptions::GetValue
bool GetValue(const char *name, T &t) const
Definition: IOptions.h:89
ROOT::Math::GSLSimAnMinimizer::GSLSimAnMinimizer
GSLSimAnMinimizer(int type=0)
Default constructor.
Definition: GSLSimAnMinimizer.cxx:43
ROOT::Math::BasicMinimizer::SetMinValue
void SetMinValue(double val)
Definition: BasicMinimizer.h:185
ROOT::Math::MinimizerOptions::SetPrecision
void SetPrecision(double prec)
set the precision
Definition: MinimizerOptions.h:143
WrappedParamFunction.h
ROOT::Math::BasicMinimizer::VariableName
virtual std::string VariableName(unsigned int ivar) const
get name of variables (override if minimizer support storing of variable names)
Definition: BasicMinimizer.cxx:237
ROOT::Math::GSLSimAnParams::k
double k
parameters for the Boltzman distribution
Definition: GSLSimAnnealing.h:218
f
#define f(i)
Definition: RSha256.hxx:122
ROOT::Math::MinimizerOptions::SetMinimizerType
void SetMinimizerType(const char *type)
set minimizer type
Definition: MinimizerOptions.h:152
ROOT::Math::Minimizer::PrintLevel
int PrintLevel() const
minimizer configuration parameters
Definition: Minimizer.h:412
ROOT::Math::BasicMinimizer::NPar
virtual unsigned int NPar() const
total number of parameter defined
Definition: BasicMinimizer.h:158
ROOT::Math::GSLSimAnMinimizer::Minimize
virtual bool Minimize()
method to perform the minimization
Definition: GSLSimAnMinimizer.cxx:56
ROOT::Math::GSLSimAnnealing::Solve
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 ...
Definition: GSLSimAnnealing.cxx:193
ROOT::Math::GSLSimAnMinimizer::SetParameters
void SetParameters(const GSLSimAnParams &params)
set new minimizer option parameters using directly the GSLSimAnParams structure
Definition: GSLSimAnMinimizer.h:170
ROOT::Math::MinimTransformFunction
MinimTransformFunction class to perform a transformations on the variables to deal with fixed or limi...
Definition: MinimTransformFunction.h:49
ROOT::Math::GSLSimAnMinimizer::SetOptions
virtual void SetOptions(const ROOT::Math::MinimizerOptions &opt)
set new minimizer options
Definition: GSLSimAnMinimizer.cxx:166
ROOT::Math::GenAlgoOptions
class implementing generic options for a numerical algorithm Just store the options in a map of strin...
Definition: GenAlgoOptions.h:41
ROOT::Math::GSLSimAnParams
structure holding the simulated annealing parameters
Definition: GSLSimAnnealing.h:199
FitMethodFunction.h
ROOT::Math::GSLSimAnParams::n_tries
int n_tries
Definition: GSLSimAnnealing.h:214
MATH_ERROR_MSG
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:83
ROOT::Math::MinimizerOptions::SetPrintLevel
void SetPrintLevel(int level)
set print level
Definition: MinimizerOptions.h:131
ROOT::Math::BasicMinimizer::MinValue
virtual double MinValue() const
return minimum function value
Definition: BasicMinimizer.h:146
MATH_WARN_MSG
#define MATH_WARN_MSG(loc, str)
Definition: Error.h:80
ROOT::Math::MinimizerOptions::SetErrorDef
void SetErrorDef(double err)
set error def
Definition: MinimizerOptions.h:149
ROOT::Math::GSLSimAnParams::step_size
double step_size
Definition: GSLSimAnnealing.h:216
MinimTransformFunction.h
ROOT::Math::GSLSimAnMinimizer::~GSLSimAnMinimizer
virtual ~GSLSimAnMinimizer()
Destructor (no operations)
Definition: GSLSimAnMinimizer.cxx:52
ROOT::Math::MinimizerOptions::SetMaxFunctionCalls
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
Definition: MinimizerOptions.h:134
ROOT::Math::MinimizerOptions::SetMinimizerAlgorithm
void SetMinimizerAlgorithm(const char *type)
set minimizer algorithm
Definition: MinimizerOptions.h:155
xmin
float xmin
Definition: THbookFile.cxx:95
ROOT::Math::GSLSimAnMinimizer::fSolver
ROOT::Math::GSLSimAnnealing fSolver
Definition: GSLSimAnMinimizer.h:177
Error.h
ROOT::Math::BasicMinimizer::SetFinalValues
void SetFinalValues(const double *x)
Definition: BasicMinimizer.cxx:332
ROOT::Math::GSLSimAnParams::t_initial
double t_initial
Definition: GSLSimAnnealing.h:219
ROOT::Math::MinimizerOptions::SetMaxIterations
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
Definition: MinimizerOptions.h:137
ROOT::Math::MinimizerOptions
Minimizer options.
Definition: MinimizerOptions.h:42
ROOT::Math::BasicMinimizer::StepSizes
virtual const double * StepSizes() const
accessor methods
Definition: BasicMinimizer.h:173
ROOT::Math::IOptions
Generic interface for defining configuration options of a numerical algorithm.
Definition: IOptions.h:41
ROOT::Math::GSLSimAnParams::t_min
double t_min
Definition: GSLSimAnnealing.h:221
ROOT::Math::GSLSimAnMinimizer::MinimizerParameters
const GSLSimAnParams & MinimizerParameters() const
Get current minimizer option parameteres.
Definition: GSLSimAnMinimizer.h:163
ROOT::Math::MinimizerOptions::SetTolerance
void SetTolerance(double tol)
set the tolerance
Definition: MinimizerOptions.h:140
ROOT::Math::BasicMinimizer::X
virtual const double * X() const
return pointer to X values at the minimum
Definition: BasicMinimizer.h:149
ROOT::Math::GSLSimAnParams::iters_fixed_T
int iters_fixed_T
Definition: GSLSimAnnealing.h:215
ROOT::Math::MinimizerOptions::ExtraOptions
const IOptions * ExtraOptions() const
return extra options (NULL pointer if they are not present)
Definition: MinimizerOptions.h:116
ROOT::Math::MinimTransformFunction::InvStepTransformation
void InvStepTransformation(const double *x, const double *sext, double *sint) const
inverse transformation for steps (external -> internal) at external point x
Definition: MinimTransformFunction.cxx:110
ROOT::Math::BasicMinimizer::NDim
virtual unsigned int NDim() const
number of dimensions
Definition: BasicMinimizer.h:152
GSLSimAnMinimizer.h
ROOT::Math::MinimTransformFunction::NDim
unsigned int NDim() const
Retrieve the dimension of the function.
Definition: MinimTransformFunction.h:82
ROOT::Math::MinimizerOptions::SetStrategy
void SetStrategy(int stra)
set the strategy
Definition: MinimizerOptions.h:146
ROOT::Math::MultiNumGradFunction
MultiNumGradFunction class to wrap a normal function in a gradient function using numerical gradient ...
Definition: MultiNumGradFunction.h:87
GenAlgoOptions.h
ROOT::Math::BasicMinimizer::CreateTransformation
MinimTransformFunction * CreateTransformation(std::vector< double > &startValues, const ROOT::Math::IMultiGradFunction *func=0)
Definition: BasicMinimizer.cxx:282
ROOT::Math::IBaseFunctionMultiDimTempl
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
MultiNumGradFunction.h
ROOT::Math::GSLSimAnMinimizer::NCalls
unsigned int NCalls() const
number of calls
Definition: GSLSimAnMinimizer.cxx:127
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::Math::MultiNumGradFunction::SetOwnership
void SetOwnership(bool on=true)
Definition: MultiNumGradFunction.h:162
ROOT::Math::IOptions::SetValue
void SetValue(const char *name, double val)
generic methods for retrivieng options
Definition: IOptions.h:60
Math
ROOT::Math::GSLSimAnMinimizer::Options
virtual ROOT::Math::MinimizerOptions Options() const
Get current minimizer options.
Definition: GSLSimAnMinimizer.cxx:138
ROOT::Math::MinimizerOptions::SetExtraOptions
void SetExtraOptions(const IOptions &opt)
set extra options (in this case pointer is cloned)
Definition: MinimizerOptions.cxx:205
ROOT::Math::BasicMinimizer::ObjFunction
const ROOT::Math::IMultiGenFunction * ObjFunction() const
return pointer to used objective function
Definition: BasicMinimizer.h:161
ROOT::Math::GSLSimAnParams::mu_t
double mu_t
Definition: GSLSimAnnealing.h:220
ROOT::Math::MinimTransformFunction::OriginalFunction
const IMultiGradFunction * OriginalFunction() const
Definition: MinimTransformFunction.h:117