Sameer Agarwal | fa01519 | 2012-06-11 14:21:42 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2012 Google Inc. All rights reserved. |
| 3 | // http://code.google.com/p/ceres-solver/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 | // used to endorse or promote products derived from this software without |
| 15 | // specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | // POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Author: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
| 31 | #ifndef CERES_INTERNAL_DOGLEG_STRATEGY_H_ |
| 32 | #define CERES_INTERNAL_DOGLEG_STRATEGY_H_ |
| 33 | |
| 34 | #include "ceres/linear_solver.h" |
| 35 | #include "ceres/trust_region_strategy.h" |
| 36 | |
| 37 | namespace ceres { |
| 38 | namespace internal { |
| 39 | |
| 40 | // Dogleg step computation and trust region sizing strategy based on |
| 41 | // on "Methods for Nonlinear Least Squares" by K. Madsen, H.B. Nielsen |
| 42 | // and O. Tingleff. Available to download from |
| 43 | // |
| 44 | // http://www2.imm.dtu.dk/pubdb/views/edoc_download.php/3215/pdf/imm3215.pdf |
| 45 | // |
| 46 | // One minor modification is that instead of computing the pure |
| 47 | // Gauss-Newton step, we compute a regularized version of it. This is |
| 48 | // because the Jacobian is often rank-deficient and in such cases |
| 49 | // using a direct solver leads to numerical failure. |
| 50 | class DoglegStrategy : public TrustRegionStrategy { |
| 51 | public: |
| 52 | DoglegStrategy(const TrustRegionStrategy::Options& options); |
| 53 | virtual ~DoglegStrategy() {} |
| 54 | |
| 55 | // TrustRegionStrategy interface |
| 56 | virtual LinearSolver::Summary ComputeStep( |
| 57 | const TrustRegionStrategy::PerSolveOptions& per_solve_options, |
| 58 | SparseMatrix* jacobian, |
| 59 | const double* residuals, |
| 60 | double* step); |
| 61 | virtual void StepAccepted(double step_quality); |
| 62 | virtual void StepRejected(double step_quality); |
| 63 | virtual void StepIsInvalid(); |
| 64 | |
| 65 | virtual double Radius() const; |
| 66 | |
| 67 | private: |
Sameer Agarwal | fa01519 | 2012-06-11 14:21:42 -0700 | [diff] [blame] | 68 | LinearSolver::Summary ComputeGaussNewtonStep(SparseMatrix* jacobian, |
| 69 | const double* residuals); |
Markus Moll | a3fb17c | 2012-08-15 15:37:27 +0200 | [diff] [blame^] | 70 | void ComputeCauchyPoint(SparseMatrix* jacobian); |
| 71 | void ComputeGradient(SparseMatrix* jacobian, const double* residuals); |
Sameer Agarwal | fa01519 | 2012-06-11 14:21:42 -0700 | [diff] [blame] | 72 | void ComputeDoglegStep(double* step); |
| 73 | |
| 74 | LinearSolver* linear_solver_; |
| 75 | double radius_; |
| 76 | const double max_radius_; |
| 77 | |
| 78 | const double min_diagonal_; |
| 79 | const double max_diagonal_; |
| 80 | |
| 81 | // mu is used to scale the diagonal matrix used to make the |
| 82 | // Gauss-Newton solve full rank. In each solve, the strategy starts |
| 83 | // out with mu = min_mu, and tries values upto max_mu. If the user |
| 84 | // reports an invalid step, the value of mu_ is increased so that |
| 85 | // the next solve starts with a stronger regularization. |
| 86 | // |
| 87 | // If a successful step is reported, then the value of mu_ is |
| 88 | // decreased with a lower bound of min_mu_. |
| 89 | double mu_; |
| 90 | const double min_mu_; |
| 91 | const double max_mu_; |
| 92 | const double mu_increase_factor_; |
| 93 | const double increase_threshold_; |
| 94 | const double decrease_threshold_; |
| 95 | |
Markus Moll | a3fb17c | 2012-08-15 15:37:27 +0200 | [diff] [blame^] | 96 | Vector diagonal_; // sqrt(diag(J^T J)) |
Sameer Agarwal | fa01519 | 2012-06-11 14:21:42 -0700 | [diff] [blame] | 97 | Vector lm_diagonal_; |
| 98 | |
| 99 | Vector gradient_; |
| 100 | Vector gauss_newton_step_; |
| 101 | |
| 102 | // cauchy_step = alpha * gradient |
| 103 | double alpha_; |
| 104 | double dogleg_step_norm_; |
| 105 | |
| 106 | // When, ComputeStep is called, reuse_ indicates whether the |
| 107 | // Gauss-Newton and Cauchy steps from the last call to ComputeStep |
| 108 | // can be reused or not. |
| 109 | // |
| 110 | // If the user called StepAccepted, then it is expected that the |
| 111 | // user has recomputed the Jacobian matrix and new Gauss-Newton |
| 112 | // solve is needed and reuse is set to false. |
| 113 | // |
| 114 | // If the user called StepRejected, then it is expected that the |
| 115 | // user wants to solve the trust region problem with the same matrix |
| 116 | // but a different trust region radius and the Gauss-Newton and |
| 117 | // Cauchy steps can be reused to compute the Dogleg, thus reuse is |
| 118 | // set to true. |
| 119 | // |
| 120 | // If the user called StepIsInvalid, then there was a numerical |
| 121 | // problem with the step computed in the last call to ComputeStep, |
| 122 | // and the regularization used to do the Gauss-Newton solve is |
| 123 | // increased and a new solve should be done when ComputeStep is |
| 124 | // called again, thus reuse is set to false. |
| 125 | bool reuse_; |
| 126 | }; |
| 127 | |
| 128 | } // namespace internal |
| 129 | } // namespace ceres |
| 130 | |
| 131 | #endif // CERES_INTERNAL_DOGLEG_STRATEGY_H_ |