28 #ifndef CLevenbergMarquardt_H
29 #define CLevenbergMarquardt_H
50 template <
typename VECTORTYPE = mrpt::vector_
double,
class USERPARAM = VECTORTYPE >
73 const VECTORTYPE &x_old,
74 const VECTORTYPE &x_incr,
75 const USERPARAM &user_param);
101 VECTORTYPE &out_optimal_x,
102 const VECTORTYPE &x0,
104 const VECTORTYPE &increments,
105 const USERPARAM &userParam,
107 bool verbose =
false,
108 const size_t maxIter = 200,
112 bool returnPath =
true,
116 using namespace mrpt;
117 using namespace mrpt::utils;
118 using namespace mrpt::math;
123 VECTORTYPE &x=out_optimal_x;
126 ASSERT_( increments.size() == x0.size() );
136 out_info.
H.multiply_AtA(J);
138 const size_t H_len = out_info.
H.getColCount();
141 functor(x, userParam ,f_x);
142 J.multiply_Atb(f_x, g);
148 NUMTYPE lambda = tau * out_info.
H.maximumDiagonal();
153 VECTORTYPE xnew, f_xnew ;
156 const size_t N = x.size();
159 out_info.
path.setSize(maxIter,N+1);
160 out_info.
path.block(iter,0,1,N) = x.transpose();
161 }
else out_info.
path = Eigen::Matrix<NUMTYPE,Eigen::Dynamic,Eigen::Dynamic>();
163 while (!found && ++iter<maxIter)
167 for (
size_t k=0;k<H_len;k++)
171 AUX.multiply_Ab(g,h_lm);
179 if (h_lm_n2<e2*(x_n2+e2))
183 if (verbose)
printf_debug(
"[LM] End condition: %e < %e\n", h_lm_n2, e2*(x_n2+e2) );
188 if (!x_increment_adder)
190 else x_increment_adder(xnew, x, h_lm, userParam);
192 functor(xnew, userParam ,f_xnew );
193 const double F_xnew = pow(
math::norm(f_xnew), 2);
196 VECTORTYPE tmp(h_lm);
199 tmp.array() *=h_lm.array();
200 double denom = tmp.sum();
201 double l = (F_x - F_xnew) / denom;
211 out_info.
H.multiply_AtA(J);
212 J.multiply_Atb(f_x, g);
217 lambda *= max(0.33, 1-pow(2*l-1,3) );
229 out_info.
path.block(iter,0,1,x.size()) = x.transpose();
230 out_info.
path(iter,x.size()) = F_x;
239 if (returnPath) out_info.
path.setSize(iter,N+1);