blob: 6a21cbeac11e3de356201f690fc41651b42245a4 [file] [log] [blame]
Sameer Agarwal1d11be92012-11-25 19:28:06 -08001// Ceres Solver - A fast non-linear least squares minimizer
Keir Mierle7492b0d2015-03-17 22:30:16 -07002// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
Sameer Agarwal1d11be92012-11-25 19:28:06 -08004//
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 Stewart9aa0e3c2013-07-05 20:22:37 +010036#include <string>
Sameer Agarwal1d11be92012-11-25 19:28:06 -080037#include <vector>
38#include "ceres/internal/eigen.h"
39#include "ceres/internal/port.h"
Sameer Agarwal09244012013-06-30 14:33:23 -070040#include "ceres/types.h"
Sameer Agarwal1d11be92012-11-25 19:28:06 -080041
42namespace ceres {
43namespace internal {
44
45class Evaluator;
Alex Stewart9aa0e3c2013-07-05 20:22:37 +010046struct FunctionSample;
Alex Stewart9ad59a72014-11-17 22:20:46 +000047class LineSearchFunction;
Sameer Agarwal1d11be92012-11-25 19:28:06 -080048
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.
59class LineSearch {
60 public:
Alex Stewart9ad59a72014-11-17 22:20:46 +000061 struct Summary;
Sameer Agarwal1d11be92012-11-25 19:28:06 -080062
63 struct Options {
64 Options()
Sameer Agarwal09244012013-06-30 14:33:23 -070065 : interpolation_type(CUBIC),
Sameer Agarwal1d11be92012-11-25 19:28:06 -080066 sufficient_decrease(1e-4),
Alex Stewart9aa0e3c2013-07-05 20:22:37 +010067 max_step_contraction(1e-3),
68 min_step_contraction(0.9),
Sameer Agarwal09244012013-06-30 14:33:23 -070069 min_step_size(1e-9),
Alex Stewart9aa0e3c2013-07-05 20:22:37 +010070 max_num_iterations(20),
71 sufficient_curvature_decrease(0.9),
72 max_step_expansion(10.0),
Sameer Agarwal3dd7ed72014-02-24 22:01:45 -080073 is_silent(false),
Sameer Agarwal1d11be92012-11-25 19:28:06 -080074 function(NULL) {}
75
Sameer Agarwal1d11be92012-11-25 19:28:06 -080076 // Degree of the polynomial used to approximate the objective
Sameer Agarwal09244012013-06-30 14:33:23 -070077 // function.
78 LineSearchInterpolationType interpolation_type;
Sameer Agarwal1d11be92012-11-25 19:28:06 -080079
Alex Stewart9aa0e3c2013-07-05 20:22:37 +010080 // Armijo and Wolfe line search parameters.
Sameer Agarwal1d11be92012-11-25 19:28:06 -080081
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 Stewart9aa0e3c2013-07-05 20:22:37 +010093 // In each iteration of the Armijo / Wolfe line search,
Sameer Agarwal1d11be92012-11-25 19:28:06 -080094 //
Alex Stewart9aa0e3c2013-07-05 20:22:37 +010095 // 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 Agarwal1d11be92012-11-25 19:28:06 -0800102
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100103 // In each iteration of the Armijo / Wolfe line search,
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800104 //
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100105 // 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 Agarwal1d11be92012-11-25 19:28:06 -0800111
112 // If during the line search, the step_size falls below this
113 // value, it is truncated to zero.
Sameer Agarwal09244012013-06-30 14:33:23 -0700114 double min_step_size;
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800115
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100116 // 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 Agarwal3dd7ed72014-02-24 22:01:45 -0800147 bool is_silent;
148
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800149 // The one dimensional function that the line search algorithm
150 // minimizes.
Alex Stewart9ad59a72014-11-17 22:20:46 +0000151 LineSearchFunction* function;
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800152 };
153
154 // Result of the line search.
155 struct Summary {
156 Summary()
157 : success(false),
158 optimal_step_size(0.0),
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100159 num_function_evaluations(0),
160 num_gradient_evaluations(0),
Alex Stewart9ad59a72014-11-17 22:20:46 +0000161 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 Agarwal1d11be92012-11-25 19:28:06 -0800166
167 bool success;
168 double optimal_step_size;
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100169 int num_function_evaluations;
170 int num_gradient_evaluations;
171 int num_iterations;
Alex Stewart9ad59a72014-11-17 22:20:46 +0000172 // 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 Agarwal05a07ec2015-01-07 15:10:46 -0800182 std::string error;
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800183 };
184
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100185 explicit LineSearch(const LineSearch::Options& options);
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800186 virtual ~LineSearch() {}
187
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100188 static LineSearch* Create(const LineSearchType line_search_type,
189 const LineSearch::Options& options,
Sameer Agarwal05a07ec2015-01-07 15:10:46 -0800190 std::string* error);
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100191
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800192 // Perform the line search.
193 //
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100194 // step_size_estimate must be a positive number.
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800195 //
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 Agarwal1d11be92012-11-25 19:28:06 -0800200 //
201 // Summary::success is true if a non-zero step size is found.
Alex Stewart9ad59a72014-11-17 22:20:46 +0000202 void Search(double step_size_estimate,
203 double initial_cost,
204 double initial_gradient,
205 Summary* summary) const;
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100206 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 Stewart9ad59a72014-11-17 22:20:46 +0000218 virtual void DoSearch(double step_size_estimate,
219 double initial_cost,
220 double initial_gradient,
221 Summary* summary) const = 0;
222
223 private:
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100224 LineSearch::Options options_;
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800225};
226
Alex Stewart9ad59a72014-11-17 22:20:46 +0000227// 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.
233class LineSearchFunction {
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800234 public:
235 explicit LineSearchFunction(Evaluator* evaluator);
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800236 void Init(const Vector& position, const Vector& direction);
Alex Stewart9ad59a72014-11-17 22:20:46 +0000237 // 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 Stewart9aa0e3c2013-07-05 20:22:37 +0100248 double DirectionInfinityNorm() const;
Alex Stewart9ad59a72014-11-17 22:20:46 +0000249 // 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 Agarwal1d11be92012-11-25 19:28:06 -0800253
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 Stewart9ad59a72014-11-17 22:20:46 +0000265
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 Agarwal1d11be92012-11-25 19:28:06 -0800272};
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
279class ArmijoLineSearch : public LineSearch {
280 public:
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100281 explicit ArmijoLineSearch(const LineSearch::Options& options);
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800282 virtual ~ArmijoLineSearch() {}
Alex Stewart9ad59a72014-11-17 22:20:46 +0000283
284 private:
285 virtual void DoSearch(double step_size_estimate,
286 double initial_cost,
287 double initial_gradient,
288 Summary* summary) const;
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800289};
290
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100291// 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.
298class WolfeLineSearch : public LineSearch {
299 public:
300 explicit WolfeLineSearch(const LineSearch::Options& options);
301 virtual ~WolfeLineSearch() {}
Alex Stewart9ad59a72014-11-17 22:20:46 +0000302
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100303 // 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 Stewart9ad59a72014-11-17 22:20:46 +0000309 Summary* summary) const;
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100310 // 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 Stewart9ad59a72014-11-17 22:20:46 +0000315 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 Stewart9aa0e3c2013-07-05 20:22:37 +0100322};
323
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800324} // namespace internal
325} // namespace ceres
326
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800327#endif // CERES_INTERNAL_LINE_SEARCH_H_