Blame internal/ceres/dogleg_strategy.cc

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
Packit ea1746
#include "ceres/dogleg_strategy.h"
Packit ea1746
Packit ea1746
#include <cmath>
Packit ea1746
#include "Eigen/Dense"
Packit ea1746
#include "ceres/array_utils.h"
Packit ea1746
#include "ceres/internal/eigen.h"
Packit ea1746
#include "ceres/linear_least_squares_problems.h"
Packit ea1746
#include "ceres/linear_solver.h"
Packit ea1746
#include "ceres/polynomial.h"
Packit ea1746
#include "ceres/sparse_matrix.h"
Packit ea1746
#include "ceres/trust_region_strategy.h"
Packit ea1746
#include "ceres/types.h"
Packit ea1746
#include "glog/logging.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
namespace {
Packit ea1746
const double kMaxMu = 1.0;
Packit ea1746
const double kMinMu = 1e-8;
Packit ea1746
}
Packit ea1746
Packit ea1746
DoglegStrategy::DoglegStrategy(const TrustRegionStrategy::Options& options)
Packit ea1746
    : linear_solver_(options.linear_solver),
Packit ea1746
      radius_(options.initial_radius),
Packit ea1746
      max_radius_(options.max_radius),
Packit ea1746
      min_diagonal_(options.min_lm_diagonal),
Packit ea1746
      max_diagonal_(options.max_lm_diagonal),
Packit ea1746
      mu_(kMinMu),
Packit ea1746
      min_mu_(kMinMu),
Packit ea1746
      max_mu_(kMaxMu),
Packit ea1746
      mu_increase_factor_(10.0),
Packit ea1746
      increase_threshold_(0.75),
Packit ea1746
      decrease_threshold_(0.25),
Packit ea1746
      dogleg_step_norm_(0.0),
Packit ea1746
      reuse_(false),
Packit ea1746
      dogleg_type_(options.dogleg_type) {
Packit ea1746
  CHECK_NOTNULL(linear_solver_);
Packit ea1746
  CHECK_GT(min_diagonal_, 0.0);
Packit ea1746
  CHECK_LE(min_diagonal_, max_diagonal_);
Packit ea1746
  CHECK_GT(max_radius_, 0.0);
Packit ea1746
}
Packit ea1746
Packit ea1746
// If the reuse_ flag is not set, then the Cauchy point (scaled
Packit ea1746
// gradient) and the new Gauss-Newton step are computed from
Packit ea1746
// scratch. The Dogleg step is then computed as interpolation of these
Packit ea1746
// two vectors.
Packit ea1746
TrustRegionStrategy::Summary DoglegStrategy::ComputeStep(
Packit ea1746
    const TrustRegionStrategy::PerSolveOptions& per_solve_options,
Packit ea1746
    SparseMatrix* jacobian,
Packit ea1746
    const double* residuals,
Packit ea1746
    double* step) {
Packit ea1746
  CHECK_NOTNULL(jacobian);
Packit ea1746
  CHECK_NOTNULL(residuals);
Packit ea1746
  CHECK_NOTNULL(step);
Packit ea1746
Packit ea1746
  const int n = jacobian->num_cols();
Packit ea1746
  if (reuse_) {
Packit ea1746
    // Gauss-Newton and gradient vectors are always available, only a
Packit ea1746
    // new interpolant need to be computed. For the subspace case,
Packit ea1746
    // the subspace and the two-dimensional model are also still valid.
Packit ea1746
    switch (dogleg_type_) {
Packit ea1746
      case TRADITIONAL_DOGLEG:
Packit ea1746
        ComputeTraditionalDoglegStep(step);
Packit ea1746
        break;
Packit ea1746
Packit ea1746
      case SUBSPACE_DOGLEG:
Packit ea1746
        ComputeSubspaceDoglegStep(step);
Packit ea1746
        break;
Packit ea1746
    }
Packit ea1746
    TrustRegionStrategy::Summary summary;
Packit ea1746
    summary.num_iterations = 0;
Packit ea1746
    summary.termination_type = LINEAR_SOLVER_SUCCESS;
Packit ea1746
    return summary;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  reuse_ = true;
Packit ea1746
  // Check that we have the storage needed to hold the various
Packit ea1746
  // temporary vectors.
Packit ea1746
  if (diagonal_.rows() != n) {
Packit ea1746
    diagonal_.resize(n, 1);
Packit ea1746
    gradient_.resize(n, 1);
Packit ea1746
    gauss_newton_step_.resize(n, 1);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Vector used to form the diagonal matrix that is used to
Packit ea1746
  // regularize the Gauss-Newton solve and that defines the
Packit ea1746
  // elliptical trust region
Packit ea1746
  //
Packit ea1746
  //   || D * step || <= radius_ .
Packit ea1746
  //
Packit ea1746
  jacobian->SquaredColumnNorm(diagonal_.data());
Packit ea1746
  for (int i = 0; i < n; ++i) {
Packit ea1746
    diagonal_[i] = std::min(std::max(diagonal_[i], min_diagonal_),
Packit ea1746
                            max_diagonal_);
Packit ea1746
  }
Packit ea1746
  diagonal_ = diagonal_.array().sqrt();
Packit ea1746
Packit ea1746
  ComputeGradient(jacobian, residuals);
Packit ea1746
  ComputeCauchyPoint(jacobian);
Packit ea1746
Packit ea1746
  LinearSolver::Summary linear_solver_summary =
Packit ea1746
      ComputeGaussNewtonStep(per_solve_options, jacobian, residuals);
Packit ea1746
Packit ea1746
  TrustRegionStrategy::Summary summary;
Packit ea1746
  summary.residual_norm = linear_solver_summary.residual_norm;
Packit ea1746
  summary.num_iterations = linear_solver_summary.num_iterations;
Packit ea1746
  summary.termination_type = linear_solver_summary.termination_type;
Packit ea1746
Packit ea1746
  if (linear_solver_summary.termination_type == LINEAR_SOLVER_FATAL_ERROR) {
Packit ea1746
    return summary;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  if (linear_solver_summary.termination_type != LINEAR_SOLVER_FAILURE) {
Packit ea1746
    switch (dogleg_type_) {
Packit ea1746
      // Interpolate the Cauchy point and the Gauss-Newton step.
Packit ea1746
      case TRADITIONAL_DOGLEG:
Packit ea1746
        ComputeTraditionalDoglegStep(step);
Packit ea1746
        break;
Packit ea1746
Packit ea1746
      // Find the minimum in the subspace defined by the
Packit ea1746
      // Cauchy point and the (Gauss-)Newton step.
Packit ea1746
      case SUBSPACE_DOGLEG:
Packit ea1746
        if (!ComputeSubspaceModel(jacobian)) {
Packit ea1746
          summary.termination_type = LINEAR_SOLVER_FAILURE;
Packit ea1746
          break;
Packit ea1746
        }
Packit ea1746
        ComputeSubspaceDoglegStep(step);
Packit ea1746
        break;
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  return summary;
Packit ea1746
}
Packit ea1746
Packit ea1746
// The trust region is assumed to be elliptical with the
Packit ea1746
// diagonal scaling matrix D defined by sqrt(diagonal_).
Packit ea1746
// It is implemented by substituting step' = D * step.
Packit ea1746
// The trust region for step' is spherical.
Packit ea1746
// The gradient, the Gauss-Newton step, the Cauchy point,
Packit ea1746
// and all calculations involving the Jacobian have to
Packit ea1746
// be adjusted accordingly.
Packit ea1746
void DoglegStrategy::ComputeGradient(
Packit ea1746
    SparseMatrix* jacobian,
Packit ea1746
    const double* residuals) {
Packit ea1746
  gradient_.setZero();
Packit ea1746
  jacobian->LeftMultiply(residuals, gradient_.data());
Packit ea1746
  gradient_.array() /= diagonal_.array();
Packit ea1746
}
Packit ea1746
Packit ea1746
// The Cauchy point is the global minimizer of the quadratic model
Packit ea1746
// along the one-dimensional subspace spanned by the gradient.
Packit ea1746
void DoglegStrategy::ComputeCauchyPoint(SparseMatrix* jacobian) {
Packit ea1746
  // alpha * -gradient is the Cauchy point.
Packit ea1746
  Vector Jg(jacobian->num_rows());
Packit ea1746
  Jg.setZero();
Packit ea1746
  // The Jacobian is scaled implicitly by computing J * (D^-1 * (D^-1 * g))
Packit ea1746
  // instead of (J * D^-1) * (D^-1 * g).
Packit ea1746
  Vector scaled_gradient =
Packit ea1746
      (gradient_.array() / diagonal_.array()).matrix();
Packit ea1746
  jacobian->RightMultiply(scaled_gradient.data(), Jg.data());
Packit ea1746
  alpha_ = gradient_.squaredNorm() / Jg.squaredNorm();
Packit ea1746
}
Packit ea1746
Packit ea1746
// The dogleg step is defined as the intersection of the trust region
Packit ea1746
// boundary with the piecewise linear path from the origin to the Cauchy
Packit ea1746
// point and then from there to the Gauss-Newton point (global minimizer
Packit ea1746
// of the model function). The Gauss-Newton point is taken if it lies
Packit ea1746
// within the trust region.
Packit ea1746
void DoglegStrategy::ComputeTraditionalDoglegStep(double* dogleg) {
Packit ea1746
  VectorRef dogleg_step(dogleg, gradient_.rows());
Packit ea1746
Packit ea1746
  // Case 1. The Gauss-Newton step lies inside the trust region, and
Packit ea1746
  // is therefore the optimal solution to the trust-region problem.
Packit ea1746
  const double gradient_norm = gradient_.norm();
Packit ea1746
  const double gauss_newton_norm = gauss_newton_step_.norm();
Packit ea1746
  if (gauss_newton_norm <= radius_) {
Packit ea1746
    dogleg_step = gauss_newton_step_;
Packit ea1746
    dogleg_step_norm_ = gauss_newton_norm;
Packit ea1746
    dogleg_step.array() /= diagonal_.array();
Packit ea1746
    VLOG(3) << "GaussNewton step size: " << dogleg_step_norm_
Packit ea1746
            << " radius: " << radius_;
Packit ea1746
    return;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Case 2. The Cauchy point and the Gauss-Newton steps lie outside
Packit ea1746
  // the trust region. Rescale the Cauchy point to the trust region
Packit ea1746
  // and return.
Packit ea1746
  if  (gradient_norm * alpha_ >= radius_) {
Packit ea1746
    dogleg_step = -(radius_ / gradient_norm) * gradient_;
Packit ea1746
    dogleg_step_norm_ = radius_;
Packit ea1746
    dogleg_step.array() /= diagonal_.array();
Packit ea1746
    VLOG(3) << "Cauchy step size: " << dogleg_step_norm_
Packit ea1746
            << " radius: " << radius_;
Packit ea1746
    return;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Case 3. The Cauchy point is inside the trust region and the
Packit ea1746
  // Gauss-Newton step is outside. Compute the line joining the two
Packit ea1746
  // points and the point on it which intersects the trust region
Packit ea1746
  // boundary.
Packit ea1746
Packit ea1746
  // a = alpha * -gradient
Packit ea1746
  // b = gauss_newton_step
Packit ea1746
  const double b_dot_a = -alpha_ * gradient_.dot(gauss_newton_step_);
Packit ea1746
  const double a_squared_norm = pow(alpha_ * gradient_norm, 2.0);
Packit ea1746
  const double b_minus_a_squared_norm =
Packit ea1746
      a_squared_norm - 2 * b_dot_a + pow(gauss_newton_norm, 2);
Packit ea1746
Packit ea1746
  // c = a' (b - a)
Packit ea1746
  //   = alpha * -gradient' gauss_newton_step - alpha^2 |gradient|^2
Packit ea1746
  const double c = b_dot_a - a_squared_norm;
Packit ea1746
  const double d = sqrt(c * c + b_minus_a_squared_norm *
Packit ea1746
                        (pow(radius_, 2.0) - a_squared_norm));
Packit ea1746
Packit ea1746
  double beta =
Packit ea1746
      (c <= 0)
Packit ea1746
      ? (d - c) /  b_minus_a_squared_norm
Packit ea1746
      : (radius_ * radius_ - a_squared_norm) / (d + c);
Packit ea1746
  dogleg_step = (-alpha_ * (1.0 - beta)) * gradient_
Packit ea1746
      + beta * gauss_newton_step_;
Packit ea1746
  dogleg_step_norm_ = dogleg_step.norm();
Packit ea1746
  dogleg_step.array() /= diagonal_.array();
Packit ea1746
  VLOG(3) << "Dogleg step size: " << dogleg_step_norm_
Packit ea1746
          << " radius: " << radius_;
Packit ea1746
}
Packit ea1746
Packit ea1746
// The subspace method finds the minimum of the two-dimensional problem
Packit ea1746
//
Packit ea1746
//   min. 1/2 x' B' H B x + g' B x
Packit ea1746
//   s.t. || B x ||^2 <= r^2
Packit ea1746
//
Packit ea1746
// where r is the trust region radius and B is the matrix with unit columns
Packit ea1746
// spanning the subspace defined by the steepest descent and Newton direction.
Packit ea1746
// This subspace by definition includes the Gauss-Newton point, which is
Packit ea1746
// therefore taken if it lies within the trust region.
Packit ea1746
void DoglegStrategy::ComputeSubspaceDoglegStep(double* dogleg) {
Packit ea1746
  VectorRef dogleg_step(dogleg, gradient_.rows());
Packit ea1746
Packit ea1746
  // The Gauss-Newton point is inside the trust region if |GN| <= radius_.
Packit ea1746
  // This test is valid even though radius_ is a length in the two-dimensional
Packit ea1746
  // subspace while gauss_newton_step_ is expressed in the (scaled)
Packit ea1746
  // higher dimensional original space. This is because
Packit ea1746
  //
Packit ea1746
  //   1. gauss_newton_step_ by definition lies in the subspace, and
Packit ea1746
  //   2. the subspace basis is orthonormal.
Packit ea1746
  //
Packit ea1746
  // As a consequence, the norm of the gauss_newton_step_ in the subspace is
Packit ea1746
  // the same as its norm in the original space.
Packit ea1746
  const double gauss_newton_norm = gauss_newton_step_.norm();
Packit ea1746
  if (gauss_newton_norm <= radius_) {
Packit ea1746
    dogleg_step = gauss_newton_step_;
Packit ea1746
    dogleg_step_norm_ = gauss_newton_norm;
Packit ea1746
    dogleg_step.array() /= diagonal_.array();
Packit ea1746
    VLOG(3) << "GaussNewton step size: " << dogleg_step_norm_
Packit ea1746
            << " radius: " << radius_;
Packit ea1746
    return;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // The optimum lies on the boundary of the trust region. The above problem
Packit ea1746
  // therefore becomes
Packit ea1746
  //
Packit ea1746
  //   min. 1/2 x^T B^T H B x + g^T B x
Packit ea1746
  //   s.t. || B x ||^2 = r^2
Packit ea1746
  //
Packit ea1746
  // Notice the equality in the constraint.
Packit ea1746
  //
Packit ea1746
  // This can be solved by forming the Lagrangian, solving for x(y), where
Packit ea1746
  // y is the Lagrange multiplier, using the gradient of the objective, and
Packit ea1746
  // putting x(y) back into the constraint. This results in a fourth order
Packit ea1746
  // polynomial in y, which can be solved using e.g. the companion matrix.
Packit ea1746
  // See the description of MakePolynomialForBoundaryConstrainedProblem for
Packit ea1746
  // details. The result is up to four real roots y*, not all of which
Packit ea1746
  // correspond to feasible points. The feasible points x(y*) have to be
Packit ea1746
  // tested for optimality.
Packit ea1746
Packit ea1746
  if (subspace_is_one_dimensional_) {
Packit ea1746
    // The subspace is one-dimensional, so both the gradient and
Packit ea1746
    // the Gauss-Newton step point towards the same direction.
Packit ea1746
    // In this case, we move along the gradient until we reach the trust
Packit ea1746
    // region boundary.
Packit ea1746
    dogleg_step = -(radius_ / gradient_.norm()) * gradient_;
Packit ea1746
    dogleg_step_norm_ = radius_;
Packit ea1746
    dogleg_step.array() /= diagonal_.array();
Packit ea1746
    VLOG(3) << "Dogleg subspace step size (1D): " << dogleg_step_norm_
Packit ea1746
            << " radius: " << radius_;
Packit ea1746
    return;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  Vector2d minimum(0.0, 0.0);
Packit ea1746
  if (!FindMinimumOnTrustRegionBoundary(&minimum)) {
Packit ea1746
    // For the positive semi-definite case, a traditional dogleg step
Packit ea1746
    // is taken in this case.
Packit ea1746
    LOG(WARNING) << "Failed to compute polynomial roots. "
Packit ea1746
                 << "Taking traditional dogleg step instead.";
Packit ea1746
    ComputeTraditionalDoglegStep(dogleg);
Packit ea1746
    return;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Test first order optimality at the minimum.
Packit ea1746
  // The first order KKT conditions state that the minimum x*
Packit ea1746
  // has to satisfy either || x* ||^2 < r^2 (i.e. has to lie within
Packit ea1746
  // the trust region), or
Packit ea1746
  //
Packit ea1746
  //   (B x* + g) + y x* = 0
Packit ea1746
  //
Packit ea1746
  // for some positive scalar y.
Packit ea1746
  // Here, as it is already known that the minimum lies on the boundary, the
Packit ea1746
  // latter condition is tested. To allow for small imprecisions, we test if
Packit ea1746
  // the angle between (B x* + g) and -x* is smaller than acos(0.99).
Packit ea1746
  // The exact value of the cosine is arbitrary but should be close to 1.
Packit ea1746
  //
Packit ea1746
  // This condition should not be violated. If it is, the minimum was not
Packit ea1746
  // correctly determined.
Packit ea1746
  const double kCosineThreshold = 0.99;
Packit ea1746
  const Vector2d grad_minimum = subspace_B_ * minimum + subspace_g_;
Packit ea1746
  const double cosine_angle = -minimum.dot(grad_minimum) /
Packit ea1746
      (minimum.norm() * grad_minimum.norm());
Packit ea1746
  if (cosine_angle < kCosineThreshold) {
Packit ea1746
    LOG(WARNING) << "First order optimality seems to be violated "
Packit ea1746
                 << "in the subspace method!\n"
Packit ea1746
                 << "Cosine of angle between x and B x + g is "
Packit ea1746
                 << cosine_angle << ".\n"
Packit ea1746
                 << "Taking a regular dogleg step instead.\n"
Packit ea1746
                 << "Please consider filing a bug report if this "
Packit ea1746
                 << "happens frequently or consistently.\n";
Packit ea1746
    ComputeTraditionalDoglegStep(dogleg);
Packit ea1746
    return;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Create the full step from the optimal 2d solution.
Packit ea1746
  dogleg_step = subspace_basis_ * minimum;
Packit ea1746
  dogleg_step_norm_ = radius_;
Packit ea1746
  dogleg_step.array() /= diagonal_.array();
Packit ea1746
  VLOG(3) << "Dogleg subspace step size: " << dogleg_step_norm_
Packit ea1746
          << " radius: " << radius_;
Packit ea1746
}
Packit ea1746
Packit ea1746
// Build the polynomial that defines the optimal Lagrange multipliers.
Packit ea1746
// Let the Lagrangian be
Packit ea1746
//
Packit ea1746
//   L(x, y) = 0.5 x^T B x + x^T g + y (0.5 x^T x - 0.5 r^2).       (1)
Packit ea1746
//
Packit ea1746
// Stationary points of the Lagrangian are given by
Packit ea1746
//
Packit ea1746
//   0 = d L(x, y) / dx = Bx + g + y x                              (2)
Packit ea1746
//   0 = d L(x, y) / dy = 0.5 x^T x - 0.5 r^2                       (3)
Packit ea1746
//
Packit ea1746
// For any given y, we can solve (2) for x as
Packit ea1746
//
Packit ea1746
//   x(y) = -(B + y I)^-1 g .                                       (4)
Packit ea1746
//
Packit ea1746
// As B + y I is 2x2, we form the inverse explicitly:
Packit ea1746
//
Packit ea1746
//   (B + y I)^-1 = (1 / det(B + y I)) adj(B + y I)                 (5)
Packit ea1746
//
Packit ea1746
// where adj() denotes adjugation. This should be safe, as B is positive
Packit ea1746
// semi-definite and y is necessarily positive, so (B + y I) is indeed
Packit ea1746
// invertible.
Packit ea1746
// Plugging (5) into (4) and the result into (3), then dividing by 0.5 we
Packit ea1746
// obtain
Packit ea1746
//
Packit ea1746
//   0 = (1 / det(B + y I))^2 g^T adj(B + y I)^T adj(B + y I) g - r^2
Packit ea1746
//                                                                  (6)
Packit ea1746
//
Packit ea1746
// or
Packit ea1746
//
Packit ea1746
//   det(B + y I)^2 r^2 = g^T adj(B + y I)^T adj(B + y I) g         (7a)
Packit ea1746
//                      = g^T adj(B)^T adj(B) g
Packit ea1746
//                           + 2 y g^T adj(B)^T g + y^2 g^T g       (7b)
Packit ea1746
//
Packit ea1746
// as
Packit ea1746
//
Packit ea1746
//   adj(B + y I) = adj(B) + y I = adj(B)^T + y I .                 (8)
Packit ea1746
//
Packit ea1746
// The left hand side can be expressed explicitly using
Packit ea1746
//
Packit ea1746
//   det(B + y I) = det(B) + y tr(B) + y^2 .                        (9)
Packit ea1746
//
Packit ea1746
// So (7) is a polynomial in y of degree four.
Packit ea1746
// Bringing everything back to the left hand side, the coefficients can
Packit ea1746
// be read off as
Packit ea1746
//
Packit ea1746
//     y^4  r^2
Packit ea1746
//   + y^3  2 r^2 tr(B)
Packit ea1746
//   + y^2 (r^2 tr(B)^2 + 2 r^2 det(B) - g^T g)
Packit ea1746
//   + y^1 (2 r^2 det(B) tr(B) - 2 g^T adj(B)^T g)
Packit ea1746
//   + y^0 (r^2 det(B)^2 - g^T adj(B)^T adj(B) g)
Packit ea1746
//
Packit ea1746
Vector DoglegStrategy::MakePolynomialForBoundaryConstrainedProblem() const {
Packit ea1746
  const double detB = subspace_B_.determinant();
Packit ea1746
  const double trB = subspace_B_.trace();
Packit ea1746
  const double r2 = radius_ * radius_;
Packit ea1746
  Matrix2d B_adj;
Packit ea1746
  B_adj <<  subspace_B_(1, 1) , -subspace_B_(0, 1),
Packit ea1746
            -subspace_B_(1, 0) ,  subspace_B_(0, 0);
Packit ea1746
Packit ea1746
  Vector polynomial(5);
Packit ea1746
  polynomial(0) = r2;
Packit ea1746
  polynomial(1) = 2.0 * r2 * trB;
Packit ea1746
  polynomial(2) = r2 * (trB * trB + 2.0 * detB) - subspace_g_.squaredNorm();
Packit ea1746
  polynomial(3) = -2.0 * (subspace_g_.transpose() * B_adj * subspace_g_
Packit ea1746
      - r2 * detB * trB);
Packit ea1746
  polynomial(4) = r2 * detB * detB - (B_adj * subspace_g_).squaredNorm();
Packit ea1746
Packit ea1746
  return polynomial;
Packit ea1746
}
Packit ea1746
Packit ea1746
// Given a Lagrange multiplier y that corresponds to a stationary point
Packit ea1746
// of the Lagrangian L(x, y), compute the corresponding x from the
Packit ea1746
// equation
Packit ea1746
//
Packit ea1746
//   0 = d L(x, y) / dx
Packit ea1746
//     = B * x + g + y * x
Packit ea1746
//     = (B + y * I) * x + g
Packit ea1746
//
Packit ea1746
DoglegStrategy::Vector2d DoglegStrategy::ComputeSubspaceStepFromRoot(
Packit ea1746
    double y) const {
Packit ea1746
  const Matrix2d B_i = subspace_B_ + y * Matrix2d::Identity();
Packit ea1746
  return -B_i.partialPivLu().solve(subspace_g_);
Packit ea1746
}
Packit ea1746
Packit ea1746
// This function evaluates the quadratic model at a point x in the
Packit ea1746
// subspace spanned by subspace_basis_.
Packit ea1746
double DoglegStrategy::EvaluateSubspaceModel(const Vector2d& x) const {
Packit ea1746
  return 0.5 * x.dot(subspace_B_ * x) + subspace_g_.dot(x);
Packit ea1746
}
Packit ea1746
Packit ea1746
// This function attempts to solve the boundary-constrained subspace problem
Packit ea1746
//
Packit ea1746
//   min. 1/2 x^T B^T H B x + g^T B x
Packit ea1746
//   s.t. || B x ||^2 = r^2
Packit ea1746
//
Packit ea1746
// where B is an orthonormal subspace basis and r is the trust-region radius.
Packit ea1746
//
Packit ea1746
// This is done by finding the roots of a fourth degree polynomial. If the
Packit ea1746
// root finding fails, the function returns false and minimum will be set
Packit ea1746
// to (0, 0). If it succeeds, true is returned.
Packit ea1746
//
Packit ea1746
// In the failure case, another step should be taken, such as the traditional
Packit ea1746
// dogleg step.
Packit ea1746
bool DoglegStrategy::FindMinimumOnTrustRegionBoundary(Vector2d* minimum) const {
Packit ea1746
  CHECK_NOTNULL(minimum);
Packit ea1746
Packit ea1746
  // Return (0, 0) in all error cases.
Packit ea1746
  minimum->setZero();
Packit ea1746
Packit ea1746
  // Create the fourth-degree polynomial that is a necessary condition for
Packit ea1746
  // optimality.
Packit ea1746
  const Vector polynomial = MakePolynomialForBoundaryConstrainedProblem();
Packit ea1746
Packit ea1746
  // Find the real parts y_i of its roots (not only the real roots).
Packit ea1746
  Vector roots_real;
Packit ea1746
  if (!FindPolynomialRoots(polynomial, &roots_real, NULL)) {
Packit ea1746
    // Failed to find the roots of the polynomial, i.e. the candidate
Packit ea1746
    // solutions of the constrained problem. Report this back to the caller.
Packit ea1746
    return false;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // For each root y, compute B x(y) and check for feasibility.
Packit ea1746
  // Notice that there should always be four roots, as the leading term of
Packit ea1746
  // the polynomial is r^2 and therefore non-zero. However, as some roots
Packit ea1746
  // may be complex, the real parts are not necessarily unique.
Packit ea1746
  double minimum_value = std::numeric_limits<double>::max();
Packit ea1746
  bool valid_root_found = false;
Packit ea1746
  for (int i = 0; i < roots_real.size(); ++i) {
Packit ea1746
    const Vector2d x_i = ComputeSubspaceStepFromRoot(roots_real(i));
Packit ea1746
Packit ea1746
    // Not all roots correspond to points on the trust region boundary.
Packit ea1746
    // There are at most four candidate solutions. As we are interested
Packit ea1746
    // in the minimum, it is safe to consider all of them after projecting
Packit ea1746
    // them onto the trust region boundary.
Packit ea1746
    if (x_i.norm() > 0) {
Packit ea1746
      const double f_i = EvaluateSubspaceModel((radius_ / x_i.norm()) * x_i);
Packit ea1746
      valid_root_found = true;
Packit ea1746
      if (f_i < minimum_value) {
Packit ea1746
        minimum_value = f_i;
Packit ea1746
        *minimum = x_i;
Packit ea1746
      }
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  return valid_root_found;
Packit ea1746
}
Packit ea1746
Packit ea1746
LinearSolver::Summary DoglegStrategy::ComputeGaussNewtonStep(
Packit ea1746
    const PerSolveOptions& per_solve_options,
Packit ea1746
    SparseMatrix* jacobian,
Packit ea1746
    const double* residuals) {
Packit ea1746
  const int n = jacobian->num_cols();
Packit ea1746
  LinearSolver::Summary linear_solver_summary;
Packit ea1746
  linear_solver_summary.termination_type = LINEAR_SOLVER_FAILURE;
Packit ea1746
Packit ea1746
  // The Jacobian matrix is often quite poorly conditioned. Thus it is
Packit ea1746
  // necessary to add a diagonal matrix at the bottom to prevent the
Packit ea1746
  // linear solver from failing.
Packit ea1746
  //
Packit ea1746
  // We do this by computing the same diagonal matrix as the one used
Packit ea1746
  // by Levenberg-Marquardt (other choices are possible), and scaling
Packit ea1746
  // it by a small constant (independent of the trust region radius).
Packit ea1746
  //
Packit ea1746
  // If the solve fails, the multiplier to the diagonal is increased
Packit ea1746
  // up to max_mu_ by a factor of mu_increase_factor_ every time. If
Packit ea1746
  // the linear solver is still not successful, the strategy returns
Packit ea1746
  // with LINEAR_SOLVER_FAILURE.
Packit ea1746
  //
Packit ea1746
  // Next time when a new Gauss-Newton step is requested, the
Packit ea1746
  // multiplier starts out from the last successful solve.
Packit ea1746
  //
Packit ea1746
  // When a step is declared successful, the multiplier is decreased
Packit ea1746
  // by half of mu_increase_factor_.
Packit ea1746
Packit ea1746
  while (mu_ < max_mu_) {
Packit ea1746
    // Dogleg, as far as I (sameeragarwal) understand it, requires a
Packit ea1746
    // reasonably good estimate of the Gauss-Newton step. This means
Packit ea1746
    // that we need to solve the normal equations more or less
Packit ea1746
    // exactly. This is reflected in the values of the tolerances set
Packit ea1746
    // below.
Packit ea1746
    //
Packit ea1746
    // For now, this strategy should only be used with exact
Packit ea1746
    // factorization based solvers, for which these tolerances are
Packit ea1746
    // automatically satisfied.
Packit ea1746
    //
Packit ea1746
    // The right way to combine inexact solves with trust region
Packit ea1746
    // methods is to use Stiehaug's method.
Packit ea1746
    LinearSolver::PerSolveOptions solve_options;
Packit ea1746
    solve_options.q_tolerance = 0.0;
Packit ea1746
    solve_options.r_tolerance = 0.0;
Packit ea1746
Packit ea1746
    lm_diagonal_ = diagonal_ * std::sqrt(mu_);
Packit ea1746
    solve_options.D = lm_diagonal_.data();
Packit ea1746
Packit ea1746
    // As in the LevenbergMarquardtStrategy, solve Jy = r instead
Packit ea1746
    // of Jx = -r and later set x = -y to avoid having to modify
Packit ea1746
    // either jacobian or residuals.
Packit ea1746
    InvalidateArray(n, gauss_newton_step_.data());
Packit ea1746
    linear_solver_summary = linear_solver_->Solve(jacobian,
Packit ea1746
                                                  residuals,
Packit ea1746
                                                  solve_options,
Packit ea1746
                                                  gauss_newton_step_.data());
Packit ea1746
Packit ea1746
    if (per_solve_options.dump_format_type == CONSOLE ||
Packit ea1746
        (per_solve_options.dump_format_type != CONSOLE &&
Packit ea1746
         !per_solve_options.dump_filename_base.empty())) {
Packit ea1746
      if (!DumpLinearLeastSquaresProblem(per_solve_options.dump_filename_base,
Packit ea1746
                                         per_solve_options.dump_format_type,
Packit ea1746
                                         jacobian,
Packit ea1746
                                         solve_options.D,
Packit ea1746
                                         residuals,
Packit ea1746
                                         gauss_newton_step_.data(),
Packit ea1746
                                         0)) {
Packit ea1746
        LOG(ERROR) << "Unable to dump trust region problem."
Packit ea1746
                   << " Filename base: "
Packit ea1746
                   << per_solve_options.dump_filename_base;
Packit ea1746
      }
Packit ea1746
    }
Packit ea1746
Packit ea1746
    if (linear_solver_summary.termination_type == LINEAR_SOLVER_FATAL_ERROR) {
Packit ea1746
      return linear_solver_summary;
Packit ea1746
    }
Packit ea1746
Packit ea1746
    if (linear_solver_summary.termination_type == LINEAR_SOLVER_FAILURE ||
Packit ea1746
        !IsArrayValid(n, gauss_newton_step_.data())) {
Packit ea1746
      mu_ *= mu_increase_factor_;
Packit ea1746
      VLOG(2) << "Increasing mu " << mu_;
Packit ea1746
      linear_solver_summary.termination_type = LINEAR_SOLVER_FAILURE;
Packit ea1746
      continue;
Packit ea1746
    }
Packit ea1746
    break;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  if (linear_solver_summary.termination_type != LINEAR_SOLVER_FAILURE) {
Packit ea1746
    // The scaled Gauss-Newton step is D * GN:
Packit ea1746
    //
Packit ea1746
    //     - (D^-1 J^T J D^-1)^-1 (D^-1 g)
Packit ea1746
    //   = - D (J^T J)^-1 D D^-1 g
Packit ea1746
    //   = D -(J^T J)^-1 g
Packit ea1746
    //
Packit ea1746
    gauss_newton_step_.array() *= -diagonal_.array();
Packit ea1746
  }
Packit ea1746
Packit ea1746
  return linear_solver_summary;
Packit ea1746
}
Packit ea1746
Packit ea1746
void DoglegStrategy::StepAccepted(double step_quality) {
Packit ea1746
  CHECK_GT(step_quality, 0.0);
Packit ea1746
Packit ea1746
  if (step_quality < decrease_threshold_) {
Packit ea1746
    radius_ *= 0.5;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  if (step_quality > increase_threshold_) {
Packit ea1746
    radius_ = std::max(radius_, 3.0 * dogleg_step_norm_);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Reduce the regularization multiplier, in the hope that whatever
Packit ea1746
  // was causing the rank deficiency has gone away and we can return
Packit ea1746
  // to doing a pure Gauss-Newton solve.
Packit ea1746
  mu_ = std::max(min_mu_, 2.0 * mu_ / mu_increase_factor_);
Packit ea1746
  reuse_ = false;
Packit ea1746
}
Packit ea1746
Packit ea1746
void DoglegStrategy::StepRejected(double step_quality) {
Packit ea1746
  radius_ *= 0.5;
Packit ea1746
  reuse_ = true;
Packit ea1746
}
Packit ea1746
Packit ea1746
void DoglegStrategy::StepIsInvalid() {
Packit ea1746
  mu_ *= mu_increase_factor_;
Packit ea1746
  reuse_ = false;
Packit ea1746
}
Packit ea1746
Packit ea1746
double DoglegStrategy::Radius() const {
Packit ea1746
  return radius_;
Packit ea1746
}
Packit ea1746
Packit ea1746
bool DoglegStrategy::ComputeSubspaceModel(SparseMatrix* jacobian) {
Packit ea1746
  // Compute an orthogonal basis for the subspace using QR decomposition.
Packit ea1746
  Matrix basis_vectors(jacobian->num_cols(), 2);
Packit ea1746
  basis_vectors.col(0) = gradient_;
Packit ea1746
  basis_vectors.col(1) = gauss_newton_step_;
Packit ea1746
  Eigen::ColPivHouseholderQR<Matrix> basis_qr(basis_vectors);
Packit ea1746
Packit ea1746
  switch (basis_qr.rank()) {
Packit ea1746
    case 0:
Packit ea1746
      // This should never happen, as it implies that both the gradient
Packit ea1746
      // and the Gauss-Newton step are zero. In this case, the minimizer should
Packit ea1746
      // have stopped due to the gradient being too small.
Packit ea1746
      LOG(ERROR) << "Rank of subspace basis is 0. "
Packit ea1746
                 << "This means that the gradient at the current iterate is "
Packit ea1746
                 << "zero but the optimization has not been terminated. "
Packit ea1746
                 << "You may have found a bug in Ceres.";
Packit ea1746
      return false;
Packit ea1746
Packit ea1746
    case 1:
Packit ea1746
      // Gradient and Gauss-Newton step coincide, so we lie on one of the
Packit ea1746
      // major axes of the quadratic problem. In this case, we simply move
Packit ea1746
      // along the gradient until we reach the trust region boundary.
Packit ea1746
      subspace_is_one_dimensional_ = true;
Packit ea1746
      return true;
Packit ea1746
Packit ea1746
    case 2:
Packit ea1746
      subspace_is_one_dimensional_ = false;
Packit ea1746
      break;
Packit ea1746
Packit ea1746
    default:
Packit ea1746
      LOG(ERROR) << "Rank of the subspace basis matrix is reported to be "
Packit ea1746
                 << "greater than 2. As the matrix contains only two "
Packit ea1746
                 << "columns this cannot be true and is indicative of "
Packit ea1746
                 << "a bug.";
Packit ea1746
      return false;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // The subspace is two-dimensional, so compute the subspace model.
Packit ea1746
  // Given the basis U, this is
Packit ea1746
  //
Packit ea1746
  //   subspace_g_ = g_scaled^T U
Packit ea1746
  //
Packit ea1746
  // and
Packit ea1746
  //
Packit ea1746
  //   subspace_B_ = U^T (J_scaled^T J_scaled) U
Packit ea1746
  //
Packit ea1746
  // As J_scaled = J * D^-1, the latter becomes
Packit ea1746
  //
Packit ea1746
  //   subspace_B_ = ((U^T D^-1) J^T) (J (D^-1 U))
Packit ea1746
  //               = (J (D^-1 U))^T (J (D^-1 U))
Packit ea1746
Packit ea1746
  subspace_basis_ =
Packit ea1746
      basis_qr.householderQ() * Matrix::Identity(jacobian->num_cols(), 2);
Packit ea1746
Packit ea1746
  subspace_g_ = subspace_basis_.transpose() * gradient_;
Packit ea1746
Packit ea1746
  Eigen::Matrix<double, 2, Eigen::Dynamic, Eigen::RowMajor>
Packit ea1746
      Jb(2, jacobian->num_rows());
Packit ea1746
  Jb.setZero();
Packit ea1746
Packit ea1746
  Vector tmp;
Packit ea1746
  tmp = (subspace_basis_.col(0).array() / diagonal_.array()).matrix();
Packit ea1746
  jacobian->RightMultiply(tmp.data(), Jb.row(0).data());
Packit ea1746
  tmp = (subspace_basis_.col(1).array() / diagonal_.array()).matrix();
Packit ea1746
  jacobian->RightMultiply(tmp.data(), Jb.row(1).data());
Packit ea1746
Packit ea1746
  subspace_B_ = Jb * Jb.transpose();
Packit ea1746
Packit ea1746
  return true;
Packit ea1746
}
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres