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