9 #ifndef CLevenbergMarquardt_H
10 #define CLevenbergMarquardt_H
34 template <
typename VECTORTYPE = Eigen::VectorXd,
class USERPARAM = VECTORTYPE>
39 using matrix_t = Eigen::Matrix<NUMTYPE, Eigen::Dynamic, Eigen::Dynamic>;
58 const VECTORTYPE&
x,
const USERPARAM&
y, VECTORTYPE& out)>;
64 VECTORTYPE& x_new,
const VECTORTYPE& x_old,
const VECTORTYPE& x_incr,
65 const USERPARAM& user_param)>;
101 VECTORTYPE& out_optimal_x,
const VECTORTYPE& x0,
TFunctorEval functor,
102 const VECTORTYPE& increments,
const USERPARAM& userParam,
105 const size_t maxIter = 200,
const NUMTYPE tau = 1e-3,
109 using namespace mrpt;
117 VECTORTYPE&
x = out_optimal_x;
120 ASSERT_(increments.size() == x0.size());
130 out_info.
H.multiply_AtA(J);
132 const size_t H_len = out_info.
H.cols();
135 functor(
x, userParam, f_x);
136 J.multiply_Atb(f_x,
g);
143 "End condition: math::norm_inf(g)<=e1 :%f\n",
146 NUMTYPE lambda = tau * out_info.
H.maximumDiagonal();
151 VECTORTYPE xnew, f_xnew;
154 const size_t N =
x.size();
158 out_info.
path.setSize(maxIter, N + 1);
159 out_info.
path.block(iter, 0, 1, N) =
x.transpose();
162 out_info.
path = Eigen::Matrix<
NUMTYPE, Eigen::Dynamic,
165 while (!found && ++iter < maxIter)
169 for (
size_t k = 0; k < H_len; k++) H(k, k) += lambda;
172 AUX.multiply_Ab(
g, h_lm);
182 if (h_lm_n2 < e2 * (x_n2 + e2))
193 if (!x_increment_adder)
196 x_increment_adder(xnew,
x, h_lm, userParam);
198 functor(xnew, userParam, f_xnew);
199 const double F_xnew = pow(
math::norm(f_xnew), 2);
202 VECTORTYPE tmp(h_lm);
205 tmp.array() *= h_lm.array();
206 double denom = tmp.sum();
207 double l = (F_x - F_xnew) / denom;
217 x, functor, increments, userParam, J);
218 out_info.
H.multiply_AtA(J);
219 J.multiply_Atb(f_x,
g);
225 "End condition: math::norm_inf(g)<=e1 : %e\n",
228 lambda *= max(0.33, 1 - pow(2 * l - 1, 3));
240 out_info.
path.block(iter, 0, 1,
x.size()) =
x.transpose();
241 out_info.
path(iter,
x.size()) = F_x;
250 if (returnPath) out_info.
path.setSize(iter, N + 1);
An implementation of the Levenberg-Marquardt algorithm for least-square minimization.
void execute(VECTORTYPE &out_optimal_x, const VECTORTYPE &x0, TFunctorEval functor, const VECTORTYPE &increments, const USERPARAM &userParam, TResultInfo &out_info, mrpt::system::VerbosityLevel verbosity=mrpt::system::LVL_INFO, const size_t maxIter=200, const NUMTYPE tau=1e-3, const NUMTYPE e1=1e-8, const NUMTYPE e2=1e-8, bool returnPath=true, TFunctorIncrement x_increment_adder=nullptr)
Executes the LM-method, with derivatives estimated from functor is a user-provided function which tak...
Eigen::Matrix< NUMTYPE, Eigen::Dynamic, Eigen::Dynamic > matrix_t
CLevenbergMarquardtTempl()
std::function< void(const VECTORTYPE &x, const USERPARAM &y, VECTORTYPE &out)> TFunctorEval
The type of the function passed to execute.
std::function< void(VECTORTYPE &x_new, const VECTORTYPE &x_old, const VECTORTYPE &x_incr, const USERPARAM &user_param)> TFunctorIncrement
The type of an optional functor passed to execute to replace the Euclidean addition "x_new = x_old + ...
typename VECTORTYPE::Scalar NUMTYPE
Versatile class for consistent logging and management of output messages.
COutputLogger()
Default class constructor.
void setMinLoggingLevel(const VerbosityLevel level)
Set the minimum logging level for which the incoming logs are going to be taken into account.
void logFmt(const VerbosityLevel level, const char *fmt,...) const MRPT_printf_format_check(3
Alternative logging method, which mimics the printf behavior.
#define ASSERT_(f)
Defines an assertion mechanism.
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
std::string sprintf_vector(const char *fmt, const std::vector< T > &V)
Generates a string for a vector in the format [A,B,C,...] to std::cout, and the fmt string for each v...
This base provides a set of functions for maths stuff.
void estimateJacobian(const VECTORLIKE &x, const std::function< void(const VECTORLIKE &x, const USERPARAM &y, VECTORLIKE3 &out)> &functor, const VECTORLIKE2 &increments, const USERPARAM &userParam, MATRIXLIKE &out_Jacobian)
Estimate the Jacobian of a multi-dimensional function around a point "x", using finite differences of...
CONTAINER::Scalar norm_inf(const CONTAINER &v)
CONTAINER::Scalar norm(const CONTAINER &v)
VerbosityLevel
Enumeration of available verbosity levels.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
This file implements several operations that operate element-wise on individual or pairs of container...
VECTORTYPE last_err_vector
The last error vector returned by the user-provided functor.
matrix_t H
This matrix can be used to obtain an estimate of the optimal parameters covariance matrix:
size_t iterations_executed
matrix_t path
Each row is the optimized value at each iteration.