eigen/unsupported/Eigen/src/NonLinearOptimization/dogleg.h

105 lines
3.1 KiB
C
Raw Normal View History

namespace Eigen {
namespace internal {
2009-08-24 03:39:47 +08:00
template <typename Scalar>
void dogleg(
const Matrix< Scalar, Dynamic, Dynamic > &qrfac,
2009-08-24 03:39:47 +08:00
const Matrix< Scalar, Dynamic, 1 > &diag,
const Matrix< Scalar, Dynamic, 1 > &qtb,
Scalar delta,
Matrix< Scalar, Dynamic, 1 > &x)
{
typedef DenseIndex Index;
/* Local variables */
Index i, j;
Scalar sum, temp, alpha, bnorm;
2009-08-22 13:31:14 +08:00
Scalar gnorm, qnorm;
Scalar sgnorm;
/* Function Body */
const Scalar epsmch = NumTraits<Scalar>::epsilon();
const Index n = qrfac.cols();
2009-08-24 03:39:47 +08:00
assert(n==qtb.size());
assert(n==x.size());
2010-01-26 14:36:15 +08:00
assert(n==diag.size());
Matrix< Scalar, Dynamic, 1 > wa1(n), wa2(n);
2010-01-26 14:36:15 +08:00
/* first, calculate the gauss-newton direction. */
for (j = n-1; j >=0; --j) {
temp = qrfac(j,j);
if (temp == 0.) {
2010-01-26 14:36:15 +08:00
temp = epsmch * qrfac.col(j).head(j+1).maxCoeff();
2009-08-24 22:39:49 +08:00
if (temp == 0.)
2009-08-24 03:47:55 +08:00
temp = epsmch;
}
2010-01-26 14:36:15 +08:00
if (j==n-1)
x[j] = qtb[j] / temp;
else
x[j] = (qtb[j] - qrfac.row(j).tail(n-j-1).dot(x.tail(n-j-1))) / temp;
}
2009-08-24 22:39:49 +08:00
/* test whether the gauss-newton direction is acceptable. */
2010-01-28 11:19:39 +08:00
qnorm = diag.cwiseProduct(x).stableNorm();
2009-08-24 03:47:55 +08:00
if (qnorm <= delta)
return;
2010-01-26 14:36:15 +08:00
// TODO : this path is not tested by Eigen unit tests
2009-08-24 22:39:49 +08:00
/* the gauss-newton direction is not acceptable. */
/* next, calculate the scaled gradient direction. */
2010-01-26 14:36:15 +08:00
wa1.fill(0.);
2009-08-24 03:39:47 +08:00
for (j = 0; j < n; ++j) {
2010-01-28 11:19:39 +08:00
wa1.tail(n-j) += qrfac.row(j).tail(n-j) * qtb[j];
wa1[j] /= diag[j];
}
2009-08-24 22:39:49 +08:00
/* calculate the norm of the scaled gradient and test for */
/* the special case in which the scaled gradient is zero. */
2009-08-24 03:39:47 +08:00
gnorm = wa1.stableNorm();
sgnorm = 0.;
alpha = delta / qnorm;
if (gnorm == 0.)
2009-08-24 22:39:49 +08:00
goto algo_end;
2009-08-24 22:39:49 +08:00
/* calculate the point along the scaled gradient */
/* at which the quadratic is minimized. */
2010-01-05 22:38:20 +08:00
wa1.array() /= (diag*gnorm).array();
2010-01-26 14:36:15 +08:00
// TODO : once unit tests cover this part,:
// wa2 = qrfac.template triangularView<Upper>() * wa1;
2009-08-24 03:39:47 +08:00
for (j = 0; j < n; ++j) {
sum = 0.;
2009-08-24 03:39:47 +08:00
for (i = j; i < n; ++i) {
sum += qrfac(j,i) * wa1[i];
}
wa2[j] = sum;
}
2009-08-24 03:39:47 +08:00
temp = wa2.stableNorm();
sgnorm = gnorm / temp / temp;
2009-08-24 22:39:49 +08:00
/* test whether the scaled gradient direction is acceptable. */
alpha = 0.;
2009-08-24 22:39:49 +08:00
if (sgnorm >= delta)
goto algo_end;
2009-08-24 22:39:49 +08:00
/* the scaled gradient direction is not acceptable. */
/* finally, calculate the point along the dogleg */
/* at which the quadratic is minimized. */
2009-08-24 03:39:47 +08:00
bnorm = qtb.stableNorm();
temp = bnorm / gnorm * (bnorm / qnorm) * (sgnorm / delta);
temp = temp - delta / qnorm * abs2(sgnorm / delta) + sqrt(abs2(temp - delta / qnorm) + (1.-abs2(delta / qnorm)) * (1.-abs2(sgnorm / delta)));
alpha = delta / qnorm * (1. - abs2(sgnorm / delta)) / temp;
2009-08-24 22:39:49 +08:00
algo_end:
2009-08-24 22:39:49 +08:00
/* form appropriate convex combination of the gauss-newton */
/* direction and the scaled gradient direction. */
temp = (1.-alpha) * (std::min)(sgnorm,delta);
x = temp * wa1 + alpha * x;
2009-08-24 03:39:47 +08:00
}
} // end namespace internal
} // end namespace Eigen