blob: 613499522bfcff8c6cc450f39c5a53aa301e05b6 [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: keir@google.com (Keir Mierle)
30// sameeragarwal@google.com (Sameer Agarwal)
31//
32// End-to-end tests for Ceres using Powell's function.
33
34#include <cmath>
35#include <cstdlib>
36
37#include "ceres/autodiff_cost_function.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070038#include "ceres/internal/config.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080039#include "ceres/problem.h"
40#include "ceres/solver.h"
41#include "ceres/test_util.h"
42#include "ceres/types.h"
43#include "glog/logging.h"
44#include "gtest/gtest.h"
45
Austin Schuh3de38b02024-06-25 18:25:10 -070046namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080047
48// This class implements the SystemTestProblem interface and provides
49// access to an implementation of Powell's singular function.
50//
51// F = 1/2 (f1^2 + f2^2 + f3^2 + f4^2)
52//
53// f1 = x1 + 10*x2;
54// f2 = sqrt(5) * (x3 - x4)
55// f3 = (x2 - 2*x3)^2
56// f4 = sqrt(10) * (x1 - x4)^2
57//
58// The starting values are x1 = 3, x2 = -1, x3 = 0, x4 = 1.
59// The minimum is 0 at (x1, x2, x3, x4) = 0.
60//
61// From: Testing Unconstrained Optimization Software by Jorge J. More, Burton S.
62// Garbow and Kenneth E. Hillstrom in ACM Transactions on Mathematical Software,
63// Vol 7(1), March 1981.
64class PowellsFunction {
65 public:
66 PowellsFunction() {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080067 x_[0] = 3.0;
Austin Schuh70cc9552019-01-21 19:46:48 -080068 x_[1] = -1.0;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080069 x_[2] = 0.0;
70 x_[3] = 1.0;
Austin Schuh70cc9552019-01-21 19:46:48 -080071
72 problem_.AddResidualBlock(
Austin Schuh3de38b02024-06-25 18:25:10 -070073 new AutoDiffCostFunction<F1, 1, 1, 1>(new F1), nullptr, &x_[0], &x_[1]);
Austin Schuh70cc9552019-01-21 19:46:48 -080074 problem_.AddResidualBlock(
Austin Schuh3de38b02024-06-25 18:25:10 -070075 new AutoDiffCostFunction<F2, 1, 1, 1>(new F2), nullptr, &x_[2], &x_[3]);
Austin Schuh70cc9552019-01-21 19:46:48 -080076 problem_.AddResidualBlock(
Austin Schuh3de38b02024-06-25 18:25:10 -070077 new AutoDiffCostFunction<F3, 1, 1, 1>(new F3), nullptr, &x_[1], &x_[2]);
Austin Schuh70cc9552019-01-21 19:46:48 -080078 problem_.AddResidualBlock(
Austin Schuh3de38b02024-06-25 18:25:10 -070079 new AutoDiffCostFunction<F4, 1, 1, 1>(new F4), nullptr, &x_[0], &x_[3]);
Austin Schuh70cc9552019-01-21 19:46:48 -080080
81 // Settings for the reference solution.
82 options_.linear_solver_type = ceres::DENSE_QR;
83 options_.max_num_iterations = 10;
84 options_.num_threads = 1;
85 }
86
87 Problem* mutable_problem() { return &problem_; }
88 Solver::Options* mutable_solver_options() { return &options_; }
89
90 static double kResidualTolerance;
91
92 private:
93 // Templated functions used for automatically differentiated cost
94 // functions.
95 class F1 {
96 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080097 template <typename T>
98 bool operator()(const T* const x1, const T* const x2, T* residual) const {
Austin Schuh70cc9552019-01-21 19:46:48 -080099 // f1 = x1 + 10 * x2;
Austin Schuh3de38b02024-06-25 18:25:10 -0700100 *residual = x1[0] + 10.0 * x2[0];
Austin Schuh70cc9552019-01-21 19:46:48 -0800101 return true;
102 }
103 };
104
105 class F2 {
106 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800107 template <typename T>
108 bool operator()(const T* const x3, const T* const x4, T* residual) const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800109 // f2 = sqrt(5) (x3 - x4)
Austin Schuh3de38b02024-06-25 18:25:10 -0700110 *residual = sqrt(5.0) * (x3[0] - x4[0]);
Austin Schuh70cc9552019-01-21 19:46:48 -0800111 return true;
112 }
113 };
114
115 class F3 {
116 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800117 template <typename T>
Austin Schuh3de38b02024-06-25 18:25:10 -0700118 bool operator()(const T* const x2, const T* const x3, T* residual) const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800119 // f3 = (x2 - 2 x3)^2
Austin Schuh3de38b02024-06-25 18:25:10 -0700120 residual[0] = (x2[0] - 2.0 * x3[0]) * (x2[0] - 2.0 * x3[0]);
Austin Schuh70cc9552019-01-21 19:46:48 -0800121 return true;
122 }
123 };
124
125 class F4 {
126 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800127 template <typename T>
128 bool operator()(const T* const x1, const T* const x4, T* residual) const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800129 // f4 = sqrt(10) (x1 - x4)^2
130 residual[0] = sqrt(10.0) * (x1[0] - x4[0]) * (x1[0] - x4[0]);
131 return true;
132 }
133 };
134
135 double x_[4];
136 Problem problem_;
137 Solver::Options options_;
138};
139
140double PowellsFunction::kResidualTolerance = 1e-8;
141
Austin Schuh3de38b02024-06-25 18:25:10 -0700142using PowellTest = SystemTest<PowellsFunction>;
Austin Schuh70cc9552019-01-21 19:46:48 -0800143
144TEST_F(PowellTest, DenseQR) {
145 PowellsFunction powells_function;
146 Solver::Options* options = powells_function.mutable_solver_options();
147 options->linear_solver_type = DENSE_QR;
148 RunSolverForConfigAndExpectResidualsMatch(*options,
149 powells_function.mutable_problem());
150}
151
152TEST_F(PowellTest, DenseNormalCholesky) {
153 PowellsFunction powells_function;
154 Solver::Options* options = powells_function.mutable_solver_options();
155 options->linear_solver_type = DENSE_NORMAL_CHOLESKY;
156 RunSolverForConfigAndExpectResidualsMatch(*options,
157 powells_function.mutable_problem());
158}
159
160TEST_F(PowellTest, DenseSchur) {
161 PowellsFunction powells_function;
162 Solver::Options* options = powells_function.mutable_solver_options();
163 options->linear_solver_type = DENSE_SCHUR;
164 RunSolverForConfigAndExpectResidualsMatch(*options,
165 powells_function.mutable_problem());
166}
167
168TEST_F(PowellTest, IterativeSchurWithJacobi) {
169 PowellsFunction powells_function;
170 Solver::Options* options = powells_function.mutable_solver_options();
171 options->linear_solver_type = ITERATIVE_SCHUR;
172 options->sparse_linear_algebra_library_type = NO_SPARSE;
173 options->preconditioner_type = JACOBI;
174 RunSolverForConfigAndExpectResidualsMatch(*options,
175 powells_function.mutable_problem());
176}
177
178#ifndef CERES_NO_SUITESPARSE
179TEST_F(PowellTest, SparseNormalCholeskyUsingSuiteSparse) {
180 PowellsFunction powells_function;
181 Solver::Options* options = powells_function.mutable_solver_options();
182 options->linear_solver_type = SPARSE_NORMAL_CHOLESKY;
183 options->sparse_linear_algebra_library_type = SUITE_SPARSE;
184 RunSolverForConfigAndExpectResidualsMatch(*options,
185 powells_function.mutable_problem());
186}
187#endif // CERES_NO_SUITESPARSE
188
Austin Schuh70cc9552019-01-21 19:46:48 -0800189#ifndef CERES_NO_ACCELERATE_SPARSE
190TEST_F(PowellTest, SparseNormalCholeskyUsingAccelerateSparse) {
191 PowellsFunction powells_function;
192 Solver::Options* options = powells_function.mutable_solver_options();
193 options->linear_solver_type = SPARSE_NORMAL_CHOLESKY;
194 options->sparse_linear_algebra_library_type = ACCELERATE_SPARSE;
195 RunSolverForConfigAndExpectResidualsMatch(*options,
196 powells_function.mutable_problem());
197}
198#endif // CERES_NO_ACCELERATE_SPARSE
199
200#ifdef CERES_USE_EIGEN_SPARSE
201TEST_F(PowellTest, SparseNormalCholeskyUsingEigenSparse) {
202 PowellsFunction powells_function;
203 Solver::Options* options = powells_function.mutable_solver_options();
204 options->linear_solver_type = SPARSE_NORMAL_CHOLESKY;
205 options->sparse_linear_algebra_library_type = EIGEN_SPARSE;
206 RunSolverForConfigAndExpectResidualsMatch(*options,
207 powells_function.mutable_problem());
208}
209#endif // CERES_USE_EIGEN_SPARSE
210
Austin Schuh3de38b02024-06-25 18:25:10 -0700211} // namespace ceres::internal