blob: f8eabf610f2f30a0c5b975b58f9659f143e04f9c [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: strandmark@google.com (Petter Strandmark)
30
Austin Schuh70cc9552019-01-21 19:46:48 -080031#include "ceres/gradient_problem_solver.h"
32
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080033#include "ceres/gradient_problem.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080034#include "gtest/gtest.h"
35
Austin Schuh3de38b02024-06-25 18:25:10 -070036namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080037
38// Rosenbrock function; see http://en.wikipedia.org/wiki/Rosenbrock_function .
39class Rosenbrock : public ceres::FirstOrderFunction {
40 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080041 bool Evaluate(const double* parameters,
42 double* cost,
43 double* gradient) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -080044 const double x = parameters[0];
45 const double y = parameters[1];
46
47 cost[0] = (1.0 - x) * (1.0 - x) + 100.0 * (y - x * x) * (y - x * x);
Austin Schuh3de38b02024-06-25 18:25:10 -070048 if (gradient != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -080049 gradient[0] = -2.0 * (1.0 - x) - 200.0 * (y - x * x) * 2.0 * x;
50 gradient[1] = 200.0 * (y - x * x);
51 }
52 return true;
53 }
54
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080055 int NumParameters() const final { return 2; }
Austin Schuh70cc9552019-01-21 19:46:48 -080056};
57
58TEST(GradientProblemSolver, SolvesRosenbrockWithDefaultOptions) {
59 const double expected_tolerance = 1e-9;
60 double parameters[2] = {-1.2, 0.0};
61
62 ceres::GradientProblemSolver::Options options;
63 ceres::GradientProblemSolver::Summary summary;
64 ceres::GradientProblem problem(new Rosenbrock());
65 ceres::Solve(options, problem, parameters, &summary);
66
67 EXPECT_EQ(CONVERGENCE, summary.termination_type);
68 EXPECT_NEAR(1.0, parameters[0], expected_tolerance);
69 EXPECT_NEAR(1.0, parameters[1], expected_tolerance);
70}
71
72class QuadraticFunction : public ceres::FirstOrderFunction {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080073 bool Evaluate(const double* parameters,
74 double* cost,
75 double* gradient) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -080076 const double x = parameters[0];
77 *cost = 0.5 * (5.0 - x) * (5.0 - x);
Austin Schuh3de38b02024-06-25 18:25:10 -070078 if (gradient != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -080079 gradient[0] = x - 5.0;
80 }
81
82 return true;
83 }
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080084 int NumParameters() const final { return 1; }
Austin Schuh70cc9552019-01-21 19:46:48 -080085};
86
87struct RememberingCallback : public IterationCallback {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080088 explicit RememberingCallback(double* x) : calls(0), x(x) {}
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080089 CallbackReturnType operator()(const IterationSummary& summary) final {
Austin Schuh70cc9552019-01-21 19:46:48 -080090 x_values.push_back(*x);
91 return SOLVER_CONTINUE;
92 }
93 int calls;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080094 double* x;
Austin Schuh70cc9552019-01-21 19:46:48 -080095 std::vector<double> x_values;
96};
97
Austin Schuh70cc9552019-01-21 19:46:48 -080098TEST(Solver, UpdateStateEveryIterationOption) {
99 double x = 50.0;
100 const double original_x = x;
101
102 ceres::GradientProblem problem(new QuadraticFunction);
103 ceres::GradientProblemSolver::Options options;
104 RememberingCallback callback(&x);
105 options.callbacks.push_back(&callback);
106 ceres::GradientProblemSolver::Summary summary;
107
108 int num_iterations;
109
110 // First try: no updating.
111 ceres::Solve(options, problem, &x, &summary);
112 num_iterations = summary.iterations.size() - 1;
113 EXPECT_GT(num_iterations, 1);
Austin Schuh3de38b02024-06-25 18:25:10 -0700114 for (double value : callback.x_values) {
115 EXPECT_EQ(50.0, value);
Austin Schuh70cc9552019-01-21 19:46:48 -0800116 }
117
118 // Second try: with updating
119 x = 50.0;
120 options.update_state_every_iteration = true;
121 callback.x_values.clear();
122 ceres::Solve(options, problem, &x, &summary);
123 num_iterations = summary.iterations.size() - 1;
124 EXPECT_GT(num_iterations, 1);
125 EXPECT_EQ(original_x, callback.x_values[0]);
126 EXPECT_NE(original_x, callback.x_values[1]);
127}
128
Austin Schuh3de38b02024-06-25 18:25:10 -0700129} // namespace ceres::internal