/******************************************************************** ** 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 : ICLMath/src/LevenbergMarquardtFitter.cpp ** ** Module : ICLMath ** ** Authors: Christof Elbrechter ** ** ** ** ** ** 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 #include using namespace icl::utils; namespace icl{ namespace math{ template LevenbergMarquardtFitter::LevenbergMarquardtFitter(){ } template LevenbergMarquardtFitter::LevenbergMarquardtFitter(Function f, Jacobian j, Scalar initialLambda, int maxIterations, Scalar minError, Scalar lambdaMultiplier, const std::string &linSolver){ init(f,j,initialLambda,maxIterations,minError,lambdaMultiplier,linSolver); } template void LevenbergMarquardtFitter::init(Function f, Jacobian j, Scalar initialLambda, int maxIterations, Scalar minError, Scalar lambdaMultiplier, const std::string &linSolver){ this->f = f; if(j){ this->j = j; }else{ this->j = create_numerical_jacobian(f); } this->initialLambda = initialLambda; this->maxIterations = maxIterations; this->minError = minError; this->lambdaMultiplier = lambdaMultiplier; this->linSolver = linSolver; } template typename LevenbergMarquardtFitter::Result LevenbergMarquardtFitter::fit(const Matrix &xs, const Vector &ys, Params params){ const int I = xs.cols(); const int D = xs.rows(); const int P = params.dim(); const int MAX_IT = maxIterations; const Scalar MIN_E = minError; y_est.setDim(D); y_est_new.setDim(D); dst.setDim(D); params_new.setDim(P); J.setBounds(P,D); H.setBounds(P,P); H_damped.setBounds(P,P); H.setBounds(P,P); Scalar lambda=initialLambda; Scalar e = 1e38; Scalar e_new = 0; bool dirty=true; std::vector xis(D); for(int i=0;i(xs.row_begin(i)),false); } int it = 0; for(;it < MAX_IT; ++it){ if(dirty){ for(int i=0;i y_est["< struct NumericJacobian : public FunctionImpl&, const DynColVector&, DynColVector&>{ typedef typename LevenbergMarquardtFitter::Function Function; typedef typename LevenbergMarquardtFitter::Params Params; typedef typename LevenbergMarquardtFitter::Vector Vector; Function f; Scalar delta; NumericJacobian(Function f, Scalar delta): f(f),delta(delta){} virtual void operator()(const Params ¶ms, const Vector &x, Vector &target) const{ Vector p = params; for(unsigned int i=0;i typename LevenbergMarquardtFitter::Jacobian LevenbergMarquardtFitter::create_numerical_jacobian(Function f, float delta){ return Jacobian(new NumericJacobian(f,delta)); } template void LevenbergMarquardtFitter::setDebugCallback(DebugCallback dbg){ this->dbg = dbg; } template typename LevenbergMarquardtFitter::Data LevenbergMarquardtFitter::create_data(const Params &p, Function f, int xDim, int num, Scalar minX, Scalar maxX){ URand r(minX,maxX); Data data = { Matrix(xDim,num), Vector(num) }; for(int i=0;i; template class LevenbergMarquardtFitter; } // namespace math }