Logo ROOT  
Reference Guide
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 
34 namespace 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  */
50 class GSLSimAnFunc {
51 public:
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 
65 protected:
66 
67  /**
68  derived classes might need to re-define completely the class
69  */
70  GSLSimAnFunc() :
71  fFunc(0)
72  {}
73 
74 public:
75 
76 
77  /// virtual distructor (no operations)
78  virtual ~GSLSimAnFunc() { } //
79 
80 
81  /**
82  fast copy method called by GSL simuated 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 
147 private:
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 */
161 struct GSLSimAnParams {
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 */
193 class GSLSimAnnealing {
194 
195 public:
196 
197  /**
198  Default constructor
199  */
200  GSLSimAnnealing ();
201 
202  /**
203  Destructor (no operations)
204  */
205  ~GSLSimAnnealing () {}
206 
207 private:
208  // usually copying is non trivial, so we make this unaccessible
209 
210  /**
211  Copy constructor
212  */
213  GSLSimAnnealing(const GSLSimAnnealing &) {}
214 
215  /**
216  Assignment operator
217  */
219  if (this == &rhs) return *this; // time saving self-test
220  return *this;
221  }
222 
223 public:
224 
225 
226  /**
227  solve the simulated annealing given a multi-dim function, the initial vector parameters
228  and a vector containing the scaling factors for the parameters
229  */
230  int Solve(const ROOT::Math::IMultiGenFunction & func, const double * x0, const double * scale, double * xmin, bool debug = false);
231 
232  /**
233  solve the simulated annealing given a GSLSimAnFunc object
234  The object will contain the initial state at the beginning and the final minimum state at the end
235  */
236  int Solve(GSLSimAnFunc & func, bool debug = false);
237 
238 
239  GSLSimAnParams & Params() { return fParams; }
240  const GSLSimAnParams & Params() const { return fParams; }
241  void SetParams(const GSLSimAnParams & params) { fParams = params; }
242 
243 
244 protected:
245 
246 
247 private:
248 
249  GSLSimAnParams fParams; // parameters for GSLSimAnnealig
250 
251 };
252 
253  } // end namespace Math
254 
255 } // end namespace ROOT
256 
257 
258 #endif /* ROOT_Math_GSLSimAnnealing */
ROOT::Math::GSLSimAnFunc::Energy
virtual double Energy() const
evaluate the energy ( objective function value) re-implement by derived classes if needed to be modif...
Definition: GSLSimAnnealing.cxx:60
ROOT::Math::GSLSimAnnealing::Params
GSLSimAnParams & Params()
Definition: GSLSimAnnealing.h:277
ROOT::Math::GSLSimAnnealing::GSLSimAnnealing
GSLSimAnnealing()
Default constructor.
Definition: GSLSimAnnealing.cxx:184
ROOT::Math::GSLSimAnFunc::GSLSimAnFunc
GSLSimAnFunc()
derived classes might need to re-define completely the class
Definition: GSLSimAnnealing.h:108
ROOT::Math::GSLSimAnParams::k
double k
parameters for the Boltzman distribution
Definition: GSLSimAnnealing.h:218
f
#define f(i)
Definition: RSha256.hxx:122
ROOT::Math::GSLSimAnFunc::~GSLSimAnFunc
virtual ~GSLSimAnFunc()
virtual distructor (no operations)
Definition: GSLSimAnnealing.h:116
ROOT::Math::GSLSimAnFunc::Distance
virtual double Distance(const GSLSimAnFunc &func) const
calculate the distance (metric) between this one and another configuration Presently a cartesian metr...
Definition: GSLSimAnnealing.cxx:76
ROOT::Math::GSLSimAnFunc::fScale
std::vector< double > fScale
Definition: GSLSimAnnealing.h:188
ROOT::Math::GSLSimAnFunc::fX
std::vector< double > fX
Definition: GSLSimAnnealing.h:187
r
ROOT::R::TRInterface & r
Definition: Object.C:4
ROOT::Math::GSLSimAnFunc::FastCopy
virtual GSLSimAnFunc & FastCopy(const GSLSimAnFunc &f)
fast copy method called by GSL simuated annealing internally copy only the things which have been cha...
Definition: GSLSimAnnealing.cxx:106
IFunctionfwd.h
ROOT::Math::GSLSimAnnealing::Solve
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 ...
Definition: GSLSimAnnealing.cxx:193
ROOT::Math::GSLSimAnnealing::~GSLSimAnnealing
~GSLSimAnnealing()
Destructor (no operations)
Definition: GSLSimAnnealing.h:243
x
Double_t x[n]
Definition: legend1.C:17
ROOT::Math::GSLSimAnFunc::fFunc
const ROOT::Math::IMultiGenFunction * fFunc
Definition: GSLSimAnnealing.h:189
ROOT::Math::GSLSimAnParams
structure holding the simulated annealing parameters
Definition: GSLSimAnnealing.h:199
ROOT::Math::GSLSimAnParams::n_tries
int n_tries
Definition: GSLSimAnnealing.h:214
ROOT::Math::GSLSimAnFunc::SetX
void SetX(const double *x)
change the x values (used by sim annealing to take a step)
Definition: GSLSimAnnealing.h:164
ROOT::Math::GSLSimAnFunc::NDim
unsigned int NDim() const
Definition: GSLSimAnnealing.h:173
ROOT::Math::GSLSimAnnealing
GSLSimAnnealing class for performing a simulated annealing search of a multidimensional function.
Definition: GSLSimAnnealing.h:231
ROOT::Math::GSLSimAnParams::step_size
double step_size
Definition: GSLSimAnnealing.h:216
xmin
float xmin
Definition: THbookFile.cxx:95
ROOT::Math::GSLSimAnnealing::fParams
GSLSimAnParams fParams
Definition: GSLSimAnnealing.h:287
ROOT::Math::GSLSimAnFunc::X
const std::vector< double > & X() const
Definition: GSLSimAnnealing.h:177
ROOT::Math::GSLSimAnParams::t_initial
double t_initial
Definition: GSLSimAnnealing.h:219
ROOT::Math::GSLSimAnFunc::Step
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...
Definition: GSLSimAnnealing.cxx:65
ROOT::Math::GSLSimAnnealing::SetParams
void SetParams(const GSLSimAnParams &params)
Definition: GSLSimAnnealing.h:279
ROOT::Math::GSLSimAnFunc::Print
virtual void Print()
print the position in the standard output std::ostream GSL prints in addition n iteration,...
Definition: GSLSimAnnealing.cxx:93
ROOT::Math::GSLSimAnParams::t_min
double t_min
Definition: GSLSimAnnealing.h:221
ROOT::Math::GSLSimAnnealing::operator=
GSLSimAnnealing & operator=(const GSLSimAnnealing &rhs)
Assignment operator.
Definition: GSLSimAnnealing.h:256
ROOT::Math::GSLSimAnParams::iters_fixed_T
int iters_fixed_T
Definition: GSLSimAnnealing.h:215
ROOT::Math::GSLSimAnFunc
GSLSimAnFunc class description.
Definition: GSLSimAnnealing.h:88
ROOT::Math::IBaseFunctionMultiDimTempl
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
ROOT::Math::GSLSimAnFunc::Scale
double Scale(unsigned int i) const
Definition: GSLSimAnnealing.h:179
ROOT::Math::GSLRandomEngine
GSLRandomEngine Base class for all GSL random engines, normally user instantiate the derived classes ...
Definition: GSLRndmEngines.h:103
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::Math::GSLSimAnParams::GSLSimAnParams
GSLSimAnParams()
Definition: GSLSimAnnealing.h:202
Math
ROOT::Math::GSLSimAnFunc::Clone
virtual GSLSimAnFunc * Clone() const
clone method.
Definition: GSLSimAnnealing.h:130
ROOT::Math::GSLSimAnParams::mu_t
double mu_t
Definition: GSLSimAnnealing.h:220