blob: 7ce110c51c651f02e7df2004dc58b5707a74c338 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080031#include "ceres/evaluation_callback.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080032
33#include <cmath>
34#include <limits>
Austin Schuh3de38b02024-06-25 18:25:10 -070035#include <memory>
Austin Schuh70cc9552019-01-21 19:46:48 -080036#include <vector>
37
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080038#include "ceres/autodiff_cost_function.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080039#include "ceres/problem.h"
40#include "ceres/problem_impl.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080041#include "ceres/sized_cost_function.h"
42#include "ceres/solver.h"
43#include "gtest/gtest.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080044
Austin Schuh3de38b02024-06-25 18:25:10 -070045namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080046
47// Use an inline hash function to avoid portability wrangling. Algorithm from
48// Daniel Bernstein, known as the "djb2" hash.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080049template <typename T>
Austin Schuh70cc9552019-01-21 19:46:48 -080050uint64_t Djb2Hash(const T* data, const int size) {
51 uint64_t hash = 5381;
Austin Schuh3de38b02024-06-25 18:25:10 -070052 const auto* data_as_bytes = reinterpret_cast<const uint8_t*>(data);
Austin Schuh70cc9552019-01-21 19:46:48 -080053 for (int i = 0; i < sizeof(*data) * size; ++i) {
54 hash = hash * 33 + data_as_bytes[i];
55 }
56 return hash;
57}
58
59const double kUninitialized = 0;
60
61// Generally multiple inheritance is a terrible idea, but in this (test)
62// case it makes for a relatively elegant test implementation.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080063struct WigglyBowlCostFunctionAndEvaluationCallback : SizedCostFunction<2, 2>,
64 EvaluationCallback {
65 explicit WigglyBowlCostFunctionAndEvaluationCallback(double* parameter)
Austin Schuh70cc9552019-01-21 19:46:48 -080066 : 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
Austin Schuh70cc9552019-01-21 19:46:48 -080075 // Evaluation callback interface. This checks that all the preconditions are
76 // met at the point that Ceres calls into it.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080077 void PrepareForEvaluation(bool evaluate_jacobians,
78 bool new_evaluation_point) final {
Austin Schuh70cc9552019-01-21 19:46:48 -080079 // At this point, the incoming parameters are implicitly pushed by Ceres
80 // into the user parameter blocks; in contrast to in Evaluate().
81 uint64_t incoming_parameter_hash = Djb2Hash(user_parameter_block, 2);
82
83 // Check: Prepare() & Evaluate() come in pairs, in that order. Before this
84 // call, the number of calls excluding this one should match.
85 EXPECT_EQ(prepare_num_calls, evaluate_num_calls);
86
87 // Check: new_evaluation_point indicates that the parameter has changed.
88 if (new_evaluation_point) {
89 // If it's a new evaluation point, then the parameter should have
90 // changed. Technically, it's not required that it must change but
91 // in practice it does, and that helps with testing.
92 EXPECT_NE(evaluate_last_parameter_hash, incoming_parameter_hash);
93 EXPECT_NE(prepare_parameter_hash, incoming_parameter_hash);
94 } else {
95 // If this is the same evaluation point as last time, ensure that
96 // the parameters match both from the previous evaluate, the
97 // previous prepare, and the current prepare.
98 EXPECT_EQ(evaluate_last_parameter_hash, prepare_parameter_hash);
99 EXPECT_EQ(evaluate_last_parameter_hash, incoming_parameter_hash);
100 }
101
102 // Save details for to check at the next call to Evaluate().
103 prepare_num_calls++;
104 prepare_requested_jacobians = evaluate_jacobians;
105 prepare_new_evaluation_point = new_evaluation_point;
106 prepare_parameter_hash = incoming_parameter_hash;
107 }
108
109 // Cost function interface. This checks that preconditions that were
110 // set as part of the PrepareForEvaluation() call are met in this one.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800111 bool Evaluate(double const* const* parameters,
112 double* residuals,
113 double** jacobians) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -0800114 // Cost function implementation of the "Wiggly Bowl" function:
115 //
116 // 1/2 * [(y - a*sin(x))^2 + x^2],
117 //
118 // expressed as a Ceres cost function with two residuals:
119 //
120 // r[0] = y - a*sin(x)
121 // r[1] = x.
122 //
123 // This is harder to optimize than the Rosenbrock function because the
124 // minimizer has to navigate a sine-shaped valley while descending the 1D
125 // parabola formed along the y axis. Note that the "a" needs to be more
126 // than 5 to get a strong enough wiggle effect in the cost surface to
127 // trigger failed iterations in the optimizer.
128 const double a = 10.0;
129 double x = (*parameters)[0];
130 double y = (*parameters)[1];
131 residuals[0] = y - a * sin(x);
132 residuals[1] = x;
Austin Schuh3de38b02024-06-25 18:25:10 -0700133 if (jacobians != nullptr) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800134 (*jacobians)[2 * 0 + 0] = -a * cos(x); // df1/dx
135 (*jacobians)[2 * 0 + 1] = 1.0; // df1/dy
136 (*jacobians)[2 * 1 + 0] = 1.0; // df2/dx
137 (*jacobians)[2 * 1 + 1] = 0.0; // df2/dy
Austin Schuh70cc9552019-01-21 19:46:48 -0800138 }
139
140 uint64_t incoming_parameter_hash = Djb2Hash(*parameters, 2);
141
142 // Check: PrepareForEvaluation() & Evaluate() come in pairs, in that order.
143 EXPECT_EQ(prepare_num_calls, evaluate_num_calls + 1);
144
145 // Check: if new_evaluation_point indicates that the parameter has
146 // changed, it has changed; otherwise it is the same.
147 if (prepare_new_evaluation_point) {
148 EXPECT_NE(evaluate_last_parameter_hash, incoming_parameter_hash);
149 } else {
150 EXPECT_NE(evaluate_last_parameter_hash, kUninitialized);
151 EXPECT_EQ(evaluate_last_parameter_hash, incoming_parameter_hash);
152 }
153
154 // Check: Parameter matches value in in parameter blocks during prepare.
155 EXPECT_EQ(prepare_parameter_hash, incoming_parameter_hash);
156
157 // Check: jacobians are requested if they were in PrepareForEvaluation().
Austin Schuh3de38b02024-06-25 18:25:10 -0700158 EXPECT_EQ(prepare_requested_jacobians, jacobians != nullptr);
Austin Schuh70cc9552019-01-21 19:46:48 -0800159
160 evaluate_num_calls++;
161 evaluate_last_parameter_hash = incoming_parameter_hash;
162 return true;
163 }
164
165 // Pointer to the parameter block associated with this cost function.
166 // Contents should get set by Ceres before calls to PrepareForEvaluation()
167 // and Evaluate().
168 double* user_parameter_block;
169
170 // Track state: PrepareForEvaluation().
171 //
172 // These track details from the PrepareForEvaluation() call (hence the
173 // "prepare_" prefix), which are checked for consistency in Evaluate().
174 int prepare_num_calls;
175 bool prepare_requested_jacobians;
176 bool prepare_new_evaluation_point;
177 uint64_t prepare_parameter_hash;
178
179 // Track state: Evaluate().
180 //
181 // These track details from the Evaluate() call (hence the "evaluate_"
182 // prefix), which are then checked for consistency in the calls to
183 // PrepareForEvaluation(). Mutable is reasonable for this case.
184 mutable int evaluate_num_calls;
185 mutable uint64_t evaluate_last_parameter_hash;
186};
187
188TEST(EvaluationCallback, WithTrustRegionMinimizer) {
189 double parameters[2] = {50.0, 50.0};
190 const uint64_t original_parameters_hash = Djb2Hash(parameters, 2);
191
192 WigglyBowlCostFunctionAndEvaluationCallback cost_function(parameters);
193 Problem::Options problem_options;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800194 problem_options.evaluation_callback = &cost_function;
Austin Schuh70cc9552019-01-21 19:46:48 -0800195 problem_options.cost_function_ownership = DO_NOT_TAKE_OWNERSHIP;
196 Problem problem(problem_options);
Austin Schuh3de38b02024-06-25 18:25:10 -0700197 problem.AddResidualBlock(&cost_function, nullptr, parameters);
Austin Schuh70cc9552019-01-21 19:46:48 -0800198
199 Solver::Options options;
200 options.linear_solver_type = DENSE_QR;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800201 options.max_num_iterations = 50;
Austin Schuh70cc9552019-01-21 19:46:48 -0800202
203 // Run the solve. Checking is done inside the cost function / callback.
204 Solver::Summary summary;
205 Solve(options, &problem, &summary);
206
207 // Ensure that this was a hard cost function (not all steps succeed).
208 EXPECT_GT(summary.num_successful_steps, 10);
209 EXPECT_GT(summary.num_unsuccessful_steps, 10);
210
211 // Ensure PrepareForEvaluation() is called the appropriate number of times.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800212 EXPECT_EQ(
213 cost_function.prepare_num_calls,
214 // Unsuccessful steps are evaluated only once (no jacobians).
215 summary.num_unsuccessful_steps +
216 // Successful steps are evaluated twice: with and without jacobians.
217 2 * summary.num_successful_steps
218 // Final iteration doesn't re-evaluate the jacobian.
219 // Note: This may be sensitive to tweaks to the TR algorithm; if
220 // this becomes too brittle, remove this EXPECT_EQ() entirely.
221 - 1);
Austin Schuh70cc9552019-01-21 19:46:48 -0800222
223 // Ensure the callback calls ran a reasonable number of times.
224 EXPECT_GT(cost_function.prepare_num_calls, 0);
225 EXPECT_GT(cost_function.evaluate_num_calls, 0);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800226 EXPECT_EQ(cost_function.prepare_num_calls, cost_function.evaluate_num_calls);
Austin Schuh70cc9552019-01-21 19:46:48 -0800227
228 // Ensure that the parameters did actually change.
229 EXPECT_NE(Djb2Hash(parameters, 2), original_parameters_hash);
230}
231
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800232// r = 1 - x
233struct LinearResidual {
234 template <typename T>
235 bool operator()(const T* x, T* residuals) const {
236 residuals[0] = 1.0 - x[0];
237 return true;
238 }
239
240 static CostFunction* Create() {
241 return new AutoDiffCostFunction<LinearResidual, 1, 1>(new LinearResidual);
242 };
243};
244
245// Increments a counter everytime PrepareForEvaluation is called.
246class IncrementingEvaluationCallback : public EvaluationCallback {
247 public:
248 void PrepareForEvaluation(bool evaluate_jacobians,
249 bool new_evaluation_point) final {
250 (void)evaluate_jacobians;
251 (void)new_evaluation_point;
252 counter_ += 1.0;
253 }
254
Austin Schuh3de38b02024-06-25 18:25:10 -0700255 double counter() const { return counter_; }
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800256
257 private:
258 double counter_ = -1;
259};
260
261// r = IncrementingEvaluationCallback::counter - x
262struct EvaluationCallbackResidual {
263 explicit EvaluationCallbackResidual(
264 const IncrementingEvaluationCallback& callback)
265 : callback(callback) {}
266
267 template <typename T>
268 bool operator()(const T* x, T* residuals) const {
269 residuals[0] = callback.counter() - x[0];
270 return true;
271 }
272
273 const IncrementingEvaluationCallback& callback;
274
275 static CostFunction* Create(IncrementingEvaluationCallback& callback) {
276 return new AutoDiffCostFunction<EvaluationCallbackResidual, 1, 1>(
277 new EvaluationCallbackResidual(callback));
278 };
279};
280
281// The following test, constructs a problem with residual blocks all
282// of whose parameters are constant, so they are evaluated once
283// outside the Minimizer to compute Solver::Summary::fixed_cost.
284//
285// The cost function for this residual block depends on the
286// IncrementingEvaluationCallback::counter_, by checking the value of
287// the fixed cost, we can check if the IncrementingEvaluationCallback
288// was called.
289TEST(EvaluationCallback, EvaluationCallbackIsCalledBeforeFixedCostIsEvaluated) {
290 double x = 1;
291 double y = 2;
292 std::unique_ptr<IncrementingEvaluationCallback> callback(
293 new IncrementingEvaluationCallback);
294 Problem::Options problem_options;
295 problem_options.evaluation_callback = callback.get();
296 Problem problem(problem_options);
297 problem.AddResidualBlock(LinearResidual::Create(), nullptr, &x);
298 problem.AddResidualBlock(
299 EvaluationCallbackResidual::Create(*callback), nullptr, &y);
300 problem.SetParameterBlockConstant(&y);
301
302 Solver::Options options;
303 options.linear_solver_type = DENSE_QR;
304 Solver::Summary summary;
305 Solve(options, &problem, &summary);
306 EXPECT_EQ(summary.fixed_cost, 2.0);
307 EXPECT_EQ(summary.final_cost, summary.fixed_cost);
308 EXPECT_GT(callback->counter(), 0);
309}
310
311static void WithLineSearchMinimizerImpl(
Austin Schuh70cc9552019-01-21 19:46:48 -0800312 LineSearchType line_search,
313 LineSearchDirectionType line_search_direction,
314 LineSearchInterpolationType line_search_interpolation) {
315 double parameters[2] = {50.0, 50.0};
316 const uint64_t original_parameters_hash = Djb2Hash(parameters, 2);
317
318 WigglyBowlCostFunctionAndEvaluationCallback cost_function(parameters);
319 Problem::Options problem_options;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800320 problem_options.evaluation_callback = &cost_function;
Austin Schuh70cc9552019-01-21 19:46:48 -0800321 problem_options.cost_function_ownership = DO_NOT_TAKE_OWNERSHIP;
322 Problem problem(problem_options);
Austin Schuh3de38b02024-06-25 18:25:10 -0700323 problem.AddResidualBlock(&cost_function, nullptr, parameters);
Austin Schuh70cc9552019-01-21 19:46:48 -0800324
325 Solver::Options options;
326 options.linear_solver_type = DENSE_QR;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800327 options.max_num_iterations = 50;
Austin Schuh70cc9552019-01-21 19:46:48 -0800328 options.minimizer_type = ceres::LINE_SEARCH;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800329
Austin Schuh70cc9552019-01-21 19:46:48 -0800330 options.line_search_type = line_search;
331 options.line_search_direction_type = line_search_direction;
332 options.line_search_interpolation_type = line_search_interpolation;
333
334 // Run the solve. Checking is done inside the cost function / callback.
335 Solver::Summary summary;
336 Solve(options, &problem, &summary);
337
338 // Ensure the callback calls ran a reasonable number of times.
339 EXPECT_GT(summary.num_line_search_steps, 10);
340 EXPECT_GT(cost_function.prepare_num_calls, 30);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800341 EXPECT_EQ(cost_function.prepare_num_calls, cost_function.evaluate_num_calls);
Austin Schuh70cc9552019-01-21 19:46:48 -0800342
343 // Ensure that the parameters did actually change.
344 EXPECT_NE(Djb2Hash(parameters, 2), original_parameters_hash);
345}
346
347// Note: These tests omit combinations of Wolfe line search with bisection.
348// Due to an implementation quirk in Wolfe line search with bisection, there
349// are calls to re-evaluate an existing point with new_point = true. That
350// causes the (overly) strict tests to break, since they check the new_point
351// preconditions in an if-and-only-if way. Strictly speaking, if new_point =
352// true, the interface does not *require* that the point has changed; only that
353// if new_point = false, the same point is reused.
354//
355// Since the strict checking is useful to verify that there aren't missed
356// optimizations, omit tests of the Wolfe with bisection cases.
357
358// Wolfe with L-BFGS.
359TEST(EvaluationCallback, WithLineSearchMinimizerWolfeLbfgsCubic) {
360 WithLineSearchMinimizerImpl(WOLFE, LBFGS, CUBIC);
361}
362TEST(EvaluationCallback, WithLineSearchMinimizerWolfeLbfgsQuadratic) {
363 WithLineSearchMinimizerImpl(WOLFE, LBFGS, QUADRATIC);
364}
365
366// Wolfe with full BFGS.
367TEST(EvaluationCallback, WithLineSearchMinimizerWolfeBfgsCubic) {
368 WithLineSearchMinimizerImpl(WOLFE, BFGS, CUBIC);
369}
370
371TEST(EvaluationCallback, WithLineSearchMinimizerWolfeBfgsQuadratic) {
372 WithLineSearchMinimizerImpl(WOLFE, BFGS, QUADRATIC);
373}
374
375// Armijo with nonlinear conjugate gradient.
376TEST(EvaluationCallback, WithLineSearchMinimizerArmijoCubic) {
377 WithLineSearchMinimizerImpl(ARMIJO, NONLINEAR_CONJUGATE_GRADIENT, CUBIC);
378}
379
380TEST(EvaluationCallback, WithLineSearchMinimizerArmijoBisection) {
381 WithLineSearchMinimizerImpl(ARMIJO, NONLINEAR_CONJUGATE_GRADIENT, BISECTION);
382}
383
384TEST(EvaluationCallback, WithLineSearchMinimizerArmijoQuadratic) {
385 WithLineSearchMinimizerImpl(ARMIJO, NONLINEAR_CONJUGATE_GRADIENT, QUADRATIC);
386}
387
Austin Schuh3de38b02024-06-25 18:25:10 -0700388} // namespace ceres::internal