ROOT  6.06/09
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  */
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 */
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 = 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;
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 
195 public:
196 
197  /**
198  Default constructor
199  */
200  GSLSimAnnealing ();
201 
202  /**
203  Destructor (no operations)
204  */
206 
207 private:
208  // usually copying is non trivial, so we make this unaccessible
209 
210  /**
211  Copy constructor
212  */
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 
242 
243 protected:
244 
245 
246 private:
247 
248  GSLSimAnParams fParams; // parameters for GSLSimAnnealig
249 
250 };
251 
252  } // end namespace Math
253 
254 } // end namespace ROOT
255 
256 
257 #endif /* ROOT_Math_GSLSimAnnealing */
GSLSimAnnealing()
Default constructor.
GSLSimAnnealing class for performing a simulated annealing search of a multidimensional function...
float xmin
Definition: THbookFile.cxx:93
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
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...
const ROOT::Math::IMultiGenFunction * fFunc
GSLSimAnFunc class description.
virtual double Distance(const GSLSimAnFunc &func) const
calculate the distance (metric) between this one and another configuration Presently a cartesian metr...
unsigned int NDim() const
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 ...
Double_t x[n]
Definition: legend1.C:17
double k
parameters for the Boltzman distribution
GSLRandomEngine Base class for all GSL random engines, normally user instantiate the derived classes ...
void SetX(unsigned int i, double x)
virtual void Print()
print the position in the standard output std::ostream GSL prints in addition n iteration, n function calls, temperature and energy re-implement by derived classes if necessary
virtual double Energy() const
evaluate the energy ( objective function value) re-implement by derived classes if needed to be modif...
GSLSimAnnealing(const GSLSimAnnealing &)
Copy constructor.
void SetX(const double *x)
change the x values (used by sim annealing to take a step)
std::vector< double > fScale
ROOT::R::TRInterface & r
Definition: Object.C:4
const GSLSimAnParams & Params() const
virtual GSLSimAnFunc & FastCopy(const GSLSimAnFunc &f)
fast copy method called by GSL simuated annealing internally copy only the things which have been cha...
double f(double x)
GSLSimAnFunc()
derived classes might need to re-define completely the class
double func(double *x, double *p)
Definition: stressTF1.cxx:213
Namespace for new Math classes and functions.
virtual GSLSimAnFunc * Clone() const
clone method.
double Scale(unsigned int i) const
bool debug
GSLSimAnnealing & operator=(const GSLSimAnnealing &rhs)
Assignment operator.
~GSLSimAnnealing()
Destructor (no operations)
std::vector< double > fX
const std::vector< double > & X() const
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
virtual ~GSLSimAnFunc()
virtual distructor (no operations)
structure holding the simulated annealing parameters
void SetX(IT begin, IT end)
double X(unsigned int i) const