blob: a28d5a839885c5b60bd4e0ce4851e0fb71fd8513 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2018 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: mierle@gmail.com (Keir Mierle)
30
31#include "ceres/solver.h"
32
33#include <cmath>
34#include <limits>
35#include <vector>
36
37#include "gtest/gtest.h"
38#include "ceres/sized_cost_function.h"
39#include "ceres/problem.h"
40#include "ceres/problem_impl.h"
41
42namespace ceres {
43namespace internal {
44
45// Use an inline hash function to avoid portability wrangling. Algorithm from
46// Daniel Bernstein, known as the "djb2" hash.
47template<typename T>
48uint64_t Djb2Hash(const T* data, const int size) {
49 uint64_t hash = 5381;
50 const uint8_t* data_as_bytes = reinterpret_cast<const uint8_t*>(data);
51 for (int i = 0; i < sizeof(*data) * size; ++i) {
52 hash = hash * 33 + data_as_bytes[i];
53 }
54 return hash;
55}
56
57const double kUninitialized = 0;
58
59// Generally multiple inheritance is a terrible idea, but in this (test)
60// case it makes for a relatively elegant test implementation.
61struct WigglyBowlCostFunctionAndEvaluationCallback :
62 SizedCostFunction<2, 2>,
63 EvaluationCallback {
64
65 explicit WigglyBowlCostFunctionAndEvaluationCallback(double *parameter)
66 : EvaluationCallback(),
67 user_parameter_block(parameter),
68 prepare_num_calls(0),
69 prepare_requested_jacobians(false),
70 prepare_new_evaluation_point(false),
71 prepare_parameter_hash(kUninitialized),
72 evaluate_num_calls(0),
73 evaluate_last_parameter_hash(kUninitialized) {}
74
75 virtual ~WigglyBowlCostFunctionAndEvaluationCallback() {}
76
77 // Evaluation callback interface. This checks that all the preconditions are
78 // met at the point that Ceres calls into it.
79 virtual void PrepareForEvaluation(bool evaluate_jacobians,
80 bool new_evaluation_point) {
81 // At this point, the incoming parameters are implicitly pushed by Ceres
82 // into the user parameter blocks; in contrast to in Evaluate().
83 uint64_t incoming_parameter_hash = Djb2Hash(user_parameter_block, 2);
84
85 // Check: Prepare() & Evaluate() come in pairs, in that order. Before this
86 // call, the number of calls excluding this one should match.
87 EXPECT_EQ(prepare_num_calls, evaluate_num_calls);
88
89 // Check: new_evaluation_point indicates that the parameter has changed.
90 if (new_evaluation_point) {
91 // If it's a new evaluation point, then the parameter should have
92 // changed. Technically, it's not required that it must change but
93 // in practice it does, and that helps with testing.
94 EXPECT_NE(evaluate_last_parameter_hash, incoming_parameter_hash);
95 EXPECT_NE(prepare_parameter_hash, incoming_parameter_hash);
96 } else {
97 // If this is the same evaluation point as last time, ensure that
98 // the parameters match both from the previous evaluate, the
99 // previous prepare, and the current prepare.
100 EXPECT_EQ(evaluate_last_parameter_hash, prepare_parameter_hash);
101 EXPECT_EQ(evaluate_last_parameter_hash, incoming_parameter_hash);
102 }
103
104 // Save details for to check at the next call to Evaluate().
105 prepare_num_calls++;
106 prepare_requested_jacobians = evaluate_jacobians;
107 prepare_new_evaluation_point = new_evaluation_point;
108 prepare_parameter_hash = incoming_parameter_hash;
109 }
110
111 // Cost function interface. This checks that preconditions that were
112 // set as part of the PrepareForEvaluation() call are met in this one.
113 virtual bool Evaluate(double const* const* parameters,
114 double* residuals,
115 double** jacobians) const {
116 // Cost function implementation of the "Wiggly Bowl" function:
117 //
118 // 1/2 * [(y - a*sin(x))^2 + x^2],
119 //
120 // expressed as a Ceres cost function with two residuals:
121 //
122 // r[0] = y - a*sin(x)
123 // r[1] = x.
124 //
125 // This is harder to optimize than the Rosenbrock function because the
126 // minimizer has to navigate a sine-shaped valley while descending the 1D
127 // parabola formed along the y axis. Note that the "a" needs to be more
128 // than 5 to get a strong enough wiggle effect in the cost surface to
129 // trigger failed iterations in the optimizer.
130 const double a = 10.0;
131 double x = (*parameters)[0];
132 double y = (*parameters)[1];
133 residuals[0] = y - a * sin(x);
134 residuals[1] = x;
135 if (jacobians != NULL) {
136 (*jacobians)[2 * 0 + 0] = - a * cos(x); // df1/dx
137 (*jacobians)[2 * 0 + 1] = 1.0; // df1/dy
138 (*jacobians)[2 * 1 + 0] = 1.0; // df2/dx
139 (*jacobians)[2 * 1 + 1] = 0.0; // df2/dy
140 }
141
142 uint64_t incoming_parameter_hash = Djb2Hash(*parameters, 2);
143
144 // Check: PrepareForEvaluation() & Evaluate() come in pairs, in that order.
145 EXPECT_EQ(prepare_num_calls, evaluate_num_calls + 1);
146
147 // Check: if new_evaluation_point indicates that the parameter has
148 // changed, it has changed; otherwise it is the same.
149 if (prepare_new_evaluation_point) {
150 EXPECT_NE(evaluate_last_parameter_hash, incoming_parameter_hash);
151 } else {
152 EXPECT_NE(evaluate_last_parameter_hash, kUninitialized);
153 EXPECT_EQ(evaluate_last_parameter_hash, incoming_parameter_hash);
154 }
155
156 // Check: Parameter matches value in in parameter blocks during prepare.
157 EXPECT_EQ(prepare_parameter_hash, incoming_parameter_hash);
158
159 // Check: jacobians are requested if they were in PrepareForEvaluation().
160 EXPECT_EQ(prepare_requested_jacobians, jacobians != NULL);
161
162 evaluate_num_calls++;
163 evaluate_last_parameter_hash = incoming_parameter_hash;
164 return true;
165 }
166
167 // Pointer to the parameter block associated with this cost function.
168 // Contents should get set by Ceres before calls to PrepareForEvaluation()
169 // and Evaluate().
170 double* user_parameter_block;
171
172 // Track state: PrepareForEvaluation().
173 //
174 // These track details from the PrepareForEvaluation() call (hence the
175 // "prepare_" prefix), which are checked for consistency in Evaluate().
176 int prepare_num_calls;
177 bool prepare_requested_jacobians;
178 bool prepare_new_evaluation_point;
179 uint64_t prepare_parameter_hash;
180
181 // Track state: Evaluate().
182 //
183 // These track details from the Evaluate() call (hence the "evaluate_"
184 // prefix), which are then checked for consistency in the calls to
185 // PrepareForEvaluation(). Mutable is reasonable for this case.
186 mutable int evaluate_num_calls;
187 mutable uint64_t evaluate_last_parameter_hash;
188};
189
190TEST(EvaluationCallback, WithTrustRegionMinimizer) {
191 double parameters[2] = {50.0, 50.0};
192 const uint64_t original_parameters_hash = Djb2Hash(parameters, 2);
193
194 WigglyBowlCostFunctionAndEvaluationCallback cost_function(parameters);
195 Problem::Options problem_options;
196 problem_options.cost_function_ownership = DO_NOT_TAKE_OWNERSHIP;
197 Problem problem(problem_options);
198 problem.AddResidualBlock(&cost_function, NULL, parameters);
199
200 Solver::Options options;
201 options.linear_solver_type = DENSE_QR;
202 options.max_num_iterations = 300; // Cost function is hard.
203 options.evaluation_callback = &cost_function;
204
205 // Run the solve. Checking is done inside the cost function / callback.
206 Solver::Summary summary;
207 Solve(options, &problem, &summary);
208
209 // Ensure that this was a hard cost function (not all steps succeed).
210 EXPECT_GT(summary.num_successful_steps, 10);
211 EXPECT_GT(summary.num_unsuccessful_steps, 10);
212
213 // Ensure PrepareForEvaluation() is called the appropriate number of times.
214 EXPECT_EQ(cost_function.prepare_num_calls,
215 // Unsuccessful steps are evaluated only once (no jacobians).
216 summary.num_unsuccessful_steps +
217 // Successful steps are evaluated twice: with and without jacobians.
218 2 * summary.num_successful_steps
219 // Final iteration doesn't re-evaluate the jacobian.
220 // Note: This may be sensitive to tweaks to the TR algorithm; if
221 // this becomes too brittle, remove this EXPECT_EQ() entirely.
222 - 1);
223
224 // Ensure the callback calls ran a reasonable number of times.
225 EXPECT_GT(cost_function.prepare_num_calls, 0);
226 EXPECT_GT(cost_function.evaluate_num_calls, 0);
227 EXPECT_EQ(cost_function.prepare_num_calls,
228 cost_function.evaluate_num_calls);
229
230 // Ensure that the parameters did actually change.
231 EXPECT_NE(Djb2Hash(parameters, 2), original_parameters_hash);
232}
233
234void WithLineSearchMinimizerImpl(
235 LineSearchType line_search,
236 LineSearchDirectionType line_search_direction,
237 LineSearchInterpolationType line_search_interpolation) {
238 double parameters[2] = {50.0, 50.0};
239 const uint64_t original_parameters_hash = Djb2Hash(parameters, 2);
240
241 WigglyBowlCostFunctionAndEvaluationCallback cost_function(parameters);
242 Problem::Options problem_options;
243 problem_options.cost_function_ownership = DO_NOT_TAKE_OWNERSHIP;
244 Problem problem(problem_options);
245 problem.AddResidualBlock(&cost_function, NULL, parameters);
246
247 Solver::Options options;
248 options.linear_solver_type = DENSE_QR;
249 options.max_num_iterations = 300; // Cost function is hard.
250 options.minimizer_type = ceres::LINE_SEARCH;
251 options.evaluation_callback = &cost_function;
252 options.line_search_type = line_search;
253 options.line_search_direction_type = line_search_direction;
254 options.line_search_interpolation_type = line_search_interpolation;
255
256 // Run the solve. Checking is done inside the cost function / callback.
257 Solver::Summary summary;
258 Solve(options, &problem, &summary);
259
260 // Ensure the callback calls ran a reasonable number of times.
261 EXPECT_GT(summary.num_line_search_steps, 10);
262 EXPECT_GT(cost_function.prepare_num_calls, 30);
263 EXPECT_EQ(cost_function.prepare_num_calls,
264 cost_function.evaluate_num_calls);
265
266 // Ensure that the parameters did actually change.
267 EXPECT_NE(Djb2Hash(parameters, 2), original_parameters_hash);
268}
269
270// Note: These tests omit combinations of Wolfe line search with bisection.
271// Due to an implementation quirk in Wolfe line search with bisection, there
272// are calls to re-evaluate an existing point with new_point = true. That
273// causes the (overly) strict tests to break, since they check the new_point
274// preconditions in an if-and-only-if way. Strictly speaking, if new_point =
275// true, the interface does not *require* that the point has changed; only that
276// if new_point = false, the same point is reused.
277//
278// Since the strict checking is useful to verify that there aren't missed
279// optimizations, omit tests of the Wolfe with bisection cases.
280
281// Wolfe with L-BFGS.
282TEST(EvaluationCallback, WithLineSearchMinimizerWolfeLbfgsCubic) {
283 WithLineSearchMinimizerImpl(WOLFE, LBFGS, CUBIC);
284}
285TEST(EvaluationCallback, WithLineSearchMinimizerWolfeLbfgsQuadratic) {
286 WithLineSearchMinimizerImpl(WOLFE, LBFGS, QUADRATIC);
287}
288
289// Wolfe with full BFGS.
290TEST(EvaluationCallback, WithLineSearchMinimizerWolfeBfgsCubic) {
291 WithLineSearchMinimizerImpl(WOLFE, BFGS, CUBIC);
292}
293
294TEST(EvaluationCallback, WithLineSearchMinimizerWolfeBfgsQuadratic) {
295 WithLineSearchMinimizerImpl(WOLFE, BFGS, QUADRATIC);
296}
297
298// Armijo with nonlinear conjugate gradient.
299TEST(EvaluationCallback, WithLineSearchMinimizerArmijoCubic) {
300 WithLineSearchMinimizerImpl(ARMIJO, NONLINEAR_CONJUGATE_GRADIENT, CUBIC);
301}
302
303TEST(EvaluationCallback, WithLineSearchMinimizerArmijoBisection) {
304 WithLineSearchMinimizerImpl(ARMIJO, NONLINEAR_CONJUGATE_GRADIENT, BISECTION);
305}
306
307TEST(EvaluationCallback, WithLineSearchMinimizerArmijoQuadratic) {
308 WithLineSearchMinimizerImpl(ARMIJO, NONLINEAR_CONJUGATE_GRADIENT, QUADRATIC);
309}
310
311} // namespace internal
312} // namespace ceres