TR-mbed 1.0
Loading...
Searching...
No Matches
LevenbergMarquardt.h
Go to the documentation of this file.
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Thomas Capricelli <orzel@freehackers.org>
5// Copyright (C) 2012 Desire Nuentsa <desire.nuentsa_wakam@inria.fr>
6//
7// The algorithm of this class initially comes from MINPACK whose original authors are:
8// Copyright Jorge More - Argonne National Laboratory
9// Copyright Burt Garbow - Argonne National Laboratory
10// Copyright Ken Hillstrom - Argonne National Laboratory
11//
12// This Source Code Form is subject to the terms of the Minpack license
13// (a BSD-like license) described in the campaigned CopyrightMINPACK.txt file.
14//
15// This Source Code Form is subject to the terms of the Mozilla
16// Public License v. 2.0. If a copy of the MPL was not distributed
17// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
18
19#ifndef EIGEN_LEVENBERGMARQUARDT_H
20#define EIGEN_LEVENBERGMARQUARDT_H
21
22
23namespace Eigen {
40
41template <typename _Scalar, int NX=Dynamic, int NY=Dynamic>
43{
44 typedef _Scalar Scalar;
45 enum {
48 };
53 const int m_inputs, m_values;
54
57
58 int inputs() const { return m_inputs; }
59 int values() const { return m_values; }
60
61 //int operator()(const InputType &x, ValueType& fvec) { }
62 // should be defined in derived classes
63
64 //int df(const InputType &x, JacobianType& fjac) { }
65 // should be defined in derived classes
66};
67
68template <typename _Scalar, typename _Index>
70{
71 typedef _Scalar Scalar;
72 typedef _Index Index;
77 enum {
80 };
81
83
84 int inputs() const { return m_inputs; }
85 int values() const { return m_values; }
86
87 const int m_inputs, m_values;
88 //int operator()(const InputType &x, ValueType& fvec) { }
89 // to be defined in the functor
90
91 //int df(const InputType &x, JacobianType& fjac) { }
92 // to be defined in the functor if no automatic differentiation
93
94};
95namespace internal {
96template <typename QRSolver, typename VectorType>
97void lmpar2(const QRSolver &qr, const VectorType &diag, const VectorType &qtb,
98 typename VectorType::Scalar m_delta, typename VectorType::Scalar &par,
99 VectorType &x);
100 }
109template<typename _FunctorType>
111{
112 public:
113 typedef _FunctorType FunctorType;
114 typedef typename FunctorType::QRSolver QRSolver;
115 typedef typename FunctorType::JacobianType JacobianType;
116 typedef typename JacobianType::Scalar Scalar;
117 typedef typename JacobianType::RealScalar RealScalar;
118 typedef typename QRSolver::StorageIndex PermIndex;
121 public:
123 : m_functor(functor),m_nfev(0),m_njev(0),m_fnorm(0.0),m_gnorm(0),
124 m_isInitialized(false),m_info(InvalidInput)
125 {
127 m_useExternalScaling=false;
128 }
129
134 FVectorType &x,
135 const Scalar tol = std::sqrt(NumTraits<Scalar>::epsilon())
136 );
138 FunctorType &functor,
139 FVectorType &x,
140 Index *nfev,
141 const Scalar tol = std::sqrt(NumTraits<Scalar>::epsilon())
142 );
143
146 {
147 using std::sqrt;
148
149 m_factor = 100.;
150 m_maxfev = 400;
151 m_ftol = sqrt(NumTraits<RealScalar>::epsilon());
152 m_xtol = sqrt(NumTraits<RealScalar>::epsilon());
153 m_gtol = 0. ;
154 m_epsfcn = 0. ;
155 }
156
158 void setXtol(RealScalar xtol) { m_xtol = xtol; }
159
161 void setFtol(RealScalar ftol) { m_ftol = ftol; }
162
164 void setGtol(RealScalar gtol) { m_gtol = gtol; }
165
167 void setFactor(RealScalar factor) { m_factor = factor; }
168
170 void setEpsilon (RealScalar epsfcn) { m_epsfcn = epsfcn; }
171
173 void setMaxfev(Index maxfev) {m_maxfev = maxfev; }
174
176 void setExternalScaling(bool value) {m_useExternalScaling = value; }
177
179 RealScalar xtol() const {return m_xtol; }
180
182 RealScalar ftol() const {return m_ftol; }
183
185 RealScalar gtol() const {return m_gtol; }
186
188 RealScalar factor() const {return m_factor; }
189
191 RealScalar epsilon() const {return m_epsfcn; }
192
194 Index maxfev() const {return m_maxfev; }
195
197 FVectorType& diag() {return m_diag; }
198
200 Index iterations() { return m_iter; }
201
203 Index nfev() { return m_nfev; }
204
206 Index njev() { return m_njev; }
207
209 RealScalar fnorm() {return m_fnorm; }
210
212 RealScalar gnorm() {return m_gnorm; }
213
215 RealScalar lm_param(void) { return m_par; }
216
219 FVectorType& fvec() {return m_fvec; }
220
223 JacobianType& jacobian() {return m_fjac; }
224
228 JacobianType& matrixR() {return m_rfactor; }
229
232 PermutationType permutation() {return m_permutation; }
233
244 {
245
246 return m_info;
247 }
248 private:
249 JacobianType m_fjac;
250 JacobianType m_rfactor; // The triangular matrix R from the QR of the jacobian matrix m_fjac
251 FunctorType &m_functor;
252 FVectorType m_fvec, m_qtf, m_diag;
253 Index n;
254 Index m;
255 Index m_nfev;
256 Index m_njev;
257 RealScalar m_fnorm; // Norm of the current vector function
258 RealScalar m_gnorm; //Norm of the gradient of the error
259 RealScalar m_factor; //
260 Index m_maxfev; // Maximum number of function evaluation
261 RealScalar m_ftol; //Tolerance in the norm of the vector function
262 RealScalar m_xtol; //
263 RealScalar m_gtol; //tolerance of the norm of the error gradient
264 RealScalar m_epsfcn; //
265 Index m_iter; // Number of iterations performed
266 RealScalar m_delta;
267 bool m_useExternalScaling;
268 PermutationType m_permutation;
269 FVectorType m_wa1, m_wa2, m_wa3, m_wa4; //Temporary vectors
270 RealScalar m_par;
271 bool m_isInitialized; // Check whether the minimization step has been called
272 ComputationInfo m_info;
273};
274
275template<typename FunctorType>
278{
279 LevenbergMarquardtSpace::Status status = minimizeInit(x);
281 m_isInitialized = true;
282 return status;
283 }
284 do {
285// std::cout << " uv " << x.transpose() << "\n";
286 status = minimizeOneStep(x);
287 } while (status==LevenbergMarquardtSpace::Running);
288 m_isInitialized = true;
289 return status;
290}
291
292template<typename FunctorType>
295{
296 n = x.size();
297 m = m_functor.values();
298
299 m_wa1.resize(n); m_wa2.resize(n); m_wa3.resize(n);
300 m_wa4.resize(m);
301 m_fvec.resize(m);
302 //FIXME Sparse Case : Allocate space for the jacobian
303 m_fjac.resize(m, n);
304// m_fjac.reserve(VectorXi::Constant(n,5)); // FIXME Find a better alternative
305 if (!m_useExternalScaling)
306 m_diag.resize(n);
307 eigen_assert( (!m_useExternalScaling || m_diag.size()==n) && "When m_useExternalScaling is set, the caller must provide a valid 'm_diag'");
308 m_qtf.resize(n);
309
310 /* Function Body */
311 m_nfev = 0;
312 m_njev = 0;
313
314 /* check the input parameters for errors. */
315 if (n <= 0 || m < n || m_ftol < 0. || m_xtol < 0. || m_gtol < 0. || m_maxfev <= 0 || m_factor <= 0.){
316 m_info = InvalidInput;
318 }
319
320 if (m_useExternalScaling)
321 for (Index j = 0; j < n; ++j)
322 if (m_diag[j] <= 0.)
323 {
324 m_info = InvalidInput;
326 }
327
328 /* evaluate the function at the starting point */
329 /* and calculate its norm. */
330 m_nfev = 1;
331 if ( m_functor(x, m_fvec) < 0)
333 m_fnorm = m_fvec.stableNorm();
334
335 /* initialize levenberg-marquardt parameter and iteration counter. */
336 m_par = 0.;
337 m_iter = 1;
338
340}
341
342template<typename FunctorType>
345 FVectorType &x,
346 const Scalar tol
347 )
348{
349 n = x.size();
350 m = m_functor.values();
351
352 /* check the input parameters for errors. */
353 if (n <= 0 || m < n || tol < 0.)
355
356 resetParameters();
357 m_ftol = tol;
358 m_xtol = tol;
359 m_maxfev = 100*(n+1);
360
361 return minimize(x);
362}
363
364
365template<typename FunctorType>
368 FunctorType &functor,
369 FVectorType &x,
370 Index *nfev,
371 const Scalar tol
372 )
373{
374 Index n = x.size();
375 Index m = functor.values();
376
377 /* check the input parameters for errors. */
378 if (n <= 0 || m < n || tol < 0.)
380
381 NumericalDiff<FunctorType> numDiff(functor);
382 // embedded LevenbergMarquardt
384 lm.setFtol(tol);
385 lm.setXtol(tol);
386 lm.setMaxfev(200*(n+1));
387
389 if (nfev)
390 * nfev = lm.nfev();
391 return info;
392}
393
394} // end namespace Eigen
395
396#endif // EIGEN_LEVENBERGMARQUARDT_H
Matrix3f m
Definition AngleAxis_mimic_euler.cpp:1
int n
Definition BiCGSTAB_simple.cpp:1
#define eigen_assert(x)
Definition Macros.h:1037
SCALAR Scalar
Definition bench_gemm.cpp:46
NumTraits< Scalar >::Real RealScalar
Definition bench_gemm.cpp:47
Householder rank-revealing QR decomposition of a matrix with column-pivoting.
Definition ColPivHouseholderQR.h:53
Performs non linear optimization over a non-linear function, using a variant of the Levenberg Marquar...
Definition LevenbergMarquardt.h:111
static LevenbergMarquardtSpace::Status lmdif1(FunctorType &functor, FVectorType &x, Index *nfev, const Scalar tol=std::sqrt(NumTraits< Scalar >::epsilon()))
Definition LevenbergMarquardt.h:367
FVectorType & diag()
Definition LevenbergMarquardt.h:197
void setFtol(RealScalar ftol)
Definition LevenbergMarquardt.h:161
FunctorType::QRSolver QRSolver
Definition LevenbergMarquardt.h:114
LevenbergMarquardtSpace::Status minimizeOneStep(FVectorType &x)
Definition LMonestep.h:21
void resetParameters()
Definition LevenbergMarquardt.h:145
RealScalar gtol() const
Definition LevenbergMarquardt.h:185
PermutationMatrix< Dynamic, Dynamic, int > PermutationType
Definition LevenbergMarquardt.h:120
RealScalar xtol() const
Definition LevenbergMarquardt.h:179
RealScalar factor() const
Definition LevenbergMarquardt.h:188
_FunctorType FunctorType
Definition LevenbergMarquardt.h:113
void setFactor(RealScalar factor)
Definition LevenbergMarquardt.h:167
Index njev()
Definition LevenbergMarquardt.h:206
QRSolver::StorageIndex PermIndex
Definition LevenbergMarquardt.h:118
JacobianType & matrixR()
Definition LevenbergMarquardt.h:228
void setEpsilon(RealScalar epsfcn)
Definition LevenbergMarquardt.h:170
FunctorType::JacobianType JacobianType
Definition LevenbergMarquardt.h:115
Index maxfev() const
Definition LevenbergMarquardt.h:194
void setExternalScaling(bool value)
Definition LevenbergMarquardt.h:176
JacobianType & jacobian()
Definition LevenbergMarquardt.h:223
LevenbergMarquardtSpace::Status minimize(FVectorType &x)
Definition LevenbergMarquardt.h:277
PermutationType permutation()
Definition LevenbergMarquardt.h:232
void setXtol(RealScalar xtol)
Definition LevenbergMarquardt.h:158
JacobianType::RealScalar RealScalar
Definition LevenbergMarquardt.h:117
Index nfev
Definition LevenbergMarquardt.h:110
JacobianType::Scalar Scalar
Definition LevenbergMarquardt.h:116
LevenbergMarquardtSpace::Status lmder1(FVectorType &x, const Scalar tol=std::sqrt(NumTraits< Scalar >::epsilon()))
Definition LevenbergMarquardt.h:344
RealScalar ftol() const
Definition LevenbergMarquardt.h:182
FVectorType & fvec()
Definition LevenbergMarquardt.h:219
RealScalar lm_param(void)
Definition LevenbergMarquardt.h:215
RealScalar epsilon() const
Definition LevenbergMarquardt.h:191
RealScalar gnorm()
Definition LevenbergMarquardt.h:212
RealScalar fnorm()
Definition LevenbergMarquardt.h:209
LevenbergMarquardt(FunctorType &functor)
Definition LevenbergMarquardt.h:122
DenseIndex Index
Definition LevenbergMarquardt.h:58
LevenbergMarquardtSpace::Status minimizeInit(FVectorType &x)
Definition LevenbergMarquardt.h:294
void setGtol(RealScalar gtol)
Definition LevenbergMarquardt.h:164
Index nfev()
Definition LevenbergMarquardt.h:203
ComputationInfo info() const
Reports whether the minimization was successful.
Definition LevenbergMarquardt.h:243
Index iterations()
Definition LevenbergMarquardt.h:200
void setMaxfev(Index maxfev)
Definition LevenbergMarquardt.h:173
Matrix< Scalar, Dynamic, 1 > FVectorType
Definition LevenbergMarquardt.h:119
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:180
Definition NumericalDiff.h:37
Permutation matrix.
Definition PermutationMatrix.h:298
A versatible sparse matrix representation.
Definition SparseMatrix.h:98
Sparse left-looking QR factorization with numerical column pivoting.
Definition SparseQR.h:85
Definition XprHelper.h:110
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition gnuplot_common_settings.hh:12
ComputationInfo
Definition Constants.h:440
@ InvalidInput
Definition Constants.h:449
else if n * info
Definition cholesky.cpp:18
Status
Definition LevenbergMarquardt.h:25
@ RelativeReductionTooSmall
Definition LevenbergMarquardt.h:29
@ GtolTooSmall
Definition LevenbergMarquardt.h:36
@ NotStarted
Definition LevenbergMarquardt.h:26
@ UserAsked
Definition LevenbergMarquardt.h:37
@ Running
Definition LevenbergMarquardt.h:27
@ FtolTooSmall
Definition LevenbergMarquardt.h:34
@ TooManyFunctionEvaluation
Definition LevenbergMarquardt.h:33
@ XtolTooSmall
Definition LevenbergMarquardt.h:35
@ CosinusTooSmall
Definition LevenbergMarquardt.h:32
@ RelativeErrorTooSmall
Definition LevenbergMarquardt.h:30
@ ImproperInputParameters
Definition LevenbergMarquardt.h:28
@ RelativeErrorAndReductionTooSmall
Definition LevenbergMarquardt.h:31
void lmpar2(const QRSolver &qr, const VectorType &diag, const VectorType &qtb, typename VectorType::Scalar m_delta, typename VectorType::Scalar &par, VectorType &x)
Definition LMpar.h:20
Namespace containing all symbols from the Eigen library.
Definition bench_norm.cpp:85
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:74
const int Dynamic
Definition Constants.h:22
Definition BandTriangularSolver.h:13
void qr()
Definition qr_colpivoting.cpp:99
Definition LevenbergMarquardt.h:43
Matrix< Scalar, ValuesAtCompileTime, InputsAtCompileTime > JacobianType
Definition LevenbergMarquardt.h:51
const int m_values
Definition LevenbergMarquardt.h:53
int inputs() const
Definition LevenbergMarquardt.h:58
Matrix< Scalar, InputsAtCompileTime, 1 > InputType
Definition LevenbergMarquardt.h:49
const int m_inputs
Definition LevenbergMarquardt.h:53
DenseFunctor()
Definition LevenbergMarquardt.h:55
_Scalar Scalar
Definition LevenbergMarquardt.h:44
Matrix< Scalar, ValuesAtCompileTime, 1 > ValueType
Definition LevenbergMarquardt.h:50
DenseFunctor(int inputs, int values)
Definition LevenbergMarquardt.h:56
ColPivHouseholderQR< JacobianType > QRSolver
Definition LevenbergMarquardt.h:52
int values() const
Definition LevenbergMarquardt.h:59
@ InputsAtCompileTime
Definition LevenbergMarquardt.h:46
@ ValuesAtCompileTime
Definition LevenbergMarquardt.h:47
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:233
Definition LevenbergMarquardt.h:70
SparseFunctor(int inputs, int values)
Definition LevenbergMarquardt.h:82
@ ValuesAtCompileTime
Definition LevenbergMarquardt.h:79
@ InputsAtCompileTime
Definition LevenbergMarquardt.h:78
const int m_inputs
Definition LevenbergMarquardt.h:87
_Index Index
Definition LevenbergMarquardt.h:72
int inputs() const
Definition LevenbergMarquardt.h:84
int values() const
Definition LevenbergMarquardt.h:85
SparseQR< JacobianType, COLAMDOrdering< int > > QRSolver
Definition LevenbergMarquardt.h:76
const int m_values
Definition LevenbergMarquardt.h:87
Matrix< Scalar, Dynamic, 1 > ValueType
Definition LevenbergMarquardt.h:74
Matrix< Scalar, Dynamic, 1 > InputType
Definition LevenbergMarquardt.h:73
SparseMatrix< Scalar, ColMajor, Index > JacobianType
Definition LevenbergMarquardt.h:75
_Scalar Scalar
Definition LevenbergMarquardt.h:71
Definition FFTW.cpp:65
std::ptrdiff_t j
Definition tut_arithmetic_redux_minmax.cpp:2