20#include <gsl/gsl_blas.h>
21#include <gsl/gsl_version.h>
26Kernel::Logger
g_log(
"LevenbergMarquardtMinimizer");
28bool cannotReachSpecifiedToleranceInF(
int errorCode) {
return errorCode == GSL_ETOLF; }
29bool cannotReachSpecifiedToleranceInX(
int errorCode) {
return errorCode == GSL_ETOLX; }
37 :
m_data(
nullptr), gslContainer(), m_gslSolver(
nullptr), m_function(), m_absError(1e-4), m_relError(1e-4) {
38 declareProperty(
"AbsError", m_absError,
39 "Absolute error allowed for "
40 "parameters - a stopping parameter "
42 declareProperty(
"RelError", m_relError,
43 "Relative error allowed for "
44 "parameters - a stopping parameter "
50 auto leastSquares = std::dynamic_pointer_cast<CostFunctions::CostFuncLeastSquares>(costFunction);
52 m_data = std::make_unique<GSL_FitData>(leastSquares);
54 throw std::runtime_error(
"LevenbergMarquardt can only be used with Least "
55 "squares cost function.");
59 const gsl_multifit_fdfsolver_type *T = gsl_multifit_fdfsolver_lmsder;
73 throw std::runtime_error(
"Levenberg-Marquardt minimizer failed to initialize. \n" +
std::to_string(
m_data->n) +
78 m_function = leastSquares->getFittingFunction();
91 int retVal = gsl_multifit_fdfsolver_iterate(
m_gslSolver);
102 if (retVal == GSL_CONTINUE || retVal == GSL_ENOPROG) {
104 for (
size_t i = 0; i <
m_function->nParams(); i++) {
111 retVal = GSL_CONTINUE;
114 if (retVal && retVal != GSL_CONTINUE) {
116 if (cannotReachSpecifiedToleranceInF(retVal)) {
118 }
else if (cannotReachSpecifiedToleranceInX(retVal)) {
125 return retVal != GSL_SUCCESS;
143#if GSL_MAJOR_VERSION < 2
148 gsl_multifit_covar(J, epsrel, covar);
#define DECLARE_FUNCMINIMIZER(classname, username)
Macro for declaring a new type of minimizers to be used with the FuncMinimizerFactory.
const std::vector< Type > & m_data
std::string m_errorString
Error string.
Implementing Levenberg-Marquardt by wrapping the IFuncMinimizer interface around the GSL implementati...
double m_absError
Absolute error required for parameters.
std::unique_ptr< GSL_FitData > m_data
GSL data container.
double m_relError
Relative error required for parameters.
gsl_multifit_fdfsolver * m_gslSolver
pointer to the GSL solver doing the work
gsl_multifit_function_fdf gslContainer
GSL minimizer container.
double costFunctionVal() override
Return current value of the cost function.
API::IFunction_sptr m_function
Stored to access IFunction interface in iterate()
bool iterate(size_t) override
Do one iteration.
~LevenbergMarquardtMinimizer() override
constructor and destructor
void initialize(API::ICostFunction_sptr costFunction, size_t maxIterations=0) override
Initialize minimizer, i.e. pass a function to minimize.
void calCovarianceMatrix(double epsrel, gsl_matrix *covar)
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< ICostFunction > ICostFunction_sptr
define a shared pointer to a cost function
int gsl_fdf(const gsl_vector *x, void *params, gsl_vector *f, gsl_matrix *J)
Fit derivatives and function GSL wrapper.
int gsl_f(const gsl_vector *x, void *params, gsl_vector *f)
Fit GSL function wrapper.
int gsl_df(const gsl_vector *x, void *params, gsl_matrix *J)
Fit GSL derivative function wrapper.
std::string to_string(const wide_integer< Bits, Signed > &n)