Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame^] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2015 Google Inc. All rights reserved. |
| 3 | // http://ceres-solver.org/ |
| 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_PUBLIC_GRADIENT_PROBLEM_SOLVER_H_ |
| 32 | #define CERES_PUBLIC_GRADIENT_PROBLEM_SOLVER_H_ |
| 33 | |
| 34 | #include <cmath> |
| 35 | #include <string> |
| 36 | #include <vector> |
| 37 | #include "ceres/internal/port.h" |
| 38 | #include "ceres/iteration_callback.h" |
| 39 | #include "ceres/types.h" |
| 40 | #include "ceres/internal/disable_warnings.h" |
| 41 | |
| 42 | namespace ceres { |
| 43 | |
| 44 | class GradientProblem; |
| 45 | |
| 46 | class CERES_EXPORT GradientProblemSolver { |
| 47 | public: |
| 48 | virtual ~GradientProblemSolver(); |
| 49 | |
| 50 | // The options structure contains, not surprisingly, options that control how |
| 51 | // the solver operates. The defaults should be suitable for a wide range of |
| 52 | // problems; however, better performance is often obtainable with tweaking. |
| 53 | // |
| 54 | // The constants are defined inside types.h |
| 55 | struct CERES_EXPORT Options { |
| 56 | // Returns true if the options struct has a valid |
| 57 | // configuration. Returns false otherwise, and fills in *error |
| 58 | // with a message describing the problem. |
| 59 | bool IsValid(std::string* error) const; |
| 60 | |
| 61 | // Minimizer options ---------------------------------------- |
| 62 | LineSearchDirectionType line_search_direction_type = LBFGS; |
| 63 | LineSearchType line_search_type = WOLFE; |
| 64 | NonlinearConjugateGradientType nonlinear_conjugate_gradient_type = |
| 65 | FLETCHER_REEVES; |
| 66 | |
| 67 | // The LBFGS hessian approximation is a low rank approximation to |
| 68 | // the inverse of the Hessian matrix. The rank of the |
| 69 | // approximation determines (linearly) the space and time |
| 70 | // complexity of using the approximation. Higher the rank, the |
| 71 | // better is the quality of the approximation. The increase in |
| 72 | // quality is however is bounded for a number of reasons. |
| 73 | // |
| 74 | // 1. The method only uses secant information and not actual |
| 75 | // derivatives. |
| 76 | // |
| 77 | // 2. The Hessian approximation is constrained to be positive |
| 78 | // definite. |
| 79 | // |
| 80 | // So increasing this rank to a large number will cost time and |
| 81 | // space complexity without the corresponding increase in solution |
| 82 | // quality. There are no hard and fast rules for choosing the |
| 83 | // maximum rank. The best choice usually requires some problem |
| 84 | // specific experimentation. |
| 85 | // |
| 86 | // For more theoretical and implementation details of the LBFGS |
| 87 | // method, please see: |
| 88 | // |
| 89 | // Nocedal, J. (1980). "Updating Quasi-Newton Matrices with |
| 90 | // Limited Storage". Mathematics of Computation 35 (151): 773–782. |
| 91 | int max_lbfgs_rank = 20; |
| 92 | |
| 93 | // As part of the (L)BFGS update step (BFGS) / right-multiply step (L-BFGS), |
| 94 | // the initial inverse Hessian approximation is taken to be the Identity. |
| 95 | // However, Oren showed that using instead I * \gamma, where \gamma is |
| 96 | // chosen to approximate an eigenvalue of the true inverse Hessian can |
| 97 | // result in improved convergence in a wide variety of cases. Setting |
| 98 | // use_approximate_eigenvalue_bfgs_scaling to true enables this scaling. |
| 99 | // |
| 100 | // It is important to note that approximate eigenvalue scaling does not |
| 101 | // always improve convergence, and that it can in fact significantly degrade |
| 102 | // performance for certain classes of problem, which is why it is disabled |
| 103 | // by default. In particular it can degrade performance when the |
| 104 | // sensitivity of the problem to different parameters varies significantly, |
| 105 | // as in this case a single scalar factor fails to capture this variation |
| 106 | // and detrimentally downscales parts of the jacobian approximation which |
| 107 | // correspond to low-sensitivity parameters. It can also reduce the |
| 108 | // robustness of the solution to errors in the jacobians. |
| 109 | // |
| 110 | // Oren S.S., Self-scaling variable metric (SSVM) algorithms |
| 111 | // Part II: Implementation and experiments, Management Science, |
| 112 | // 20(5), 863-874, 1974. |
| 113 | bool use_approximate_eigenvalue_bfgs_scaling = false; |
| 114 | |
| 115 | // Degree of the polynomial used to approximate the objective |
| 116 | // function. Valid values are BISECTION, QUADRATIC and CUBIC. |
| 117 | // |
| 118 | // BISECTION corresponds to pure backtracking search with no |
| 119 | // interpolation. |
| 120 | LineSearchInterpolationType line_search_interpolation_type = CUBIC; |
| 121 | |
| 122 | // If during the line search, the step_size falls below this |
| 123 | // value, it is truncated to zero. |
| 124 | double min_line_search_step_size = 1e-9; |
| 125 | |
| 126 | // Line search parameters. |
| 127 | |
| 128 | // Solving the line search problem exactly is computationally |
| 129 | // prohibitive. Fortunately, line search based optimization |
| 130 | // algorithms can still guarantee convergence if instead of an |
| 131 | // exact solution, the line search algorithm returns a solution |
| 132 | // which decreases the value of the objective function |
| 133 | // sufficiently. More precisely, we are looking for a step_size |
| 134 | // s.t. |
| 135 | // |
| 136 | // f(step_size) <= f(0) + sufficient_decrease * f'(0) * step_size |
| 137 | // |
| 138 | double line_search_sufficient_function_decrease = 1e-4; |
| 139 | |
| 140 | // In each iteration of the line search, |
| 141 | // |
| 142 | // new_step_size >= max_line_search_step_contraction * step_size |
| 143 | // |
| 144 | // Note that by definition, for contraction: |
| 145 | // |
| 146 | // 0 < max_step_contraction < min_step_contraction < 1 |
| 147 | // |
| 148 | double max_line_search_step_contraction = 1e-3; |
| 149 | |
| 150 | // In each iteration of the line search, |
| 151 | // |
| 152 | // new_step_size <= min_line_search_step_contraction * step_size |
| 153 | // |
| 154 | // Note that by definition, for contraction: |
| 155 | // |
| 156 | // 0 < max_step_contraction < min_step_contraction < 1 |
| 157 | // |
| 158 | double min_line_search_step_contraction = 0.6; |
| 159 | |
| 160 | // Maximum number of trial step size iterations during each line search, |
| 161 | // if a step size satisfying the search conditions cannot be found within |
| 162 | // this number of trials, the line search will terminate. |
| 163 | int max_num_line_search_step_size_iterations = 20; |
| 164 | |
| 165 | // Maximum number of restarts of the line search direction algorithm before |
| 166 | // terminating the optimization. Restarts of the line search direction |
| 167 | // algorithm occur when the current algorithm fails to produce a new descent |
| 168 | // direction. This typically indicates a numerical failure, or a breakdown |
| 169 | // in the validity of the approximations used. |
| 170 | int max_num_line_search_direction_restarts = 5; |
| 171 | |
| 172 | // The strong Wolfe conditions consist of the Armijo sufficient |
| 173 | // decrease condition, and an additional requirement that the |
| 174 | // step-size be chosen s.t. the _magnitude_ ('strong' Wolfe |
| 175 | // conditions) of the gradient along the search direction |
| 176 | // decreases sufficiently. Precisely, this second condition |
| 177 | // is that we seek a step_size s.t. |
| 178 | // |
| 179 | // |f'(step_size)| <= sufficient_curvature_decrease * |f'(0)| |
| 180 | // |
| 181 | // Where f() is the line search objective and f'() is the derivative |
| 182 | // of f w.r.t step_size (d f / d step_size). |
| 183 | double line_search_sufficient_curvature_decrease = 0.9; |
| 184 | |
| 185 | // During the bracketing phase of the Wolfe search, the step size is |
| 186 | // increased until either a point satisfying the Wolfe conditions is |
| 187 | // found, or an upper bound for a bracket containing a point satisfying |
| 188 | // the conditions is found. Precisely, at each iteration of the |
| 189 | // expansion: |
| 190 | // |
| 191 | // new_step_size <= max_step_expansion * step_size. |
| 192 | // |
| 193 | // By definition for expansion, max_step_expansion > 1.0. |
| 194 | double max_line_search_step_expansion = 10.0; |
| 195 | |
| 196 | // Maximum number of iterations for the minimizer to run for. |
| 197 | int max_num_iterations = 50; |
| 198 | |
| 199 | // Maximum time for which the minimizer should run for. |
| 200 | double max_solver_time_in_seconds = 1e9; |
| 201 | |
| 202 | // Minimizer terminates when |
| 203 | // |
| 204 | // (new_cost - old_cost) < function_tolerance * old_cost; |
| 205 | // |
| 206 | double function_tolerance = 1e-6; |
| 207 | |
| 208 | // Minimizer terminates when |
| 209 | // |
| 210 | // max_i |x - Project(Plus(x, -g(x))| < gradient_tolerance |
| 211 | // |
| 212 | // This value should typically be 1e-4 * function_tolerance. |
| 213 | double gradient_tolerance = 1e-10; |
| 214 | |
| 215 | // Minimizer terminates when |
| 216 | // |
| 217 | // |step|_2 <= parameter_tolerance * ( |x|_2 + parameter_tolerance) |
| 218 | // |
| 219 | double parameter_tolerance = 1e-8; |
| 220 | |
| 221 | // Logging options --------------------------------------------------------- |
| 222 | |
| 223 | LoggingType logging_type = PER_MINIMIZER_ITERATION; |
| 224 | |
| 225 | // By default the Minimizer progress is logged to VLOG(1), which |
| 226 | // is sent to STDERR depending on the vlog level. If this flag is |
| 227 | // set to true, and logging_type is not SILENT, the logging output |
| 228 | // is sent to STDOUT. |
| 229 | bool minimizer_progress_to_stdout = false; |
| 230 | |
| 231 | // If true, the user's parameter blocks are updated at the end of |
| 232 | // every Minimizer iteration, otherwise they are updated when the |
| 233 | // Minimizer terminates. This is useful if, for example, the user |
| 234 | // wishes to visualize the state of the optimization every |
| 235 | // iteration. |
| 236 | bool update_state_every_iteration = false; |
| 237 | |
| 238 | // Callbacks that are executed at the end of each iteration of the |
| 239 | // Minimizer. An iteration may terminate midway, either due to |
| 240 | // numerical failures or because one of the convergence tests has |
| 241 | // been satisfied. In this case none of the callbacks are |
| 242 | // executed. |
| 243 | |
| 244 | // Callbacks are executed in the order that they are specified in |
| 245 | // this vector. By default, parameter blocks are updated only at |
| 246 | // the end of the optimization, i.e when the Minimizer |
| 247 | // terminates. This behaviour is controlled by |
| 248 | // update_state_every_variable. If the user wishes to have access |
| 249 | // to the update parameter blocks when his/her callbacks are |
| 250 | // executed, then set update_state_every_iteration to true. |
| 251 | // |
| 252 | // The solver does NOT take ownership of these pointers. |
| 253 | std::vector<IterationCallback*> callbacks; |
| 254 | }; |
| 255 | |
| 256 | struct CERES_EXPORT Summary { |
| 257 | // A brief one line description of the state of the solver after |
| 258 | // termination. |
| 259 | std::string BriefReport() const; |
| 260 | |
| 261 | // A full multiline description of the state of the solver after |
| 262 | // termination. |
| 263 | std::string FullReport() const; |
| 264 | |
| 265 | bool IsSolutionUsable() const; |
| 266 | |
| 267 | // Minimizer summary ------------------------------------------------- |
| 268 | TerminationType termination_type = FAILURE; |
| 269 | |
| 270 | // Reason why the solver terminated. |
| 271 | std::string message = "ceres::GradientProblemSolve was not called."; |
| 272 | |
| 273 | // Cost of the problem (value of the objective function) before |
| 274 | // the optimization. |
| 275 | double initial_cost = -1.0; |
| 276 | |
| 277 | // Cost of the problem (value of the objective function) after the |
| 278 | // optimization. |
| 279 | double final_cost = -1.0; |
| 280 | |
| 281 | // IterationSummary for each minimizer iteration in order. |
| 282 | std::vector<IterationSummary> iterations; |
| 283 | |
| 284 | // Number of times the cost (and not the gradient) was evaluated. |
| 285 | int num_cost_evaluations = -1; |
| 286 | |
| 287 | // Number of times the gradient (and the cost) were evaluated. |
| 288 | int num_gradient_evaluations = -1; |
| 289 | |
| 290 | // Sum total of all time spent inside Ceres when Solve is called. |
| 291 | double total_time_in_seconds = -1.0; |
| 292 | |
| 293 | // Time (in seconds) spent evaluating the cost. |
| 294 | double cost_evaluation_time_in_seconds = -1.0; |
| 295 | |
| 296 | // Time (in seconds) spent evaluating the gradient. |
| 297 | double gradient_evaluation_time_in_seconds = -1.0; |
| 298 | |
| 299 | // Time (in seconds) spent minimizing the interpolating polynomial |
| 300 | // to compute the next candidate step size as part of a line search. |
| 301 | double line_search_polynomial_minimization_time_in_seconds = -1.0; |
| 302 | |
| 303 | // Number of parameters in the problem. |
| 304 | int num_parameters = -1; |
| 305 | |
| 306 | // Dimension of the tangent space of the problem. |
| 307 | int num_local_parameters = -1; |
| 308 | |
| 309 | // Type of line search direction used. |
| 310 | LineSearchDirectionType line_search_direction_type = LBFGS; |
| 311 | |
| 312 | // Type of the line search algorithm used. |
| 313 | LineSearchType line_search_type = WOLFE; |
| 314 | |
| 315 | // When performing line search, the degree of the polynomial used |
| 316 | // to approximate the objective function. |
| 317 | LineSearchInterpolationType line_search_interpolation_type = CUBIC; |
| 318 | |
| 319 | // If the line search direction is NONLINEAR_CONJUGATE_GRADIENT, |
| 320 | // then this indicates the particular variant of non-linear |
| 321 | // conjugate gradient used. |
| 322 | NonlinearConjugateGradientType nonlinear_conjugate_gradient_type = FLETCHER_REEVES; |
| 323 | |
| 324 | // If the type of the line search direction is LBFGS, then this |
| 325 | // indicates the rank of the Hessian approximation. |
| 326 | int max_lbfgs_rank = -1; |
| 327 | }; |
| 328 | |
| 329 | // Once a least squares problem has been built, this function takes |
| 330 | // the problem and optimizes it based on the values of the options |
| 331 | // parameters. Upon return, a detailed summary of the work performed |
| 332 | // by the preprocessor, the non-linear minimizer and the linear |
| 333 | // solver are reported in the summary object. |
| 334 | virtual void Solve(const GradientProblemSolver::Options& options, |
| 335 | const GradientProblem& problem, |
| 336 | double* parameters, |
| 337 | GradientProblemSolver::Summary* summary); |
| 338 | }; |
| 339 | |
| 340 | // Helper function which avoids going through the interface. |
| 341 | CERES_EXPORT void Solve(const GradientProblemSolver::Options& options, |
| 342 | const GradientProblem& problem, |
| 343 | double* parameters, |
| 344 | GradientProblemSolver::Summary* summary); |
| 345 | |
| 346 | } // namespace ceres |
| 347 | |
| 348 | #include "ceres/internal/reenable_warnings.h" |
| 349 | |
| 350 | #endif // CERES_PUBLIC_GRADIENT_PROBLEM_SOLVER_H_ |