9 #ifndef CLevenbergMarquardt_H 10 #define CLevenbergMarquardt_H 30 template <
typename VECTORTYPE = Eigen::VectorXd,
class USERPARAM = VECTORTYPE >
35 typedef Eigen::Matrix<NUMTYPE,Eigen::Dynamic,Eigen::Dynamic>
matrix_t;
39 mrpt::utils::COutputLogger(
"CLevenbergMarquardt")
56 const VECTORTYPE &x_old,
57 const VECTORTYPE &x_incr,
58 const USERPARAM &user_param);
84 VECTORTYPE &out_optimal_x,
87 const VECTORTYPE &increments,
88 const USERPARAM &userParam,
90 mrpt::utils::VerbosityLevel verbosity = mrpt::utils::LVL_INFO,
91 const size_t maxIter = 200,
95 bool returnPath =
true,
106 this->setMinLoggingLevel(verbosity);
108 VECTORTYPE &
x=out_optimal_x;
111 ASSERT_( increments.size() == x0.size() );
121 out_info.
H.multiply_AtA(J);
123 const size_t H_len = out_info.
H.getColCount();
126 functor(
x, userParam ,f_x);
127 J.multiply_Atb(f_x,
g);
131 if (found) logFmt(mrpt::utils::LVL_INFO,
"End condition: math::norm_inf(g)<=e1 :%f\n",
math::norm_inf(
g));
133 NUMTYPE lambda = tau * out_info.
H.maximumDiagonal();
138 VECTORTYPE xnew, f_xnew ;
141 const size_t N =
x.size();
144 out_info.
path.setSize(maxIter,N+1);
145 out_info.
path.block(iter,0,1,N) =
x.transpose();
146 }
else out_info.
path = Eigen::Matrix<NUMTYPE,Eigen::Dynamic,Eigen::Dynamic>();
148 while (!found && ++iter<maxIter)
152 for (
size_t k=0;k<H_len;k++)
156 AUX.multiply_Ab(
g,h_lm);
162 logFmt(mrpt::utils::LVL_DEBUG,
"Iter:%u x=%s\n",(
unsigned)iter,
sprintf_vector(
" %f",
x).c_str() );
164 if (h_lm_n2<e2*(x_n2+e2))
168 logFmt(mrpt::utils::LVL_INFO,
"End condition: %e < %e\n", h_lm_n2, e2*(x_n2+e2) );
173 if (!x_increment_adder)
175 else x_increment_adder(xnew,
x, h_lm, userParam);
177 functor(xnew, userParam ,f_xnew );
178 const double F_xnew = pow(
math::norm(f_xnew), 2);
181 VECTORTYPE tmp(h_lm);
184 tmp.array() *=h_lm.array();
185 double denom = tmp.sum();
186 double l = (F_x - F_xnew) / denom;
196 out_info.
H.multiply_AtA(J);
197 J.multiply_Atb(f_x,
g);
200 if (found) logFmt(mrpt::utils::LVL_INFO,
"End condition: math::norm_inf(g)<=e1 : %e\n",
math::norm_inf(
g) );
202 lambda *= max(0.33, 1-pow(2*l-1,3) );
214 out_info.
path.block(iter,0,1,
x.size()) =
x.transpose();
215 out_info.
path(iter,
x.size()) = F_x;
224 if (returnPath) out_info.
path.setSize(iter,N+1);
An implementation of the Levenberg-Marquardt algorithm for least-square minimization.
CLevenbergMarquardtTempl()
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=NULL)
Executes the LM-method, with derivatives estimated from functor is a user-provided function which tak...
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.
CLevenbergMarquardtTempl< mrpt::math::CVectorDouble > CLevenbergMarquardt
The default name for the LM class is an instantiation for "double".
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
Eigen::Matrix< NUMTYPE, Eigen::Dynamic, Eigen::Dynamic > matrix_t
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.
void(* TFunctorEval)(const VECTORTYPE &x, const USERPARAM &y, VECTORTYPE &out)
The type of the function passed to execute.
void(* TFunctorIncrement)(VECTORTYPE &x_new, const VECTORTYPE &x_old, const VECTORTYPE &x_incr, const USERPARAM &user_param)
The type of an optional functor passed to execute to replace the Euclidean addition "x_new = x_old + ...
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
void estimateJacobian(const VECTORLIKE &x, void(*functor)(const VECTORLIKE &x, const USERPARAM &y, VECTORLIKE3 &out), 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(const CONTAINER &v)