|
|
|||
File indexing completed on 2026-08-05 09:32:02
0001 // @(#)root/mathmore:$Id$ 0002 // Author: L. Moneta Thu Jan 25 11:13:48 2007 0003 0004 /********************************************************************** 0005 * * 0006 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT * 0007 * * 0008 * This library is free software; you can redistribute it and/or * 0009 * modify it under the terms of the GNU General Public License * 0010 * as published by the Free Software Foundation; either version 2 * 0011 * of the License, or (at your option) any later version. * 0012 * * 0013 * This library is distributed in the hope that it will be useful, * 0014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 0016 * General Public License for more details. * 0017 * * 0018 * You should have received a copy of the GNU General Public License * 0019 * along with this library (see file COPYING); if not, write * 0020 * to the Free Software Foundation, Inc., 59 Temple Place, Suite * 0021 * 330, Boston, MA 02111-1307 USA, or contact the author. * 0022 * * 0023 **********************************************************************/ 0024 0025 // Header file for class GSLSimAnnealing 0026 0027 #ifndef ROOT_Math_GSLSimAnnealing 0028 #define ROOT_Math_GSLSimAnnealing 0029 0030 #include "Math/IFunctionfwd.h" 0031 0032 #include <algorithm> 0033 #include <vector> 0034 0035 namespace ROOT { 0036 0037 namespace Math { 0038 0039 class GSLRandomEngine; 0040 0041 //_____________________________________________________________________________ 0042 /** 0043 GSLSimAnFunc class description. 0044 Interface class for the objetive function to be used in simulated annealing 0045 If user wants to re-implement some of the methods (like the one defining the metric) which are used by the 0046 the simulated annealing algorithm must build a user derived class. 0047 NOTE: Derived classes must re-implement the assignment and copy constructor to call them of the parent class 0048 0049 @ingroup MultiMin 0050 */ 0051 class GSLSimAnFunc { 0052 public: 0053 0054 /** 0055 construct from an interface of a multi-dimensional function 0056 */ 0057 GSLSimAnFunc(const ROOT::Math::IMultiGenFunction & func, const double * x); 0058 0059 /** 0060 construct from an interface of a multi-dimensional function 0061 Use optionally a scale factor (for each coordinate) which can be used to scale the step sizes 0062 (this is used for example by the minimization algorithm) 0063 */ 0064 GSLSimAnFunc(const ROOT::Math::IMultiGenFunction & func, const double * x, const double * scale); 0065 0066 protected: 0067 0068 /** 0069 derived classes might need to re-define completely the class 0070 */ 0071 GSLSimAnFunc() : 0072 fFunc(nullptr) 0073 {} 0074 0075 public: 0076 0077 0078 /// virtual destructor (no operations) 0079 virtual ~GSLSimAnFunc() { } // 0080 0081 0082 /** 0083 fast copy method called by GSL simulated annealing internally 0084 copy only the things which have been changed 0085 must be re-implemented by derived classes if needed 0086 */ 0087 virtual GSLSimAnFunc & FastCopy(const GSLSimAnFunc & f); 0088 0089 0090 /** 0091 clone method. Needs to be re-implemented by the derived classes for deep copying 0092 */ 0093 virtual GSLSimAnFunc * Clone() const { 0094 return new GSLSimAnFunc(*this); 0095 } 0096 0097 /** 0098 evaluate the energy ( objective function value) 0099 re-implement by derived classes if needed to be modified 0100 */ 0101 virtual double Energy() const; 0102 0103 /** 0104 change the x[i] value using a random value urndm generated between [0,1] 0105 up to a maximum value maxstep 0106 re-implement by derived classes if needed to be modified 0107 */ 0108 virtual void Step(const GSLRandomEngine & r, double maxstep); 0109 0110 /** 0111 calculate the distance (metric) between this one and another configuration 0112 Presently a cartesian metric is used. 0113 re-implement by derived classes if needed to be modified 0114 */ 0115 virtual double Distance(const GSLSimAnFunc & func) const; 0116 0117 /** 0118 print the position in the standard output std::ostream 0119 GSL prints in addition n iteration, n function calls, temperature and energy 0120 re-implement by derived classes if necessary 0121 */ 0122 virtual void Print(); 0123 0124 /** 0125 change the x values (used by sim annealing to take a step) 0126 */ 0127 void SetX(const double * x) { 0128 std::copy(x, x+ fX.size(), fX.begin() ); 0129 } 0130 0131 template <class IT> 0132 void SetX(IT begin, IT end) { 0133 std::copy(begin, end, fX.begin() ); 0134 } 0135 0136 unsigned int NDim() const { return fX.size(); } 0137 0138 double X(unsigned int i) const { return fX[i]; } 0139 0140 const std::vector<double> & X() const { return fX; } 0141 0142 double Scale(unsigned int i) const { return fScale[i]; } 0143 0144 void SetX(unsigned int i, double x) { fX[i] = x; } 0145 0146 // use compiler generated copy ctror and assignment operators 0147 0148 private: 0149 0150 std::vector<double> fX; 0151 std::vector<double> fScale; 0152 const ROOT::Math::IMultiGenFunction * fFunc; 0153 0154 }; 0155 0156 //_____________________________________________________ 0157 /** 0158 structure holding the simulated annealing parameters 0159 0160 @ingroup MultiMin 0161 */ 0162 struct GSLSimAnParams { 0163 0164 // constructor with some default values 0165 GSLSimAnParams() { 0166 n_tries = 200; 0167 iters_fixed_T = 10; 0168 step_size = 10; 0169 // the following parameters are for the Boltzmann distribution */ 0170 k = 1.0; 0171 t_initial = 0.002; 0172 mu_t = 1.005; 0173 t_min = 2.0E-6; 0174 } 0175 0176 0177 int n_tries; // number of points to try for each step 0178 int iters_fixed_T; // number of iterations at each temperature 0179 double step_size; // max step size used in random walk 0180 /// parameters for the Boltzman distribution 0181 double k; 0182 double t_initial; 0183 double mu_t; 0184 double t_min; 0185 }; 0186 0187 //___________________________________________________________________________ 0188 /** 0189 GSLSimAnnealing class for performing a simulated annealing search of 0190 a multidimensional function 0191 0192 @ingroup MultiMin 0193 */ 0194 class GSLSimAnnealing { 0195 0196 public: 0197 0198 /** 0199 Default constructor 0200 */ 0201 GSLSimAnnealing (); 0202 0203 /** 0204 Destructor (no operations) 0205 */ 0206 ~GSLSimAnnealing () {} 0207 0208 // usually copying is non trivial, so we delete this 0209 GSLSimAnnealing(const GSLSimAnnealing &) = delete; 0210 GSLSimAnnealing & operator = (const GSLSimAnnealing & rhs) = delete; 0211 GSLSimAnnealing(GSLSimAnnealing &&) = delete; 0212 GSLSimAnnealing & operator = (GSLSimAnnealing && rhs) = delete; 0213 0214 /** 0215 solve the simulated annealing given a multi-dim function, the initial vector parameters 0216 and a vector containing the scaling factors for the parameters 0217 */ 0218 int Solve(const ROOT::Math::IMultiGenFunction & func, const double * x0, const double * scale, double * xmin, bool debug = false); 0219 0220 /** 0221 solve the simulated annealing given a GSLSimAnFunc object 0222 The object will contain the initial state at the beginning and the final minimum state at the end 0223 */ 0224 int Solve(GSLSimAnFunc & func, bool debug = false); 0225 0226 0227 GSLSimAnParams & Params() { return fParams; } 0228 const GSLSimAnParams & Params() const { return fParams; } 0229 void SetParams(const GSLSimAnParams & params) { fParams = params; } 0230 0231 private: 0232 0233 GSLSimAnParams fParams; // parameters for GSLSimAnnealig 0234 0235 }; 0236 0237 } // end namespace Math 0238 0239 } // end namespace ROOT 0240 0241 0242 #endif /* ROOT_Math_GSLSimAnnealing */
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|