Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
GSLSimAnnealing.h
Go to the documentation of this file.
1// @(#)root/mathmore:$Id$
2// Author: L. Moneta Thu Jan 25 11:13:48 2007
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU General Public License *
10 * as published by the Free Software Foundation; either version 2 *
11 * of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this library (see file COPYING); if not, write *
20 * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
21 * 330, Boston, MA 02111-1307 USA, or contact the author. *
22 * *
23 **********************************************************************/
24
25// Header file for class GSLSimAnnealing
26
27#ifndef ROOT_Math_GSLSimAnnealing
28#define ROOT_Math_GSLSimAnnealing
29
30#include "Math/IFunctionfwd.h"
31
32#include <vector>
33
34namespace ROOT {
35
36 namespace Math {
37
38 class GSLRandomEngine;
39
40//_____________________________________________________________________________
41/**
42 GSLSimAnFunc class description.
43 Interface class for the objetive function to be used in simulated annealing
44 If user wants to re-implement some of the methods (like the one defining the metric) which are used by the
45 the simulated annealing algorithm must build a user derived class.
46 NOTE: Derived classes must re-implement the assignment and copy constructor to call them of the parent class
47
48 @ingroup MultiMin
49 */
51public:
52
53 /**
54 construct from an interface of a multi-dimensional function
55 */
56 GSLSimAnFunc(const ROOT::Math::IMultiGenFunction & func, const double * x);
57
58 /**
59 construct from an interface of a multi-dimensional function
60 Use optionally a scale factor (for each coordinate) which can be used to scale the step sizes
61 (this is used for example by the minimization algorithm)
62 */
63 GSLSimAnFunc(const ROOT::Math::IMultiGenFunction & func, const double * x, const double * scale);
64
65protected:
66
67 /**
68 derived classes might need to re-define completely the class
69 */
71 fFunc(nullptr)
72 {}
73
74public:
75
76
77 /// virtual destructor (no operations)
78 virtual ~GSLSimAnFunc() { } //
79
80
81 /**
82 fast copy method called by GSL simulated annealing internally
83 copy only the things which have been changed
84 must be re-implemented by derived classes if needed
85 */
86 virtual GSLSimAnFunc & FastCopy(const GSLSimAnFunc & f);
87
88
89 /**
90 clone method. Needs to be re-implemented by the derived classes for deep copying
91 */
92 virtual GSLSimAnFunc * Clone() const {
93 return new GSLSimAnFunc(*this);
94 }
95
96 /**
97 evaluate the energy ( objective function value)
98 re-implement by derived classes if needed to be modified
99 */
100 virtual double Energy() const;
101
102 /**
103 change the x[i] value using a random value urndm generated between [0,1]
104 up to a maximum value maxstep
105 re-implement by derived classes if needed to be modified
106 */
107 virtual void Step(const GSLRandomEngine & r, double maxstep);
108
109 /**
110 calculate the distance (metric) between this one and another configuration
111 Presently a cartesian metric is used.
112 re-implement by derived classes if needed to be modified
113 */
114 virtual double Distance(const GSLSimAnFunc & func) const;
115
116 /**
117 print the position in the standard output std::ostream
118 GSL prints in addition n iteration, n function calls, temperature and energy
119 re-implement by derived classes if necessary
120 */
121 virtual void Print();
122
123 /**
124 change the x values (used by sim annealing to take a step)
125 */
126 void SetX(const double * x) {
127 std::copy(x, x+ fX.size(), fX.begin() );
128 }
129
130 template <class IT>
131 void SetX(IT begin, IT end) {
132 std::copy(begin, end, fX.begin() );
133 }
134
135 unsigned int NDim() const { return fX.size(); }
136
137 double X(unsigned int i) const { return fX[i]; }
138
139 const std::vector<double> & X() const { return fX; }
140
141 double Scale(unsigned int i) const { return fScale[i]; }
142
143 void SetX(unsigned int i, double x) { fX[i] = x; }
144
145 // use compiler generated copy ctror and assignment operators
146
147private:
148
149 std::vector<double> fX;
150 std::vector<double> fScale;
152
153};
154
155//_____________________________________________________
156/**
157 structure holding the simulated annealing parameters
158
159 @ingroup MultiMin
160*/
162
163 // constructor with some default values
165 n_tries = 200;
166 iters_fixed_T = 10;
167 step_size = 10;
168 // the following parameters are for the Boltzmann distribution */
169 k = 1.0;
170 t_initial = 0.002;
171 mu_t = 1.005;
172 t_min = 2.0E-6;
173 }
174
175
176 int n_tries; // number of points to try for each step
177 int iters_fixed_T; // number of iterations at each temperature
178 double step_size; // max step size used in random walk
179 /// parameters for the Boltzman distribution
180 double k;
181 double t_initial;
182 double mu_t;
183 double t_min;
184};
185
186//___________________________________________________________________________
187/**
188 GSLSimAnnealing class for performing a simulated annealing search of
189 a multidimensional function
190
191 @ingroup MultiMin
192*/
194
195public:
196
197 /**
198 Default constructor
199 */
201
202 /**
203 Destructor (no operations)
204 */
206
207 // usually copying is non trivial, so we delete this
212
213 /**
214 solve the simulated annealing given a multi-dim function, the initial vector parameters
215 and a vector containing the scaling factors for the parameters
216 */
217 int Solve(const ROOT::Math::IMultiGenFunction & func, const double * x0, const double * scale, double * xmin, bool debug = false);
218
219 /**
220 solve the simulated annealing given a GSLSimAnFunc object
221 The object will contain the initial state at the beginning and the final minimum state at the end
222 */
223 int Solve(GSLSimAnFunc & func, bool debug = false);
224
225
227 const GSLSimAnParams & Params() const { return fParams; }
228 void SetParams(const GSLSimAnParams & params) { fParams = params; }
229
230private:
231
232 GSLSimAnParams fParams; // parameters for GSLSimAnnealig
233
234};
235
236 } // end namespace Math
237
238} // end namespace ROOT
239
240
241#endif /* ROOT_Math_GSLSimAnnealing */
#define f(i)
Definition RSha256.hxx:104
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
float xmin
GSLRandomEngine Base class for all GSL random engines, normally user instantiate the derived classes ...
GSLSimAnFunc class description.
void SetX(IT begin, IT end)
double X(unsigned int i) const
double Scale(unsigned int i) const
const std::vector< double > & X() const
virtual double Distance(const GSLSimAnFunc &func) const
calculate the distance (metric) between this one and another configuration Presently a cartesian metr...
std::vector< double > fX
virtual void Print()
print the position in the standard output std::ostream GSL prints in addition n iteration,...
GSLSimAnFunc()
derived classes might need to re-define completely the class
virtual ~GSLSimAnFunc()
virtual destructor (no operations)
unsigned int NDim() const
virtual GSLSimAnFunc & FastCopy(const GSLSimAnFunc &f)
fast copy method called by GSL simulated annealing internally copy only the things which have been ch...
void SetX(const double *x)
change the x values (used by sim annealing to take a step)
virtual double Energy() const
evaluate the energy ( objective function value) re-implement by derived classes if needed to be modif...
virtual void Step(const GSLRandomEngine &r, double maxstep)
change the x[i] value using a random value urndm generated between [0,1] up to a maximum value maxste...
void SetX(unsigned int i, double x)
virtual GSLSimAnFunc * Clone() const
clone method.
std::vector< double > fScale
const ROOT::Math::IMultiGenFunction * fFunc
GSLSimAnnealing class for performing a simulated annealing search of a multidimensional function.
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 ...
const GSLSimAnParams & Params() const
void SetParams(const GSLSimAnParams &params)
GSLSimAnnealing()
Default constructor.
GSLSimAnnealing(const GSLSimAnnealing &)=delete
GSLSimAnnealing(GSLSimAnnealing &&)=delete
~GSLSimAnnealing()
Destructor (no operations)
GSLSimAnnealing & operator=(const GSLSimAnnealing &rhs)=delete
Documentation for the abstract class IBaseFunctionMultiDim.
Definition IFunction.h:61
Double_t x[n]
Definition legend1.C:17
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