19 #ifndef EIGEN_LEVENBERGMARQUARDT_H 20 #define EIGEN_LEVENBERGMARQUARDT_H 24 namespace LevenbergMarquardtSpace {
28 ImproperInputParameters = 0,
29 RelativeReductionTooSmall = 1,
30 RelativeErrorTooSmall = 2,
31 RelativeErrorAndReductionTooSmall = 3,
33 TooManyFunctionEvaluation = 5,
41 template <
typename _Scalar,
int NX=Dynamic,
int NY=Dynamic>
46 InputsAtCompileTime = NX,
47 ValuesAtCompileTime = NY
53 const int m_inputs, m_values;
55 DenseFunctor() : m_inputs(InputsAtCompileTime), m_values(ValuesAtCompileTime) {}
56 DenseFunctor(
int inputs,
int values) : m_inputs(inputs), m_values(values) {}
58 int inputs()
const {
return m_inputs; }
59 int values()
const {
return m_values; }
68 template <
typename _Scalar,
typename _Index>
82 SparseFunctor(
int inputs,
int values) : m_inputs(inputs), m_values(values) {}
84 int inputs()
const {
return m_inputs; }
85 int values()
const {
return m_values; }
87 const int m_inputs, m_values;
96 template <
typename QRSolver,
typename VectorType>
109 template<
typename _FunctorType>
113 typedef _FunctorType FunctorType;
114 typedef typename FunctorType::QRSolver QRSolver;
115 typedef typename FunctorType::JacobianType JacobianType;
117 typedef typename JacobianType::RealScalar RealScalar;
118 typedef typename JacobianType::Index Index;
119 typedef typename QRSolver::Index PermIndex;
124 : m_functor(functor),m_nfev(0),m_njev(0),m_fnorm(0.0),m_gnorm(0),
128 m_useExternalScaling=
false;
131 LevenbergMarquardtSpace::Status minimize(FVectorType &x);
132 LevenbergMarquardtSpace::Status minimizeInit(FVectorType &x);
133 LevenbergMarquardtSpace::Status minimizeOneStep(FVectorType &x);
134 LevenbergMarquardtSpace::Status lmder1(
138 static LevenbergMarquardtSpace::Status lmdif1(
139 FunctorType &functor,
157 void setXtol(RealScalar xtol) { m_xtol = xtol; }
160 void setFtol(RealScalar ftol) { m_ftol = ftol; }
163 void setGtol(RealScalar gtol) { m_gtol = gtol; }
166 void setFactor(RealScalar factor) { m_factor = factor; }
178 FVectorType&
diag() {
return m_diag; }
184 Index
nfev() {
return m_nfev; }
187 Index
njev() {
return m_njev; }
190 RealScalar
fnorm() {
return m_fnorm; }
193 RealScalar
gnorm() {
return m_gnorm; }
200 FVectorType&
fvec() {
return m_fvec; }
231 JacobianType m_rfactor;
232 FunctorType &m_functor;
233 FVectorType m_fvec, m_qtf, m_diag;
248 bool m_useExternalScaling;
249 PermutationType m_permutation;
250 FVectorType m_wa1, m_wa2, m_wa3, m_wa4;
252 bool m_isInitialized;
256 template<
typename FunctorType>
257 LevenbergMarquardtSpace::Status
260 LevenbergMarquardtSpace::Status status = minimizeInit(x);
261 if (status==LevenbergMarquardtSpace::ImproperInputParameters) {
262 m_isInitialized =
true;
267 status = minimizeOneStep(x);
268 }
while (status==LevenbergMarquardtSpace::Running);
269 m_isInitialized =
true;
273 template<
typename FunctorType>
274 LevenbergMarquardtSpace::Status
278 m = m_functor.values();
280 m_wa1.resize(n); m_wa2.resize(n); m_wa3.resize(n);
286 if (!m_useExternalScaling)
288 eigen_assert( (!m_useExternalScaling || m_diag.size()==n) ||
"When m_useExternalScaling is set, the caller must provide a valid 'm_diag'");
296 if (n <= 0 || m < n || m_ftol < 0. || m_xtol < 0. || m_gtol < 0. || m_maxfev <= 0 || m_factor <= 0.){
298 return LevenbergMarquardtSpace::ImproperInputParameters;
301 if (m_useExternalScaling)
302 for (Index j = 0; j < n; ++j)
306 return LevenbergMarquardtSpace::ImproperInputParameters;
312 if ( m_functor(x, m_fvec) < 0)
313 return LevenbergMarquardtSpace::UserAsked;
314 m_fnorm = m_fvec.stableNorm();
320 return LevenbergMarquardtSpace::NotStarted;
323 template<
typename FunctorType>
324 LevenbergMarquardtSpace::Status
331 m = m_functor.values();
334 if (n <= 0 || m < n || tol < 0.)
335 return LevenbergMarquardtSpace::ImproperInputParameters;
340 m_maxfev = 100*(n+1);
346 template<
typename FunctorType>
347 LevenbergMarquardtSpace::Status
349 FunctorType &functor,
356 Index m = functor.values();
359 if (n <= 0 || m < n || tol < 0.)
360 return LevenbergMarquardtSpace::ImproperInputParameters;
369 LevenbergMarquardtSpace::Status info = LevenbergMarquardtSpace::Status(lm.minimize(x));
377 #endif // EIGEN_LEVENBERGMARQUARDT_H RealScalar fnorm()
Definition: LevenbergMarquardt.h:190
Performs non linear optimization over a non-linear function, using a variant of the Levenberg Marquar...
Definition: LevenbergMarquardt.h:110
JacobianType & jacobian()
Definition: LevenbergMarquardt.h:204
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: TestIMU_Common.h:87
FVectorType & fvec()
Definition: LevenbergMarquardt.h:200
Holds information about the various numeric (i.e.
Definition: NumTraits.h:88
Definition: XprHelper.h:32
PermutationType permutation()
the permutation used in the QR factorization
Definition: LevenbergMarquardt.h:213
void setFtol(RealScalar ftol)
Sets the tolerance for the norm of the vector function.
Definition: LevenbergMarquardt.h:160
Sparse left-looking rank-revealing QR factorization.
Definition: SparseQR.h:16
ComputationInfo info() const
Reports whether the minimization was successful.
Definition: LevenbergMarquardt.h:224
void setEpsilon(RealScalar epsfcn)
Sets the error precision.
Definition: LevenbergMarquardt.h:169
void setMaxfev(Index maxfev)
Sets the maximum number of function evaluation.
Definition: LevenbergMarquardt.h:172
Index nfev()
Definition: LevenbergMarquardt.h:184
Definition: LevenbergMarquardt.h:42
Householder rank-revealing QR decomposition of a matrix with column-pivoting.
Definition: ForwardDeclarations.h:222
Index njev()
Definition: LevenbergMarquardt.h:187
RealScalar lm_param(void)
Definition: LevenbergMarquardt.h:196
Definition: LevenbergMarquardt.h:69
void resetParameters()
Sets the default parameters.
Definition: LevenbergMarquardt.h:146
The inputs are invalid, or the algorithm has been improperly called.
Definition: Constants.h:383
Definition: BandTriangularSolver.h:13
void setFactor(RealScalar factor)
Sets the step bound for the diagonal shift.
Definition: LevenbergMarquardt.h:166
FVectorType & diag()
Definition: LevenbergMarquardt.h:178
void setXtol(RealScalar xtol)
Sets the tolerance for the norm of the solution vector.
Definition: LevenbergMarquardt.h:157
JacobianType & matrixR()
Definition: LevenbergMarquardt.h:209
Index iterations()
Definition: LevenbergMarquardt.h:181
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time, and that instead the value is stored in some runtime variable.
Definition: Constants.h:21
This class allows you to add a method df() to your functor, which will use numerical differentiation ...
Definition: NumericalDiff.h:36
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
void setGtol(RealScalar gtol)
Sets the tolerance for the norm of the gradient of the error vector.
Definition: LevenbergMarquardt.h:163
ComputationInfo
Enum for reporting the status of a computation.
Definition: Constants.h:374
void setExternalScaling(bool value)
Use an external Scaling.
Definition: LevenbergMarquardt.h:175
RealScalar gnorm()
Definition: LevenbergMarquardt.h:193
double Scalar
Common scalar type.
Definition: FlexibleKalmanBase.h:48