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