GSLMultiMinimizer.h

Go to the documentation of this file.
00001 // @(#)root/mathmore:$Id: GSLMultiMinimizer.h 25486 2008-09-22 12:43:03Z moneta $
00002 // Author: L. Moneta Tue Dec 19 14:09:15 2006
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  * This library is free software; you can redistribute it and/or      *
00009  * modify it under the terms of the GNU General Public License        *
00010  * as published by the Free Software Foundation; either version 2     *
00011  * of the License, or (at your option) any later version.             *
00012  *                                                                    *
00013  * This library is distributed in the hope that it will be useful,    *
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
00016  * General Public License for more details.                           *
00017  *                                                                    *
00018  * You should have received a copy of the GNU General Public License  *
00019  * along with this library (see file COPYING); if not, write          *
00020  * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
00021  * 330, Boston, MA 02111-1307 USA, or contact the author.             *
00022  *                                                                    *
00023  **********************************************************************/
00024 
00025 // Header file for class GSLMultiMinimizer
00026 
00027 #ifndef ROOT_Math_GSLMultiMinimizer
00028 #define ROOT_Math_GSLMultiMinimizer
00029 
00030 #include "gsl/gsl_vector.h"
00031 #include "gsl/gsl_multimin.h"
00032 #include "GSLMultiMinFunctionWrapper.h"
00033 
00034 #include "Math/Error.h"
00035 
00036 #include "Math/IFunction.h"
00037 
00038 #include <cassert> 
00039 
00040 namespace ROOT { 
00041 
00042    namespace Math { 
00043 
00044 
00045 /** 
00046    GSLMultiMinimizer class , for minimizing multi-dimensional function 
00047    using derivatives 
00048 
00049    @ingroup MultiMin
00050 
00051 */ 
00052 class GSLMultiMinimizer {
00053 
00054 public: 
00055 
00056    /** 
00057       Default constructor
00058    */ 
00059    GSLMultiMinimizer (ROOT::Math::EGSLMinimizerType type)  : 
00060       fMinimizer(0),
00061       fType(0),
00062       fVec(0)
00063    {
00064       switch(type)
00065       {
00066       case ROOT::Math::kConjugateFR :
00067          fType = gsl_multimin_fdfminimizer_conjugate_fr; 
00068          break; 
00069       case ROOT::Math::kConjugatePR : 
00070          fType = gsl_multimin_fdfminimizer_conjugate_pr; 
00071          break;
00072       case ROOT::Math::kVectorBFGS : 
00073          fType = gsl_multimin_fdfminimizer_vector_bfgs; 
00074          break; 
00075       case ROOT::Math::kVectorBFGS2 : 
00076 #if defined (GSL_VERSION_NUM) && GSL_VERSION_NUM >= 1009 
00077          // bfgs2 is available only for v>= 1.9 
00078          fType = gsl_multimin_fdfminimizer_vector_bfgs2; 
00079 #else 
00080          MATH_INFO_MSG("GSLMultiMinimizer","minimizer BFSG2 does not exist with this GSL version , use BFGS");
00081          fType = gsl_multimin_fdfminimizer_vector_bfgs;
00082 #endif 
00083          break; 
00084       case ROOT::Math::kSteepestDescent:
00085          fType = gsl_multimin_fdfminimizer_steepest_descent; 
00086          break; 
00087       default: 
00088          fType = gsl_multimin_fdfminimizer_conjugate_fr; 
00089          break; 
00090       }
00091                                      
00092    }
00093 
00094    /** 
00095       Destructor 
00096    */ 
00097    ~GSLMultiMinimizer ()  {
00098       if (fMinimizer != 0 ) gsl_multimin_fdfminimizer_free(fMinimizer); 
00099       // can free vector (is copied inside)
00100       if (fVec != 0) gsl_vector_free(fVec); 
00101    }  
00102 
00103 private:
00104    // usually copying is non trivial, so we make this unaccessible
00105 
00106    /** 
00107       Copy constructor
00108    */ 
00109    GSLMultiMinimizer(const GSLMultiMinimizer &) {} 
00110 
00111    /** 
00112       Assignment operator
00113    */ 
00114    GSLMultiMinimizer & operator = (const GSLMultiMinimizer & rhs)  {
00115       if (this == &rhs) return *this;  // time saving self-test
00116       return *this;
00117    }
00118 
00119 public: 
00120 
00121    /**
00122       set the function to be minimize the initial minimizer parameters, 
00123       step size and tolerance in the line search 
00124     */ 
00125    int Set(const ROOT::Math::IMultiGradFunction & func, const double * x, double stepSize, double tol) { 
00126       // create function wrapper
00127       fFunc.SetFunction(func); 
00128       // create minimizer object (free previous one if already existing) 
00129       unsigned int ndim = func.NDim(); 
00130       CreateMinimizer( ndim ); 
00131       // set initial values 
00132       if (fVec != 0) gsl_vector_free(fVec);   
00133       fVec = gsl_vector_alloc( ndim ); 
00134       std::copy(x,x+ndim, fVec->data); 
00135       assert(fMinimizer != 0); 
00136       return gsl_multimin_fdfminimizer_set(fMinimizer, fFunc.GetFunc(), fVec, stepSize, tol); 
00137    }
00138 
00139    /// create the minimizer from the type and size 
00140    void CreateMinimizer(unsigned int n) { 
00141       if (fMinimizer) gsl_multimin_fdfminimizer_free(fMinimizer); 
00142       fMinimizer = gsl_multimin_fdfminimizer_alloc(fType, n);
00143    }  
00144 
00145    std::string Name() const { 
00146       if (fMinimizer == 0) return "undefined";
00147       return std::string(gsl_multimin_fdfminimizer_name(fMinimizer) ); 
00148    }
00149    
00150    int Iterate() { 
00151       if (fMinimizer == 0) return -1; 
00152       return gsl_multimin_fdfminimizer_iterate(fMinimizer); 
00153    }
00154 
00155    /// x values at the minimum 
00156    double * X() const { 
00157       if (fMinimizer == 0) return 0; 
00158       gsl_vector * x =  gsl_multimin_fdfminimizer_x(fMinimizer);       
00159       return x->data; 
00160    } 
00161 
00162    /// function value at the minimum 
00163    double Minimum() const { 
00164       if (fMinimizer == 0) return 0; 
00165       return gsl_multimin_fdfminimizer_minimum(fMinimizer);       
00166    }
00167 
00168    /// gradient value at the minimum 
00169    double * Gradient() const { 
00170       if (fMinimizer == 0) return 0; 
00171       gsl_vector * g =  gsl_multimin_fdfminimizer_gradient(fMinimizer);       
00172       return g->data; 
00173    }
00174 
00175    /// restart minimization from current point
00176    int Restart() { 
00177       if (fMinimizer == 0) return -1; 
00178       return gsl_multimin_fdfminimizer_restart(fMinimizer); 
00179    }
00180 
00181    /// test gradient (ask from minimizer gradient vector) 
00182    int TestGradient(double absTol) const { 
00183       if (fMinimizer == 0) return -1; 
00184       gsl_vector * g =  gsl_multimin_fdfminimizer_gradient(fMinimizer);       
00185       return gsl_multimin_test_gradient( g, absTol);      
00186    }
00187 
00188    /// test gradient (require a vector gradient) 
00189    int TestGradient(const double * g, double absTol) const { 
00190       if (fVec == 0 ) return -1; 
00191       unsigned int n = fVec->size; 
00192       if (n == 0 ) return -1; 
00193       std::copy(g,g+n, fVec->data);
00194       return gsl_multimin_test_gradient( fVec, absTol);      
00195    }
00196 
00197 
00198 private: 
00199 
00200    gsl_multimin_fdfminimizer * fMinimizer; 
00201    GSLMultiMinDerivFunctionWrapper fFunc; 
00202    const gsl_multimin_fdfminimizer_type * fType; 
00203    // cached vector to avoid re-allocating every time a new one
00204    mutable gsl_vector * fVec; 
00205 
00206 }; 
00207 
00208    } // end namespace Math
00209 
00210 } // end namespace ROOT
00211 
00212 
00213 #endif /* ROOT_Math_GSLMultiMinimizer */

Generated on Tue Jul 5 14:34:56 2011 for ROOT_528-00b_version by  doxygen 1.5.1