Image Component Library (ICL)
LevenbergMarquardtFitter.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLMath/src/ICLMath/LevenbergMarquardtFitter.h **
10 ** Module : ICLMath **
11 ** Authors: Christof Elbrechter, Sergius Gaulik **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLMath/DynVector.h>
35 #include <ICLUtils/Function.h>
36 
37 namespace icl{
38  namespace math{
39 
40 
42 
251  template<class Scalar>
253  public:
254 
258 
260  struct Result{
261  int iteration;
262  Scalar initialError;
263  Scalar error;
264  std::vector<Scalar> lambdas;
266 
268  friend ICLMath_API inline std::ostream &operator<<(std::ostream &str, const Result &d){
269  return str << "iteration: " << d.iteration << " initial error: " << d.initialError << " error:" << d.error
270  << " lambda[0]:" << d.lambdas[0] << " params:" << d.params.transp();
271  }
272  };
273 
275  struct Data{
278  };
279 
283 
285 
288 
291 
292 
293 
294  protected:
297 
299  bool useMat;
300  Scalar tau;
302  Scalar minError;
304  Scalar eps1;
305  Scalar eps2 ;
306  std::string linSolver;
307 
311 
313  std::vector<Jacobian> js;
314  std::vector<JacobianMat> jsMat;
315 
319  //Matrix y_est_tmp; //!< current estimated outputs (with temporary update)
321 
322 
324  Scalar error(const Matrix &ys, const Matrix &y_est) const;
325 
326  public:
327 
330 
332 
353  LevenbergMarquardtFitter(Function f, int outputDim,
354  const std::vector<Jacobian> &js=std::vector<Jacobian>(),
355  Scalar tau=1.e-3, int maxIterations=200,
356  Scalar minError = 1.e-6, Scalar lambdaMultiplier=10,
357  Scalar eps1 = 1.49012e-08, Scalar eps2 = 1.49012e-08,
358  const std::string &linSolver="svd");
359  LevenbergMarquardtFitter(FunctionMat f, int outputDim,
360  const std::vector<JacobianMat> &js=std::vector<JacobianMat>(),
361  Scalar tau=1.e-3, int maxIterations=200,
362  Scalar minError = 1.e-6, Scalar lambdaMultiplier=10,
363  Scalar eps1 = 1.49012e-08, Scalar eps2 = 1.49012e-08,
364  const std::string &linSolver="svd");
365 
367 
370  void setUseMultiThreading(bool enable);
371 
373 
374  void init(Function f, int outputDim,
375  const std::vector<Jacobian> &js=std::vector<Jacobian>(),
376  Scalar tau=1.e-8, int maxIterations=1000,
377  Scalar minError = 1.e-6, Scalar lambdaMultiplier=10,
378  Scalar eps1 = 1.49012e-08, Scalar eps2 = 1.49012e-08,
379  const std::string &linSolver="svd");
380  void init(FunctionMat f, int outputDim,
381  const std::vector<JacobianMat> &js=std::vector<JacobianMat>(),
382  Scalar tau=1.e-8, int maxIterations=1000,
383  Scalar minError = 1.e-6, Scalar lambdaMultiplier=10,
384  Scalar eps1 = 1.49012e-08, Scalar eps2 = 1.49012e-08,
385  const std::string &linSolver="svd");
386 
388 
396  Result fit(const Matrix &xs, const Matrix &ys, Params initParams);
397 
398 
399 
401 
417  static Jacobian create_numerical_jacobian(int o, Function f, float delta=1.E-5);
418  static JacobianMat create_numerical_jacobian(int o, FunctionMat f, float delta=1.E-5);
419 
421  static std::vector<Jacobian> create_numerical_jacobians(int n, Function f, float delta=1.e-5);
422  static std::vector<JacobianMat> create_numerical_jacobians(int n, FunctionMat f, float delta=1.e-5);
423 
425 
432  static Data create_data(const Params &p, Function f, int xDim, int yDim,
433  int num=1000, Scalar minX=-5, Scalar maxX=5);
434 
436  static void default_debug_callback(const Result &r);
437 
439  void setDebugCallback(DebugCallback dbg=default_debug_callback);
440 
441  private:
443  Result fitVec(const Matrix &xs, const Matrix &ys, Params initParams);
445  Result fitMat(const Matrix &xs, const Matrix &ys, Params initParams);
446  };
447  } // namespace math
448 } // namespace icl
DynMatrix< Scalar > Matrix
matrix type (used for input data)
Definition: LevenbergMarquardtFitter.h:257
icl::utils::Function< void, const Params &, const Vector &, Vector & > Jacobian
jacobian of F
Definition: LevenbergMarquardtFitter.h:286
Scalar eps1
minimum F'(parameters) threshold
Definition: LevenbergMarquardtFitter.h:304
icl::utils::Function< Matrix, const Params &, const Matrix & > FunctionMat
Definition: LevenbergMarquardtFitter.h:282
undocument this line if you encounter any issues!
Definition: Any.h:37
Scalar lambdaMultiplier
mulitplier that is used to increase/decreas lambda
Definition: LevenbergMarquardtFitter.h:303
DynColVector< Scalar > Params
parameter vector type
Definition: LevenbergMarquardtFitter.h:256
#define ICLMath_API
Definition: CompatMacros.h:173
Scalar initialError
initial error
Definition: LevenbergMarquardtFitter.h:262
Vector dy
buffer for current delta
Definition: LevenbergMarquardtFitter.h:320
bool useMat
flag whether matrices in the error function
Definition: LevenbergMarquardtFitter.h:299
utility structure that is used in the static create_data utlity method
Definition: LevenbergMarquardtFitter.h:275
icl::utils::Function< void, const Result & > DebugCallback
Optionally given debug callback, that is called in every iterations.
Definition: LevenbergMarquardtFitter.h:290
int maxIterations
maximum number of iterations
Definition: LevenbergMarquardtFitter.h:301
friend ICLMath_API std::ostream & operator<<(std::ostream &str, const Result &d)
overloaded ostream-operator
Definition: LevenbergMarquardtFitter.h:268
Utility structure, that represents a fitting result.
Definition: LevenbergMarquardtFitter.h:260
std::string linSolver
linear solver that is used
Definition: LevenbergMarquardtFitter.h:306
Matrix J
Jacobian Matrix (rows are Ji)
Definition: LevenbergMarquardtFitter.h:309
Function f
Function f.
Definition: LevenbergMarquardtFitter.h:295
Scalar error
reached error
Definition: LevenbergMarquardtFitter.h:263
Matrix y
outputs
Definition: LevenbergMarquardtFitter.h:277
Matrix y_est
current estimated outputs
Definition: LevenbergMarquardtFitter.h:318
std::vector< Jacobian > js
output buffers
Definition: LevenbergMarquardtFitter.h:313
Params params
final parameters
Definition: LevenbergMarquardtFitter.h:265
Scalar tau
used for initial damping parameter lambda
Definition: LevenbergMarquardtFitter.h:300
icl::utils::Function< void, const Params &, const Matrix &, Matrix & > JacobianMat
Definition: LevenbergMarquardtFitter.h:287
DynMatrix transp() const
matrix transposed
Definition: DynMatrix.h:979
Vector dst
b in Mx=b of linear system solved internally
Definition: LevenbergMarquardtFitter.h:308
DynColVector< Scalar > Vector
vector type
Definition: LevenbergMarquardtFitter.h:255
Scalar minError
minimum error threshold
Definition: LevenbergMarquardtFitter.h:302
std::vector< Scalar > lambdas
last lambdas (one per output)
Definition: LevenbergMarquardtFitter.h:264
std::string str(const T &t)
convert a data type into a string using an std::ostringstream instance
Definition: StringUtils.h:136
ICLCV_API void error(const char *msg)
Display error message and terminate program.
icl::utils::Function< Vector, const Params &, const Vector & > Function
to-be-optimized function type y = f(params, x)
Definition: LevenbergMarquardtFitter.h:281
Matrix x
input (each row is a data sample)
Definition: LevenbergMarquardtFitter.h:276
std::vector< JacobianMat > jsMat
Definition: LevenbergMarquardtFitter.h:314
FunctionMat fMat
Function f.
Definition: LevenbergMarquardtFitter.h:296
Utility class implementing the multidimensional Levenberg Marquardt Algorithm for non-linear Optimiza...
Definition: LevenbergMarquardtFitter.h:252
Params params_new
new parameters (after update step)
Definition: LevenbergMarquardtFitter.h:317
Scalar eps2
minimum change in parameters threshold
Definition: LevenbergMarquardtFitter.h:305
bool useMultiThreading
flag whether multithreading is enabled
Definition: LevenbergMarquardtFitter.h:298
DebugCallback dbg
debug callback
Definition: LevenbergMarquardtFitter.h:316
Matrix H
Hessian Matrix (J^T J)
Definition: LevenbergMarquardtFitter.h:310
int iteration
number of iterations needed
Definition: LevenbergMarquardtFitter.h:261