blob: 684a7369b3a219ce5777934198bac692bfdb8ae6 [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
Sameer Agarwal8140f0f2013-03-12 09:45:08 -070041#ifndef CERES_NO_LINE_SEARCH_MINIMIZER
42
Sameer Agarwalf4d01642012-11-26 12:55:58 -080043#include "ceres/line_search_minimizer.h"
44
45#include <algorithm>
46#include <cstdlib>
47#include <cmath>
Sameer Agarwalf4d01642012-11-26 12:55:58 -080048#include <string>
49#include <vector>
Sameer Agarwalf4d01642012-11-26 12:55:58 -080050
51#include "Eigen/Dense"
52#include "ceres/array_utils.h"
53#include "ceres/evaluator.h"
54#include "ceres/internal/eigen.h"
Sameer Agarwal9883fc32012-11-30 12:32:43 -080055#include "ceres/internal/port.h"
Sameer Agarwalf4d01642012-11-26 12:55:58 -080056#include "ceres/internal/scoped_ptr.h"
57#include "ceres/line_search.h"
Sameer Agarwal9883fc32012-11-30 12:32:43 -080058#include "ceres/line_search_direction.h"
Sameer Agarwalf4d01642012-11-26 12:55:58 -080059#include "ceres/stringprintf.h"
60#include "ceres/types.h"
61#include "ceres/wall_time.h"
62#include "glog/logging.h"
63
64namespace ceres {
65namespace internal {
66namespace {
67// Small constant for various floating point issues.
Sameer Agarwalc89ea4b2013-01-09 16:09:35 -080068// TODO(sameeragarwal): Change to a better name if this has only one
69// use.
Sameer Agarwalf4d01642012-11-26 12:55:58 -080070const double kEpsilon = 1e-12;
Sameer Agarwalc89ea4b2013-01-09 16:09:35 -080071
Sameer Agarwal9883fc32012-11-30 12:32:43 -080072bool Evaluate(Evaluator* evaluator,
73 const Vector& x,
74 LineSearchMinimizer::State* state) {
75 const bool status = evaluator->Evaluate(x.data(),
76 &(state->cost),
77 NULL,
78 state->gradient.data(),
79 NULL);
80 if (status) {
81 state->gradient_squared_norm = state->gradient.squaredNorm();
82 state->gradient_max_norm = state->gradient.lpNorm<Eigen::Infinity>();
Sameer Agarwalf4d01642012-11-26 12:55:58 -080083 }
Sameer Agarwal9883fc32012-11-30 12:32:43 -080084
85 return status;
Sameer Agarwalf4d01642012-11-26 12:55:58 -080086}
87
Sameer Agarwal9883fc32012-11-30 12:32:43 -080088} // namespace
Sameer Agarwalf4d01642012-11-26 12:55:58 -080089
90void LineSearchMinimizer::Minimize(const Minimizer::Options& options,
91 double* parameters,
92 Solver::Summary* summary) {
93 double start_time = WallTimeInSeconds();
94 double iteration_start_time = start_time;
Sameer Agarwalf4d01642012-11-26 12:55:58 -080095
Sameer Agarwal9883fc32012-11-30 12:32:43 -080096 Evaluator* evaluator = CHECK_NOTNULL(options.evaluator);
Sameer Agarwalf4d01642012-11-26 12:55:58 -080097 const int num_parameters = evaluator->NumParameters();
98 const int num_effective_parameters = evaluator->NumEffectiveParameters();
99
100 summary->termination_type = NO_CONVERGENCE;
101 summary->num_successful_steps = 0;
102 summary->num_unsuccessful_steps = 0;
103
104 VectorRef x(parameters, num_parameters);
105
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800106 State current_state(num_parameters, num_effective_parameters);
107 State previous_state(num_parameters, num_effective_parameters);
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800108
109 Vector delta(num_effective_parameters);
110 Vector x_plus_delta(num_parameters);
111
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800112 IterationSummary iteration_summary;
113 iteration_summary.iteration = 0;
114 iteration_summary.step_is_valid = false;
115 iteration_summary.step_is_successful = false;
116 iteration_summary.cost_change = 0.0;
117 iteration_summary.gradient_max_norm = 0.0;
118 iteration_summary.step_norm = 0.0;
119 iteration_summary.linear_solver_iterations = 0;
120 iteration_summary.step_solver_time_in_seconds = 0;
121
122 // Do initial cost and Jacobian evaluation.
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800123 if (!Evaluate(evaluator, x, &current_state)) {
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800124 LOG(WARNING) << "Terminating: Cost and gradient evaluation failed.";
125 summary->termination_type = NUMERICAL_FAILURE;
126 return;
127 }
128
Sameer Agarwalf102a682013-02-11 15:08:40 -0800129 summary->initial_cost = current_state.cost + summary->fixed_cost;
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800130 iteration_summary.cost = current_state.cost + summary->fixed_cost;
Sameer Agarwalf102a682013-02-11 15:08:40 -0800131
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800132 iteration_summary.gradient_max_norm = current_state.gradient_max_norm;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800133
134 // The initial gradient max_norm is bounded from below so that we do
135 // not divide by zero.
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800136 const double initial_gradient_max_norm =
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800137 max(iteration_summary.gradient_max_norm, kEpsilon);
138 const double absolute_gradient_tolerance =
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800139 options.gradient_tolerance * initial_gradient_max_norm;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800140
141 if (iteration_summary.gradient_max_norm <= absolute_gradient_tolerance) {
142 summary->termination_type = GRADIENT_TOLERANCE;
143 VLOG(1) << "Terminating: Gradient tolerance reached."
144 << "Relative gradient max norm: "
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800145 << iteration_summary.gradient_max_norm / initial_gradient_max_norm
146 << " <= " << options.gradient_tolerance;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800147 return;
148 }
149
150 iteration_summary.iteration_time_in_seconds =
151 WallTimeInSeconds() - iteration_start_time;
152 iteration_summary.cumulative_time_in_seconds =
153 WallTimeInSeconds() - start_time
154 + summary->preprocessor_time_in_seconds;
155 summary->iterations.push_back(iteration_summary);
156
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800157 LineSearchDirection::Options line_search_direction_options;
158 line_search_direction_options.num_parameters = num_effective_parameters;
159 line_search_direction_options.type = options.line_search_direction_type;
160 line_search_direction_options.nonlinear_conjugate_gradient_type =
161 options.nonlinear_conjugate_gradient_type;
162 line_search_direction_options.max_lbfgs_rank = options.max_lbfgs_rank;
163 scoped_ptr<LineSearchDirection> line_search_direction(
164 LineSearchDirection::Create(line_search_direction_options));
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800165
166 LineSearchFunction line_search_function(evaluator);
167 LineSearch::Options line_search_options;
168 line_search_options.function = &line_search_function;
169
170 // TODO(sameeragarwal): Make this parameterizable over different
171 // line searches.
172 ArmijoLineSearch line_search;
173 LineSearch::Summary line_search_summary;
174
175 while (true) {
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800176 if (!RunCallbacks(options.callbacks, iteration_summary, summary)) {
177 return;
178 }
179
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800180 iteration_start_time = WallTimeInSeconds();
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800181 if (iteration_summary.iteration >= options.max_num_iterations) {
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800182 summary->termination_type = NO_CONVERGENCE;
183 VLOG(1) << "Terminating: Maximum number of iterations reached.";
184 break;
185 }
186
187 const double total_solver_time = iteration_start_time - start_time +
188 summary->preprocessor_time_in_seconds;
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800189 if (total_solver_time >= options.max_solver_time_in_seconds) {
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800190 summary->termination_type = NO_CONVERGENCE;
191 VLOG(1) << "Terminating: Maximum solver time reached.";
192 break;
193 }
194
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800195 iteration_summary = IterationSummary();
196 iteration_summary.iteration = summary->iterations.back().iteration + 1;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800197
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800198 bool line_search_status = true;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800199 if (iteration_summary.iteration == 1) {
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800200 current_state.search_direction = -current_state.gradient;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800201 } else {
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800202 line_search_status = line_search_direction->NextDirection(
203 previous_state,
204 current_state,
205 &current_state.search_direction);
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800206 }
207
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800208 if (!line_search_status) {
209 LOG(WARNING) << "Line search direction computation failed. "
210 "Resorting to steepest descent.";
211 current_state.search_direction = -current_state.gradient;
212 }
213
214 line_search_function.Init(x, current_state.search_direction);
215 current_state.directional_derivative =
216 current_state.gradient.dot(current_state.search_direction);
217
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800218 // TODO(sameeragarwal): Refactor this into its own object and add
219 // explanations for the various choices.
220 const double initial_step_size = (iteration_summary.iteration == 1)
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800221 ? min(1.0, 1.0 / current_state.gradient_max_norm)
222 : min(1.0, 2.0 * (current_state.cost - previous_state.cost) /
223 current_state.directional_derivative);
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800224
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800225 line_search.Search(line_search_options,
226 initial_step_size,
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800227 current_state.cost,
228 current_state.directional_derivative,
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800229 &line_search_summary);
230
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800231 current_state.step_size = line_search_summary.optimal_step_size;
232 delta = current_state.step_size * current_state.search_direction;
233
234 previous_state = current_state;
235
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800236 // TODO(sameeragarwal): Collect stats.
237 if (!evaluator->Plus(x.data(), delta.data(), x_plus_delta.data()) ||
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800238 !Evaluate(evaluator, x_plus_delta, &current_state)) {
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800239 LOG(WARNING) << "Evaluation failed.";
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800240 } else {
241 x = x_plus_delta;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800242 }
243
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800244 iteration_summary.gradient_max_norm = current_state.gradient_max_norm;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800245 if (iteration_summary.gradient_max_norm <= absolute_gradient_tolerance) {
246 summary->termination_type = GRADIENT_TOLERANCE;
247 VLOG(1) << "Terminating: Gradient tolerance reached."
248 << "Relative gradient max norm: "
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800249 << iteration_summary.gradient_max_norm / initial_gradient_max_norm
250 << " <= " << options.gradient_tolerance;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800251 break;
252 }
253
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800254 iteration_summary.cost_change = previous_state.cost - current_state.cost;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800255 const double absolute_function_tolerance =
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800256 options.function_tolerance * previous_state.cost;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800257 if (fabs(iteration_summary.cost_change) < absolute_function_tolerance) {
258 VLOG(1) << "Terminating. Function tolerance reached. "
259 << "|cost_change|/cost: "
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800260 << fabs(iteration_summary.cost_change) / previous_state.cost
261 << " <= " << options.function_tolerance;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800262 summary->termination_type = FUNCTION_TOLERANCE;
263 return;
264 }
265
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800266 iteration_summary.cost = current_state.cost + summary->fixed_cost;
267 iteration_summary.step_norm = delta.norm();
268 iteration_summary.step_is_valid = true;
269 iteration_summary.step_is_successful = true;
270 iteration_summary.step_norm = delta.norm();
271 iteration_summary.step_size = current_state.step_size;
272 iteration_summary.line_search_function_evaluations =
273 line_search_summary.num_evaluations;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800274 iteration_summary.iteration_time_in_seconds =
275 WallTimeInSeconds() - iteration_start_time;
276 iteration_summary.cumulative_time_in_seconds =
277 WallTimeInSeconds() - start_time
278 + summary->preprocessor_time_in_seconds;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800279
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800280 summary->iterations.push_back(iteration_summary);
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800281 }
282}
283
284} // namespace internal
285} // namespace ceres
Sameer Agarwal8140f0f2013-03-12 09:45:08 -0700286
Sameer Agarwal700d50d2013-03-12 16:12:42 -0700287#endif // CERES_NO_LINE_SEARCH_MINIMIZER