Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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: fredp@google.com (Fred Pighin) |
| 30 | // |
| 31 | // TODO(sameeragarwal): More comprehensive testing with larger and |
| 32 | // more badly conditioned problem. |
| 33 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 34 | #include "ceres/conjugate_gradients_solver.h" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 35 | |
| 36 | #include <memory> |
| 37 | |
| 38 | #include "ceres/internal/eigen.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 39 | #include "ceres/linear_solver.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 40 | #include "ceres/preconditioner.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 41 | #include "ceres/triplet_sparse_matrix.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 42 | #include "ceres/types.h" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 43 | #include "gtest/gtest.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 44 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 45 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 46 | |
| 47 | TEST(ConjugateGradientTest, Solves3x3IdentitySystem) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 48 | double diagonal[] = {1.0, 1.0, 1.0}; |
| 49 | std::unique_ptr<TripletSparseMatrix> A( |
| 50 | TripletSparseMatrix::CreateSparseDiagonalMatrix(diagonal, 3)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 51 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 62 | 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 Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 68 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 69 | Vector scratch[4]; |
| 70 | for (int i = 0; i < 4; ++i) { |
| 71 | scratch[i] = Vector::Zero(A->num_cols()); |
| 72 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 73 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 74 | 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 Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 81 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 82 | EXPECT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 83 | 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 Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 90 | TEST(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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 128 | 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 Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 134 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 135 | 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 Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 144 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 145 | auto summary = ConjugateGradientsSolver( |
| 146 | cg_options, lhs, b, preconditioner, scratch_array, x); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 147 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 148 | EXPECT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 149 | |
| 150 | ASSERT_DOUBLE_EQ(0, x(0)); |
| 151 | ASSERT_DOUBLE_EQ(1, x(1)); |
| 152 | ASSERT_DOUBLE_EQ(2, x(2)); |
| 153 | } |
| 154 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 155 | } // namespace ceres::internal |