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 | #include "ceres/line_search.h" |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <cmath> |
| 35 | #include <iomanip> |
| 36 | #include <iostream> // NOLINT |
| 37 | |
| 38 | #include "ceres/evaluator.h" |
| 39 | #include "ceres/function_sample.h" |
| 40 | #include "ceres/internal/eigen.h" |
| 41 | #include "ceres/map_util.h" |
| 42 | #include "ceres/polynomial.h" |
| 43 | #include "ceres/stringprintf.h" |
| 44 | #include "ceres/wall_time.h" |
| 45 | #include "glog/logging.h" |
| 46 | |
| 47 | namespace ceres { |
| 48 | namespace internal { |
| 49 | |
| 50 | using std::map; |
| 51 | using std::ostream; |
| 52 | using std::string; |
| 53 | using std::vector; |
| 54 | |
| 55 | namespace { |
| 56 | // Precision used for floating point values in error message output. |
| 57 | const int kErrorMessageNumericPrecision = 8; |
| 58 | } // namespace |
| 59 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 60 | ostream& operator<<(ostream& os, const FunctionSample& sample); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 61 | |
| 62 | // Convenience stream operator for pushing FunctionSamples into log messages. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 63 | ostream& operator<<(ostream& os, const FunctionSample& sample) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 64 | os << sample.ToDebugString(); |
| 65 | return os; |
| 66 | } |
| 67 | |
| 68 | LineSearch::LineSearch(const LineSearch::Options& options) |
| 69 | : options_(options) {} |
| 70 | |
| 71 | LineSearch* LineSearch::Create(const LineSearchType line_search_type, |
| 72 | const LineSearch::Options& options, |
| 73 | string* error) { |
| 74 | LineSearch* line_search = NULL; |
| 75 | switch (line_search_type) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 76 | case ceres::ARMIJO: |
| 77 | line_search = new ArmijoLineSearch(options); |
| 78 | break; |
| 79 | case ceres::WOLFE: |
| 80 | line_search = new WolfeLineSearch(options); |
| 81 | break; |
| 82 | default: |
| 83 | *error = string("Invalid line search algorithm type: ") + |
| 84 | LineSearchTypeToString(line_search_type) + |
| 85 | string(", unable to create line search."); |
| 86 | return NULL; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 87 | } |
| 88 | return line_search; |
| 89 | } |
| 90 | |
| 91 | LineSearchFunction::LineSearchFunction(Evaluator* evaluator) |
| 92 | : evaluator_(evaluator), |
| 93 | position_(evaluator->NumParameters()), |
| 94 | direction_(evaluator->NumEffectiveParameters()), |
| 95 | scaled_direction_(evaluator->NumEffectiveParameters()), |
| 96 | initial_evaluator_residual_time_in_seconds(0.0), |
| 97 | initial_evaluator_jacobian_time_in_seconds(0.0) {} |
| 98 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 99 | void LineSearchFunction::Init(const Vector& position, const Vector& direction) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 100 | position_ = position; |
| 101 | direction_ = direction; |
| 102 | } |
| 103 | |
| 104 | void LineSearchFunction::Evaluate(const double x, |
| 105 | const bool evaluate_gradient, |
| 106 | FunctionSample* output) { |
| 107 | output->x = x; |
| 108 | output->vector_x_is_valid = false; |
| 109 | output->value_is_valid = false; |
| 110 | output->gradient_is_valid = false; |
| 111 | output->vector_gradient_is_valid = false; |
| 112 | |
| 113 | scaled_direction_ = output->x * direction_; |
| 114 | output->vector_x.resize(position_.rows(), 1); |
| 115 | if (!evaluator_->Plus(position_.data(), |
| 116 | scaled_direction_.data(), |
| 117 | output->vector_x.data())) { |
| 118 | return; |
| 119 | } |
| 120 | output->vector_x_is_valid = true; |
| 121 | |
| 122 | double* gradient = NULL; |
| 123 | if (evaluate_gradient) { |
| 124 | output->vector_gradient.resize(direction_.rows(), 1); |
| 125 | gradient = output->vector_gradient.data(); |
| 126 | } |
| 127 | const bool eval_status = evaluator_->Evaluate( |
| 128 | output->vector_x.data(), &(output->value), NULL, gradient, NULL); |
| 129 | |
| 130 | if (!eval_status || !std::isfinite(output->value)) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | output->value_is_valid = true; |
| 135 | if (!evaluate_gradient) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | output->gradient = direction_.dot(output->vector_gradient); |
| 140 | if (!std::isfinite(output->gradient)) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | output->gradient_is_valid = true; |
| 145 | output->vector_gradient_is_valid = true; |
| 146 | } |
| 147 | |
| 148 | double LineSearchFunction::DirectionInfinityNorm() const { |
| 149 | return direction_.lpNorm<Eigen::Infinity>(); |
| 150 | } |
| 151 | |
| 152 | void LineSearchFunction::ResetTimeStatistics() { |
| 153 | const map<string, CallStatistics> evaluator_statistics = |
| 154 | evaluator_->Statistics(); |
| 155 | |
| 156 | initial_evaluator_residual_time_in_seconds = |
| 157 | FindWithDefault( |
| 158 | evaluator_statistics, "Evaluator::Residual", CallStatistics()) |
| 159 | .time; |
| 160 | initial_evaluator_jacobian_time_in_seconds = |
| 161 | FindWithDefault( |
| 162 | evaluator_statistics, "Evaluator::Jacobian", CallStatistics()) |
| 163 | .time; |
| 164 | } |
| 165 | |
| 166 | void LineSearchFunction::TimeStatistics( |
| 167 | double* cost_evaluation_time_in_seconds, |
| 168 | double* gradient_evaluation_time_in_seconds) const { |
| 169 | const map<string, CallStatistics> evaluator_time_statistics = |
| 170 | evaluator_->Statistics(); |
| 171 | *cost_evaluation_time_in_seconds = |
| 172 | FindWithDefault( |
| 173 | evaluator_time_statistics, "Evaluator::Residual", CallStatistics()) |
| 174 | .time - |
| 175 | initial_evaluator_residual_time_in_seconds; |
| 176 | // Strictly speaking this will slightly underestimate the time spent |
| 177 | // evaluating the gradient of the line search univariate cost function as it |
| 178 | // does not count the time spent performing the dot product with the direction |
| 179 | // vector. However, this will typically be small by comparison, and also |
| 180 | // allows direct subtraction of the timing information from the totals for |
| 181 | // the evaluator returned in the solver summary. |
| 182 | *gradient_evaluation_time_in_seconds = |
| 183 | FindWithDefault( |
| 184 | evaluator_time_statistics, "Evaluator::Jacobian", CallStatistics()) |
| 185 | .time - |
| 186 | initial_evaluator_jacobian_time_in_seconds; |
| 187 | } |
| 188 | |
| 189 | void LineSearch::Search(double step_size_estimate, |
| 190 | double initial_cost, |
| 191 | double initial_gradient, |
| 192 | Summary* summary) const { |
| 193 | const double start_time = WallTimeInSeconds(); |
| 194 | CHECK(summary != nullptr); |
| 195 | *summary = LineSearch::Summary(); |
| 196 | |
| 197 | summary->cost_evaluation_time_in_seconds = 0.0; |
| 198 | summary->gradient_evaluation_time_in_seconds = 0.0; |
| 199 | summary->polynomial_minimization_time_in_seconds = 0.0; |
| 200 | options().function->ResetTimeStatistics(); |
| 201 | this->DoSearch(step_size_estimate, initial_cost, initial_gradient, summary); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 202 | options().function->TimeStatistics( |
| 203 | &summary->cost_evaluation_time_in_seconds, |
| 204 | &summary->gradient_evaluation_time_in_seconds); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 205 | |
| 206 | summary->total_time_in_seconds = WallTimeInSeconds() - start_time; |
| 207 | } |
| 208 | |
| 209 | // Returns step_size \in [min_step_size, max_step_size] which minimizes the |
| 210 | // polynomial of degree defined by interpolation_type which interpolates all |
| 211 | // of the provided samples with valid values. |
| 212 | double LineSearch::InterpolatingPolynomialMinimizingStepSize( |
| 213 | const LineSearchInterpolationType& interpolation_type, |
| 214 | const FunctionSample& lowerbound, |
| 215 | const FunctionSample& previous, |
| 216 | const FunctionSample& current, |
| 217 | const double min_step_size, |
| 218 | const double max_step_size) const { |
| 219 | if (!current.value_is_valid || |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 220 | (interpolation_type == BISECTION && max_step_size <= current.x)) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 221 | // Either: sample is invalid; or we are using BISECTION and contracting |
| 222 | // the step size. |
| 223 | return std::min(std::max(current.x * 0.5, min_step_size), max_step_size); |
| 224 | } else if (interpolation_type == BISECTION) { |
| 225 | CHECK_GT(max_step_size, current.x); |
| 226 | // We are expanding the search (during a Wolfe bracketing phase) using |
| 227 | // BISECTION interpolation. Using BISECTION when trying to expand is |
| 228 | // strictly speaking an oxymoron, but we define this to mean always taking |
| 229 | // the maximum step size so that the Armijo & Wolfe implementations are |
| 230 | // agnostic to the interpolation type. |
| 231 | return max_step_size; |
| 232 | } |
| 233 | // Only check if lower-bound is valid here, where it is required |
| 234 | // to avoid replicating current.value_is_valid == false |
| 235 | // behaviour in WolfeLineSearch. |
| 236 | CHECK(lowerbound.value_is_valid) |
| 237 | << std::scientific << std::setprecision(kErrorMessageNumericPrecision) |
| 238 | << "Ceres bug: lower-bound sample for interpolation is invalid, " |
| 239 | << "please contact the developers!, interpolation_type: " |
| 240 | << LineSearchInterpolationTypeToString(interpolation_type) |
| 241 | << ", lowerbound: " << lowerbound << ", previous: " << previous |
| 242 | << ", current: " << current; |
| 243 | |
| 244 | // Select step size by interpolating the function and gradient values |
| 245 | // and minimizing the corresponding polynomial. |
| 246 | vector<FunctionSample> samples; |
| 247 | samples.push_back(lowerbound); |
| 248 | |
| 249 | if (interpolation_type == QUADRATIC) { |
| 250 | // Two point interpolation using function values and the |
| 251 | // gradient at the lower bound. |
| 252 | samples.push_back(FunctionSample(current.x, current.value)); |
| 253 | |
| 254 | if (previous.value_is_valid) { |
| 255 | // Three point interpolation, using function values and the |
| 256 | // gradient at the lower bound. |
| 257 | samples.push_back(FunctionSample(previous.x, previous.value)); |
| 258 | } |
| 259 | } else if (interpolation_type == CUBIC) { |
| 260 | // Two point interpolation using the function values and the gradients. |
| 261 | samples.push_back(current); |
| 262 | |
| 263 | if (previous.value_is_valid) { |
| 264 | // Three point interpolation using the function values and |
| 265 | // the gradients. |
| 266 | samples.push_back(previous); |
| 267 | } |
| 268 | } else { |
| 269 | LOG(FATAL) << "Ceres bug: No handler for interpolation_type: " |
| 270 | << LineSearchInterpolationTypeToString(interpolation_type) |
| 271 | << ", please contact the developers!"; |
| 272 | } |
| 273 | |
| 274 | double step_size = 0.0, unused_min_value = 0.0; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 275 | MinimizeInterpolatingPolynomial( |
| 276 | samples, min_step_size, max_step_size, &step_size, &unused_min_value); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 277 | return step_size; |
| 278 | } |
| 279 | |
| 280 | ArmijoLineSearch::ArmijoLineSearch(const LineSearch::Options& options) |
| 281 | : LineSearch(options) {} |
| 282 | |
| 283 | void ArmijoLineSearch::DoSearch(const double step_size_estimate, |
| 284 | const double initial_cost, |
| 285 | const double initial_gradient, |
| 286 | Summary* summary) const { |
| 287 | CHECK_GE(step_size_estimate, 0.0); |
| 288 | CHECK_GT(options().sufficient_decrease, 0.0); |
| 289 | CHECK_LT(options().sufficient_decrease, 1.0); |
| 290 | CHECK_GT(options().max_num_iterations, 0); |
| 291 | LineSearchFunction* function = options().function; |
| 292 | |
| 293 | // Note initial_cost & initial_gradient are evaluated at step_size = 0, |
| 294 | // not step_size_estimate, which is our starting guess. |
| 295 | FunctionSample initial_position(0.0, initial_cost, initial_gradient); |
| 296 | initial_position.vector_x = function->position(); |
| 297 | initial_position.vector_x_is_valid = true; |
| 298 | |
| 299 | const double descent_direction_max_norm = function->DirectionInfinityNorm(); |
| 300 | FunctionSample previous; |
| 301 | FunctionSample current; |
| 302 | |
| 303 | // As the Armijo line search algorithm always uses the initial point, for |
| 304 | // which both the function value and derivative are known, when fitting a |
| 305 | // minimizing polynomial, we can fit up to a quadratic without requiring the |
| 306 | // gradient at the current query point. |
| 307 | const bool kEvaluateGradient = options().interpolation_type == CUBIC; |
| 308 | |
| 309 | ++summary->num_function_evaluations; |
| 310 | if (kEvaluateGradient) { |
| 311 | ++summary->num_gradient_evaluations; |
| 312 | } |
| 313 | |
| 314 | function->Evaluate(step_size_estimate, kEvaluateGradient, ¤t); |
| 315 | while (!current.value_is_valid || |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 316 | current.value > (initial_cost + options().sufficient_decrease * |
| 317 | initial_gradient * current.x)) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 318 | // If current.value_is_valid is false, we treat it as if the cost at that |
| 319 | // point is not large enough to satisfy the sufficient decrease condition. |
| 320 | ++summary->num_iterations; |
| 321 | if (summary->num_iterations >= options().max_num_iterations) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 322 | summary->error = StringPrintf( |
| 323 | "Line search failed: Armijo failed to find a point " |
| 324 | "satisfying the sufficient decrease condition within " |
| 325 | "specified max_num_iterations: %d.", |
| 326 | options().max_num_iterations); |
| 327 | if (!options().is_silent) { |
| 328 | LOG(WARNING) << summary->error; |
| 329 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 330 | return; |
| 331 | } |
| 332 | |
| 333 | const double polynomial_minimization_start_time = WallTimeInSeconds(); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 334 | const double step_size = this->InterpolatingPolynomialMinimizingStepSize( |
| 335 | options().interpolation_type, |
| 336 | initial_position, |
| 337 | previous, |
| 338 | current, |
| 339 | (options().max_step_contraction * current.x), |
| 340 | (options().min_step_contraction * current.x)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 341 | summary->polynomial_minimization_time_in_seconds += |
| 342 | (WallTimeInSeconds() - polynomial_minimization_start_time); |
| 343 | |
| 344 | if (step_size * descent_direction_max_norm < options().min_step_size) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 345 | summary->error = StringPrintf( |
| 346 | "Line search failed: step_size too small: %.5e " |
| 347 | "with descent_direction_max_norm: %.5e.", |
| 348 | step_size, |
| 349 | descent_direction_max_norm); |
| 350 | if (!options().is_silent) { |
| 351 | LOG(WARNING) << summary->error; |
| 352 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
| 356 | previous = current; |
| 357 | |
| 358 | ++summary->num_function_evaluations; |
| 359 | if (kEvaluateGradient) { |
| 360 | ++summary->num_gradient_evaluations; |
| 361 | } |
| 362 | |
| 363 | function->Evaluate(step_size, kEvaluateGradient, ¤t); |
| 364 | } |
| 365 | |
| 366 | summary->optimal_point = current; |
| 367 | summary->success = true; |
| 368 | } |
| 369 | |
| 370 | WolfeLineSearch::WolfeLineSearch(const LineSearch::Options& options) |
| 371 | : LineSearch(options) {} |
| 372 | |
| 373 | void WolfeLineSearch::DoSearch(const double step_size_estimate, |
| 374 | const double initial_cost, |
| 375 | const double initial_gradient, |
| 376 | Summary* summary) const { |
| 377 | // All parameters should have been validated by the Solver, but as |
| 378 | // invalid values would produce crazy nonsense, hard check them here. |
| 379 | CHECK_GE(step_size_estimate, 0.0); |
| 380 | CHECK_GT(options().sufficient_decrease, 0.0); |
| 381 | CHECK_GT(options().sufficient_curvature_decrease, |
| 382 | options().sufficient_decrease); |
| 383 | CHECK_LT(options().sufficient_curvature_decrease, 1.0); |
| 384 | CHECK_GT(options().max_step_expansion, 1.0); |
| 385 | |
| 386 | // Note initial_cost & initial_gradient are evaluated at step_size = 0, |
| 387 | // not step_size_estimate, which is our starting guess. |
| 388 | FunctionSample initial_position(0.0, initial_cost, initial_gradient); |
| 389 | initial_position.vector_x = options().function->position(); |
| 390 | initial_position.vector_x_is_valid = true; |
| 391 | bool do_zoom_search = false; |
| 392 | // Important: The high/low in bracket_high & bracket_low refer to their |
| 393 | // _function_ values, not their step sizes i.e. it is _not_ required that |
| 394 | // bracket_low.x < bracket_high.x. |
| 395 | FunctionSample solution, bracket_low, bracket_high; |
| 396 | |
| 397 | // Wolfe bracketing phase: Increases step_size until either it finds a point |
| 398 | // that satisfies the (strong) Wolfe conditions, or an interval that brackets |
| 399 | // step sizes which satisfy the conditions. From Nocedal & Wright [1] p61 the |
| 400 | // interval: (step_size_{k-1}, step_size_{k}) contains step lengths satisfying |
| 401 | // the strong Wolfe conditions if one of the following conditions are met: |
| 402 | // |
| 403 | // 1. step_size_{k} violates the sufficient decrease (Armijo) condition. |
| 404 | // 2. f(step_size_{k}) >= f(step_size_{k-1}). |
| 405 | // 3. f'(step_size_{k}) >= 0. |
| 406 | // |
| 407 | // Caveat: If f(step_size_{k}) is invalid, then step_size is reduced, ignoring |
| 408 | // this special case, step_size monotonically increases during bracketing. |
| 409 | if (!this->BracketingPhase(initial_position, |
| 410 | step_size_estimate, |
| 411 | &bracket_low, |
| 412 | &bracket_high, |
| 413 | &do_zoom_search, |
| 414 | summary)) { |
| 415 | // Failed to find either a valid point, a valid bracket satisfying the Wolfe |
| 416 | // conditions, or even a step size > minimum tolerance satisfying the Armijo |
| 417 | // condition. |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | if (!do_zoom_search) { |
| 422 | // Either: Bracketing phase already found a point satisfying the strong |
| 423 | // Wolfe conditions, thus no Zoom required. |
| 424 | // |
| 425 | // Or: Bracketing failed to find a valid bracket or a point satisfying the |
| 426 | // strong Wolfe conditions within max_num_iterations, or whilst searching |
| 427 | // shrank the bracket width until it was below our minimum tolerance. |
| 428 | // As these are 'artificial' constraints, and we would otherwise fail to |
| 429 | // produce a valid point when ArmijoLineSearch would succeed, we return the |
| 430 | // point with the lowest cost found thus far which satsifies the Armijo |
| 431 | // condition (but not the Wolfe conditions). |
| 432 | summary->optimal_point = bracket_low; |
| 433 | summary->success = true; |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | VLOG(3) << std::scientific << std::setprecision(kErrorMessageNumericPrecision) |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 438 | << "Starting line search zoom phase with bracket_low: " << bracket_low |
| 439 | << ", bracket_high: " << bracket_high |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 440 | << ", bracket width: " << fabs(bracket_low.x - bracket_high.x) |
| 441 | << ", bracket abs delta cost: " |
| 442 | << fabs(bracket_low.value - bracket_high.value); |
| 443 | |
| 444 | // Wolfe Zoom phase: Called when the Bracketing phase finds an interval of |
| 445 | // non-zero, finite width that should bracket step sizes which satisfy the |
| 446 | // (strong) Wolfe conditions (before finding a step size that satisfies the |
| 447 | // conditions). Zoom successively decreases the size of the interval until a |
| 448 | // step size which satisfies the Wolfe conditions is found. The interval is |
| 449 | // defined by bracket_low & bracket_high, which satisfy: |
| 450 | // |
| 451 | // 1. The interval bounded by step sizes: bracket_low.x & bracket_high.x |
| 452 | // contains step sizes that satsify the strong Wolfe conditions. |
| 453 | // 2. bracket_low.x is of all the step sizes evaluated *which satisifed the |
| 454 | // Armijo sufficient decrease condition*, the one which generated the |
| 455 | // smallest function value, i.e. bracket_low.value < |
| 456 | // f(all other steps satisfying Armijo). |
| 457 | // - Note that this does _not_ (necessarily) mean that initially |
| 458 | // bracket_low.value < bracket_high.value (although this is typical) |
| 459 | // e.g. when bracket_low = initial_position, and bracket_high is the |
| 460 | // first sample, and which does not satisfy the Armijo condition, |
| 461 | // but still has bracket_high.value < initial_position.value. |
| 462 | // 3. bracket_high is chosen after bracket_low, s.t. |
| 463 | // bracket_low.gradient * (bracket_high.x - bracket_low.x) < 0. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 464 | if (!this->ZoomPhase( |
| 465 | initial_position, bracket_low, bracket_high, &solution, summary) && |
| 466 | !solution.value_is_valid) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 467 | // Failed to find a valid point (given the specified decrease parameters) |
| 468 | // within the specified bracket. |
| 469 | return; |
| 470 | } |
| 471 | // Ensure that if we ran out of iterations whilst zooming the bracket, or |
| 472 | // shrank the bracket width to < tolerance and failed to find a point which |
| 473 | // satisfies the strong Wolfe curvature condition, that we return the point |
| 474 | // amongst those found thus far, which minimizes f() and satisfies the Armijo |
| 475 | // condition. |
| 476 | |
| 477 | if (!solution.value_is_valid || solution.value > bracket_low.value) { |
| 478 | summary->optimal_point = bracket_low; |
| 479 | } else { |
| 480 | summary->optimal_point = solution; |
| 481 | } |
| 482 | |
| 483 | summary->success = true; |
| 484 | } |
| 485 | |
| 486 | // Returns true if either: |
| 487 | // |
| 488 | // A termination condition satisfying the (strong) Wolfe bracketing conditions |
| 489 | // is found: |
| 490 | // |
| 491 | // - A valid point, defined as a bracket of zero width [zoom not required]. |
| 492 | // - A valid bracket (of width > tolerance), [zoom required]. |
| 493 | // |
| 494 | // Or, searching was stopped due to an 'artificial' constraint, i.e. not |
| 495 | // a condition imposed / required by the underlying algorithm, but instead an |
| 496 | // engineering / implementation consideration. But a step which exceeds the |
| 497 | // minimum step size, and satsifies the Armijo condition was still found, |
| 498 | // and should thus be used [zoom not required]. |
| 499 | // |
| 500 | // Returns false if no step size > minimum step size was found which |
| 501 | // satisfies at least the Armijo condition. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 502 | bool WolfeLineSearch::BracketingPhase(const FunctionSample& initial_position, |
| 503 | const double step_size_estimate, |
| 504 | FunctionSample* bracket_low, |
| 505 | FunctionSample* bracket_high, |
| 506 | bool* do_zoom_search, |
| 507 | Summary* summary) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 508 | LineSearchFunction* function = options().function; |
| 509 | |
| 510 | FunctionSample previous = initial_position; |
| 511 | FunctionSample current; |
| 512 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 513 | const double descent_direction_max_norm = function->DirectionInfinityNorm(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 514 | |
| 515 | *do_zoom_search = false; |
| 516 | *bracket_low = initial_position; |
| 517 | |
| 518 | // As we require the gradient to evaluate the Wolfe condition, we always |
| 519 | // calculate it together with the value, irrespective of the interpolation |
| 520 | // type. As opposed to only calculating the gradient after the Armijo |
| 521 | // condition is satisifed, as the computational saving from this approach |
| 522 | // would be slight (perhaps even negative due to the extra call). Also, |
| 523 | // always calculating the value & gradient together protects against us |
| 524 | // reporting invalid solutions if the cost function returns slightly different |
| 525 | // function values when evaluated with / without gradients (due to numerical |
| 526 | // issues). |
| 527 | ++summary->num_function_evaluations; |
| 528 | ++summary->num_gradient_evaluations; |
| 529 | const bool kEvaluateGradient = true; |
| 530 | function->Evaluate(step_size_estimate, kEvaluateGradient, ¤t); |
| 531 | while (true) { |
| 532 | ++summary->num_iterations; |
| 533 | |
| 534 | if (current.value_is_valid && |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 535 | (current.value > (initial_position.value + |
| 536 | options().sufficient_decrease * |
| 537 | initial_position.gradient * current.x) || |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 538 | (previous.value_is_valid && current.value > previous.value))) { |
| 539 | // Bracket found: current step size violates Armijo sufficient decrease |
| 540 | // condition, or has stepped past an inflection point of f() relative to |
| 541 | // previous step size. |
| 542 | *do_zoom_search = true; |
| 543 | *bracket_low = previous; |
| 544 | *bracket_high = current; |
| 545 | VLOG(3) << std::scientific |
| 546 | << std::setprecision(kErrorMessageNumericPrecision) |
| 547 | << "Bracket found: current step (" << current.x |
| 548 | << ") violates Armijo sufficient condition, or has passed an " |
| 549 | << "inflection point of f() based on value."; |
| 550 | break; |
| 551 | } |
| 552 | |
| 553 | if (current.value_is_valid && |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 554 | fabs(current.gradient) <= -options().sufficient_curvature_decrease * |
| 555 | initial_position.gradient) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 556 | // Current step size satisfies the strong Wolfe conditions, and is thus a |
| 557 | // valid termination point, therefore a Zoom not required. |
| 558 | *bracket_low = current; |
| 559 | *bracket_high = current; |
| 560 | VLOG(3) << std::scientific |
| 561 | << std::setprecision(kErrorMessageNumericPrecision) |
| 562 | << "Bracketing phase found step size: " << current.x |
| 563 | << ", satisfying strong Wolfe conditions, initial_position: " |
| 564 | << initial_position << ", current: " << current; |
| 565 | break; |
| 566 | |
| 567 | } else if (current.value_is_valid && current.gradient >= 0) { |
| 568 | // Bracket found: current step size has stepped past an inflection point |
| 569 | // of f(), but Armijo sufficient decrease is still satisfied and |
| 570 | // f(current) is our best minimum thus far. Remember step size |
| 571 | // monotonically increases, thus previous_step_size < current_step_size |
| 572 | // even though f(previous) > f(current). |
| 573 | *do_zoom_search = true; |
| 574 | // Note inverse ordering from first bracket case. |
| 575 | *bracket_low = current; |
| 576 | *bracket_high = previous; |
| 577 | VLOG(3) << "Bracket found: current step (" << current.x |
| 578 | << ") satisfies Armijo, but has gradient >= 0, thus have passed " |
| 579 | << "an inflection point of f()."; |
| 580 | break; |
| 581 | |
| 582 | } else if (current.value_is_valid && |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 583 | fabs(current.x - previous.x) * descent_direction_max_norm < |
| 584 | options().min_step_size) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 585 | // We have shrunk the search bracket to a width less than our tolerance, |
| 586 | // and still not found either a point satisfying the strong Wolfe |
| 587 | // conditions, or a valid bracket containing such a point. Stop searching |
| 588 | // and set bracket_low to the size size amongst all those tested which |
| 589 | // minimizes f() and satisfies the Armijo condition. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 590 | |
| 591 | if (!options().is_silent) { |
| 592 | LOG(WARNING) << "Line search failed: Wolfe bracketing phase shrank " |
| 593 | << "bracket width: " << fabs(current.x - previous.x) |
| 594 | << ", to < tolerance: " << options().min_step_size |
| 595 | << ", with descent_direction_max_norm: " |
| 596 | << descent_direction_max_norm << ", and failed to find " |
| 597 | << "a point satisfying the strong Wolfe conditions or a " |
| 598 | << "bracketing containing such a point. Accepting " |
| 599 | << "point found satisfying Armijo condition only, to " |
| 600 | << "allow continuation."; |
| 601 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 602 | *bracket_low = current; |
| 603 | break; |
| 604 | |
| 605 | } else if (summary->num_iterations >= options().max_num_iterations) { |
| 606 | // Check num iterations bound here so that we always evaluate the |
| 607 | // max_num_iterations-th iteration against all conditions, and |
| 608 | // then perform no additional (unused) evaluations. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 609 | summary->error = StringPrintf( |
| 610 | "Line search failed: Wolfe bracketing phase failed to " |
| 611 | "find a point satisfying strong Wolfe conditions, or a " |
| 612 | "bracket containing such a point within specified " |
| 613 | "max_num_iterations: %d", |
| 614 | options().max_num_iterations); |
| 615 | if (!options().is_silent) { |
| 616 | LOG(WARNING) << summary->error; |
| 617 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 618 | // Ensure that bracket_low is always set to the step size amongst all |
| 619 | // those tested which minimizes f() and satisfies the Armijo condition |
| 620 | // when we terminate due to the 'artificial' max_num_iterations condition. |
| 621 | *bracket_low = |
| 622 | current.value_is_valid && current.value < bracket_low->value |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 623 | ? current |
| 624 | : *bracket_low; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 625 | break; |
| 626 | } |
| 627 | // Either: f(current) is invalid; or, f(current) is valid, but does not |
| 628 | // satisfy the strong Wolfe conditions itself, or the conditions for |
| 629 | // being a boundary of a bracket. |
| 630 | |
| 631 | // If f(current) is valid, (but meets no criteria) expand the search by |
| 632 | // increasing the step size. If f(current) is invalid, contract the step |
| 633 | // size. |
| 634 | // |
| 635 | // In Nocedal & Wright [1] (p60), the step-size can only increase in the |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 636 | // bracketing phase: step_size_{k+1} \in [step_size_k, step_size_k * |
| 637 | // factor]. However this does not account for the function returning invalid |
| 638 | // values which we support, in which case we need to contract the step size |
| 639 | // whilst ensuring that we do not invert the bracket, i.e, we require that: |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 640 | // step_size_{k-1} <= step_size_{k+1} < step_size_k. |
| 641 | const double min_step_size = |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 642 | current.value_is_valid ? current.x : previous.x; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 643 | const double max_step_size = |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 644 | current.value_is_valid ? (current.x * options().max_step_expansion) |
| 645 | : current.x; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 646 | |
| 647 | // We are performing 2-point interpolation only here, but the API of |
| 648 | // InterpolatingPolynomialMinimizingStepSize() allows for up to |
| 649 | // 3-point interpolation, so pad call with a sample with an invalid |
| 650 | // value that will therefore be ignored. |
| 651 | const FunctionSample unused_previous; |
| 652 | DCHECK(!unused_previous.value_is_valid); |
| 653 | // Contracts step size if f(current) is not valid. |
| 654 | const double polynomial_minimization_start_time = WallTimeInSeconds(); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 655 | const double step_size = this->InterpolatingPolynomialMinimizingStepSize( |
| 656 | options().interpolation_type, |
| 657 | previous, |
| 658 | unused_previous, |
| 659 | current, |
| 660 | min_step_size, |
| 661 | max_step_size); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 662 | summary->polynomial_minimization_time_in_seconds += |
| 663 | (WallTimeInSeconds() - polynomial_minimization_start_time); |
| 664 | if (step_size * descent_direction_max_norm < options().min_step_size) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 665 | summary->error = StringPrintf( |
| 666 | "Line search failed: step_size too small: %.5e " |
| 667 | "with descent_direction_max_norm: %.5e", |
| 668 | step_size, |
| 669 | descent_direction_max_norm); |
| 670 | if (!options().is_silent) { |
| 671 | LOG(WARNING) << summary->error; |
| 672 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 673 | return false; |
| 674 | } |
| 675 | |
| 676 | // Only advance the lower boundary (in x) of the bracket if f(current) |
| 677 | // is valid such that we can support contracting the step size when |
| 678 | // f(current) is invalid without risking inverting the bracket in x, i.e. |
| 679 | // prevent previous.x > current.x. |
| 680 | previous = current.value_is_valid ? current : previous; |
| 681 | ++summary->num_function_evaluations; |
| 682 | ++summary->num_gradient_evaluations; |
| 683 | function->Evaluate(step_size, kEvaluateGradient, ¤t); |
| 684 | } |
| 685 | |
| 686 | // Ensure that even if a valid bracket was found, we will only mark a zoom |
| 687 | // as required if the bracket's width is greater than our minimum tolerance. |
| 688 | if (*do_zoom_search && |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 689 | fabs(bracket_high->x - bracket_low->x) * descent_direction_max_norm < |
| 690 | options().min_step_size) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 691 | *do_zoom_search = false; |
| 692 | } |
| 693 | |
| 694 | return true; |
| 695 | } |
| 696 | |
| 697 | // Returns true iff solution satisfies the strong Wolfe conditions. Otherwise, |
| 698 | // on return false, if we stopped searching due to the 'artificial' condition of |
| 699 | // reaching max_num_iterations, solution is the step size amongst all those |
| 700 | // tested, which satisfied the Armijo decrease condition and minimized f(). |
| 701 | bool WolfeLineSearch::ZoomPhase(const FunctionSample& initial_position, |
| 702 | FunctionSample bracket_low, |
| 703 | FunctionSample bracket_high, |
| 704 | FunctionSample* solution, |
| 705 | Summary* summary) const { |
| 706 | LineSearchFunction* function = options().function; |
| 707 | |
| 708 | CHECK(bracket_low.value_is_valid && bracket_low.gradient_is_valid) |
| 709 | << std::scientific << std::setprecision(kErrorMessageNumericPrecision) |
| 710 | << "Ceres bug: f_low input to Wolfe Zoom invalid, please contact " |
| 711 | << "the developers!, initial_position: " << initial_position |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 712 | << ", bracket_low: " << bracket_low << ", bracket_high: " << bracket_high; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 713 | // We do not require bracket_high.gradient_is_valid as the gradient condition |
| 714 | // for a valid bracket is only dependent upon bracket_low.gradient, and |
| 715 | // in order to minimize jacobian evaluations, bracket_high.gradient may |
| 716 | // not have been calculated (if bracket_high.value does not satisfy the |
| 717 | // Armijo sufficient decrease condition and interpolation method does not |
| 718 | // require it). |
| 719 | // |
| 720 | // We also do not require that: bracket_low.value < bracket_high.value, |
| 721 | // although this is typical. This is to deal with the case when |
| 722 | // bracket_low = initial_position, bracket_high is the first sample, |
| 723 | // and bracket_high does not satisfy the Armijo condition, but still has |
| 724 | // bracket_high.value < initial_position.value. |
| 725 | CHECK(bracket_high.value_is_valid) |
| 726 | << std::scientific << std::setprecision(kErrorMessageNumericPrecision) |
| 727 | << "Ceres bug: f_high input to Wolfe Zoom invalid, please " |
| 728 | << "contact the developers!, initial_position: " << initial_position |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 729 | << ", bracket_low: " << bracket_low << ", bracket_high: " << bracket_high; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 730 | |
| 731 | if (bracket_low.gradient * (bracket_high.x - bracket_low.x) >= 0) { |
| 732 | // The third condition for a valid initial bracket: |
| 733 | // |
| 734 | // 3. bracket_high is chosen after bracket_low, s.t. |
| 735 | // bracket_low.gradient * (bracket_high.x - bracket_low.x) < 0. |
| 736 | // |
| 737 | // is not satisfied. As this can happen when the users' cost function |
| 738 | // returns inconsistent gradient values relative to the function values, |
| 739 | // we do not CHECK_LT(), but we do stop processing and return an invalid |
| 740 | // value. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 741 | summary->error = StringPrintf( |
| 742 | "Line search failed: Wolfe zoom phase passed a bracket " |
| 743 | "which does not satisfy: bracket_low.gradient * " |
| 744 | "(bracket_high.x - bracket_low.x) < 0 [%.8e !< 0] " |
| 745 | "with initial_position: %s, bracket_low: %s, bracket_high:" |
| 746 | " %s, the most likely cause of which is the cost function " |
| 747 | "returning inconsistent gradient & function values.", |
| 748 | bracket_low.gradient * (bracket_high.x - bracket_low.x), |
| 749 | initial_position.ToDebugString().c_str(), |
| 750 | bracket_low.ToDebugString().c_str(), |
| 751 | bracket_high.ToDebugString().c_str()); |
| 752 | if (!options().is_silent) { |
| 753 | LOG(WARNING) << summary->error; |
| 754 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 755 | solution->value_is_valid = false; |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | const int num_bracketing_iterations = summary->num_iterations; |
| 760 | const double descent_direction_max_norm = function->DirectionInfinityNorm(); |
| 761 | |
| 762 | while (true) { |
| 763 | // Set solution to bracket_low, as it is our best step size (smallest f()) |
| 764 | // found thus far and satisfies the Armijo condition, even though it does |
| 765 | // not satisfy the Wolfe condition. |
| 766 | *solution = bracket_low; |
| 767 | if (summary->num_iterations >= options().max_num_iterations) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 768 | summary->error = StringPrintf( |
| 769 | "Line search failed: Wolfe zoom phase failed to " |
| 770 | "find a point satisfying strong Wolfe conditions " |
| 771 | "within specified max_num_iterations: %d, " |
| 772 | "(num iterations taken for bracketing: %d).", |
| 773 | options().max_num_iterations, |
| 774 | num_bracketing_iterations); |
| 775 | if (!options().is_silent) { |
| 776 | LOG(WARNING) << summary->error; |
| 777 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 778 | return false; |
| 779 | } |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 780 | if (fabs(bracket_high.x - bracket_low.x) * descent_direction_max_norm < |
| 781 | options().min_step_size) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 782 | // Bracket width has been reduced below tolerance, and no point satisfying |
| 783 | // the strong Wolfe conditions has been found. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 784 | summary->error = StringPrintf( |
| 785 | "Line search failed: Wolfe zoom bracket width: %.5e " |
| 786 | "too small with descent_direction_max_norm: %.5e.", |
| 787 | fabs(bracket_high.x - bracket_low.x), |
| 788 | descent_direction_max_norm); |
| 789 | if (!options().is_silent) { |
| 790 | LOG(WARNING) << summary->error; |
| 791 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 792 | return false; |
| 793 | } |
| 794 | |
| 795 | ++summary->num_iterations; |
| 796 | // Polynomial interpolation requires inputs ordered according to step size, |
| 797 | // not f(step size). |
| 798 | const FunctionSample& lower_bound_step = |
| 799 | bracket_low.x < bracket_high.x ? bracket_low : bracket_high; |
| 800 | const FunctionSample& upper_bound_step = |
| 801 | bracket_low.x < bracket_high.x ? bracket_high : bracket_low; |
| 802 | // We are performing 2-point interpolation only here, but the API of |
| 803 | // InterpolatingPolynomialMinimizingStepSize() allows for up to |
| 804 | // 3-point interpolation, so pad call with a sample with an invalid |
| 805 | // value that will therefore be ignored. |
| 806 | const FunctionSample unused_previous; |
| 807 | DCHECK(!unused_previous.value_is_valid); |
| 808 | const double polynomial_minimization_start_time = WallTimeInSeconds(); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 809 | const double step_size = this->InterpolatingPolynomialMinimizingStepSize( |
| 810 | options().interpolation_type, |
| 811 | lower_bound_step, |
| 812 | unused_previous, |
| 813 | upper_bound_step, |
| 814 | lower_bound_step.x, |
| 815 | upper_bound_step.x); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 816 | summary->polynomial_minimization_time_in_seconds += |
| 817 | (WallTimeInSeconds() - polynomial_minimization_start_time); |
| 818 | // No check on magnitude of step size being too small here as it is |
| 819 | // lower-bounded by the initial bracket start point, which was valid. |
| 820 | // |
| 821 | // As we require the gradient to evaluate the Wolfe condition, we always |
| 822 | // calculate it together with the value, irrespective of the interpolation |
| 823 | // type. As opposed to only calculating the gradient after the Armijo |
| 824 | // condition is satisifed, as the computational saving from this approach |
| 825 | // would be slight (perhaps even negative due to the extra call). Also, |
| 826 | // always calculating the value & gradient together protects against us |
| 827 | // reporting invalid solutions if the cost function returns slightly |
| 828 | // different function values when evaluated with / without gradients (due |
| 829 | // to numerical issues). |
| 830 | ++summary->num_function_evaluations; |
| 831 | ++summary->num_gradient_evaluations; |
| 832 | const bool kEvaluateGradient = true; |
| 833 | function->Evaluate(step_size, kEvaluateGradient, solution); |
| 834 | if (!solution->value_is_valid || !solution->gradient_is_valid) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 835 | summary->error = StringPrintf( |
| 836 | "Line search failed: Wolfe Zoom phase found " |
| 837 | "step_size: %.5e, for which function is invalid, " |
| 838 | "between low_step: %.5e and high_step: %.5e " |
| 839 | "at which function is valid.", |
| 840 | solution->x, |
| 841 | bracket_low.x, |
| 842 | bracket_high.x); |
| 843 | if (!options().is_silent) { |
| 844 | LOG(WARNING) << summary->error; |
| 845 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 846 | return false; |
| 847 | } |
| 848 | |
| 849 | VLOG(3) << "Zoom iteration: " |
| 850 | << summary->num_iterations - num_bracketing_iterations |
| 851 | << ", bracket_low: " << bracket_low |
| 852 | << ", bracket_high: " << bracket_high |
| 853 | << ", minimizing solution: " << *solution; |
| 854 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 855 | if ((solution->value > (initial_position.value + |
| 856 | options().sufficient_decrease * |
| 857 | initial_position.gradient * solution->x)) || |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 858 | (solution->value >= bracket_low.value)) { |
| 859 | // Armijo sufficient decrease not satisfied, or not better |
| 860 | // than current lowest sample, use as new upper bound. |
| 861 | bracket_high = *solution; |
| 862 | continue; |
| 863 | } |
| 864 | |
| 865 | // Armijo sufficient decrease satisfied, check strong Wolfe condition. |
| 866 | if (fabs(solution->gradient) <= |
| 867 | -options().sufficient_curvature_decrease * initial_position.gradient) { |
| 868 | // Found a valid termination point satisfying strong Wolfe conditions. |
| 869 | VLOG(3) << std::scientific |
| 870 | << std::setprecision(kErrorMessageNumericPrecision) |
| 871 | << "Zoom phase found step size: " << solution->x |
| 872 | << ", satisfying strong Wolfe conditions."; |
| 873 | break; |
| 874 | |
| 875 | } else if (solution->gradient * (bracket_high.x - bracket_low.x) >= 0) { |
| 876 | bracket_high = bracket_low; |
| 877 | } |
| 878 | |
| 879 | bracket_low = *solution; |
| 880 | } |
| 881 | // Solution contains a valid point which satisfies the strong Wolfe |
| 882 | // conditions. |
| 883 | return true; |
| 884 | } |
| 885 | |
| 886 | } // namespace internal |
| 887 | } // namespace ceres |