Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Keir Mierle | 7492b0d | 2015-03-17 22:30:16 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google Inc. All rights reserved. |
| 3 | // http://ceres-solver.org/ |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 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 | // Interface for and implementation of various Line search algorithms. |
| 32 | |
| 33 | #ifndef CERES_INTERNAL_LINE_SEARCH_H_ |
| 34 | #define CERES_INTERNAL_LINE_SEARCH_H_ |
| 35 | |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 36 | #include <string> |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 37 | #include <vector> |
| 38 | #include "ceres/internal/eigen.h" |
| 39 | #include "ceres/internal/port.h" |
Sameer Agarwal | 0924401 | 2013-06-30 14:33:23 -0700 | [diff] [blame] | 40 | #include "ceres/types.h" |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 41 | |
| 42 | namespace ceres { |
| 43 | namespace internal { |
| 44 | |
| 45 | class Evaluator; |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 46 | struct FunctionSample; |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 47 | class LineSearchFunction; |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 48 | |
| 49 | // Line search is another name for a one dimensional optimization |
| 50 | // algorithm. The name "line search" comes from the fact one |
| 51 | // dimensional optimization problems that arise as subproblems of |
| 52 | // general multidimensional optimization problems. |
| 53 | // |
| 54 | // While finding the exact minimum of a one dimensionl function is |
| 55 | // hard, instances of LineSearch find a point that satisfies a |
| 56 | // sufficient decrease condition. Depending on the particular |
| 57 | // condition used, we get a variety of different line search |
| 58 | // algorithms, e.g., Armijo, Wolfe etc. |
| 59 | class LineSearch { |
| 60 | public: |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 61 | struct Summary; |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 62 | |
| 63 | struct Options { |
| 64 | Options() |
Sameer Agarwal | 0924401 | 2013-06-30 14:33:23 -0700 | [diff] [blame] | 65 | : interpolation_type(CUBIC), |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 66 | sufficient_decrease(1e-4), |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 67 | max_step_contraction(1e-3), |
| 68 | min_step_contraction(0.9), |
Sameer Agarwal | 0924401 | 2013-06-30 14:33:23 -0700 | [diff] [blame] | 69 | min_step_size(1e-9), |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 70 | max_num_iterations(20), |
| 71 | sufficient_curvature_decrease(0.9), |
| 72 | max_step_expansion(10.0), |
Sameer Agarwal | 3dd7ed7 | 2014-02-24 22:01:45 -0800 | [diff] [blame] | 73 | is_silent(false), |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 74 | function(NULL) {} |
| 75 | |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 76 | // Degree of the polynomial used to approximate the objective |
Sameer Agarwal | 0924401 | 2013-06-30 14:33:23 -0700 | [diff] [blame] | 77 | // function. |
| 78 | LineSearchInterpolationType interpolation_type; |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 79 | |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 80 | // Armijo and Wolfe line search parameters. |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 81 | |
| 82 | // Solving the line search problem exactly is computationally |
| 83 | // prohibitive. Fortunately, line search based optimization |
| 84 | // algorithms can still guarantee convergence if instead of an |
| 85 | // exact solution, the line search algorithm returns a solution |
| 86 | // which decreases the value of the objective function |
| 87 | // sufficiently. More precisely, we are looking for a step_size |
| 88 | // s.t. |
| 89 | // |
| 90 | // f(step_size) <= f(0) + sufficient_decrease * f'(0) * step_size |
| 91 | double sufficient_decrease; |
| 92 | |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 93 | // In each iteration of the Armijo / Wolfe line search, |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 94 | // |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 95 | // new_step_size >= max_step_contraction * step_size |
| 96 | // |
| 97 | // Note that by definition, for contraction: |
| 98 | // |
| 99 | // 0 < max_step_contraction < min_step_contraction < 1 |
| 100 | // |
| 101 | double max_step_contraction; |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 102 | |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 103 | // In each iteration of the Armijo / Wolfe line search, |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 104 | // |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 105 | // new_step_size <= min_step_contraction * step_size |
| 106 | // Note that by definition, for contraction: |
| 107 | // |
| 108 | // 0 < max_step_contraction < min_step_contraction < 1 |
| 109 | // |
| 110 | double min_step_contraction; |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 111 | |
| 112 | // If during the line search, the step_size falls below this |
| 113 | // value, it is truncated to zero. |
Sameer Agarwal | 0924401 | 2013-06-30 14:33:23 -0700 | [diff] [blame] | 114 | double min_step_size; |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 115 | |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 116 | // Maximum number of trial step size iterations during each line search, |
| 117 | // if a step size satisfying the search conditions cannot be found within |
| 118 | // this number of trials, the line search will terminate. |
| 119 | int max_num_iterations; |
| 120 | |
| 121 | // Wolfe-specific line search parameters. |
| 122 | |
| 123 | // The strong Wolfe conditions consist of the Armijo sufficient |
| 124 | // decrease condition, and an additional requirement that the |
| 125 | // step-size be chosen s.t. the _magnitude_ ('strong' Wolfe |
| 126 | // conditions) of the gradient along the search direction |
| 127 | // decreases sufficiently. Precisely, this second condition |
| 128 | // is that we seek a step_size s.t. |
| 129 | // |
| 130 | // |f'(step_size)| <= sufficient_curvature_decrease * |f'(0)| |
| 131 | // |
| 132 | // Where f() is the line search objective and f'() is the derivative |
| 133 | // of f w.r.t step_size (d f / d step_size). |
| 134 | double sufficient_curvature_decrease; |
| 135 | |
| 136 | // During the bracketing phase of the Wolfe search, the step size is |
| 137 | // increased until either a point satisfying the Wolfe conditions is |
| 138 | // found, or an upper bound for a bracket containing a point satisfying |
| 139 | // the conditions is found. Precisely, at each iteration of the |
| 140 | // expansion: |
| 141 | // |
| 142 | // new_step_size <= max_step_expansion * step_size. |
| 143 | // |
| 144 | // By definition for expansion, max_step_expansion > 1.0. |
| 145 | double max_step_expansion; |
| 146 | |
Sameer Agarwal | 3dd7ed7 | 2014-02-24 22:01:45 -0800 | [diff] [blame] | 147 | bool is_silent; |
| 148 | |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 149 | // The one dimensional function that the line search algorithm |
| 150 | // minimizes. |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 151 | LineSearchFunction* function; |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | // Result of the line search. |
| 155 | struct Summary { |
| 156 | Summary() |
| 157 | : success(false), |
| 158 | optimal_step_size(0.0), |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 159 | num_function_evaluations(0), |
| 160 | num_gradient_evaluations(0), |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 161 | num_iterations(0), |
| 162 | cost_evaluation_time_in_seconds(-1.0), |
| 163 | gradient_evaluation_time_in_seconds(-1.0), |
| 164 | polynomial_minimization_time_in_seconds(-1.0), |
| 165 | total_time_in_seconds(-1.0) {} |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 166 | |
| 167 | bool success; |
| 168 | double optimal_step_size; |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 169 | int num_function_evaluations; |
| 170 | int num_gradient_evaluations; |
| 171 | int num_iterations; |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 172 | // Cumulative time spent evaluating the value of the cost function across |
| 173 | // all iterations. |
| 174 | double cost_evaluation_time_in_seconds; |
| 175 | // Cumulative time spent evaluating the gradient of the cost function across |
| 176 | // all iterations. |
| 177 | double gradient_evaluation_time_in_seconds; |
| 178 | // Cumulative time spent minimizing the interpolating polynomial to compute |
| 179 | // the next candidate step size across all iterations. |
| 180 | double polynomial_minimization_time_in_seconds; |
| 181 | double total_time_in_seconds; |
Sameer Agarwal | 05a07ec | 2015-01-07 15:10:46 -0800 | [diff] [blame] | 182 | std::string error; |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 183 | }; |
| 184 | |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 185 | explicit LineSearch(const LineSearch::Options& options); |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 186 | virtual ~LineSearch() {} |
| 187 | |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 188 | static LineSearch* Create(const LineSearchType line_search_type, |
| 189 | const LineSearch::Options& options, |
Sameer Agarwal | 05a07ec | 2015-01-07 15:10:46 -0800 | [diff] [blame] | 190 | std::string* error); |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 191 | |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 192 | // Perform the line search. |
| 193 | // |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 194 | // step_size_estimate must be a positive number. |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 195 | // |
| 196 | // initial_cost and initial_gradient are the values and gradient of |
| 197 | // the function at zero. |
| 198 | // summary must not be null and will contain the result of the line |
| 199 | // search. |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 200 | // |
| 201 | // Summary::success is true if a non-zero step size is found. |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 202 | void Search(double step_size_estimate, |
| 203 | double initial_cost, |
| 204 | double initial_gradient, |
| 205 | Summary* summary) const; |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 206 | double InterpolatingPolynomialMinimizingStepSize( |
| 207 | const LineSearchInterpolationType& interpolation_type, |
| 208 | const FunctionSample& lowerbound_sample, |
| 209 | const FunctionSample& previous_sample, |
| 210 | const FunctionSample& current_sample, |
| 211 | const double min_step_size, |
| 212 | const double max_step_size) const; |
| 213 | |
| 214 | protected: |
| 215 | const LineSearch::Options& options() const { return options_; } |
| 216 | |
| 217 | private: |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 218 | virtual void DoSearch(double step_size_estimate, |
| 219 | double initial_cost, |
| 220 | double initial_gradient, |
| 221 | Summary* summary) const = 0; |
| 222 | |
| 223 | private: |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 224 | LineSearch::Options options_; |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 225 | }; |
| 226 | |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 227 | // An object used by the line search to access the function values |
| 228 | // and gradient of the one dimensional function being optimized. |
| 229 | // |
| 230 | // In practice, this object provides access to the objective |
| 231 | // function value and the directional derivative of the underlying |
| 232 | // optimization problem along a specific search direction. |
| 233 | class LineSearchFunction { |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 234 | public: |
| 235 | explicit LineSearchFunction(Evaluator* evaluator); |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 236 | void Init(const Vector& position, const Vector& direction); |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 237 | // Evaluate the line search objective |
| 238 | // |
| 239 | // f(x) = p(position + x * direction) |
| 240 | // |
| 241 | // Where, p is the objective function of the general optimization |
| 242 | // problem. |
| 243 | // |
| 244 | // g is the gradient f'(x) at x. |
| 245 | // |
| 246 | // f must not be null. The gradient is computed only if g is not null. |
| 247 | bool Evaluate(double x, double* f, double* g); |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 248 | double DirectionInfinityNorm() const; |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 249 | // Resets to now, the start point for the results from TimeStatistics(). |
| 250 | void ResetTimeStatistics(); |
| 251 | void TimeStatistics(double* cost_evaluation_time_in_seconds, |
| 252 | double* gradient_evaluation_time_in_seconds) const; |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 253 | |
| 254 | private: |
| 255 | Evaluator* evaluator_; |
| 256 | Vector position_; |
| 257 | Vector direction_; |
| 258 | |
| 259 | // evaluation_point = Evaluator::Plus(position_, x * direction_); |
| 260 | Vector evaluation_point_; |
| 261 | |
| 262 | // scaled_direction = x * direction_; |
| 263 | Vector scaled_direction_; |
| 264 | Vector gradient_; |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 265 | |
| 266 | // We may not exclusively own the evaluator (e.g. in the Trust Region |
| 267 | // minimizer), hence we need to save the initial evaluation durations for the |
| 268 | // value & gradient to accurately determine the duration of the evaluations |
| 269 | // we invoked. These are reset by a call to ResetTimeStatistics(). |
| 270 | double initial_evaluator_residual_time_in_seconds; |
| 271 | double initial_evaluator_jacobian_time_in_seconds; |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | // Backtracking and interpolation based Armijo line search. This |
| 275 | // implementation is based on the Armijo line search that ships in the |
| 276 | // minFunc package by Mark Schmidt. |
| 277 | // |
| 278 | // For more details: http://www.di.ens.fr/~mschmidt/Software/minFunc.html |
| 279 | class ArmijoLineSearch : public LineSearch { |
| 280 | public: |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 281 | explicit ArmijoLineSearch(const LineSearch::Options& options); |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 282 | virtual ~ArmijoLineSearch() {} |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 283 | |
| 284 | private: |
| 285 | virtual void DoSearch(double step_size_estimate, |
| 286 | double initial_cost, |
| 287 | double initial_gradient, |
| 288 | Summary* summary) const; |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 289 | }; |
| 290 | |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 291 | // Bracketing / Zoom Strong Wolfe condition line search. This implementation |
| 292 | // is based on the pseudo-code algorithm presented in Nocedal & Wright [1] |
| 293 | // (p60-61) with inspiration from the WolfeLineSearch which ships with the |
| 294 | // minFunc package by Mark Schmidt [2]. |
| 295 | // |
| 296 | // [1] Nocedal J., Wright S., Numerical Optimization, 2nd Ed., Springer, 1999. |
| 297 | // [2] http://www.di.ens.fr/~mschmidt/Software/minFunc.html. |
| 298 | class WolfeLineSearch : public LineSearch { |
| 299 | public: |
| 300 | explicit WolfeLineSearch(const LineSearch::Options& options); |
| 301 | virtual ~WolfeLineSearch() {} |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 302 | |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 303 | // Returns true iff either a valid point, or valid bracket are found. |
| 304 | bool BracketingPhase(const FunctionSample& initial_position, |
| 305 | const double step_size_estimate, |
| 306 | FunctionSample* bracket_low, |
| 307 | FunctionSample* bracket_high, |
| 308 | bool* perform_zoom_search, |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 309 | Summary* summary) const; |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 310 | // Returns true iff final_line_sample satisfies strong Wolfe conditions. |
| 311 | bool ZoomPhase(const FunctionSample& initial_position, |
| 312 | FunctionSample bracket_low, |
| 313 | FunctionSample bracket_high, |
| 314 | FunctionSample* solution, |
Alex Stewart | 9ad59a7 | 2014-11-17 22:20:46 +0000 | [diff] [blame] | 315 | Summary* summary) const; |
| 316 | |
| 317 | private: |
| 318 | virtual void DoSearch(double step_size_estimate, |
| 319 | double initial_cost, |
| 320 | double initial_gradient, |
| 321 | Summary* summary) const; |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 322 | }; |
| 323 | |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 324 | } // namespace internal |
| 325 | } // namespace ceres |
| 326 | |
Sameer Agarwal | 1d11be9 | 2012-11-25 19:28:06 -0800 | [diff] [blame] | 327 | #endif // CERES_INTERNAL_LINE_SEARCH_H_ |