Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#include "Math/GenAlgoOptions.h"
21
22#include <iostream>
23#include <cassert>
24
25namespace ROOT {
26
27 namespace Math {
28
29
30
31// GSLSimAnMinimizer implementation
32
33GSLSimAnMinimizer::GSLSimAnMinimizer( int /* ROOT::Math::EGSLSimAnMinimizerType type */ ) :
35{
36 // Constructor implementation : create GSLMultiFit wrapper object
37
39 fOptions.SetMinimizerType("GSLSimAn");
40 // set dummy values since those are not used
48
49 // set extra options
51}
52
54}
55
56
58 // set initial parameters of the minimizer
59 int debugLevel = PrintLevel();
60
61 if (debugLevel >= 1)
62 std::cout << "Minimize using GSLSimAnMinimizer " << std::endl;
63
64
65 const ROOT::Math::IMultiGenFunction * function = ObjFunction();
66 if (function == 0) {
67 MATH_ERROR_MSG("GSLSimAnMinimizer::Minimize","Function has not been set");
68 return false;
69 }
70
71 // set Sim. An. parameters from existing minimizer options
73 if (debugLevel >= 1) {
74 std::cout << "Parameters for simulated annealing: " << std::endl;
75 auto simanOpt = fOptions.ExtraOptions();
76 if (simanOpt)
77 simanOpt->Print();
78 else
79 std::cout << "no simulated annealing options available" << std::endl;
80 }
81
82 // vector of internal values (copied by default)
83 unsigned int npar = NPar();
84 std::vector<double> xvar;
85 std::vector<double> steps(StepSizes(),StepSizes()+npar);
86
87 // needed for the transformation
88 MultiNumGradFunction * gradFunc = new MultiNumGradFunction( *function );
89 gradFunc->SetOwnership();
90
91 MinimTransformFunction * trFunc = CreateTransformation(xvar, gradFunc );
92 // ObjFunction() will return now the new transformed function
93
94 if (trFunc) {
95 // transform also the step sizes
96 trFunc->InvStepTransformation(X(), StepSizes(), &steps[0]);
97 steps.resize( trFunc->NDim() );
98 }
99
100 assert (xvar.size() == steps.size() );
101
102
103#ifdef DEBUG
104 for (unsigned int i = 0; i < npar ; ++i) {
105 std::cout << "x = " << xvar[i] << " steps " << steps[i] << " x " << X()[i] << std::endl;
106 }
107 std::cout << "f(x) = " << (*ObjFunction())(&xvar.front() ) << std::endl;
108 std::cout << "f(x) not transf = " << (*function)( X() ) << std::endl;
109 if (trFunc) std::cout << "ftrans(x) = " << (*trFunc) (&xvar.front() ) << std::endl;
110#endif
111
112 // output vector
113 std::vector<double> xmin(xvar.size() );
114
115
116 int iret = fSolver.Solve(*ObjFunction(), &xvar.front(), &steps.front(), &xmin[0], (debugLevel > 1) );
117
118 SetMinValue( (*ObjFunction())(&xmin.front() ) );
119
120 SetFinalValues(&xmin.front());
121
122
123 if (debugLevel >=1 ) {
124 if (iret == 0)
125 std::cout << "GSLSimAnMinimizer: Minimum Found" << std::endl;
126 else
127 std::cout << "GSLSimAnMinimizer: Error in solving" << std::endl;
128
129 int pr = std::cout.precision(18);
130 std::cout << "FVAL = " << MinValue() << std::endl;
131 std::cout.precision(pr);
132 for (unsigned int i = 0; i < NDim(); ++i)
133 std::cout << VariableName(i) << "\t = " << X()[i] << std::endl;
134 }
135
136
137 return ( iret == 0) ? true : false;
138}
139
140
141unsigned int GSLSimAnMinimizer::NCalls() const {
142 // return number of function calls
145 if (tfunc) f = dynamic_cast<const ROOT::Math::MultiNumGradFunction *>(tfunc->OriginalFunction());
146 else
147 f = dynamic_cast<const ROOT::Math::MultiNumGradFunction *>(ObjFunction());
148 if (f) return f->NCalls();
149 return 0;
150}
151
152
154{
155 // set the extra minimizer options from GSLSimAnParams
157 simanOpt.SetValue("n_tries",params.n_tries);
158 simanOpt.SetValue("iters_fixed_T",params.iters_fixed_T);
159 simanOpt.SetValue("step_size",params.step_size);
160 simanOpt.SetValue("k",params.k);
161 simanOpt.SetValue("t_initial",params.t_initial);
162 simanOpt.SetValue("mu_t",params.mu_t);
163 simanOpt.SetValue("t_min",params.t_min);
164
165 fOptions.SetExtraOptions(simanOpt);
166}
167
168
170{
171 // get the specific simulated annealing options from MinimizerOptions
172 const ROOT::Math::IOptions *simanOpt = options.ExtraOptions();
173 if (!simanOpt) {
174 return;
175 }
176
177 // get the various options. In case they are not defined the default parameters
178 // will not be modified and will be used
179 GSLSimAnParams params;
180 simanOpt->GetValue("n_tries", params.n_tries);
181 simanOpt->GetValue("iters_fixed_T", params.iters_fixed_T);
182 simanOpt->GetValue("step_size", params.step_size);
183 simanOpt->GetValue("k", params.k);
184 simanOpt->GetValue("t_initial", params.t_initial);
185 simanOpt->GetValue("mu_t", params.mu_t);
186 simanOpt->GetValue("t_min", params.t_min);
187
188 SetParameters(params);
189}
190
191 } // end namespace Math
192
193} // end namespace ROOT
#define MATH_ERROR_MSG(loc, str)
Definition Error.h:83
#define f(i)
Definition RSha256.hxx:104
float xmin
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
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)
void SetParameters(const GSLSimAnParams &params)
set new minimizer option parameters using directly the GSLSimAnParams structure
GSLSimAnMinimizer(int type=0)
Default constructor.
void DoSetSimAnParameters(const MinimizerOptions &opt)
set minimizer option parameters from stored ROOT::Math::MinimizerOptions (fOpt)
const GSLSimAnParams & MinimizerParameters() const
Get current minimizer option parameteres.
ROOT::Math::GSLSimAnnealing fSolver
unsigned int NCalls() const
number of calls
virtual bool Minimize()
method to perform the minimization
void DoSetMinimOptions(const GSLSimAnParams &params)
Set the Minimizer options from the simulated annealing parameters.
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 ...
class implementing generic options for a numerical algorithm Just store the options in a map of strin...
Documentation for the abstract class IBaseFunctionMultiDim.
Definition IFunction.h:62
Generic interface for defining configuration options of a numerical algorithm.
Definition IOptions.h:31
virtual void Print(std::ostream &=std::cout) const
print options
Definition IOptions.h:99
void SetValue(const char *name, double val)
generic methods for retrivieng options
Definition IOptions.h:45
bool GetValue(const char *name, T &t) const
Definition IOptions.h:74
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 SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
void SetStrategy(int stra)
set the strategy
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
const IOptions * ExtraOptions() const
return extra options (NULL pointer if they are not present)
void SetMinimizerType(const char *type)
set minimizer type
void SetExtraOptions(const IOptions &opt)
set extra options (in this case pointer is cloned)
void SetPrecision(double prec)
set the precision
void SetErrorDef(double err)
set error def
void SetPrintLevel(int level)
set print level
void SetMinimizerAlgorithm(const char *type)
set minimizer algorithm
void SetTolerance(double tol)
set the tolerance
MinimizerOptions fOptions
Definition Minimizer.h:491
int PrintLevel() const
minimizer configuration parameters
Definition Minimizer.h:407
MultiNumGradFunction class to wrap a normal function in a gradient function using numerical gradient ...
Namespace for new Math classes and functions.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
structure holding the simulated annealing parameters
double k
parameters for the Boltzman distribution