File indexing completed on 2025-01-18 09:57:04
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef EIGEN_LMONESTEP_H
0015 #define EIGEN_LMONESTEP_H
0016
0017 namespace Eigen {
0018
0019 template<typename FunctorType>
0020 LevenbergMarquardtSpace::Status
0021 LevenbergMarquardt<FunctorType>::minimizeOneStep(FVectorType &x)
0022 {
0023 using std::abs;
0024 using std::sqrt;
0025 RealScalar temp, temp1,temp2;
0026 RealScalar ratio;
0027 RealScalar pnorm, xnorm, fnorm1, actred, dirder, prered;
0028 eigen_assert(x.size()==n);
0029
0030 temp = 0.0; xnorm = 0.0;
0031
0032 Index df_ret = m_functor.df(x, m_fjac);
0033 if (df_ret<0)
0034 return LevenbergMarquardtSpace::UserAsked;
0035 if (df_ret>0)
0036
0037 m_nfev += df_ret;
0038 else m_njev++;
0039
0040
0041 for (int j = 0; j < x.size(); ++j)
0042 m_wa2(j) = m_fjac.col(j).blueNorm();
0043 QRSolver qrfac(m_fjac);
0044 if(qrfac.info() != Success) {
0045 m_info = NumericalIssue;
0046 return LevenbergMarquardtSpace::ImproperInputParameters;
0047 }
0048
0049 m_rfactor = qrfac.matrixR();
0050 m_permutation = (qrfac.colsPermutation());
0051
0052
0053
0054 if (m_iter == 1) {
0055 if (!m_useExternalScaling)
0056 for (Index j = 0; j < n; ++j)
0057 m_diag[j] = (m_wa2[j]==0.)? 1. : m_wa2[j];
0058
0059
0060
0061 xnorm = m_diag.cwiseProduct(x).stableNorm();
0062 m_delta = m_factor * xnorm;
0063 if (m_delta == 0.)
0064 m_delta = m_factor;
0065 }
0066
0067
0068
0069 m_wa4 = m_fvec;
0070 m_wa4 = qrfac.matrixQ().adjoint() * m_fvec;
0071 m_qtf = m_wa4.head(n);
0072
0073
0074 m_gnorm = 0.;
0075 if (m_fnorm != 0.)
0076 for (Index j = 0; j < n; ++j)
0077 if (m_wa2[m_permutation.indices()[j]] != 0.)
0078 m_gnorm = (std::max)(m_gnorm, abs( m_rfactor.col(j).head(j+1).dot(m_qtf.head(j+1)/m_fnorm) / m_wa2[m_permutation.indices()[j]]));
0079
0080
0081 if (m_gnorm <= m_gtol) {
0082 m_info = Success;
0083 return LevenbergMarquardtSpace::CosinusTooSmall;
0084 }
0085
0086
0087 if (!m_useExternalScaling)
0088 m_diag = m_diag.cwiseMax(m_wa2);
0089
0090 do {
0091
0092 internal::lmpar2(qrfac, m_diag, m_qtf, m_delta, m_par, m_wa1);
0093
0094
0095 m_wa1 = -m_wa1;
0096 m_wa2 = x + m_wa1;
0097 pnorm = m_diag.cwiseProduct(m_wa1).stableNorm();
0098
0099
0100 if (m_iter == 1)
0101 m_delta = (std::min)(m_delta,pnorm);
0102
0103
0104 if ( m_functor(m_wa2, m_wa4) < 0)
0105 return LevenbergMarquardtSpace::UserAsked;
0106 ++m_nfev;
0107 fnorm1 = m_wa4.stableNorm();
0108
0109
0110 actred = -1.;
0111 if (Scalar(.1) * fnorm1 < m_fnorm)
0112 actred = 1. - numext::abs2(fnorm1 / m_fnorm);
0113
0114
0115
0116 m_wa3 = m_rfactor.template triangularView<Upper>() * (m_permutation.inverse() *m_wa1);
0117 temp1 = numext::abs2(m_wa3.stableNorm() / m_fnorm);
0118 temp2 = numext::abs2(sqrt(m_par) * pnorm / m_fnorm);
0119 prered = temp1 + temp2 / Scalar(.5);
0120 dirder = -(temp1 + temp2);
0121
0122
0123
0124 ratio = 0.;
0125 if (prered != 0.)
0126 ratio = actred / prered;
0127
0128
0129 if (ratio <= Scalar(.25)) {
0130 if (actred >= 0.)
0131 temp = RealScalar(.5);
0132 if (actred < 0.)
0133 temp = RealScalar(.5) * dirder / (dirder + RealScalar(.5) * actred);
0134 if (RealScalar(.1) * fnorm1 >= m_fnorm || temp < RealScalar(.1))
0135 temp = Scalar(.1);
0136
0137 m_delta = temp * (std::min)(m_delta, pnorm / RealScalar(.1));
0138 m_par /= temp;
0139 } else if (!(m_par != 0. && ratio < RealScalar(.75))) {
0140 m_delta = pnorm / RealScalar(.5);
0141 m_par = RealScalar(.5) * m_par;
0142 }
0143
0144
0145 if (ratio >= RealScalar(1e-4)) {
0146
0147 x = m_wa2;
0148 m_wa2 = m_diag.cwiseProduct(x);
0149 m_fvec = m_wa4;
0150 xnorm = m_wa2.stableNorm();
0151 m_fnorm = fnorm1;
0152 ++m_iter;
0153 }
0154
0155
0156 if (abs(actred) <= m_ftol && prered <= m_ftol && Scalar(.5) * ratio <= 1. && m_delta <= m_xtol * xnorm)
0157 {
0158 m_info = Success;
0159 return LevenbergMarquardtSpace::RelativeErrorAndReductionTooSmall;
0160 }
0161 if (abs(actred) <= m_ftol && prered <= m_ftol && Scalar(.5) * ratio <= 1.)
0162 {
0163 m_info = Success;
0164 return LevenbergMarquardtSpace::RelativeReductionTooSmall;
0165 }
0166 if (m_delta <= m_xtol * xnorm)
0167 {
0168 m_info = Success;
0169 return LevenbergMarquardtSpace::RelativeErrorTooSmall;
0170 }
0171
0172
0173 if (m_nfev >= m_maxfev)
0174 {
0175 m_info = NoConvergence;
0176 return LevenbergMarquardtSpace::TooManyFunctionEvaluation;
0177 }
0178 if (abs(actred) <= NumTraits<Scalar>::epsilon() && prered <= NumTraits<Scalar>::epsilon() && Scalar(.5) * ratio <= 1.)
0179 {
0180 m_info = Success;
0181 return LevenbergMarquardtSpace::FtolTooSmall;
0182 }
0183 if (m_delta <= NumTraits<Scalar>::epsilon() * xnorm)
0184 {
0185 m_info = Success;
0186 return LevenbergMarquardtSpace::XtolTooSmall;
0187 }
0188 if (m_gnorm <= NumTraits<Scalar>::epsilon())
0189 {
0190 m_info = Success;
0191 return LevenbergMarquardtSpace::GtolTooSmall;
0192 }
0193
0194 } while (ratio < Scalar(1e-4));
0195
0196 return LevenbergMarquardtSpace::Running;
0197 }
0198
0199
0200 }
0201
0202 #endif