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
66 if (function == nullptr) {
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 std::unique_ptr<MultiNumGradFunction> gradFunc(new MultiNumGradFunction( *function ));
89 //gradFunc->SetOwnership();
90
91 std::unique_ptr<MinimTransformFunction> trFunc(CreateTransformation(xvar, gradFunc.get() ));
92
93 if (trFunc) {
94 // transform also the step sizes
95 trFunc->InvStepTransformation(X(), StepSizes(), &steps[0]);
96 steps.resize( trFunc->NDim() );
97 }
98
99 assert (xvar.size() == steps.size() );
100
101
102#ifdef DEBUG
103 for (unsigned int i = 0; i < npar ; ++i) {
104 std::cout << "x = " << xvar[i] << " steps " << steps[i] << " x " << X()[i] << std::endl;
105 }
106 std::cout << "f(x) = " << (*ObjFunction())(xvar.data() ) << std::endl;
107 std::cout << "f(x) not transf = " << (*ObjFunction())( X() ) << std::endl;
108 if (trFunc) std::cout << "ftrans(x) = " << (*trFunc) ( xvar.data() ) << std::endl;
109#endif
110
111 // output vector
112 std::vector<double> xmin(xvar.size() );
113
114
115 int iret = fSolver.Solve( (trFunc) ? *trFunc : *function, xvar.data(), steps.data(), xmin.data(), (debugLevel > 1) );
116
117 SetFinalValues(xmin.data(), trFunc.get());
118 SetMinValue( (*ObjFunction())( X() ) );
119
120
121 if (debugLevel >=1 ) {
122 if (iret == 0)
123 std::cout << "GSLSimAnMinimizer: Minimum Found" << std::endl;
124 else
125 std::cout << "GSLSimAnMinimizer: Error in solving" << std::endl;
126
127 int pr = std::cout.precision(18);
128 std::cout << "FVAL = " << MinValue() << std::endl;
129 std::cout.precision(pr);
130 for (unsigned int i = 0; i < NDim(); ++i)
131 std::cout << VariableName(i) << "\t = " << X()[i] << std::endl;
132 }
133
134
135 return ( iret == 0) ? true : false;
136}
137
138
139unsigned int GSLSimAnMinimizer::NCalls() const {
140 // return number of function calls
141 auto f = dynamic_cast<const FitMethodFunction*>(ObjFunction());
142 if (f) return f->NCalls();
143 auto gf = dynamic_cast<const FitMethodGradFunction*>(ObjFunction());
144 if (gf) return gf->NCalls();
145 return 0;
146}
147
148
150{
151 // set the extra minimizer options from GSLSimAnParams
153 simanOpt.SetValue("n_tries",params.n_tries);
154 simanOpt.SetValue("iters_fixed_T",params.iters_fixed_T);
155 simanOpt.SetValue("step_size",params.step_size);
156 simanOpt.SetValue("k",params.k);
157 simanOpt.SetValue("t_initial",params.t_initial);
158 simanOpt.SetValue("mu_t",params.mu_t);
159 simanOpt.SetValue("t_min",params.t_min);
160
161 fOptions.SetExtraOptions(simanOpt);
162}
163
164
166{
167 // get the specific simulated annealing options from MinimizerOptions
168 const ROOT::Math::IOptions *simanOpt = options.ExtraOptions();
169 if (!simanOpt) {
170 return;
171 }
172
173 // get the various options. In case they are not defined the default parameters
174 // will not be modified and will be used
175 GSLSimAnParams params;
176 simanOpt->GetValue("n_tries", params.n_tries);
177 simanOpt->GetValue("iters_fixed_T", params.iters_fixed_T);
178 simanOpt->GetValue("step_size", params.step_size);
179 simanOpt->GetValue("k", params.k);
180 simanOpt->GetValue("t_initial", params.t_initial);
181 simanOpt->GetValue("mu_t", params.mu_t);
182 simanOpt->GetValue("t_min", params.t_min);
183
184 SetParameters(params);
185}
186
187 } // end namespace Math
188
189} // end namespace ROOT
#define MATH_ERROR_MSG(loc, str)
Definition Error.h:83
#define f(i)
Definition RSha256.hxx:104
RooAbsReal & function()
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)
Base Minimizer class, which defines the basic functionality of various minimizer implementations (apa...
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:390
int PrintLevel() const
minimizer configuration parameters
Definition Minimizer.h:306
MultiNumGradFunction class to wrap a normal function in a gradient function using numerical gradient ...
Namespace for new Math classes and functions.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
structure holding the simulated annealing parameters
double k
parameters for the Boltzman distribution