blob: 47275649536d270e2f207b061af4a2dfd492f0bc [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: fredp@google.com (Fred Pighin)
30//
31// TODO(sameeragarwal): More comprehensive testing with larger and
32// more badly conditioned problem.
33
Austin Schuh70cc9552019-01-21 19:46:48 -080034#include "ceres/conjugate_gradients_solver.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080035
36#include <memory>
37
38#include "ceres/internal/eigen.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080039#include "ceres/linear_solver.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070040#include "ceres/preconditioner.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080041#include "ceres/triplet_sparse_matrix.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080042#include "ceres/types.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080043#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
47TEST(ConjugateGradientTest, Solves3x3IdentitySystem) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080048 double diagonal[] = {1.0, 1.0, 1.0};
49 std::unique_ptr<TripletSparseMatrix> A(
50 TripletSparseMatrix::CreateSparseDiagonalMatrix(diagonal, 3));
Austin Schuh70cc9552019-01-21 19:46:48 -080051 Vector b(3);
52 Vector x(3);
53
54 b(0) = 1.0;
55 b(1) = 2.0;
56 b(2) = 3.0;
57
58 x(0) = 1;
59 x(1) = 1;
60 x(2) = 1;
61
Austin Schuh3de38b02024-06-25 18:25:10 -070062 ConjugateGradientsSolverOptions cg_options;
63 cg_options.min_num_iterations = 1;
64 cg_options.max_num_iterations = 10;
65 cg_options.residual_reset_period = 20;
66 cg_options.q_tolerance = 0.0;
67 cg_options.r_tolerance = 1e-9;
Austin Schuh70cc9552019-01-21 19:46:48 -080068
Austin Schuh3de38b02024-06-25 18:25:10 -070069 Vector scratch[4];
70 for (int i = 0; i < 4; ++i) {
71 scratch[i] = Vector::Zero(A->num_cols());
72 }
Austin Schuh70cc9552019-01-21 19:46:48 -080073
Austin Schuh3de38b02024-06-25 18:25:10 -070074 IdentityPreconditioner identity(A->num_cols());
75 LinearOperatorAdapter lhs(*A);
76 LinearOperatorAdapter preconditioner(identity);
77 Vector* scratch_array[4] = {
78 &scratch[0], &scratch[1], &scratch[2], &scratch[3]};
79 auto summary = ConjugateGradientsSolver(
80 cg_options, lhs, b, preconditioner, scratch_array, x);
Austin Schuh70cc9552019-01-21 19:46:48 -080081
Austin Schuh3de38b02024-06-25 18:25:10 -070082 EXPECT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS);
Austin Schuh70cc9552019-01-21 19:46:48 -080083 ASSERT_EQ(summary.num_iterations, 1);
84
85 ASSERT_DOUBLE_EQ(1, x(0));
86 ASSERT_DOUBLE_EQ(2, x(1));
87 ASSERT_DOUBLE_EQ(3, x(2));
88}
89
Austin Schuh70cc9552019-01-21 19:46:48 -080090TEST(ConjuateGradientTest, Solves3x3SymmetricSystem) {
91 std::unique_ptr<TripletSparseMatrix> A(new TripletSparseMatrix(3, 3, 9));
92 Vector b(3);
93 Vector x(3);
94
95 // | 2 -1 0|
96 // A = |-1 2 -1| is symmetric positive definite.
97 // | 0 -1 2|
98 int* Ai = A->mutable_rows();
99 int* Aj = A->mutable_cols();
100 double* Ax = A->mutable_values();
101 int counter = 0;
102 for (int i = 0; i < 3; ++i) {
103 for (int j = 0; j < 3; ++j) {
104 Ai[counter] = i;
105 Aj[counter] = j;
106 ++counter;
107 }
108 }
109 Ax[0] = 2.;
110 Ax[1] = -1.;
111 Ax[2] = 0;
112 Ax[3] = -1.;
113 Ax[4] = 2;
114 Ax[5] = -1;
115 Ax[6] = 0;
116 Ax[7] = -1;
117 Ax[8] = 2;
118 A->set_num_nonzeros(9);
119
120 b(0) = -1;
121 b(1) = 0;
122 b(2) = 3;
123
124 x(0) = 1;
125 x(1) = 1;
126 x(2) = 1;
127
Austin Schuh3de38b02024-06-25 18:25:10 -0700128 ConjugateGradientsSolverOptions cg_options;
129 cg_options.min_num_iterations = 1;
130 cg_options.max_num_iterations = 10;
131 cg_options.residual_reset_period = 20;
132 cg_options.q_tolerance = 0.0;
133 cg_options.r_tolerance = 1e-9;
Austin Schuh70cc9552019-01-21 19:46:48 -0800134
Austin Schuh3de38b02024-06-25 18:25:10 -0700135 Vector scratch[4];
136 for (int i = 0; i < 4; ++i) {
137 scratch[i] = Vector::Zero(A->num_cols());
138 }
139 Vector* scratch_array[4] = {
140 &scratch[0], &scratch[1], &scratch[2], &scratch[3]};
141 IdentityPreconditioner identity(A->num_cols());
142 LinearOperatorAdapter lhs(*A);
143 LinearOperatorAdapter preconditioner(identity);
Austin Schuh70cc9552019-01-21 19:46:48 -0800144
Austin Schuh3de38b02024-06-25 18:25:10 -0700145 auto summary = ConjugateGradientsSolver(
146 cg_options, lhs, b, preconditioner, scratch_array, x);
Austin Schuh70cc9552019-01-21 19:46:48 -0800147
Austin Schuh3de38b02024-06-25 18:25:10 -0700148 EXPECT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS);
Austin Schuh70cc9552019-01-21 19:46:48 -0800149
150 ASSERT_DOUBLE_EQ(0, x(0));
151 ASSERT_DOUBLE_EQ(1, x(1));
152 ASSERT_DOUBLE_EQ(2, x(2));
153}
154
Austin Schuh3de38b02024-06-25 18:25:10 -0700155} // namespace ceres::internal