Logo ROOT   6.16/01
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
15#include "Math/Error.h"
16
18#include "Math/MultiNumGradFunction.h" // needed to use transformation function
20
21#include <iostream>
22#include <cassert>
23
24namespace ROOT {
25
26 namespace Math {
27
28
29
30// GSLSimAnMinimizer implementation
31
32GSLSimAnMinimizer::GSLSimAnMinimizer( int /* ROOT::Math::EGSLSimAnMinimizerType type */ ) :
34{
35 // Constructor implementation : create GSLMultiFit wrapper object
36
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
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
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
116unsigned 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
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:82
#define f(i)
Definition: RSha256.hxx:104
float xmin
Definition: THbookFile.cxx:93
Base Minimizer class, which defines the basic funcionality of various minimizer implementations (apar...
virtual double MinValue() const
return minimum function value
virtual unsigned int NPar() const
total number of parameter defined
void SetMinValue(double val)
virtual const double * StepSizes() const
accessor methods
const ROOT::Math::IMultiGenFunction * ObjFunction() const
return pointer to used objective function
virtual unsigned int NDim() const
number of dimensions
virtual std::string VariableName(unsigned int ivar) const
get name of variables (override if minimizer support storing of variable names)
virtual const double * X() const
return pointer to X values at the minimum
void SetFinalValues(const double *x)
MinimTransformFunction * CreateTransformation(std::vector< double > &startValues, const ROOT::Math::IMultiGradFunction *func=0)
virtual ~GSLSimAnMinimizer()
Destructor (no operations)
GSLSimAnMinimizer(int type=0)
Default constructor.
ROOT::Math::GSLSimAnnealing fSolver
unsigned int NCalls() const
number of function calls to reach the minimum
virtual bool Minimize()
method to perform the minimization
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 ...
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
MinimTransformFunction class to perform a transformations on the variables to deal with fixed or limi...
unsigned int NDim() const
Retrieve the dimension of the function.
const IMultiGradFunction * OriginalFunction() const
void InvStepTransformation(const double *x, const double *sext, double *sint) const
inverse transformation for steps (external -> internal) at external point x
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
Definition: Minimizer.h:451
void SetPrintLevel(int level)
set print level
Definition: Minimizer.h:445
int PrintLevel() const
minimizer configuration parameters
Definition: Minimizer.h:411
MultiNumGradFunction class to wrap a normal function in a gradient function using numerical gradient ...
Namespace for new Math classes and functions.
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:151
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21