Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLMath/src/ICLMath/LevenbergMarquardtFitter.h ** 00010 ** Module : ICLMath ** 00011 ** Authors: Christof Elbrechter ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.GPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #pragma once 00032 00033 #include <ICLMath/DynVector.h> 00034 #include <ICLUtils/Function.h> 00035 00036 namespace icl{ 00037 namespace math{ 00038 00039 00041 00250 template<class Scalar> 00251 class LevenbergMarquardtFitter{ 00252 public: 00253 00254 typedef DynColVector<Scalar> Vector; 00255 typedef DynColVector<Scalar> Params; 00256 typedef DynMatrix<Scalar> Matrix; 00257 00259 struct Result{ 00260 int iteration; 00261 Scalar error; 00262 std::vector<Scalar> lambdas; 00263 Params params; 00264 00266 friend inline std::ostream &operator<<(std::ostream &str, const Result &d){ 00267 return str << "iteration: " << d.iteration << " error:" << d.error 00268 << " lambda[0]:" << d.lambdas[0] << " params:" << d.params.transp(); 00269 } 00270 }; 00271 00273 struct Data{ 00274 Matrix x; 00275 Matrix y; 00276 }; 00277 00279 typedef icl::utils::Function<Vector,const Params&,const Vector&> Function; 00280 00282 00283 typedef icl::utils::Function<void,const Params&, const Vector&, Vector &> Jacobian; 00284 00286 typedef icl::utils::Function<void,const Result&> DebugCallback; 00287 00288 00289 00290 protected: 00291 Function f; 00292 00293 bool useMultiThreading; 00294 Scalar initialLambda; 00295 int maxIterations; 00296 Scalar minError; 00297 Scalar lambdaMultiplier; 00298 std::string linSolver; 00299 00300 Vector dst; 00301 Matrix J; 00302 Matrix H; 00303 00305 std::vector<Jacobian> js; 00306 00307 DebugCallback dbg; 00308 Params params_new; 00309 Matrix y_est; 00310 //Matrix y_est_tmp; //!< current estimated outputs (with temporary update) 00311 Vector dy; 00312 00313 00315 Scalar error(const Matrix &ys, const Matrix &y_est) const; 00316 00317 public: 00318 00320 LevenbergMarquardtFitter(); 00321 00323 00341 LevenbergMarquardtFitter(Function f, int outputDim, 00342 const std::vector<Jacobian> &js=std::vector<Jacobian>(), 00343 Scalar initialLambda=1.e-8, int maxIterations=200, 00344 Scalar minError = 1.e-6, Scalar lambdaMultiplier=10, 00345 const std::string &linSolver="svd"); 00346 00348 00351 void setUseMultiThreading(bool enable); 00352 00354 00355 void init(Function f, int outputDim, 00356 const std::vector<Jacobian> &js=std::vector<Jacobian>(), 00357 Scalar initialLambda=1.e-8, int maxIterations=1000, 00358 Scalar minError = 1.e-6, Scalar lambdaMultiplier=10, 00359 const std::string &linSolver="svd"); 00360 00362 00370 Result fit(const Matrix &xs, const Matrix &ys, Params initParams); 00371 00372 00373 00375 00391 static Jacobian create_numerical_jacobian(int o, Function f, float delta=1.E-5); 00392 00394 static std::vector<Jacobian> create_numerical_jacobians(int n, Function f, float delta=1.e-5); 00395 00397 00404 static Data create_data(const Params &p, Function f, int xDim, int yDim, 00405 int num=1000, Scalar minX=-5, Scalar maxX=5); 00406 00408 static void default_debug_callback(const Result &r); 00409 00411 void setDebugCallback(DebugCallback dbg=default_debug_callback); 00412 }; 00413 } // namespace math 00414 } // namespace icl