Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
LMonestep.h
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//
6// This code initially comes from MINPACK whose original authors are:
7// Copyright Jorge More - Argonne National Laboratory
8// Copyright Burt Garbow - Argonne National Laboratory
9// Copyright Ken Hillstrom - Argonne National Laboratory
10//
11// This Source Code Form is subject to the terms of the Minpack license
12// (a BSD-like license) described in the CopyrightMINPACK.txt file.
13// SPDX-License-Identifier: MPL-2.0 AND LicenseRef-MINPACK
14
15#ifndef EIGEN_LMONESTEP_H
16#define EIGEN_LMONESTEP_H
17
18// IWYU pragma: private
19#include "./InternalHeaderCheck.h"
20
21namespace Eigen {
22
23template <typename FunctorType>
24LevenbergMarquardtSpace::Status LevenbergMarquardt<FunctorType>::minimizeOneStep(FVectorType &x) {
25 using std::abs;
26 using std::sqrt;
27 RealScalar temp, temp1, temp2;
28 RealScalar ratio;
29 RealScalar pnorm, xnorm, fnorm1, actred, dirder, prered;
30 eigen_assert(x.size() == n); // check the caller is not cheating us
31
32 temp = 0.0;
33 xnorm = 0.0;
34 /* calculate the jacobian matrix. */
35 Index df_ret = m_functor.df(x, m_fjac);
36 if (df_ret < 0) return LevenbergMarquardtSpace::UserAsked;
37 if (df_ret > 0)
38 // numerical diff, we evaluated the function df_ret times
39 m_nfev += df_ret;
40 else
41 m_njev++;
42
43 /* compute the qr factorization of the jacobian. */
44 for (int j = 0; j < x.size(); ++j) m_wa2(j) = m_fjac.col(j).blueNorm();
45 QRSolver qrfac(m_fjac);
46 if (qrfac.info() != Success) {
47 m_info = NumericalIssue;
48 return LevenbergMarquardtSpace::ImproperInputParameters;
49 }
50 // Make a copy of the first factor with the associated permutation
51 m_rfactor = qrfac.matrixR();
52 m_permutation = (qrfac.colsPermutation());
53
54 /* on the first iteration and if external scaling is not used, scale according */
55 /* to the norms of the columns of the initial jacobian. */
56 if (m_iter == 1) {
57 if (!m_useExternalScaling)
58 for (Index j = 0; j < n; ++j) m_diag[j] = (m_wa2[j] == 0.) ? 1. : m_wa2[j];
59
60 /* on the first iteration, calculate the norm of the scaled x */
61 /* and initialize the step bound m_delta. */
62 xnorm = m_diag.cwiseProduct(x).stableNorm();
63 m_delta = m_factor * xnorm;
64 if (m_delta == 0.) m_delta = m_factor;
65 }
66
67 /* form (q transpose)*m_fvec and store the first n components in */
68 /* m_qtf. */
69 m_wa4 = m_fvec;
70 m_wa4 = qrfac.matrixQ().adjoint() * m_fvec;
71 m_qtf = m_wa4.head(n);
72
73 /* compute the norm of the scaled gradient. */
74 m_gnorm = 0.;
75 if (m_fnorm != 0.)
76 for (Index j = 0; j < n; ++j)
77 if (m_wa2[m_permutation.indices()[j]] != 0.)
78 m_gnorm = (std::max)(m_gnorm, abs(m_rfactor.col(j).head(j + 1).dot(m_qtf.head(j + 1) / m_fnorm) /
79 m_wa2[m_permutation.indices()[j]]));
80
81 /* test for convergence of the gradient norm. */
82 if (m_gnorm <= m_gtol) {
83 m_info = Success;
84 return LevenbergMarquardtSpace::CosinusTooSmall;
85 }
86
87 /* rescale if necessary. */
88 if (!m_useExternalScaling) m_diag = m_diag.cwiseMax(m_wa2);
89
90 do {
91 /* determine the levenberg-marquardt parameter. */
92 internal::lmpar2(qrfac, m_diag, m_qtf, m_delta, m_par, m_wa1);
93
94 /* store the direction p and x + p. calculate the norm of p. */
95 m_wa1 = -m_wa1;
96 m_wa2 = x + m_wa1;
97 pnorm = m_diag.cwiseProduct(m_wa1).stableNorm();
98
99 /* on the first iteration, adjust the initial step bound. */
100 if (m_iter == 1) m_delta = (std::min)(m_delta, pnorm);
101
102 /* evaluate the function at x + p and calculate its norm. */
103 if (m_functor(m_wa2, m_wa4) < 0) return LevenbergMarquardtSpace::UserAsked;
104 ++m_nfev;
105 fnorm1 = m_wa4.stableNorm();
106
107 /* compute the scaled actual reduction. */
108 actred = -1.;
109 if (Scalar(.1) * fnorm1 < m_fnorm) actred = 1. - numext::abs2(fnorm1 / m_fnorm);
110
111 /* compute the scaled predicted reduction and */
112 /* the scaled directional derivative. */
113 m_wa3.noalias() = m_rfactor.template triangularView<Upper>() * (m_permutation.inverse() * m_wa1);
114 temp1 = numext::abs2(m_wa3.stableNorm() / m_fnorm);
115 temp2 = numext::abs2(sqrt(m_par) * pnorm / m_fnorm);
116 prered = temp1 + temp2 / Scalar(.5);
117 dirder = -(temp1 + temp2);
118
119 /* compute the ratio of the actual to the predicted */
120 /* reduction. */
121 ratio = 0.;
122 if (prered != 0.) ratio = actred / prered;
123
124 /* update the step bound. */
125 if (ratio <= Scalar(.25)) {
126 if (actred >= 0.) temp = RealScalar(.5);
127 if (actred < 0.) temp = RealScalar(.5) * dirder / (dirder + RealScalar(.5) * actred);
128 if (RealScalar(.1) * fnorm1 >= m_fnorm || temp < RealScalar(.1)) temp = Scalar(.1);
129 /* Computing MIN */
130 m_delta = temp * (std::min)(m_delta, pnorm / RealScalar(.1));
131 m_par /= temp;
132 } else if (!(m_par != 0. && ratio < RealScalar(.75))) {
133 m_delta = pnorm / RealScalar(.5);
134 m_par = RealScalar(.5) * m_par;
135 }
136
137 /* test for successful iteration. */
138 if (ratio >= RealScalar(1e-4)) {
139 /* successful iteration. update x, m_fvec, and their norms. */
140 x = m_wa2;
141 m_wa2 = m_diag.cwiseProduct(x);
142 m_fvec = m_wa4;
143 xnorm = m_wa2.stableNorm();
144 m_fnorm = fnorm1;
145 ++m_iter;
146 }
147
148 /* tests for convergence. */
149 if (abs(actred) <= m_ftol && prered <= m_ftol && Scalar(.5) * ratio <= 1. && m_delta <= m_xtol * xnorm) {
150 m_info = Success;
151 return LevenbergMarquardtSpace::RelativeErrorAndReductionTooSmall;
152 }
153 if (abs(actred) <= m_ftol && prered <= m_ftol && Scalar(.5) * ratio <= 1.) {
154 m_info = Success;
155 return LevenbergMarquardtSpace::RelativeReductionTooSmall;
156 }
157 if (m_delta <= m_xtol * xnorm) {
158 m_info = Success;
159 return LevenbergMarquardtSpace::RelativeErrorTooSmall;
160 }
161
162 /* tests for termination and stringent tolerances. */
163 if (m_nfev >= m_maxfev) {
164 m_info = NoConvergence;
165 return LevenbergMarquardtSpace::TooManyFunctionEvaluation;
166 }
167 if (abs(actred) <= NumTraits<Scalar>::epsilon() && prered <= NumTraits<Scalar>::epsilon() &&
168 Scalar(.5) * ratio <= 1.) {
169 m_info = Success;
170 return LevenbergMarquardtSpace::FtolTooSmall;
171 }
172 if (m_delta <= NumTraits<Scalar>::epsilon() * xnorm) {
173 m_info = Success;
174 return LevenbergMarquardtSpace::XtolTooSmall;
175 }
176 if (m_gnorm <= NumTraits<Scalar>::epsilon()) {
177 m_info = Success;
178 return LevenbergMarquardtSpace::GtolTooSmall;
179 }
180
181 } while (ratio < Scalar(1e-4));
182
183 return LevenbergMarquardtSpace::Running;
184}
185
186} // end namespace Eigen
187
188#endif // EIGEN_LMONESTEP_H
NumericalIssue
Namespace containing all symbols from the Eigen library.