9 #ifndef CLevenbergMarquardt_H 10 #define CLevenbergMarquardt_H 36 template <
typename VECTORTYPE = Eigen::VectorXd,
class USERPARAM = VECTORTYPE>
41 typedef Eigen::Matrix<NUMTYPE, Eigen::Dynamic, Eigen::Dynamic>
matrix_t;
45 :
mrpt::utils::COutputLogger(
"CLevenbergMarquardt")
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,
106 mrpt::utils::VerbosityLevel verbosity = mrpt::utils::LVL_INFO,
107 const size_t maxIter = 200,
const NUMTYPE tau = 1e-3,
111 using namespace mrpt;
118 this->setMinLoggingLevel(verbosity);
120 VECTORTYPE&
x = out_optimal_x;
123 ASSERT_(increments.size() == x0.size());
133 out_info.
H.multiply_AtA(J);
135 const size_t H_len = out_info.
H.getColCount();
138 functor(
x, userParam, f_x);
139 J.multiply_Atb(f_x,
g);
145 mrpt::utils::LVL_INFO,
146 "End condition: math::norm_inf(g)<=e1 :%f\n",
149 NUMTYPE lambda = tau * out_info.
H.maximumDiagonal();
154 VECTORTYPE xnew, f_xnew;
157 const size_t N =
x.size();
161 out_info.
path.setSize(maxIter, N + 1);
162 out_info.
path.block(iter, 0, 1, N) =
x.transpose();
165 out_info.
path = Eigen::Matrix<
NUMTYPE, Eigen::Dynamic,
168 while (!found && ++iter < maxIter)
172 for (
size_t k = 0; k < H_len; k++) H(k, k) += lambda;
175 AUX.multiply_Ab(
g, h_lm);
182 mrpt::utils::LVL_DEBUG,
"Iter:%u x=%s\n", (
unsigned)iter,
185 if (h_lm_n2 < e2 * (x_n2 + e2))
190 mrpt::utils::LVL_INFO,
"End condition: %e < %e\n", h_lm_n2,
196 if (!x_increment_adder)
199 x_increment_adder(xnew,
x, h_lm, userParam);
201 functor(xnew, userParam, f_xnew);
202 const double F_xnew = pow(
math::norm(f_xnew), 2);
205 VECTORTYPE tmp(h_lm);
208 tmp.array() *= h_lm.array();
209 double denom = tmp.sum();
210 double l = (F_x - F_xnew) / denom;
220 x, functor, increments, userParam, J);
221 out_info.
H.multiply_AtA(J);
222 J.multiply_Atb(f_x,
g);
227 mrpt::utils::LVL_INFO,
228 "End condition: math::norm_inf(g)<=e1 : %e\n",
231 lambda *= max(0.33, 1 - pow(2 * l - 1, 3));
243 out_info.
path.block(iter, 0, 1,
x.size()) =
x.transpose();
244 out_info.
path(iter,
x.size()) = F_x;
253 if (returnPath) out_info.
path.setSize(iter, N + 1);
An implementation of the Levenberg-Marquardt algorithm for least-square minimization.
CLevenbergMarquardtTempl()
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
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 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.
void execute(VECTORTYPE &out_optimal_x, const VECTORTYPE &x0, TFunctorEval functor, const VECTORTYPE &increments, const USERPARAM &userParam, TResultInfo &out_info, mrpt::utils::VerbosityLevel verbosity=mrpt::utils::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...
CLevenbergMarquardtTempl< mrpt::math::CVectorDouble > CLevenbergMarquardt
The default name for the LM class is an instantiation for "double".
Eigen::Matrix< NUMTYPE, Eigen::Dynamic, Eigen::Dynamic > matrix_t
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.
matrix_t path
Each row is the optimized value at each iteration.
size_t iterations_executed
matrix_t H
This matrix can be used to obtain an estimate of the optimal parameters covariance matrix: With COV ...
CONTAINER::Scalar norm_inf(const CONTAINER &v)
VECTORTYPE::Scalar NUMTYPE
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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 + ...
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
std::function< void(const VECTORTYPE &x, const USERPARAM &y, VECTORTYPE &out)> TFunctorEval
The type of the function passed to execute.
CONTAINER::Scalar norm(const CONTAINER &v)