Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
GSLMultiMinimizer.h
Go to the documentation of this file.
1// @(#)root/mathmore:$Id$
2// Author: L. Moneta Tue Dec 19 14:09:15 2006
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 GSLMultiMinimizer
26
27#ifndef ROOT_Math_GSLMultiMinimizer
28#define ROOT_Math_GSLMultiMinimizer
29
30#include "gsl/gsl_vector.h"
31#include "gsl/gsl_multimin.h"
32#include "gsl/gsl_version.h"
34
35#include "Math/Error.h"
36
37#include "Math/IFunction.h"
38
39#include <cassert>
40#include <string>
41
42namespace ROOT {
43
44 namespace Math {
45
46
47/**
48 GSLMultiMinimizer class , for minimizing multi-dimensional function
49 using derivatives
50
51 @ingroup MultiMin
52
53*/
55
56public:
57
58 /**
59 Default constructor
60 */
62 fMinimizer(0),
63 fType(0),
64 fVec(0)
65 {
66 switch(type)
67 {
69 fType = gsl_multimin_fdfminimizer_conjugate_fr;
70 break;
72 fType = gsl_multimin_fdfminimizer_conjugate_pr;
73 break;
75 fType = gsl_multimin_fdfminimizer_vector_bfgs;
76 break;
78#if (GSL_MAJOR_VERSION > 1) || ((GSL_MAJOR_VERSION == 1) && (GSL_MINOR_VERSION >= 9))
79 // bfgs2 is available only for v>= 1.9
80 fType = gsl_multimin_fdfminimizer_vector_bfgs2;
81#else
82 MATH_INFO_MSG("GSLMultiMinimizer","minimizer BFSG2 does not exist with this GSL version , use BFGS");
83 fType = gsl_multimin_fdfminimizer_vector_bfgs;
84#endif
85 break;
87 fType = gsl_multimin_fdfminimizer_steepest_descent;
88 break;
89 default:
90 fType = gsl_multimin_fdfminimizer_conjugate_fr;
91 break;
92 }
93
94 }
95
96 /**
97 Destructor
98 */
100 if (fMinimizer != 0 ) gsl_multimin_fdfminimizer_free(fMinimizer);
101 // can free vector (is copied inside)
102 if (fVec != 0) gsl_vector_free(fVec);
103 }
104
105 // usually copying is non trivial, so we delete this
110
111 /**
112 set the function to be minimize the initial minimizer parameters,
113 step size and tolerance in the line search
114 */
115 int Set(const ROOT::Math::IMultiGradFunction & func, const double * x, double stepSize, double tol) {
116 // create function wrapper
117 fFunc.SetFunction(func);
118 // create minimizer object (free previous one if already existing)
119 unsigned int ndim = func.NDim();
120 CreateMinimizer( ndim );
121 // set initial values
122 if (fVec != 0) gsl_vector_free(fVec);
123 fVec = gsl_vector_alloc( ndim );
124 std::copy(x,x+ndim, fVec->data);
125 assert(fMinimizer != 0);
126 return gsl_multimin_fdfminimizer_set(fMinimizer, fFunc.GetFunc(), fVec, stepSize, tol);
127 }
128
129 /// create the minimizer from the type and size
130 void CreateMinimizer(unsigned int n) {
131 if (fMinimizer) gsl_multimin_fdfminimizer_free(fMinimizer);
132 fMinimizer = gsl_multimin_fdfminimizer_alloc(fType, n);
133 }
134
135 std::string Name() const {
136 if (fMinimizer == 0) return "undefined";
137 return std::string(gsl_multimin_fdfminimizer_name(fMinimizer) );
138 }
139
140 int Iterate() {
141 if (fMinimizer == 0) return -1;
142 return gsl_multimin_fdfminimizer_iterate(fMinimizer);
143 }
144
145 /// x values at the minimum
146 double * X() const {
147 if (fMinimizer == 0) return 0;
148 gsl_vector * x = gsl_multimin_fdfminimizer_x(fMinimizer);
149 return x->data;
150 }
151
152 /// function value at the minimum
153 double Minimum() const {
154 if (fMinimizer == 0) return 0;
155 return gsl_multimin_fdfminimizer_minimum(fMinimizer);
156 }
157
158 /// gradient value at the minimum
159 double * Gradient() const {
160 if (fMinimizer == 0) return 0;
161 gsl_vector * g = gsl_multimin_fdfminimizer_gradient(fMinimizer);
162 return g->data;
163 }
164
165 /// restart minimization from current point
166 int Restart() {
167 if (fMinimizer == 0) return -1;
168 return gsl_multimin_fdfminimizer_restart(fMinimizer);
169 }
170
171 /// test gradient (ask from minimizer gradient vector)
172 int TestGradient(double absTol) const {
173 if (fMinimizer == 0) return -1;
174 gsl_vector * g = gsl_multimin_fdfminimizer_gradient(fMinimizer);
175 return gsl_multimin_test_gradient( g, absTol);
176 }
177
178 /// test gradient (require a vector gradient)
179 int TestGradient(const double * g, double absTol) const {
180 if (fVec == 0 ) return -1;
181 unsigned int n = fVec->size;
182 if (n == 0 ) return -1;
183 std::copy(g,g+n, fVec->data);
184 return gsl_multimin_test_gradient( fVec, absTol);
185 }
186
187
188private:
189
190 gsl_multimin_fdfminimizer * fMinimizer;
192 const gsl_multimin_fdfminimizer_type * fType;
193 // cached vector to avoid re-allocating every time a new one
194 mutable gsl_vector * fVec;
195
196};
197
198 } // end namespace Math
199
200} // end namespace ROOT
201
202
203#endif /* ROOT_Math_GSLMultiMinimizer */
#define MATH_INFO_MSG(loc, str)
Pre-processor macro to report messages which can be configured to use ROOT error or simply an std::io...
Definition Error.h:77
#define g(i)
Definition RSha256.hxx:105
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Wrapper for a multi-dimensional function with derivatives used in GSL multidim minimization algorithm...
void SetFunction(const FuncType &f)
Fill gsl function structure from a C++ Function class.
GSLMultiMinimizer class , for minimizing multi-dimensional function using derivatives.
GSLMultiMinDerivFunctionWrapper fFunc
int Set(const ROOT::Math::IMultiGradFunction &func, const double *x, double stepSize, double tol)
set the function to be minimize the initial minimizer parameters, step size and tolerance in the line...
GSLMultiMinimizer & operator=(const GSLMultiMinimizer &rhs)=delete
const gsl_multimin_fdfminimizer_type * fType
gsl_multimin_fdfminimizer * fMinimizer
double Minimum() const
function value at the minimum
double * Gradient() const
gradient value at the minimum
GSLMultiMinimizer(const GSLMultiMinimizer &)=delete
int Restart()
restart minimization from current point
int TestGradient(const double *g, double absTol) const
test gradient (require a vector gradient)
GSLMultiMinimizer(ROOT::Math::EGSLMinimizerType type)
Default constructor.
GSLMultiMinimizer(GSLMultiMinimizer &&)=delete
double * X() const
x values at the minimum
void CreateMinimizer(unsigned int n)
create the minimizer from the type and size
int TestGradient(double absTol) const
test gradient (ask from minimizer gradient vector)
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition IFunction.h:168
virtual unsigned int NDim() const=0
Retrieve the dimension of the function.
EGSLMinimizerType
enumeration specifying the types of GSL minimizers
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
Namespace for new Math classes and functions.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...