blob: 95bf56e2a6b9dbaf3927e8be81a3548d81fdb800 [file] [log] [blame]
Sameer Agarwal1d11be92012-11-25 19:28:06 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: sameeragarwal@google.com (Sameer Agarwal)
30//
31// Interface for and implementation of various Line search algorithms.
32
33#ifndef CERES_INTERNAL_LINE_SEARCH_H_
34#define CERES_INTERNAL_LINE_SEARCH_H_
35
Sameer Agarwal8140f0f2013-03-12 09:45:08 -070036#ifndef CERES_NO_LINE_SEARCH_MINIMIZER
37
Sameer Agarwal1d11be92012-11-25 19:28:06 -080038#include <glog/logging.h>
39#include <vector>
40#include "ceres/internal/eigen.h"
41#include "ceres/internal/port.h"
42
43namespace ceres {
44namespace internal {
45
46class Evaluator;
47
48// Line search is another name for a one dimensional optimization
49// algorithm. The name "line search" comes from the fact one
50// dimensional optimization problems that arise as subproblems of
51// general multidimensional optimization problems.
52//
53// While finding the exact minimum of a one dimensionl function is
54// hard, instances of LineSearch find a point that satisfies a
55// sufficient decrease condition. Depending on the particular
56// condition used, we get a variety of different line search
57// algorithms, e.g., Armijo, Wolfe etc.
58class LineSearch {
59 public:
60 class Function;
61
62 struct Options {
63 Options()
64 : interpolation_degree(1),
65 use_higher_degree_interpolation_when_possible(false),
66 sufficient_decrease(1e-4),
67 min_relative_step_size_change(1e-3),
68 max_relative_step_size_change(0.6),
69 step_size_threshold(1e-9),
70 function(NULL) {}
71
72 // TODO(sameeragarwal): Replace this with enums which are common
73 // across various line searches.
74 //
75 // Degree of the polynomial used to approximate the objective
76 // function. Valid values are {0, 1, 2}.
77 //
78 // For Armijo line search
79 //
80 // 0: Bisection based backtracking search.
81 // 1: Quadratic interpolation.
82 // 2: Cubic interpolation.
83 int interpolation_degree;
84
85 // Usually its possible to increase the degree of the
86 // interpolation polynomial by storing and using an extra point.
87 bool use_higher_degree_interpolation_when_possible;
88
89 // Armijo line search parameters.
90
91 // Solving the line search problem exactly is computationally
92 // prohibitive. Fortunately, line search based optimization
93 // algorithms can still guarantee convergence if instead of an
94 // exact solution, the line search algorithm returns a solution
95 // which decreases the value of the objective function
96 // sufficiently. More precisely, we are looking for a step_size
97 // s.t.
98 //
99 // f(step_size) <= f(0) + sufficient_decrease * f'(0) * step_size
100 double sufficient_decrease;
101
102 // In each iteration of the Armijo line search,
103 //
104 // new_step_size >= min_relative_step_size_change * step_size
105 double min_relative_step_size_change;
106
107 // In each iteration of the Armijo line search,
108 //
109 // new_step_size <= max_relative_step_size_change * step_size
110 double max_relative_step_size_change;
111
112 // If during the line search, the step_size falls below this
113 // value, it is truncated to zero.
114 double step_size_threshold;
115
116 // The one dimensional function that the line search algorithm
117 // minimizes.
118 Function* function;
119 };
120
121 // An object used by the line search to access the function values
122 // and gradient of the one dimensional function being optimized.
123 //
124 // In practice, this object will provide access to the objective
125 // function value and the directional derivative of the underlying
126 // optimization problem along a specific search direction.
127 //
128 // See LineSearchFunction for an example implementation.
129 class Function {
130 public:
131 virtual ~Function() {}
132 // Evaluate the line search objective
133 //
134 // f(x) = p(position + x * direction)
135 //
136 // Where, p is the objective function of the general optimization
137 // problem.
138 //
139 // g is the gradient f'(x) at x.
140 //
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800141 // f must not be null. The gradient is computed only if g is not null.
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800142 virtual bool Evaluate(double x, double* f, double* g) = 0;
143 };
144
145 // Result of the line search.
146 struct Summary {
147 Summary()
148 : success(false),
149 optimal_step_size(0.0),
150 num_evaluations(0) {}
151
152 bool success;
153 double optimal_step_size;
154 int num_evaluations;
155 };
156
157 virtual ~LineSearch() {}
158
159 // Perform the line search.
160 //
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800161 // initial_step_size must be a positive number.
162 //
163 // initial_cost and initial_gradient are the values and gradient of
164 // the function at zero.
165 // summary must not be null and will contain the result of the line
166 // search.
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800167 //
168 // Summary::success is true if a non-zero step size is found.
169 virtual void Search(const LineSearch::Options& options,
170 double initial_step_size,
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800171 double initial_cost,
172 double initial_gradient,
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800173 Summary* summary) = 0;
174};
175
176class LineSearchFunction : public LineSearch::Function {
177 public:
178 explicit LineSearchFunction(Evaluator* evaluator);
179 virtual ~LineSearchFunction() {}
180 void Init(const Vector& position, const Vector& direction);
181 virtual bool Evaluate(const double x, double* f, double* g);
182
183 private:
184 Evaluator* evaluator_;
185 Vector position_;
186 Vector direction_;
187
188 // evaluation_point = Evaluator::Plus(position_, x * direction_);
189 Vector evaluation_point_;
190
191 // scaled_direction = x * direction_;
192 Vector scaled_direction_;
193 Vector gradient_;
194};
195
196// Backtracking and interpolation based Armijo line search. This
197// implementation is based on the Armijo line search that ships in the
198// minFunc package by Mark Schmidt.
199//
200// For more details: http://www.di.ens.fr/~mschmidt/Software/minFunc.html
201class ArmijoLineSearch : public LineSearch {
202 public:
203 virtual ~ArmijoLineSearch() {}
204 virtual void Search(const LineSearch::Options& options,
205 double initial_step_size,
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800206 double initial_cost,
207 double initial_gradient,
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800208 Summary* summary);
209};
210
211} // namespace internal
212} // namespace ceres
213
Sameer Agarwal8140f0f2013-03-12 09:45:08 -0700214#endif // CERES_NO_LINE_SEARCH_MINIMIZER
Sameer Agarwal1d11be92012-11-25 19:28:06 -0800215#endif // CERES_INTERNAL_LINE_SEARCH_H_