Blame internal/ceres/dogleg_strategy.h

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
#ifndef CERES_INTERNAL_DOGLEG_STRATEGY_H_
Packit ea1746
#define CERES_INTERNAL_DOGLEG_STRATEGY_H_
Packit ea1746
Packit ea1746
#include "ceres/linear_solver.h"
Packit ea1746
#include "ceres/trust_region_strategy.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
// Dogleg step computation and trust region sizing strategy based on
Packit ea1746
// on "Methods for Nonlinear Least Squares" by K. Madsen, H.B. Nielsen
Packit ea1746
// and O. Tingleff. Available to download from
Packit ea1746
//
Packit ea1746
// http://www2.imm.dtu.dk/pubdb/views/edoc_download.php/3215/pdf/imm3215.pdf
Packit ea1746
//
Packit ea1746
// One minor modification is that instead of computing the pure
Packit ea1746
// Gauss-Newton step, we compute a regularized version of it. This is
Packit ea1746
// because the Jacobian is often rank-deficient and in such cases
Packit ea1746
// using a direct solver leads to numerical failure.
Packit ea1746
//
Packit ea1746
// If SUBSPACE is passed as the type argument to the constructor, the
Packit ea1746
// DoglegStrategy follows the approach by Shultz, Schnabel, Byrd.
Packit ea1746
// This finds the exact optimum over the two-dimensional subspace
Packit ea1746
// spanned by the two Dogleg vectors.
Packit ea1746
class DoglegStrategy : public TrustRegionStrategy {
Packit ea1746
 public:
Packit ea1746
  explicit DoglegStrategy(const TrustRegionStrategy::Options& options);
Packit ea1746
  virtual ~DoglegStrategy() {}
Packit ea1746
Packit ea1746
  // TrustRegionStrategy interface
Packit ea1746
  virtual Summary ComputeStep(const PerSolveOptions& per_solve_options,
Packit ea1746
                              SparseMatrix* jacobian,
Packit ea1746
                              const double* residuals,
Packit ea1746
                              double* step);
Packit ea1746
  virtual void StepAccepted(double step_quality);
Packit ea1746
  virtual void StepRejected(double step_quality);
Packit ea1746
  virtual void StepIsInvalid();
Packit ea1746
Packit ea1746
  virtual double Radius() const;
Packit ea1746
Packit ea1746
  // These functions are predominantly for testing.
Packit ea1746
  Vector gradient() const { return gradient_; }
Packit ea1746
  Vector gauss_newton_step() const { return gauss_newton_step_; }
Packit ea1746
  Matrix subspace_basis() const { return subspace_basis_; }
Packit ea1746
  Vector subspace_g() const { return subspace_g_; }
Packit ea1746
  Matrix subspace_B() const { return subspace_B_; }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  typedef Eigen::Matrix<double, 2, 1, Eigen::DontAlign> Vector2d;
Packit ea1746
  typedef Eigen::Matrix<double, 2, 2, Eigen::DontAlign> Matrix2d;
Packit ea1746
Packit ea1746
  LinearSolver::Summary ComputeGaussNewtonStep(
Packit ea1746
      const PerSolveOptions& per_solve_options,
Packit ea1746
      SparseMatrix* jacobian,
Packit ea1746
      const double* residuals);
Packit ea1746
  void ComputeCauchyPoint(SparseMatrix* jacobian);
Packit ea1746
  void ComputeGradient(SparseMatrix* jacobian, const double* residuals);
Packit ea1746
  void ComputeTraditionalDoglegStep(double* step);
Packit ea1746
  bool ComputeSubspaceModel(SparseMatrix* jacobian);
Packit ea1746
  void ComputeSubspaceDoglegStep(double* step);
Packit ea1746
Packit ea1746
  bool FindMinimumOnTrustRegionBoundary(Vector2d* minimum) const;
Packit ea1746
  Vector MakePolynomialForBoundaryConstrainedProblem() const;
Packit ea1746
  Vector2d ComputeSubspaceStepFromRoot(double lambda) const;
Packit ea1746
  double EvaluateSubspaceModel(const Vector2d& x) const;
Packit ea1746
Packit ea1746
  LinearSolver* linear_solver_;
Packit ea1746
  double radius_;
Packit ea1746
  const double max_radius_;
Packit ea1746
Packit ea1746
  const double min_diagonal_;
Packit ea1746
  const double max_diagonal_;
Packit ea1746
Packit ea1746
  // mu is used to scale the diagonal matrix used to make the
Packit ea1746
  // Gauss-Newton solve full rank. In each solve, the strategy starts
Packit ea1746
  // out with mu = min_mu, and tries values upto max_mu. If the user
Packit ea1746
  // reports an invalid step, the value of mu_ is increased so that
Packit ea1746
  // the next solve starts with a stronger regularization.
Packit ea1746
  //
Packit ea1746
  // If a successful step is reported, then the value of mu_ is
Packit ea1746
  // decreased with a lower bound of min_mu_.
Packit ea1746
  double mu_;
Packit ea1746
  const double min_mu_;
Packit ea1746
  const double max_mu_;
Packit ea1746
  const double mu_increase_factor_;
Packit ea1746
  const double increase_threshold_;
Packit ea1746
  const double decrease_threshold_;
Packit ea1746
Packit ea1746
  Vector diagonal_;  // sqrt(diag(J^T J))
Packit ea1746
  Vector lm_diagonal_;
Packit ea1746
Packit ea1746
  Vector gradient_;
Packit ea1746
  Vector gauss_newton_step_;
Packit ea1746
Packit ea1746
  // cauchy_step = alpha * gradient
Packit ea1746
  double alpha_;
Packit ea1746
  double dogleg_step_norm_;
Packit ea1746
Packit ea1746
  // When, ComputeStep is called, reuse_ indicates whether the
Packit ea1746
  // Gauss-Newton and Cauchy steps from the last call to ComputeStep
Packit ea1746
  // can be reused or not.
Packit ea1746
  //
Packit ea1746
  // If the user called StepAccepted, then it is expected that the
Packit ea1746
  // user has recomputed the Jacobian matrix and new Gauss-Newton
Packit ea1746
  // solve is needed and reuse is set to false.
Packit ea1746
  //
Packit ea1746
  // If the user called StepRejected, then it is expected that the
Packit ea1746
  // user wants to solve the trust region problem with the same matrix
Packit ea1746
  // but a different trust region radius and the Gauss-Newton and
Packit ea1746
  // Cauchy steps can be reused to compute the Dogleg, thus reuse is
Packit ea1746
  // set to true.
Packit ea1746
  //
Packit ea1746
  // If the user called StepIsInvalid, then there was a numerical
Packit ea1746
  // problem with the step computed in the last call to ComputeStep,
Packit ea1746
  // and the regularization used to do the Gauss-Newton solve is
Packit ea1746
  // increased and a new solve should be done when ComputeStep is
Packit ea1746
  // called again, thus reuse is set to false.
Packit ea1746
  bool reuse_;
Packit ea1746
Packit ea1746
  // The dogleg type determines how the minimum of the local
Packit ea1746
  // quadratic model is found.
Packit ea1746
  DoglegType dogleg_type_;
Packit ea1746
Packit ea1746
  // If the type is SUBSPACE_DOGLEG, the two-dimensional
Packit ea1746
  // model 1/2 x^T B x + g^T x has to be computed and stored.
Packit ea1746
  bool subspace_is_one_dimensional_;
Packit ea1746
  Matrix subspace_basis_;
Packit ea1746
  Vector2d subspace_g_;
Packit ea1746
  Matrix2d subspace_B_;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_DOGLEG_STRATEGY_H_