Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -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_TRUST_REGION_STRATEGY_H_ |
| 32 | #define CERES_INTERNAL_TRUST_REGION_STRATEGY_H_ |
| 33 | |
Sameer Agarwal | c4a3291 | 2013-06-13 22:00:48 -0700 | [diff] [blame] | 34 | #include <string> |
| 35 | #include "ceres/internal/port.h" |
Sameer Agarwal | 79bde35 | 2013-11-21 21:33:51 -0800 | [diff] [blame] | 36 | #include "ceres/linear_solver.h" |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 37 | |
| 38 | namespace ceres { |
| 39 | namespace internal { |
| 40 | |
Sameer Agarwal | 05292bf | 2012-08-20 07:40:45 -0700 | [diff] [blame] | 41 | class LinearSolver; |
| 42 | class SparseMatrix; |
| 43 | |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 44 | // Interface for classes implementing various trust region strategies |
| 45 | // for nonlinear least squares problems. |
| 46 | // |
| 47 | // The object is expected to maintain and update a trust region |
| 48 | // radius, which it then uses to solve for the trust region step using |
| 49 | // the jacobian matrix and residual vector. |
| 50 | // |
| 51 | // Here the term trust region radius is used loosely, as the strategy |
| 52 | // is free to treat it as guidance and violate it as need be. e.g., |
| 53 | // the LevenbergMarquardtStrategy uses the inverse of the trust region |
| 54 | // radius to scale the damping term, which controls the step size, but |
| 55 | // does not set a hard limit on its size. |
| 56 | class TrustRegionStrategy { |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 57 | public: |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 58 | struct Options { |
| 59 | Options() |
| 60 | : trust_region_strategy_type(LEVENBERG_MARQUARDT), |
| 61 | initial_radius(1e4), |
| 62 | max_radius(1e32), |
Sameer Agarwal | eeedd2e | 2013-07-07 23:04:31 -0700 | [diff] [blame] | 63 | min_lm_diagonal(1e-6), |
| 64 | max_lm_diagonal(1e32), |
Markus Moll | 51cf7cb | 2012-08-20 20:10:20 +0200 | [diff] [blame] | 65 | dogleg_type(TRADITIONAL_DOGLEG) { |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | TrustRegionStrategyType trust_region_strategy_type; |
| 69 | // Linear solver used for actually solving the trust region step. |
| 70 | LinearSolver* linear_solver; |
| 71 | double initial_radius; |
| 72 | double max_radius; |
| 73 | |
| 74 | // Minimum and maximum values of the diagonal damping matrix used |
Sameer Agarwal | fa01519 | 2012-06-11 14:21:42 -0700 | [diff] [blame] | 75 | // by LevenbergMarquardtStrategy. The DoglegStrategy also uses |
| 76 | // these bounds to construct a regularizing diagonal to ensure |
| 77 | // that the Gauss-Newton step computation is of full rank. |
Sameer Agarwal | eeedd2e | 2013-07-07 23:04:31 -0700 | [diff] [blame] | 78 | double min_lm_diagonal; |
| 79 | double max_lm_diagonal; |
Markus Moll | 51cf7cb | 2012-08-20 20:10:20 +0200 | [diff] [blame] | 80 | |
| 81 | // Further specify which dogleg method to use |
| 82 | DoglegType dogleg_type; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | // Per solve options. |
| 86 | struct PerSolveOptions { |
Sameer Agarwal | c4a3291 | 2013-06-13 22:00:48 -0700 | [diff] [blame] | 87 | PerSolveOptions() |
| 88 | : eta(0), |
| 89 | dump_filename_base(""), |
| 90 | dump_format_type(TEXTFILE) { |
| 91 | } |
| 92 | |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 93 | // Forcing sequence for inexact solves. |
| 94 | double eta; |
Sameer Agarwal | c4a3291 | 2013-06-13 22:00:48 -0700 | [diff] [blame] | 95 | |
| 96 | // If non-empty and dump_format_type is not CONSOLE, the trust |
| 97 | // regions strategy will write the linear system to file(s) with |
| 98 | // name starting with dump_filename_base. If dump_format_type is |
| 99 | // CONSOLE then dump_filename_base will be ignored and the linear |
| 100 | // system will be written to the standard error. |
| 101 | string dump_filename_base; |
| 102 | DumpFormatType dump_format_type; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
Sameer Agarwal | 05292bf | 2012-08-20 07:40:45 -0700 | [diff] [blame] | 105 | struct Summary { |
| 106 | Summary() |
| 107 | : residual_norm(0.0), |
| 108 | num_iterations(-1), |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 109 | termination_type(LINEAR_SOLVER_FAILURE) { |
Sameer Agarwal | 05292bf | 2012-08-20 07:40:45 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // If the trust region problem is, |
| 113 | // |
| 114 | // 1/2 x'Ax + b'x + c, |
| 115 | // |
| 116 | // then |
| 117 | // |
| 118 | // residual_norm = |Ax -b| |
| 119 | double residual_norm; |
| 120 | |
| 121 | // Number of iterations used by the linear solver. If a linear |
| 122 | // solver was not called (e.g., DogLegStrategy after an |
| 123 | // unsuccessful step), then this would be zero. |
| 124 | int num_iterations; |
| 125 | |
| 126 | // Status of the linear solver used to solve the Newton system. |
| 127 | LinearSolverTerminationType termination_type; |
| 128 | }; |
| 129 | |
Sameer Agarwal | 59534c1 | 2012-06-15 14:36:27 -0700 | [diff] [blame] | 130 | virtual ~TrustRegionStrategy(); |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 131 | |
| 132 | // Use the current radius to solve for the trust region step. |
Sameer Agarwal | 05292bf | 2012-08-20 07:40:45 -0700 | [diff] [blame] | 133 | virtual Summary ComputeStep(const PerSolveOptions& per_solve_options, |
| 134 | SparseMatrix* jacobian, |
| 135 | const double* residuals, |
| 136 | double* step) = 0; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 137 | |
| 138 | // Inform the strategy that the current step has been accepted, and |
| 139 | // that the ratio of the decrease in the non-linear objective to the |
| 140 | // decrease in the trust region model is step_quality. |
| 141 | virtual void StepAccepted(double step_quality) = 0; |
| 142 | |
| 143 | // Inform the strategy that the current step has been rejected, and |
| 144 | // that the ratio of the decrease in the non-linear objective to the |
| 145 | // decrease in the trust region model is step_quality. |
| 146 | virtual void StepRejected(double step_quality) = 0; |
| 147 | |
Sameer Agarwal | d432f78 | 2012-06-11 11:52:32 -0700 | [diff] [blame] | 148 | // Inform the strategy that the current step has been rejected |
| 149 | // because it was found to be numerically invalid. |
| 150 | // StepRejected/StepAccepted will not be called for this step, and |
| 151 | // the strategy is free to do what it wants with this information. |
| 152 | virtual void StepIsInvalid() = 0; |
| 153 | |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 154 | // Current trust region radius. |
| 155 | virtual double Radius() const = 0; |
| 156 | |
| 157 | // Factory. |
| 158 | static TrustRegionStrategy* Create(const Options& options); |
| 159 | }; |
| 160 | |
| 161 | } // namespace internal |
| 162 | } // namespace ceres |
| 163 | |
| 164 | #endif // CERES_INTERNAL_TRUST_REGION_STRATEGY_H_ |