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 */ )
34{
35 // Constructor implementation : create GSLMultiFit wrapper object
36
38 fOptions.SetMinimizerType("GSLSimAn");
39 // set dummy values since those are not used
47
48 // set extra options
50}
51
53}
54
55
57 // set initial parameters of the minimizer
58 int debugLevel = PrintLevel();
59
60 if (debugLevel >= 1)
61 std::cout << "Minimize using GSLSimAnMinimizer " << std::endl;
62
63
64 const ROOT::Math::IMultiGenFunction * function = ObjFunction();
65 if (function == nullptr) {
66 MATH_ERROR_MSG("GSLSimAnMinimizer::Minimize","Function has not been set");
67 return false;
68 }
69
70 // set Sim. An. parameters from existing minimizer options
72 if (debugLevel >= 1) {
73 std::cout << "Parameters for simulated annealing: " << std::endl;
74 auto simanOpt = fOptions.ExtraOptions();
75 if (simanOpt)
76 simanOpt->Print();
77 else
78 std::cout << "no simulated annealing options available" << std::endl;
79 }
80
81 // vector of internal values (copied by default)
82 unsigned int npar = NPar();
83 std::vector<double> xvar;
84 std::vector<double> steps(StepSizes(),StepSizes()+npar);
85
86 // needed for the transformation
87 std::unique_ptr<MultiNumGradFunction> gradFunc(new MultiNumGradFunction( *function ));
88 //gradFunc->SetOwnership();
89
90 std::unique_ptr<MinimTransformFunction> trFunc(CreateTransformation(xvar, gradFunc.get() ));
91
92 if (trFunc) {
93 // transform also the step sizes
94 trFunc->InvStepTransformation(X(), StepSizes(), &steps[0]);
95 steps.resize( trFunc->NDim() );
96 }
97
98 assert (xvar.size() == steps.size() );
99
100
101#ifdef DEBUG
102 for (unsigned int i = 0; i < npar ; ++i) {
103 std::cout << "x = " << xvar[i] << " steps " << steps[i] << " x " << X()[i] << std::endl;
104 }
105 std::cout << "f(x) = " << (*ObjFunction())(xvar.data() ) << std::endl;
106 std::cout << "f(x) not transf = " << (*ObjFunction())( X() ) << std::endl;
107 if (trFunc) std::cout << "ftrans(x) = " << (*trFunc) ( xvar.data() ) << std::endl;
108#endif
109
110 // output vector
111 std::vector<double> xmin(xvar.size() );
112
113
114 int iret = fSolver.Solve( (trFunc) ? *trFunc : *function, xvar.data(), steps.data(), xmin.data(), (debugLevel > 1) );
115
116 SetFinalValues(xmin.data(), trFunc.get());
117 SetMinValue( (*ObjFunction())( X() ) );
118
119
120 if (debugLevel >=1 ) {
121 if (iret == 0)
122 std::cout << "GSLSimAnMinimizer: Minimum Found" << std::endl;
123 else
124 std::cout << "GSLSimAnMinimizer: Error in solving" << std::endl;
125
126 int pr = std::cout.precision(18);
127 std::cout << "FVAL = " << MinValue() << std::endl;
128 std::cout.precision(pr);
129 for (unsigned int i = 0; i < NDim(); ++i)
130 std::cout << VariableName(i) << "\t = " << X()[i] << std::endl;
131 }
132
133
134 return ( iret == 0) ? true : false;
135}
136
137
138unsigned int GSLSimAnMinimizer::NCalls() const {
139 // return number of function calls
140 auto f = dynamic_cast<const FitMethodFunction*>(ObjFunction());
141 if (f) return f->NCalls();
142 auto gf = dynamic_cast<const FitMethodGradFunction*>(ObjFunction());
143 if (gf) return gf->NCalls();
144 return 0;
145}
146
147
149{
150 // set the extra minimizer options from GSLSimAnParams
152 simanOpt.SetValue("n_tries",params.n_tries);
153 simanOpt.SetValue("iters_fixed_T",params.iters_fixed_T);
154 simanOpt.SetValue("step_size",params.step_size);
155 simanOpt.SetValue("k",params.k);
156 simanOpt.SetValue("t_initial",params.t_initial);
157 simanOpt.SetValue("mu_t",params.mu_t);
158 simanOpt.SetValue("t_min",params.t_min);
159
160 fOptions.SetExtraOptions(simanOpt);
161}
162
163
165{
166 // get the specific simulated annealing options from MinimizerOptions
167 const ROOT::Math::IOptions *simanOpt = options.ExtraOptions();
168 if (!simanOpt) {
169 return;
170 }
171
172 // get the various options. In case they are not defined the default parameters
173 // will not be modified and will be used
174 GSLSimAnParams params;
175 simanOpt->GetValue("n_tries", params.n_tries);
176 simanOpt->GetValue("iters_fixed_T", params.iters_fixed_T);
177 simanOpt->GetValue("step_size", params.step_size);
178 simanOpt->GetValue("k", params.k);
179 simanOpt->GetValue("t_initial", params.t_initial);
180 simanOpt->GetValue("mu_t", params.mu_t);
181 simanOpt->GetValue("t_min", params.t_min);
182
183 SetParameters(params);
184}
185
186 } // end namespace Math
187
188} // end namespace ROOT
#define MATH_ERROR_MSG(loc, str)
Definition Error.h:83
#define f(i)
Definition RSha256.hxx:104
float xmin
FitMethodFunction class Interface for objective functions (like chi2 and likelihood used in the fit) ...
virtual unsigned int NCalls() const
return the total number of function calls (override if needed)
virtual unsigned int NPar() const
total number of parameter defined
unsigned int NDim() const override
number of dimensions
void SetFinalValues(const double *x, const MinimTransformFunction *func=nullptr)
double MinValue() const override
return minimum function value
MinimTransformFunction * CreateTransformation(std::vector< double > &startValues, const ROOT::Math::IMultiGradFunction *func=nullptr)
virtual const double * StepSizes() const
accessor methods
const ROOT::Math::IMultiGenFunction * ObjFunction() const
return pointer to used objective function
const double * X() const override
return pointer to X values at the minimum
std::string VariableName(unsigned int ivar) const override
get name of variables (override if minimizer support storing of variable names)
bool Minimize() override
method to perform the minimization
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)
unsigned int NCalls() const override
number of calls
const GSLSimAnParams & MinimizerParameters() const
Get current minimizer option parameters.
ROOT::Math::GSLSimAnnealing fSolver
~GSLSimAnMinimizer() override
Destructor (no operations)
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:61
Generic interface for defining configuration options of a numerical algorithm.
Definition IOptions.h:28
void SetValue(const char *name, double val)
generic methods for retrieving options
Definition IOptions.h:42
bool GetValue(const char *name, T &t) const
Definition IOptions.h:54
virtual void Print(std::ostream &=std::cout) const
print options
Definition IOptions.cxx:56
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
minimizer options
Definition Minimizer.h:370
int PrintLevel() const
minimizer configuration parameters
Definition Minimizer.h:291
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