blob: ca7d639c5efbbf4e566609759c0566e420a8f606 [file] [log] [blame]
Sameer Agarwalf4d01642012-11-26 12:55:58 -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// Generic loop for line search based optimization algorithms.
32//
33// This is primarily inpsired by the minFunc packaged written by Mark
34// Schmidt.
35//
36// http://www.di.ens.fr/~mschmidt/Software/minFunc.html
37//
38// For details on the theory and implementation see "Numerical
39// Optimization" by Nocedal & Wright.
40
41#include "ceres/line_search_minimizer.h"
42
43#include <algorithm>
44#include <cstdlib>
45#include <cmath>
Sameer Agarwalf4d01642012-11-26 12:55:58 -080046#include <string>
47#include <vector>
Sameer Agarwalf4d01642012-11-26 12:55:58 -080048
49#include "Eigen/Dense"
50#include "ceres/array_utils.h"
51#include "ceres/evaluator.h"
52#include "ceres/internal/eigen.h"
Sameer Agarwal9883fc32012-11-30 12:32:43 -080053#include "ceres/internal/port.h"
Sameer Agarwalf4d01642012-11-26 12:55:58 -080054#include "ceres/internal/scoped_ptr.h"
55#include "ceres/line_search.h"
Sameer Agarwal9883fc32012-11-30 12:32:43 -080056#include "ceres/line_search_direction.h"
Sameer Agarwalf4d01642012-11-26 12:55:58 -080057#include "ceres/stringprintf.h"
58#include "ceres/types.h"
59#include "ceres/wall_time.h"
60#include "glog/logging.h"
61
62namespace ceres {
63namespace internal {
64namespace {
65// Small constant for various floating point issues.
Sameer Agarwalc89ea4b2013-01-09 16:09:35 -080066// TODO(sameeragarwal): Change to a better name if this has only one
67// use.
Sameer Agarwalf4d01642012-11-26 12:55:58 -080068const double kEpsilon = 1e-12;
Sameer Agarwalc89ea4b2013-01-09 16:09:35 -080069
Sameer Agarwal9883fc32012-11-30 12:32:43 -080070bool Evaluate(Evaluator* evaluator,
71 const Vector& x,
72 LineSearchMinimizer::State* state) {
73 const bool status = evaluator->Evaluate(x.data(),
74 &(state->cost),
75 NULL,
76 state->gradient.data(),
77 NULL);
78 if (status) {
79 state->gradient_squared_norm = state->gradient.squaredNorm();
80 state->gradient_max_norm = state->gradient.lpNorm<Eigen::Infinity>();
Sameer Agarwalf4d01642012-11-26 12:55:58 -080081 }
Sameer Agarwal9883fc32012-11-30 12:32:43 -080082
83 return status;
Sameer Agarwalf4d01642012-11-26 12:55:58 -080084}
85
Sameer Agarwal9883fc32012-11-30 12:32:43 -080086} // namespace
Sameer Agarwalf4d01642012-11-26 12:55:58 -080087
88void LineSearchMinimizer::Minimize(const Minimizer::Options& options,
89 double* parameters,
90 Solver::Summary* summary) {
91 double start_time = WallTimeInSeconds();
92 double iteration_start_time = start_time;
Sameer Agarwalf4d01642012-11-26 12:55:58 -080093
Sameer Agarwal9883fc32012-11-30 12:32:43 -080094 Evaluator* evaluator = CHECK_NOTNULL(options.evaluator);
Sameer Agarwalf4d01642012-11-26 12:55:58 -080095 const int num_parameters = evaluator->NumParameters();
96 const int num_effective_parameters = evaluator->NumEffectiveParameters();
97
98 summary->termination_type = NO_CONVERGENCE;
99 summary->num_successful_steps = 0;
100 summary->num_unsuccessful_steps = 0;
101
102 VectorRef x(parameters, num_parameters);
103
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800104 State current_state(num_parameters, num_effective_parameters);
105 State previous_state(num_parameters, num_effective_parameters);
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800106
107 Vector delta(num_effective_parameters);
108 Vector x_plus_delta(num_parameters);
109
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800110 IterationSummary iteration_summary;
111 iteration_summary.iteration = 0;
112 iteration_summary.step_is_valid = false;
113 iteration_summary.step_is_successful = false;
114 iteration_summary.cost_change = 0.0;
115 iteration_summary.gradient_max_norm = 0.0;
116 iteration_summary.step_norm = 0.0;
117 iteration_summary.linear_solver_iterations = 0;
118 iteration_summary.step_solver_time_in_seconds = 0;
119
120 // Do initial cost and Jacobian evaluation.
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800121 if (!Evaluate(evaluator, x, &current_state)) {
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800122 LOG(WARNING) << "Terminating: Cost and gradient evaluation failed.";
123 summary->termination_type = NUMERICAL_FAILURE;
124 return;
125 }
126
Sameer Agarwalf102a682013-02-11 15:08:40 -0800127 summary->initial_cost = current_state.cost + summary->fixed_cost;
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800128 iteration_summary.cost = current_state.cost + summary->fixed_cost;
Sameer Agarwalf102a682013-02-11 15:08:40 -0800129
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800130 iteration_summary.gradient_max_norm = current_state.gradient_max_norm;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800131
132 // The initial gradient max_norm is bounded from below so that we do
133 // not divide by zero.
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800134 const double initial_gradient_max_norm =
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800135 max(iteration_summary.gradient_max_norm, kEpsilon);
136 const double absolute_gradient_tolerance =
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800137 options.gradient_tolerance * initial_gradient_max_norm;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800138
139 if (iteration_summary.gradient_max_norm <= absolute_gradient_tolerance) {
140 summary->termination_type = GRADIENT_TOLERANCE;
141 VLOG(1) << "Terminating: Gradient tolerance reached."
142 << "Relative gradient max norm: "
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800143 << iteration_summary.gradient_max_norm / initial_gradient_max_norm
144 << " <= " << options.gradient_tolerance;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800145 return;
146 }
147
148 iteration_summary.iteration_time_in_seconds =
149 WallTimeInSeconds() - iteration_start_time;
150 iteration_summary.cumulative_time_in_seconds =
151 WallTimeInSeconds() - start_time
152 + summary->preprocessor_time_in_seconds;
153 summary->iterations.push_back(iteration_summary);
154
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800155 LineSearchDirection::Options line_search_direction_options;
156 line_search_direction_options.num_parameters = num_effective_parameters;
157 line_search_direction_options.type = options.line_search_direction_type;
158 line_search_direction_options.nonlinear_conjugate_gradient_type =
159 options.nonlinear_conjugate_gradient_type;
160 line_search_direction_options.max_lbfgs_rank = options.max_lbfgs_rank;
161 scoped_ptr<LineSearchDirection> line_search_direction(
162 LineSearchDirection::Create(line_search_direction_options));
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800163
164 LineSearchFunction line_search_function(evaluator);
165 LineSearch::Options line_search_options;
166 line_search_options.function = &line_search_function;
167
168 // TODO(sameeragarwal): Make this parameterizable over different
169 // line searches.
170 ArmijoLineSearch line_search;
171 LineSearch::Summary line_search_summary;
172
173 while (true) {
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800174 if (!RunCallbacks(options.callbacks, iteration_summary, summary)) {
175 return;
176 }
177
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800178 iteration_start_time = WallTimeInSeconds();
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800179 if (iteration_summary.iteration >= options.max_num_iterations) {
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800180 summary->termination_type = NO_CONVERGENCE;
181 VLOG(1) << "Terminating: Maximum number of iterations reached.";
182 break;
183 }
184
185 const double total_solver_time = iteration_start_time - start_time +
186 summary->preprocessor_time_in_seconds;
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800187 if (total_solver_time >= options.max_solver_time_in_seconds) {
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800188 summary->termination_type = NO_CONVERGENCE;
189 VLOG(1) << "Terminating: Maximum solver time reached.";
190 break;
191 }
192
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800193 iteration_summary = IterationSummary();
194 iteration_summary.iteration = summary->iterations.back().iteration + 1;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800195
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800196 bool line_search_status = true;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800197 if (iteration_summary.iteration == 1) {
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800198 current_state.search_direction = -current_state.gradient;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800199 } else {
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800200 line_search_status = line_search_direction->NextDirection(
201 previous_state,
202 current_state,
203 &current_state.search_direction);
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800204 }
205
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800206 if (!line_search_status) {
207 LOG(WARNING) << "Line search direction computation failed. "
208 "Resorting to steepest descent.";
209 current_state.search_direction = -current_state.gradient;
210 }
211
212 line_search_function.Init(x, current_state.search_direction);
213 current_state.directional_derivative =
214 current_state.gradient.dot(current_state.search_direction);
215
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800216 // TODO(sameeragarwal): Refactor this into its own object and add
217 // explanations for the various choices.
218 const double initial_step_size = (iteration_summary.iteration == 1)
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800219 ? min(1.0, 1.0 / current_state.gradient_max_norm)
220 : min(1.0, 2.0 * (current_state.cost - previous_state.cost) /
221 current_state.directional_derivative);
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800222
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800223 line_search.Search(line_search_options,
224 initial_step_size,
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800225 current_state.cost,
226 current_state.directional_derivative,
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800227 &line_search_summary);
228
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800229 current_state.step_size = line_search_summary.optimal_step_size;
230 delta = current_state.step_size * current_state.search_direction;
231
232 previous_state = current_state;
233
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800234 // TODO(sameeragarwal): Collect stats.
235 if (!evaluator->Plus(x.data(), delta.data(), x_plus_delta.data()) ||
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800236 !Evaluate(evaluator, x_plus_delta, &current_state)) {
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800237 LOG(WARNING) << "Evaluation failed.";
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800238 } else {
239 x = x_plus_delta;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800240 }
241
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800242 iteration_summary.gradient_max_norm = current_state.gradient_max_norm;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800243 if (iteration_summary.gradient_max_norm <= absolute_gradient_tolerance) {
244 summary->termination_type = GRADIENT_TOLERANCE;
245 VLOG(1) << "Terminating: Gradient tolerance reached."
246 << "Relative gradient max norm: "
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800247 << iteration_summary.gradient_max_norm / initial_gradient_max_norm
248 << " <= " << options.gradient_tolerance;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800249 break;
250 }
251
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800252 iteration_summary.cost_change = previous_state.cost - current_state.cost;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800253 const double absolute_function_tolerance =
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800254 options.function_tolerance * previous_state.cost;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800255 if (fabs(iteration_summary.cost_change) < absolute_function_tolerance) {
256 VLOG(1) << "Terminating. Function tolerance reached. "
257 << "|cost_change|/cost: "
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800258 << fabs(iteration_summary.cost_change) / previous_state.cost
259 << " <= " << options.function_tolerance;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800260 summary->termination_type = FUNCTION_TOLERANCE;
261 return;
262 }
263
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800264 iteration_summary.cost = current_state.cost + summary->fixed_cost;
265 iteration_summary.step_norm = delta.norm();
266 iteration_summary.step_is_valid = true;
267 iteration_summary.step_is_successful = true;
268 iteration_summary.step_norm = delta.norm();
269 iteration_summary.step_size = current_state.step_size;
270 iteration_summary.line_search_function_evaluations =
271 line_search_summary.num_evaluations;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800272 iteration_summary.iteration_time_in_seconds =
273 WallTimeInSeconds() - iteration_start_time;
274 iteration_summary.cumulative_time_in_seconds =
275 WallTimeInSeconds() - start_time
276 + summary->preprocessor_time_in_seconds;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800277
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800278 summary->iterations.push_back(iteration_summary);
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800279 }
280}
281
282} // namespace internal
283} // namespace ceres