Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:17

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 <vector>
0033 
0034 namespace ROOT {
0035 
0036    namespace Math {
0037 
0038       class GSLRandomEngine;
0039 
0040 //_____________________________________________________________________________
0041 /**
0042    GSLSimAnFunc class description.
0043    Interface class for the  objetive function to be used in simulated annealing
0044    If user wants to re-implement some of the methods (like the one defining the metric) which are used by the
0045    the simulated annealing algorithm must build a user derived class.
0046    NOTE: Derived classes must re-implement the assignment and copy constructor to call them of the parent class
0047 
0048    @ingroup MultiMin
0049  */
0050 class GSLSimAnFunc {
0051 public:
0052 
0053    /**
0054       construct from an interface of a multi-dimensional function
0055     */
0056    GSLSimAnFunc(const ROOT::Math::IMultiGenFunction & func, const double * x);
0057 
0058    /**
0059       construct from an interface of a multi-dimensional function
0060       Use optionally a scale factor (for each coordinate) which can  be used to scale the step sizes
0061       (this is used for example by the minimization algorithm)
0062     */
0063    GSLSimAnFunc(const ROOT::Math::IMultiGenFunction & func, const double * x, const double * scale);
0064 
0065 protected:
0066 
0067    /**
0068       derived classes might need to re-define completely the class
0069     */
0070    GSLSimAnFunc() :
0071       fFunc(nullptr)
0072    {}
0073 
0074 public:
0075 
0076 
0077    /// virtual destructor (no operations)
0078    virtual ~GSLSimAnFunc() { } //
0079 
0080 
0081    /**
0082       fast copy method called by GSL simulated annealing internally
0083       copy only the things which have been changed
0084       must be re-implemented by derived classes if needed
0085    */
0086    virtual GSLSimAnFunc & FastCopy(const GSLSimAnFunc & f);
0087 
0088 
0089    /**
0090       clone method. Needs to be re-implemented by the derived classes for deep  copying
0091     */
0092    virtual GSLSimAnFunc * Clone() const {
0093       return new GSLSimAnFunc(*this);
0094    }
0095 
0096    /**
0097       evaluate the energy ( objective function value)
0098       re-implement by derived classes if needed to be modified
0099     */
0100    virtual double Energy() const;
0101 
0102    /**
0103       change the x[i] value using a random value urndm generated between [0,1]
0104       up to a maximum value maxstep
0105       re-implement by derived classes if needed to be modified
0106     */
0107    virtual void Step(const GSLRandomEngine & r, double maxstep);
0108 
0109    /**
0110       calculate the distance (metric) between  this one and another configuration
0111       Presently a cartesian metric is used.
0112       re-implement by derived classes if needed to be modified
0113     */
0114    virtual double Distance(const GSLSimAnFunc & func) const;
0115 
0116    /**
0117       print the position in the standard output std::ostream
0118       GSL prints in addition n iteration, n function calls, temperature and energy
0119       re-implement by derived classes if necessary
0120     */
0121    virtual void Print();
0122 
0123    /**
0124        change the x values (used by sim annealing to take a step)
0125     */
0126    void SetX(const double * x) {
0127       std::copy(x, x+ fX.size(), fX.begin() );
0128    }
0129 
0130    template <class IT>
0131    void SetX(IT begin, IT end) {
0132       std::copy(begin, end, fX.begin() );
0133    }
0134 
0135    unsigned int NDim() const { return fX.size(); }
0136 
0137    double X(unsigned int i) const { return fX[i]; }
0138 
0139    const std::vector<double> &  X() const { return fX; }
0140 
0141    double Scale(unsigned int i) const { return fScale[i]; }
0142 
0143    void SetX(unsigned int i, double x) { fX[i] = x; }
0144 
0145    // use compiler generated  copy ctror and assignment operators
0146 
0147 private:
0148 
0149    std::vector<double>  fX;
0150    std::vector<double>  fScale;
0151    const ROOT::Math::IMultiGenFunction * fFunc;
0152 
0153 };
0154 
0155 //_____________________________________________________
0156 /**
0157     structure holding the simulated annealing parameters
0158 
0159    @ingroup MultiMin
0160 */
0161 struct GSLSimAnParams {
0162 
0163    // constructor with some default values
0164    GSLSimAnParams() {
0165       n_tries =    200;
0166       iters_fixed_T =  10;
0167       step_size =   10;
0168       // the following parameters are for the Boltzmann distribution */
0169       k = 1.0;
0170       t_initial =  0.002;
0171       mu_t =  1.005;
0172       t_min = 2.0E-6;
0173    }
0174 
0175 
0176    int n_tries;            // number of points to try for each step
0177    int iters_fixed_T;      // number of iterations at each temperature
0178    double step_size;       // max step size used in random walk
0179    /// parameters for the Boltzman distribution
0180    double k;
0181    double t_initial;
0182    double mu_t;
0183    double t_min;
0184 };
0185 
0186 //___________________________________________________________________________
0187 /**
0188    GSLSimAnnealing class for performing  a simulated annealing search of
0189    a multidimensional function
0190 
0191    @ingroup MultiMin
0192 */
0193 class GSLSimAnnealing {
0194 
0195 public:
0196 
0197    /**
0198       Default constructor
0199    */
0200    GSLSimAnnealing ();
0201 
0202    /**
0203       Destructor (no operations)
0204    */
0205    ~GSLSimAnnealing ()  {}
0206 
0207 private:
0208    // usually copying is non trivial, so we make this unaccessible
0209 
0210    /**
0211       Copy constructor
0212    */
0213    GSLSimAnnealing(const GSLSimAnnealing &) {}
0214 
0215    /**
0216       Assignment operator
0217    */
0218    GSLSimAnnealing & operator = (const GSLSimAnnealing & rhs)  {
0219       if (this == &rhs) return *this;  // time saving self-test
0220       return *this;
0221    }
0222 
0223 public:
0224 
0225 
0226    /**
0227       solve the simulated annealing given a multi-dim function, the initial vector parameters
0228       and a vector containing the scaling factors for the parameters
0229    */
0230    int Solve(const ROOT::Math::IMultiGenFunction & func, const double * x0, const double * scale, double * xmin, bool debug = false);
0231 
0232    /**
0233       solve the simulated annealing given a GSLSimAnFunc object
0234       The object will contain the initial state at the beginning and the final minimum state at the end
0235    */
0236    int Solve(GSLSimAnFunc & func, bool debug = false);
0237 
0238 
0239    GSLSimAnParams & Params() { return fParams; }
0240    const GSLSimAnParams & Params() const { return fParams; }
0241    void SetParams(const GSLSimAnParams & params) { fParams = params; }
0242 
0243 
0244 protected:
0245 
0246 
0247 private:
0248 
0249    GSLSimAnParams fParams; // parameters for GSLSimAnnealig
0250 
0251 };
0252 
0253    } // end namespace Math
0254 
0255 } // end namespace ROOT
0256 
0257 
0258 #endif /* ROOT_Math_GSLSimAnnealing */