1 #ifndef ARUCO_MM__LevMarq_H     2 #define ARUCO_MM__LevMarq_H     4 #include <Eigen/Cholesky>    31     typedef    std::function<void(const eVector  &, eVector &)> 
F_z_x;
    32     typedef    std::function<void(const eVector  &,  Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> &)> 
F_z_J;
    43     LevMarq(
int maxIters,
double minError,
double min_step_error_diff=0,
double tau=1 ,
double der_epsilon=1e-3);
    54     void setParams(
int maxIters,
double minError,
double min_step_error_diff=0,
double tau=1 ,
double der_epsilon=1e-3);
    67     double solve(  eVector  &z, F_z_x , 
F_z_J)throw (
std::exception);
    75     void init(eVector  &z, F_z_x )throw (
std::exception);
    82     bool step(  F_z_x f_z_x , 
F_z_J  f_z_J)throw (
std::exception);
    83     bool step(  F_z_x f_z_x)throw (
std::exception);
   105     double solve(  eVector  &z, F_z_x )throw (
std::exception);
   146 LevMarq<T>::LevMarq(
int maxIters,
double minError,
double min_step_error_diff,
double tau ,
double der_epsilon ){
   173 #pragma omp parallel for   174     for (
int i=0;i<z.
rows();i++) {
   182         J.middleCols(i,1)=dif;
   209 #define splm_get_time(a,b) std::chrono::duration_cast<std::chrono::duration<double>>(a-b).count()   222         for(
int j=1;j<JtJ.
cols();j++) 
if (JtJ(j,j)>JtJ(max,max)) max=j;
   226     double gain=0,prev_mu=0;
   228     bool isStepAccepted=
false;
   232         for(
int j=0;j<JtJ.
cols();j++)  JtJ(j,j)    += 
mu-prev_mu;
   234         eVector delta=   JtJ.ldlt().solve(B);
   235         eVector  estimated_z=
curr_z+delta;
   237         f_z_x(estimated_z,x64);
   238         auto err=x64.cwiseProduct(x64).sum();
   239         auto L=0.5*delta.transpose()*((
mu*delta) - B);
   243             mu=
mu*std::max(
double(0.33),1.-
pow(2*gain-1,3));
   249         else{ 
mu=
mu*
v; v=v*5;}
   251     }
while(gain<=0 && ntries++<5);
   253     if (
_verbose) std::cout<<std::setprecision(5) <<
"Curr Error="<<
currErr<<
" AErr(prev-curr)="<<
prevErr-
currErr<<
" gain="<<gain<<
" dumping factor="<<
mu<<std::endl;
   258      return isStepAccepted;
   284         for ( 
int i = 0; i < 
_maxIters && !mustExit; i++ ) {
   286             bool isStepAccepted=
step(f_z_x,f_J);
 bool step(F_z_x f_z_x, F_z_J f_z_J)
step gives a step of the search 
Eigen::Matrix< T, Eigen::Dynamic, 1 > eVector
double _min_step_error_diff
const CwiseUnaryOp< internal::scalar_pow_op< Scalar >, const Derived > pow(const Scalar &exponent) const 
void init(eVector &z, F_z_x)
Step by step solve mode. 
void setParams(int maxIters, double minError, double min_step_error_diff=0, double tau=1, double der_epsilon=1e-3)
setParams 
EIGEN_STRONG_INLINE Index rows() const 
EIGEN_STRONG_INLINE void resize(Index nbRows, Index nbCols)
std::function< void(const eVector &, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &)> F_z_J
void setStopFunction(std::function< bool(const eVector &)> stop_function)
TFSIMD_FORCE_INLINE const tfScalar & z() const 
std::function< bool(const eVector &)> _stopFunction
void setStepCallBackFunc(std::function< void(const eVector &)> callback)
double getCurrentSolution(eVector &z)
getCurrentSolution returns the current solution 
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > I
double getBestSolution(eVector &z)
getBestSolution sets in z the best solution up to this moment 
std::function< void(const eVector &, eVector &)> F_z_x
void calcDerivates(const eVector &z, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &, F_z_x)
EIGEN_STRONG_INLINE Index cols() const 
double solve(eVector &z, F_z_x, F_z_J)
solve non linear minimization problem ||F(z)||, where F(z)=f(z) f(z)^t 
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > J
std::function< void(const eVector &)> _step_callback