Blame internal/ceres/trust_region_minimizer.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2016 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_TRUST_REGION_MINIMIZER_H_
Packit ea1746
#define CERES_INTERNAL_TRUST_REGION_MINIMIZER_H_
Packit ea1746
Packit ea1746
#include "ceres/internal/eigen.h"
Packit ea1746
#include "ceres/internal/scoped_ptr.h"
Packit ea1746
#include "ceres/minimizer.h"
Packit ea1746
#include "ceres/solver.h"
Packit ea1746
#include "ceres/sparse_matrix.h"
Packit ea1746
#include "ceres/trust_region_step_evaluator.h"
Packit ea1746
#include "ceres/trust_region_strategy.h"
Packit ea1746
#include "ceres/types.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
// Generic trust region minimization algorithm.
Packit ea1746
//
Packit ea1746
// For example usage, see SolverImpl::Minimize.
Packit ea1746
class TrustRegionMinimizer : public Minimizer {
Packit ea1746
 public:
Packit ea1746
  ~TrustRegionMinimizer();
Packit ea1746
Packit ea1746
  // This method is not thread safe.
Packit ea1746
  virtual void Minimize(const Minimizer::Options& options,
Packit ea1746
                        double* parameters,
Packit ea1746
                        Solver::Summary* solver_summary);
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  void Init(const Minimizer::Options& options,
Packit ea1746
            double* parameters,
Packit ea1746
            Solver::Summary* solver_summary);
Packit ea1746
  bool IterationZero();
Packit ea1746
  bool FinalizeIterationAndCheckIfMinimizerCanContinue();
Packit ea1746
  bool ComputeTrustRegionStep();
Packit ea1746
Packit ea1746
  bool EvaluateGradientAndJacobian();
Packit ea1746
  void ComputeCandidatePointAndEvaluateCost();
Packit ea1746
Packit ea1746
  void DoLineSearch(const Vector& x,
Packit ea1746
                    const Vector& gradient,
Packit ea1746
                    const double cost,
Packit ea1746
                    Vector* delta);
Packit ea1746
  void DoInnerIterationsIfNeeded();
Packit ea1746
Packit ea1746
  bool ParameterToleranceReached();
Packit ea1746
  bool FunctionToleranceReached();
Packit ea1746
  bool GradientToleranceReached();
Packit ea1746
  bool MaxSolverTimeReached();
Packit ea1746
  bool MaxSolverIterationsReached();
Packit ea1746
  bool MinTrustRegionRadiusReached();
Packit ea1746
Packit ea1746
  bool IsStepSuccessful();
Packit ea1746
  void HandleUnsuccessfulStep();
Packit ea1746
  bool HandleSuccessfulStep();
Packit ea1746
  bool HandleInvalidStep();
Packit ea1746
Packit ea1746
  Minimizer::Options options_;
Packit ea1746
Packit ea1746
  // These pointers are shortcuts to objects passed to the
Packit ea1746
  // TrustRegionMinimizer. The TrustRegionMinimizer does not own them.
Packit ea1746
  double* parameters_;
Packit ea1746
  Solver::Summary* solver_summary_;
Packit ea1746
  Evaluator* evaluator_;
Packit ea1746
  SparseMatrix* jacobian_;
Packit ea1746
  TrustRegionStrategy* strategy_;
Packit ea1746
Packit ea1746
  scoped_ptr<TrustRegionStepEvaluator> step_evaluator_;
Packit ea1746
Packit ea1746
  bool is_not_silent_;
Packit ea1746
  bool inner_iterations_are_enabled_;
Packit ea1746
  bool inner_iterations_were_useful_;
Packit ea1746
Packit ea1746
  // Summary of the current iteration.
Packit ea1746
  IterationSummary iteration_summary_;
Packit ea1746
Packit ea1746
  // Dimensionality of the problem in the ambient space.
Packit ea1746
  int num_parameters_;
Packit ea1746
  // Dimensionality of the problem in the tangent space. This is the
Packit ea1746
  // number of columns in the Jacobian.
Packit ea1746
  int num_effective_parameters_;
Packit ea1746
  // Length of the residual vector, also the number of rows in the Jacobian.
Packit ea1746
  int num_residuals_;
Packit ea1746
Packit ea1746
  // Current point.
Packit ea1746
  Vector x_;
Packit ea1746
  // Residuals at x_;
Packit ea1746
  Vector residuals_;
Packit ea1746
  // Gradient at x_.
Packit ea1746
  Vector gradient_;
Packit ea1746
  // Solution computed by the inner iterations.
Packit ea1746
  Vector inner_iteration_x_;
Packit ea1746
  // model_residuals = J * trust_region_step
Packit ea1746
  Vector model_residuals_;
Packit ea1746
  Vector negative_gradient_;
Packit ea1746
  // projected_gradient_step = Plus(x, -gradient), an intermediate
Packit ea1746
  // quantity used to compute the projected gradient norm.
Packit ea1746
  Vector projected_gradient_step_;
Packit ea1746
  // The step computed by the trust region strategy. If Jacobi scaling
Packit ea1746
  // is enabled, this is a vector in the scaled space.
Packit ea1746
  Vector trust_region_step_;
Packit ea1746
  // The current proposal for how far the trust region algorithm
Packit ea1746
  // thinks we should move. In the most basic case, it is just the
Packit ea1746
  // trust_region_step_ with the Jacobi scaling undone. If bounds
Packit ea1746
  // constraints are present, then it is the result of the projected
Packit ea1746
  // line search.
Packit ea1746
  Vector delta_;
Packit ea1746
  // candidate_x  = Plus(x, delta)
Packit ea1746
  Vector candidate_x_;
Packit ea1746
  // Scaling vector to scale the columns of the Jacobian.
Packit ea1746
  Vector jacobian_scaling_;
Packit ea1746
Packit ea1746
  // Euclidean norm of x_.
Packit ea1746
  double x_norm_;
Packit ea1746
  // Cost at x_.
Packit ea1746
  double x_cost_;
Packit ea1746
  // Minimum cost encountered up till now.
Packit ea1746
  double minimum_cost_;
Packit ea1746
  // How much did the trust region strategy reduce the cost of the
Packit ea1746
  // linearized Gauss-Newton model.
Packit ea1746
  double model_cost_change_;
Packit ea1746
  // Cost at candidate_x_.
Packit ea1746
  double candidate_cost_;
Packit ea1746
Packit ea1746
  // Time at which the minimizer was started.
Packit ea1746
  double start_time_in_secs_;
Packit ea1746
  // Time at which the current iteration was started.
Packit ea1746
  double iteration_start_time_in_secs_;
Packit ea1746
  // Number of consecutive steps where the minimizer loop computed a
Packit ea1746
  // numerically invalid step.
Packit ea1746
  int num_consecutive_invalid_steps_;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_TRUST_REGION_MINIMIZER_H_