Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LevenbergMarquardtBase.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include "LevenbergMarquardtBase.hh"
26 
27 using namespace BIAS;
28 
30 LM_Compute(const int numResiduals, const int numParameters,
32  BIAS::Vector<double> &residuals)
33 {
34  // Call Levenberg-Marquardt algorithm from Minpack wrapper
35  int res = 0;
36  LM_squaredSum_ = 0;
37  LM_numIters_ = 0;
38  LM_parameters_.newsize(numParameters);
39  LM_residuals_.newsize(numResiduals);
40  LM_Jacobian_.newsize(numResiduals, numParameters);
41  res = BIAS::LevenbergMarquardtExtended(&g_MinpackFunction, this,
42  numResiduals, numParameters,
43  startX, resultX,
44  LM_tolerance_, LM_maxIters_);
45 
46  // Evaluate results
47  if (res >= 0) {
48  residuals = LM_residuals_;
49  LM_squaredSum_ = 0;
50  for (int j = 0; j < numResiduals; j++)
51  LM_squaredSum_ += LM_residuals_[j]*LM_residuals_[j];
52  }
53  return res;
54 }
55 
57 LM_ComputeWithoutJacobian(const int numResiduals,
58  const int numParameters,
59  BIAS::Vector<double> &startX,
60  BIAS::Vector<double> &resultX,
61  BIAS::Vector<double> &residuals,
62  const double epsilon)
63 {
64  // Call Levenberg-Marquardt algorithm from Minpack wrapper
65  int res = 0;
66  LM_squaredSum_ = 0;
67  LM_numIters_ = 0;
68  LM_parameters_.newsize(numParameters);
69  LM_residuals_.newsize(numResiduals);
70  LM_Jacobian_.newsize(numResiduals, numParameters);
71  LM_Jacobian_.SetZero();
72  res = BIAS::LevenbergMarquardtExtended(&g_MinpackFunctionWithoutJacobian,
73  this, numResiduals, numParameters,
74  startX, resultX, epsilon,
75  LM_tolerance_, LM_maxIters_);
76 
77  // Evaluate results
78  if (res >= 0) {
79  residuals = LM_residuals_;
80  LM_squaredSum_ = 0;
81  for (int j = 0; j < numResiduals; j++)
82  LM_squaredSum_ += LM_residuals_[j]*LM_residuals_[j];
83  }
84 
85  // Approximate final Jacobian of f
86  if (res >= 0) {
87  res = BIAS::ComputeJacobian(&g_MinpackFunctionWithoutJacobian, this,
88  numResiduals, resultX, LM_Jacobian_, epsilon);
89  } else {
90  LM_Jacobian_.SetZero(); // approximation failed!
91  }
92  return res;
93 }
long int LevenbergMarquardtExtended(minpack_funcder_mn fun, void *p, long int m, long int n, Vector< double > &InitialGuess, Vector< double > &Result, double Tolerance, long int MaxIter)
Solves an overdetermined system f(x) = 0 of m non-linear equations with n parameters using the extend...
Definition: Minpack.cpp:131
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
int LM_ComputeWithoutJacobian(const int numResiduals, const int numParameters, BIAS::Vector< double > &startX, BIAS::Vector< double > &resultX, BIAS::Vector< double > &residuals, const double epsilon=LM_DEF_EPSILON)
Compute Levenberg-Marquardt algorithm using the user defined target function f(x) implemented in LM_T...
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
long int ComputeJacobian(minpack_func_mn fun, void *p, const int NumErrors, const Vector< double > &x, Matrix< double > &fjac, const double gradientEpsilon)
Compute the Jacobian J(x) of function f at x.
Definition: Minpack.cpp:443
int LM_Compute(const int numResiduals, const int numParameters, BIAS::Vector< double > &startX, BIAS::Vector< double > &resultX, BIAS::Vector< double > &residuals)
Compute Levenberg-Marquardt algorithm using the user defined target function f(x) implemented in LM_T...