vnl_levenberg_marquardt.h
Go to the documentation of this file.
1 // This is core/vnl/algo/vnl_levenberg_marquardt.h
2 #ifndef vnl_levenberg_marquardt_h_
3 #define vnl_levenberg_marquardt_h_
4 //:
5 // \file
6 // \brief Levenberg Marquardt nonlinear least squares
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date 31 Aug 96
9 //
10 // \verbatim
11 // Modifications
12 // AGAP 160701 Some comments. Changed minimize to call the correct minimization
13 // routine.
14 // RWMC 001097 Added verbose flag to get rid of all that blathering.
15 // AWF 151197 Added trace flag to increase blather.
16 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
17 // \endverbatim
18 //
19 
20 #include <iosfwd>
21 #ifdef _MSC_VER
22 # include <vcl_msvc_warnings.h>
23 #endif
24 #include <vnl/vnl_vector.h>
25 #include <vnl/vnl_vector_fixed.h>
26 #include <vnl/vnl_matrix.h>
28 
29 #include <vnl/algo/vnl_algo_export.h>
30 
32 
33 //: Levenberg Marquardt nonlinear least squares
34 // vnl_levenberg_marquardt is an interface to the MINPACK routine lmdif,
35 // and implements Levenberg Marquardt nonlinear fitting. The function
36 // to be minimized is passed as a vnl_least_squares_function object, which
37 // may or may not wish to provide derivatives. If derivatives are not
38 // supplied, they are calculated by forward differencing, which costs
39 // one function evaluation per dimension, but is perfectly accurate.
40 // (See Hartley in ``Applications of Invariance in Computer Vision''
41 // for example).
42 
43 class VNL_ALGO_EXPORT vnl_levenberg_marquardt : public vnl_nonlinear_minimizer
44 {
45  public:
46 
47  //: Initialize with the function object that is to be minimized.
49 
50  ~vnl_levenberg_marquardt() override;
51 
52  //: Minimize the function supplied in the constructor until convergence or failure.
53  // On return, x is such that f(x) is the lowest value achieved.
54  // Returns true for convergence, false for failure.
55  // Does not use the gradient even if the cost function provides one.
56  bool minimize_without_gradient(vnl_vector<double>& x);
57 
58  //: Minimize the function supplied in the constructor until convergence or failure.
59  // On return, x is such that f(x) is the lowest value achieved.
60  // Returns true for convergence, false for failure.
61  // The cost function must provide a gradient.
62  bool minimize_using_gradient (vnl_vector<double>& x);
63 
64  //: Calls minimize_using_gradient() or minimize_without_gradient(),
65  // depending on whether the cost function provides a gradient.
66  bool minimize(vnl_vector<double>& x);
67  bool minimize(vnl_vector_fixed<double,1>& x) { vnl_vector<double> y=x.extract(1); bool b=minimize(y); x=y; return b; }
68  bool minimize(vnl_vector_fixed<double,2>& x) { vnl_vector<double> y=x.extract(2); bool b=minimize(y); x=y; return b; }
69  bool minimize(vnl_vector_fixed<double,3>& x) { vnl_vector<double> y=x.extract(3); bool b=minimize(y); x=y; return b; }
70  bool minimize(vnl_vector_fixed<double,4>& x) { vnl_vector<double> y=x.extract(4); bool b=minimize(y); x=y; return b; }
71 
72  // Coping with failure-------------------------------------------------------
73 
74  //: Provide an ASCII diagnosis of the last minimization on std::ostream.
75  void diagnose_outcome(/*std::cerr*/) const;
76  void diagnose_outcome(std::ostream&) const;
77 
78  //: Return J'*J computed at last minimum.
79  // it is an approximation of inverse of covariance
80  vnl_matrix<double> const& get_JtJ();
81 
82  protected:
83 
85  vnl_matrix<double> fdjac_; // Computed during lmdif/lmder
86  vnl_vector<long> ipvt_; // Also computed, both needed to get J'*J at end.
87 
89  bool set_covariance_; // Set if covariance_ holds J'*J
90 
91  void init(vnl_least_squares_function* f);
92 
93  // Communication with callback
94  static void lmdif_lsqfun(long* m, long* n, double* x,
95  double* fx, long* iflag, void* userdata);
96  static void lmder_lsqfun(long* m, long* n, double* x,
97  double* fx, double* fJ, long*, long* iflag,
98  void* userdata);
99 };
100 
101 //: Find minimum of "f", starting at "initial_estimate", and return.
103  vnl_vector<double> const& initial_estimate);
104 
105 
106 #endif // vnl_levenberg_marquardt_h_
An ordinary mathematical matrix.
#define m
Definition: vnl_vector.h:43
vnl_nonlinear_minimizer is a base class for nonlinear optimization.
Abstract base for minimising functions.
bool minimize(vnl_vector_fixed< double, 4 > &x)
vnl_vector< double > vnl_levenberg_marquardt_minimize(vnl_least_squares_function &f, vnl_vector< double > const &initial_estimate)
Find minimum of "f", starting at "initial_estimate", and return.
bool minimize(vnl_vector_fixed< double, 1 > &x)
Levenberg Marquardt nonlinear least squares.
vnl_levenberg_marquardt(vnl_least_squares_function &f)
Initialize with the function object that is to be minimized.
bool minimize(vnl_vector_fixed< double, 2 > &x)
Base class for nonlinear optimization.
vnl_vector< T > extract(unsigned int len, unsigned int start=0) const
Returns a subvector specified by the start index and length. O(n).
Fixed length stack-stored, space-efficient vector.
Definition: vnl_fwd.h:22
Fixed length stack-stored vector.
vnl_matrix< double > inv_covar_
vnl_least_squares_function * f_
bool minimize(vnl_vector_fixed< double, 3 > &x)