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 <algorithm>
33#include <vector>
34
35namespace ROOT {
36
37 namespace Math {
38
39 class GSLRandomEngine;
40
41//_____________________________________________________________________________
42/**
43 GSLSimAnFunc class description.
44 Interface class for the objetive function to be used in simulated annealing
45 If user wants to re-implement some of the methods (like the one defining the metric) which are used by the
46 the simulated annealing algorithm must build a user derived class.
47 NOTE: Derived classes must re-implement the assignment and copy constructor to call them of the parent class
48
49 @ingroup MultiMin
50 */
52public:
53
54 /**
55 construct from an interface of a multi-dimensional function
56 */
57 GSLSimAnFunc(const ROOT::Math::IMultiGenFunction & func, const double * x);
58
59 /**
60 construct from an interface of a multi-dimensional function
61 Use optionally a scale factor (for each coordinate) which can be used to scale the step sizes
62 (this is used for example by the minimization algorithm)
63 */
64 GSLSimAnFunc(const ROOT::Math::IMultiGenFunction & func, const double * x, const double * scale);
65
66protected:
67
68 /**
69 derived classes might need to re-define completely the class
70 */
72 fFunc(nullptr)
73 {}
74
75public:
76
77
78 /// virtual destructor (no operations)
79 virtual ~GSLSimAnFunc() { } //
80
81
82 /**
83 fast copy method called by GSL simulated annealing internally
84 copy only the things which have been changed
85 must be re-implemented by derived classes if needed
86 */
87 virtual GSLSimAnFunc & FastCopy(const GSLSimAnFunc & f);
88
89
90 /**
91 clone method. Needs to be re-implemented by the derived classes for deep copying
92 */
93 virtual GSLSimAnFunc * Clone() const {
94 return new GSLSimAnFunc(*this);
95 }
96
97 /**
98 evaluate the energy ( objective function value)
99 re-implement by derived classes if needed to be modified
100 */
101 virtual double Energy() const;
102
103 /**
104 change the x[i] value using a random value urndm generated between [0,1]
105 up to a maximum value maxstep
106 re-implement by derived classes if needed to be modified
107 */
108 virtual void Step(const GSLRandomEngine & r, double maxstep);
109
110 /**
111 calculate the distance (metric) between this one and another configuration
112 Presently a cartesian metric is used.
113 re-implement by derived classes if needed to be modified
114 */
115 virtual double Distance(const GSLSimAnFunc & func) const;
116
117 /**
118 print the position in the standard output std::ostream
119 GSL prints in addition n iteration, n function calls, temperature and energy
120 re-implement by derived classes if necessary
121 */
122 virtual void Print();
123
124 /**
125 change the x values (used by sim annealing to take a step)
126 */
127 void SetX(const double * x) {
128 std::copy(x, x+ fX.size(), fX.begin() );
129 }
130
131 template <class IT>
132 void SetX(IT begin, IT end) {
133 std::copy(begin, end, fX.begin() );
134 }
135
136 unsigned int NDim() const { return fX.size(); }
137
138 double X(unsigned int i) const { return fX[i]; }
139
140 const std::vector<double> & X() const { return fX; }
141
142 double Scale(unsigned int i) const { return fScale[i]; }
143
144 void SetX(unsigned int i, double x) { fX[i] = x; }
145
146 // use compiler generated copy ctror and assignment operators
147
148private:
149
150 std::vector<double> fX;
151 std::vector<double> fScale;
153
154};
155
156//_____________________________________________________
157/**
158 structure holding the simulated annealing parameters
159
160 @ingroup MultiMin
161*/
163
164 // constructor with some default values
166 n_tries = 200;
167 iters_fixed_T = 10;
168 step_size = 10;
169 // the following parameters are for the Boltzmann distribution */
170 k = 1.0;
171 t_initial = 0.002;
172 mu_t = 1.005;
173 t_min = 2.0E-6;
174 }
175
176
177 int n_tries; // number of points to try for each step
178 int iters_fixed_T; // number of iterations at each temperature
179 double step_size; // max step size used in random walk
180 /// parameters for the Boltzman distribution
181 double k;
182 double t_initial;
183 double mu_t;
184 double t_min;
185};
186
187//___________________________________________________________________________
188/**
189 GSLSimAnnealing class for performing a simulated annealing search of
190 a multidimensional function
191
192 @ingroup MultiMin
193*/
195
196public:
197
198 /**
199 Default constructor
200 */
202
203 /**
204 Destructor (no operations)
205 */
207
208 // usually copying is non trivial, so we delete this
213
214 /**
215 solve the simulated annealing given a multi-dim function, the initial vector parameters
216 and a vector containing the scaling factors for the parameters
217 */
218 int Solve(const ROOT::Math::IMultiGenFunction & func, const double * x0, const double * scale, double * xmin, bool debug = false);
219
220 /**
221 solve the simulated annealing given a GSLSimAnFunc object
222 The object will contain the initial state at the beginning and the final minimum state at the end
223 */
224 int Solve(GSLSimAnFunc & func, bool debug = false);
225
226
228 const GSLSimAnParams & Params() const { return fParams; }
229 void SetParams(const GSLSimAnParams & params) { fParams = params; }
230
231private:
232
233 GSLSimAnParams fParams; // parameters for GSLSimAnnealig
234
235};
236
237 } // end namespace Math
238
239} // end namespace ROOT
240
241
242#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:63
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.
structure holding the simulated annealing parameters
double k
parameters for the Boltzman distribution