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, Sergius Gaulik ** 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.LGPL ** 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 <ICLUtils/CompatMacros.h> 00034 #include <ICLMath/DynVector.h> 00035 #include <ICLUtils/Function.h> 00036 00037 namespace icl{ 00038 namespace math{ 00039 00040 00042 00251 template<class Scalar> 00252 class ICLMath_API LevenbergMarquardtFitter{ 00253 public: 00254 00255 typedef DynColVector<Scalar> Vector; 00256 typedef DynColVector<Scalar> Params; 00257 typedef DynMatrix<Scalar> Matrix; 00258 00260 struct Result{ 00261 int iteration; 00262 Scalar error; 00263 std::vector<Scalar> lambdas; 00264 Params params; 00265 00267 friend ICLMath_API inline std::ostream &operator<<(std::ostream &str, const Result &d){ 00268 return str << "iteration: " << d.iteration << " error:" << d.error 00269 << " lambda[0]:" << d.lambdas[0] << " params:" << d.params.transp(); 00270 } 00271 }; 00272 00274 struct Data{ 00275 Matrix x; 00276 Matrix y; 00277 }; 00278 00280 typedef icl::utils::Function<Vector,const Params&,const Vector&> Function; 00281 typedef icl::utils::Function<Matrix,const Params&,const Matrix&> FunctionMat; 00282 00284 00285 typedef icl::utils::Function<void,const Params&, const Vector&, Vector&> Jacobian; 00286 typedef icl::utils::Function<void,const Params&, const Matrix&, Matrix&> JacobianMat; 00287 00289 typedef icl::utils::Function<void,const Result&> DebugCallback; 00290 00291 00292 00293 protected: 00294 Function f; 00295 FunctionMat fMat; 00296 00297 bool useMultiThreading; 00298 bool useMat; 00299 Scalar tau; 00300 int maxIterations; 00301 Scalar minError; 00302 Scalar lambdaMultiplier; 00303 Scalar eps1; 00304 Scalar eps2 ; 00305 std::string linSolver; 00306 00307 Vector dst; 00308 Matrix J; 00309 Matrix H; 00310 00312 std::vector<Jacobian> js; 00313 std::vector<JacobianMat> jsMat; 00314 00315 DebugCallback dbg; 00316 Params params_new; 00317 Matrix y_est; 00318 //Matrix y_est_tmp; //!< current estimated outputs (with temporary update) 00319 Vector dy; 00320 00321 00323 Scalar error(const Matrix &ys, const Matrix &y_est) const; 00324 00325 public: 00326 00328 LevenbergMarquardtFitter(); 00329 00331 00352 LevenbergMarquardtFitter(Function f, int outputDim, 00353 const std::vector<Jacobian> &js=std::vector<Jacobian>(), 00354 Scalar tau=1.e-3, int maxIterations=200, 00355 Scalar minError = 1.e-6, Scalar lambdaMultiplier=10, 00356 Scalar eps1 = 1.49012e-08, Scalar eps2 = 1.49012e-08, 00357 const std::string &linSolver="svd"); 00358 LevenbergMarquardtFitter(FunctionMat f, int outputDim, 00359 const std::vector<JacobianMat> &js=std::vector<JacobianMat>(), 00360 Scalar tau=1.e-3, int maxIterations=200, 00361 Scalar minError = 1.e-6, Scalar lambdaMultiplier=10, 00362 Scalar eps1 = 1.49012e-08, Scalar eps2 = 1.49012e-08, 00363 const std::string &linSolver="svd"); 00364 00366 00369 void setUseMultiThreading(bool enable); 00370 00372 00373 void init(Function f, int outputDim, 00374 const std::vector<Jacobian> &js=std::vector<Jacobian>(), 00375 Scalar tau=1.e-8, int maxIterations=1000, 00376 Scalar minError = 1.e-6, Scalar lambdaMultiplier=10, 00377 Scalar eps1 = 1.49012e-08, Scalar eps2 = 1.49012e-08, 00378 const std::string &linSolver="svd"); 00379 void init(FunctionMat f, int outputDim, 00380 const std::vector<JacobianMat> &js=std::vector<JacobianMat>(), 00381 Scalar tau=1.e-8, int maxIterations=1000, 00382 Scalar minError = 1.e-6, Scalar lambdaMultiplier=10, 00383 Scalar eps1 = 1.49012e-08, Scalar eps2 = 1.49012e-08, 00384 const std::string &linSolver="svd"); 00385 00387 00395 Result fit(const Matrix &xs, const Matrix &ys, Params initParams); 00396 00397 00398 00400 00416 static Jacobian create_numerical_jacobian(int o, Function f, float delta=1.E-5); 00417 static JacobianMat create_numerical_jacobian(int o, FunctionMat f, float delta=1.E-5); 00418 00420 static std::vector<Jacobian> create_numerical_jacobians(int n, Function f, float delta=1.e-5); 00421 static std::vector<JacobianMat> create_numerical_jacobians(int n, FunctionMat f, float delta=1.e-5); 00422 00424 00431 static Data create_data(const Params &p, Function f, int xDim, int yDim, 00432 int num=1000, Scalar minX=-5, Scalar maxX=5); 00433 00435 static void default_debug_callback(const Result &r); 00436 00438 void setDebugCallback(DebugCallback dbg=default_debug_callback); 00439 00440 private: 00442 Result fitVec(const Matrix &xs, const Matrix &ys, Params initParams); 00444 Result fitMat(const Matrix &xs, const Matrix &ys, Params initParams); 00445 }; 00446 } // namespace math 00447 } // namespace icl