8 #include "gsl/gsl_vector.h" 9 #include "gsl/gsl_multimin.h" 10 #include "gsl/gsl_math.h" 11 #include "gsl/gsl_matrix.h" 12 #include "gsl/gsl_linalg.h" 13 #include "gsl/gsl_blas.h" 14 #include "gsl/gsl_errno.h" 21 using namespace CLHEP;
33 (
const FuncMinimum::GenFunc&
func ,
34 FuncMinimum::Arg& arg )
39 const size_t N = func.dimensionality () ;
41 for(
size_t i = 0 ; i <
N ; ++i ) {
42 Genfun::GENFUNCTION
fun = func.partial(i);
43 m_grad.push_back (fun.clone());
51 double fun_gsl (
const gsl_vector* v ,
56 const FuncMinimum::GenFunc& eq = *(local->
equation());
59 for (
unsigned int i = 0; i < v->size; ++i) {
60 arg[i] = gsl_vector_get (v, i);
67 void dfun_gsl (
const gsl_vector* v ,
void * params,
75 for (
unsigned int i = 0; i < v->size; ++i) {
76 arg[i] = gsl_vector_get (v, i);
79 for(
unsigned int i = 0 ; i < df->size ; ++i ) {
80 Genfun::GENFUNCTION f = *(grad[i]);
81 gsl_vector_set ( df, i, f(arg) );
89 void fdfun_gsl (
const gsl_vector* v ,
94 *f = fun_gsl( v , params );
95 dfun_gsl ( v , params, df);
111 gsl_vector_view vect = gsl_vector_view_array ( &arg[0] ,
113 FuncMinimumMisc local (func, arg);
115 gsl_multimin_function_fdf
function;
117 function.f = &fun_gsl;
118 function.df = &dfun_gsl;
119 function.fdf = &fdfun_gsl;
120 function.n = vect.vector.size;
121 function.params = (
void*) &local;
125 const gsl_multimin_fdfminimizer_type *T =
m_type ;
127 gsl_multimin_fdfminimizer *
s;
129 s = gsl_multimin_fdfminimizer_alloc ( T, vect.vector.size);
131 gsl_multimin_fdfminimizer_set ( s, &
function,
132 &vect.vector, m_step_size, m_tol);
134 for( iter = 0 ; iter <
m_max_iter ; ++iter ) {
135 status = gsl_multimin_fdfminimizer_iterate (s);
139 (
"Error from gsl_multimin_fdfminimizer_iterate '" 143 status = gsl_multimin_test_gradient (s->gradient,
147 if ( status != GSL_CONTINUE ) {
break; }
150 for (
unsigned int i = 0; i < vect.vector.size; ++i) {
151 gsl_vector_set (&vect.vector, i, gsl_vector_get (s->x, i));
154 if (status == GSL_SUCCESS) {
156 <<
"We stopped in the method on the " << iter
157 <<
" iteration (we have maximum " << m_max_iter
158 <<
" iterations)" <<
endmsg;
160 msgStream() <<
"The Euclidean norm of gradient = " 161 << gsl_blas_dnrm2 (s->gradient)
162 <<
" by the absolute tolerance = " 163 << m_norm_gradient <<
endmsg;
164 }
else if (status == GSL_CONTINUE && iter <= m_max_iter ) {
165 return Error (
"Method finished with '" 169 return Error (
"Method finished with '" +
174 gsl_multimin_fdfminimizer_free (s);
177 return Error (
"Method finished with '" 199 return Error (
"MINIMUM IS NOT FOUND. StatusCode = '" 203 HepSymMatrix cov(arg.dimension(), 0);
204 for (
unsigned int i = 0; i < arg.dimension(); ++i) {
205 auto f = func.partial(i);
206 for (
unsigned int j = i; j < arg.dimension(); ++j) {
207 auto fij = f.partial(j);
208 cov(i+1, j+1) = 0.5 * fij(arg);
213 covar = cov.inverse(inv);
215 :
Error (
"Matrix of Error is not complete successful");
225 return Error (
"Could not initialize base class GaudiTool", sc);
230 m_type = gsl_multimin_fdfminimizer_conjugate_fr ;
232 <<
"Minimization algorithm to be used: " 233 <<
"'gsl_multimin_fdfminimizer_conjugate_fr'" 235 }
else if (
"conjugate_pr" ==
m_algType ) {
236 m_type = gsl_multimin_fdfminimizer_conjugate_pr ;
238 <<
"Minimization algorithm to be used: " 239 <<
"'gsl_multimin_fdfminimizer_conjugate_pr'" 241 }
else if (
"vector_bfgs" ==
m_algType ) {
242 m_type = gsl_multimin_fdfminimizer_vector_bfgs ;
244 <<
"Minimization algorithm to be used: " <<
245 "'gsl_multimin_fdfminimizer_vector_bfgs'" <<
endmsg;
246 }
else if (
"steepest_descent" ==
m_algType ) {
247 m_type = gsl_multimin_fdfminimizer_steepest_descent ;
249 <<
"Minimization algorithm to be used: " 250 <<
"'gsl_multimin_fdfminimizer_steepest_descent'"
Genfun::Argument Arg
Argument of function "GenFunc" (.
unsigned long getCode() const
Get the status code by value.
const Arg & argument() const
const GenFunc * equation() const
bool isFailure() const
Test for a status code of FAILURE.
#define DECLARE_COMPONENT(type)
Gaudi::Property< std::string > m_algType
Gaudi::Property< double > m_max_iter
This class is used for returning status codes from appropriate routines.
Genfun::AbsFunction GenFunc
Function which we minimize (.
CLHEP::HepSymMatrix Covariance
Covariance matrix (matrix of error) (.
The simplest concrete implementation of IFuncMinimum interface.
const Gradient & gradient() const
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
StatusCode minimum(const GenFunc &func, Arg &arg) const override
Find minimum of the function "GenFunc".
double fun(const std::vector< double > &x)
MsgStream & msgStream() const
Return an uninitialized MsgStream.
const gsl_multiroot_fdfsolver_type * m_type
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
StatusCode initialize() override
Overriding initialize.