Go to the documentation of this file.
9 #ifndef CLevenbergMarquardt_H
10 #define CLevenbergMarquardt_H
36 template <
typename VECTORTYPE = Eigen::VectorXd,
class USERPARAM = VECTORTYPE>
41 using matrix_t = Eigen::Matrix<NUMTYPE, Eigen::Dynamic, Eigen::Dynamic>;
60 const VECTORTYPE&
x,
const USERPARAM&
y, VECTORTYPE& out)>;
66 VECTORTYPE& x_new,
const VECTORTYPE& x_old,
const VECTORTYPE& x_incr,
67 const USERPARAM& user_param)>;
103 VECTORTYPE& out_optimal_x,
const VECTORTYPE& x0,
TFunctorEval functor,
104 const VECTORTYPE& increments,
const USERPARAM& userParam,
107 const size_t maxIter = 200,
const NUMTYPE tau = 1e-3,
111 using namespace mrpt;
119 VECTORTYPE&
x = out_optimal_x;
122 ASSERT_(increments.size() == x0.size());
132 out_info.
H.multiply_AtA(J);
134 const size_t H_len = out_info.
H.cols();
137 functor(
x, userParam, f_x);
138 J.multiply_Atb(f_x,
g);
145 "End condition: math::norm_inf(g)<=e1 :%f\n",
148 NUMTYPE lambda = tau * out_info.
H.maximumDiagonal();
153 VECTORTYPE xnew, f_xnew;
156 const size_t N =
x.size();
160 out_info.
path.setSize(maxIter, N + 1);
161 out_info.
path.block(iter, 0, 1, N) =
x.transpose();
164 out_info.
path = Eigen::Matrix<
NUMTYPE, Eigen::Dynamic,
167 while (!found && ++iter < maxIter)
171 for (
size_t k = 0; k < H_len; k++) H(k, k) += lambda;
174 AUX.multiply_Ab(
g, h_lm);
184 if (h_lm_n2 < e2 * (x_n2 + e2))
195 if (!x_increment_adder)
198 x_increment_adder(xnew,
x, h_lm, userParam);
200 functor(xnew, userParam, f_xnew);
201 const double F_xnew = pow(
math::norm(f_xnew), 2);
204 VECTORTYPE tmp(h_lm);
207 tmp.array() *= h_lm.array();
208 double denom = tmp.sum();
209 double l = (F_x - F_xnew) / denom;
219 x, functor, increments, userParam, J);
220 out_info.
H.multiply_AtA(J);
221 J.multiply_Atb(f_x,
g);
227 "End condition: math::norm_inf(g)<=e1 : %e\n",
230 lambda *= max(0.33, 1 - pow(2 * l - 1, 3));
242 out_info.
path.block(iter, 0, 1,
x.size()) =
x.transpose();
243 out_info.
path(iter,
x.size()) = F_x;
252 if (returnPath) out_info.
path.setSize(iter, N + 1);
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...
CONTAINER::Scalar norm_inf(const CONTAINER &v)
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 + ...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERT_(f)
Defines an assertion mechanism.
CONTAINER::Scalar norm(const CONTAINER &v)
void logFmt(const VerbosityLevel level, const char *fmt,...) const MRPT_printf_format_check(3
Alternative logging method, which mimics the printf behavior.
VerbosityLevel
Enumeration of available verbosity levels.
VECTORTYPE last_err_vector
The last error vector returned by the user-provided functor.
void setMinLoggingLevel(const VerbosityLevel level)
Set the minimum logging level for which the incoming logs are going to be taken into account.
An implementation of the Levenberg-Marquardt algorithm for least-square minimization.
size_t iterations_executed
Versatile class for consistent logging and management of output messages.
typename VECTORTYPE::Scalar NUMTYPE
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
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...
This base provides a set of functions for maths stuff.
std::function< void(const VECTORTYPE &x, const USERPARAM &y, VECTORTYPE &out)> TFunctorEval
The type of the function passed to execute.
matrix_t path
Each row is the optimized value at each iteration.
Eigen::Matrix< NUMTYPE, Eigen::Dynamic, Eigen::Dynamic > matrix_t
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...
COutputLogger()
Default class constructor.
CLevenbergMarquardtTempl()
matrix_t H
This matrix can be used to obtain an estimate of the optimal parameters covariance matrix:
Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at mié 12 jul 2023 10:03:34 CEST | |