/******************************************************************** ** Image Component Library (ICL) ** ** ** ** Copyright (C) 2006-2012 CITEC, University of Bielefeld ** ** Neuroinformatics Group ** ** Website: www.iclcv.org and ** ** http://opensource.cit-ec.de/projects/icl ** ** ** ** File : ICLAlgorithms/src/LM.cpp ** ** Module : ICLAlgorithms ** ** Authors: Christian Groszewski ** ** ** ** ** ** Commercial License ** ** ICL can be used commercially, please refer to our website ** ** www.iclcv.org for more details. ** ** ** ** GNU General Public License Usage ** ** Alternatively, this file may be used under the terms of the ** ** GNU General Public License version 3.0 as published by the ** ** Free Software Foundation and appearing in the file LICENSE.GPL ** ** included in the packaging of this file. Please review the ** ** following information to ensure the GNU General Public License ** ** version 3.0 requirements will be met: ** ** http://www.gnu.org/copyleft/gpl.html. ** ** ** ** The development of this software was supported by the ** ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** ** Forschungsgemeinschaft (DFG) in the context of the German ** ** Excellence Initiative. ** ** ** *********************************************************************/ #include #include namespace icl{ LMA::LMA(){} LMA::~LMA(){} double LMA::dot(DynMatrix &a, DynMatrix &b){ double dotp = 0.0; if(a.rows() == b.rows()){ for(unsigned int i=0;i &x,DynMatrix ¶ms, DynMatrix *(*func)(const DynMatrix &x, DynMatrix ¶ms), DynMatrix *(*jacobian)(const DynMatrix &x, DynMatrix ¶ms), double error, int MaxIter){ double change = 1.0; int iter = 0; DynMatrix *jac = 0; DynMatrix *val = 0; DynMatrix ex(1,x.rows()); double err = 0; while ((change > 1e-10)&&(iter < MaxIter)){ //std::cout << "iter: " << iter << " change: " << change << std::endl; jac = jacobian(x, params); val = func(x,params); //err = 0; for(unsigned int i=0;idim();++i){ int m = isnan((*jac)[i]); int n = isinf((*jac)[i]); if(m !=0 || n!=0) stop = true; } if (stop){//jac->cond() > thresh_cond || k){ std::cout << "ohoh\n"; change = 0; //delete jac; } else { DynMatrix JJT = jac->transp(); DynMatrix JJ2 = JJT*(*jac); if(JJ2.det() <1e-8){ change = 0; delete jac; continue; } DynMatrix param_innov(1,params.dim()), param_up(1,params.dim()); DynMatrix temp = JJ2.pinv()*JJT; param_innov = temp*ex; param_up = params + param_innov; change = param_innov.norm()/param_up.norm(); params = param_up; //SHOW(params); iter = iter + 1; } delete jac; } } //ok double LMA::solve(const DynMatrix &x, DynMatrix *(*func)(const DynMatrix &x, DynMatrix ¶ms), DynMatrix *(*jacobian)(const DynMatrix &x, DynMatrix ¶ms), DynMatrix ¶ms_est,const double thresh, const int n_iters, bool debug_output){ double lambda=0.1; bool updateJ=true; int paramcount = params_est.rows(); DynMatrix *y_est = 0; DynMatrix *y_est_lm = 0; DynMatrix d(1,x.rows()); DynMatrix dp(1,x.rows()); DynMatrix d_lm(1,x.rows()); DynMatrix *J = 0; DynMatrix Jt; DynMatrix H(paramcount,paramcount); DynMatrix H_lm; DynMatrix eye(paramcount,paramcount); DynMatrix params_lm(1,params_est.rows()); double e = 10e6; double e_lm = 0.0; int it=0; while(itrows();++i) d[i] = (*y_est)[i]; Jt = J->transp(); (Jt).mult(*J,H); if (it==0){ e=dot(d,d); } } for(int i=0;i dst; Jt.mult(d,dst); H_lm = H + eye; dp = H_lm.solve(dst); dp.mult(-1,dp); DynMatrix dst2; for(int i=0;irows();++i) d_lm[i]=(*y_est_lm)[i]; e_lm=dot(d_lm,d_lm); if(debug_output) std::cout << e << std::endl; if (e_lm