21#include <gsl/gsl_blas.h>
26Kernel::Logger
g_log(
"LevenbergMarquardMD");
35 :
IFuncMinimizer(), m_tau(1e-6), m_mu(1e-6), m_nu(2.0), m_rho(1.0), m_F(0.0) {
36 declareProperty(
"MuMax", 1e6,
"Maximum value of mu - a stopping parameter in failure.");
37 declareProperty(
"AbsError", 0.0001,
38 "Absolute error allowed for parameters - "
39 "a stopping parameter in success.");
40 declareProperty(
"Verbose",
false,
"Make output more verbose.");
45 m_costFunction = std::dynamic_pointer_cast<CostFunctions::CostFuncFitting>(function);
47 throw std::invalid_argument(
"Levenberg-Marquardt minimizer works only with "
48 "functions which define the Hessian. Different function was given.");
62 throw std::runtime_error(
"Cost function isn't set up.");
92 g_log.
warning() <<
"===========================================================\n";
105 std::vector<double> sf(
n);
107 for (
size_t i = 0; i <
n; ++i) {
112 double tmp = H.get(i, i) +
m_mu *
d;
122 for (
size_t i = 0; i <
n; ++i) {
123 double d = dd.
get(i);
124 dd.
set(i,
d / sf[i]);
125 for (
size_t j = i; j <
n; ++j) {
126 const double f = sf[i] * sf[j];
127 double tmp = H.get(i, j);
128 H.set(i, j,
tmp / f);
131 H.set(j, i,
tmp / f);
136 if (verbose &&
m_rho > 0) {
139 for (
size_t j = 0; j <
n; ++j) {
152 }
catch (std::runtime_error &
error) {
159 for (
size_t j = 0; j <
n; ++j) {
164 for (
size_t j = 0; j <
n; ++j) {
171 for (
size_t i = 0; i <
n; ++i) {
172 double d = dx.
get(i);
173 dx.
set(i,
d / sf[i]);
175 dd.
set(i,
d * sf[i]);
186 for (
size_t i = 0; i <
n; ++i) {
187 g_log.
warning() <<
"Parameter(" << i <<
")=" << parameters[i] <<
'\n';
202 gsl_blas_dgemv(CblasNoTrans, -0.5, &tempHessianTrGSL.matrix, &dxGSL.vector, 1., &ddGSL.vector);
205 gsl_blas_ddot(&ddGSL.vector, &dxGSL.vector, &dL);
219 double dx_norm = gsl_blas_dnrm2(&dxGSL.vector);
220 if (dx_norm < absError) {
222 g_log.
warning() <<
"Successful fit, parameters changed by less than " << absError <<
'\n';
231 g_log.
warning() <<
"Successful fit, cost function didn't change.\n";
237 if (
fabs(dL) == 0.0) {
256 const double I3 = 1.0 / 3.0;
265 g_log.
warning() <<
"Good iteration, accept new parameters.\n";
277 g_log.
warning() <<
"Bad iteration, increase mu and revert changes to parameters.\n";
287 throw std::runtime_error(
"Cost function isn't set up.");
#define DECLARE_FUNCMINIMIZER(classname, username)
Macro for declaring a new type of minimizers to be used with the FuncMinimizerFactory.
An interface for function minimizers.
std::string m_errorString
Error string.
A wrapper around Eigen::Matrix.
const map_type inspector() const
Get a const copy of the Eigen matrix.
A wrapper around Eigen::Vector.
void set(const size_t i, const double value)
Set an element.
double get(const size_t i) const
Get an element.
vec_map_type & mutator()
Get the map of the eigen vector.
const vec_map_type inspector() const
Get the const map of the eigen vector.
Implementing Levenberg-Marquardt algorithm.
double m_mu
The damping mu parameter in the Levenberg-Marquardt method.
std::shared_ptr< CostFunctions::CostFuncFitting > m_costFunction
Pointer to the cost function.
double m_rho
The rho parameter in the Levenberg-Marquardt method.
std::vector< double > m_D
bool iterate(size_t iteration) override
Do one iteration.
void initialize(API::ICostFunction_sptr function, size_t maxIterations=0) override
Initialize minimizer, i.e. pass a function to minimize.
double m_nu
The nu parameter in the Levenberg-Marquardt method.
double m_F
To keep function value.
double costFunctionVal() override
Return current value of the cost function.
double m_tau
The tau parameter in the Levenberg-Marquardt method.
void warning(const std::string &msg)
Logs at warning level.
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
gsl_vector_view getGSLVectorView(vec_map_type &v)
take data from Eigen Vector and take a gsl view
gsl_matrix_const_view const getGSLMatrixView_const(const map_type m)
take data from a constEigen Matrix and return a transposed gsl view.
gsl_vector_const_view const getGSLVectorView_const(const vec_map_type v)
take const data from Eigen Vector and take a gsl view