Debian Codegraph @

internal/ceres/conjugate_gradients_solver_test.cc

raw symbols SCIP
1 // Ceres Solver - A fast non-linear least squares minimizercxx . . $ `<file>/internal/ceres/conjugate_gradients_solver_test.cc`/
2 // Copyright 2023 Google Inc. All rights reserved.
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: [email protected] (Fred Pighin)
30 //
31 // TODO(sameeragarwal): More comprehensive testing with larger and
32 // more badly conditioned problem.
33
34 #include "ceres/conjugate_gradients_solver.h"cxx . . $ `<file>/internal/ceres/conjugate_gradients_solver.h`/
35
36 #include <memory>
37
38 #include "ceres/internal/eigen.h"cxx . . $ `<file>/include/ceres/internal/eigen.h`/
39 #include "ceres/linear_solver.h"cxx . . $ `<file>/internal/ceres/linear_solver.h`/
40 #include "ceres/preconditioner.h"cxx . . $ `<file>/internal/ceres/preconditioner.h`/
41 #include "ceres/triplet_sparse_matrix.h"cxx . . $ `<file>/internal/ceres/triplet_sparse_matrix.h`/
42 #include "ceres/types.h"cxx . . $ `<file>/include/ceres/types.h`/
43 #include "gtest/gtest.h"cxx . . $ `<file>/internal/ceres/gtest/gtest.h`/
44
45 namespace cerescxx . . $ ceres/::internalcxx . . $ ceres/internal/ {
46
47 TESTcxx . . $ `internal/ceres/gtest/gtest.h:12235:9`!(ConjugateGradientTest, Solves3x3IdentitySystem) {
48 double diagonal[] = {1.0, 1.0, 1.0};
49 stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<TripletSparseMatrixcxx . . $ ceres/internal/TripletSparseMatrix#> A(
50 TripletSparseMatrixcxx . . $ ceres/internal/TripletSparseMatrix#::CreateSparseDiagonalMatrixcxx . . $ ceres/internal/TripletSparseMatrix#CreateSparseDiagonalMatrix(876eb2cf1c0eb4c2).(diagonal, 3));
51 Vectorcxx . . $ ceres/Vector# bcxx . . $ Eigen/Matrix#Matrix(edcd8b2679e9bd1a).(3);
52 Vectorcxx . . $ ceres/Vector# xcxx . . $ Eigen/Matrix#Matrix(edcd8b2679e9bd1a).(3);
53
54 b(cxx . . $ Eigen/DenseCoeffsBase#`operator()`(1905f20a50b1238d).0) = 1.0;
55 b(cxx . . $ Eigen/DenseCoeffsBase#`operator()`(1905f20a50b1238d).1) = 2.0;
56 b(cxx . . $ Eigen/DenseCoeffsBase#`operator()`(1905f20a50b1238d).2) = 3.0;
57
58 x(cxx . . $ Eigen/DenseCoeffsBase#`operator()`(1905f20a50b1238d).0) = 1;
59 x(cxx . . $ Eigen/DenseCoeffsBase#`operator()`(1905f20a50b1238d).1) = 1;
60 x(cxx . . $ Eigen/DenseCoeffsBase#`operator()`(1905f20a50b1238d).2) = 1;
61
62 ConjugateGradientsSolverOptionscxx . . $ ceres/internal/ConjugateGradientsSolverOptions# cg_options;
63 cg_options.min_num_iterationscxx . . $ ceres/internal/ConjugateGradientsSolverOptions#min_num_iterations. = 1;
64 cg_options.max_num_iterationscxx . . $ ceres/internal/ConjugateGradientsSolverOptions#max_num_iterations. = 10;
65 cg_options.residual_reset_periodcxx . . $ ceres/internal/ConjugateGradientsSolverOptions#residual_reset_period. = 20;
66 cg_options.q_tolerancecxx . . $ ceres/internal/ConjugateGradientsSolverOptions#q_tolerance. = 0.0;
67 cg_options.r_tolerancecxx . . $ ceres/internal/ConjugateGradientsSolverOptions#r_tolerance. = 1e-9;
68
69 Vectorcxx . . $ ceres/Vector# scratchcxx . . $ Eigen/Matrix#Matrix(49f6e7a06ebc5aa8).[4];
70 for (int i = 0; i < 4; ++i) {
71 scratch[i] =cxx . . $ Eigen/Matrix#`operator=`(24f3321d7c59ffed). Vectorcxx . . $ ceres/Vector#::Zerocxx . . $ Eigen/DenseBase#Zero(b89831c754d9e743).(A->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).num_colscxx . . $ ceres/internal/TripletSparseMatrix#num_cols(455f465bc33b4cdf).());
72 }
73
74 IdentityPreconditionercxx . . $ ceres/internal/IdentityPreconditioner# identitycxx . . $ ceres/internal/IdentityPreconditioner#IdentityPreconditioner(d4f767463ce0a6b3).(A->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).num_colscxx . . $ ceres/internal/TripletSparseMatrix#num_cols(455f465bc33b4cdf).());
75 LinearOperatorAdaptercxx . . $ ceres/internal/LinearOperatorAdapter# lhscxx . . $ ceres/internal/LinearOperatorAdapter#LinearOperatorAdapter(834cfd4de106c3a6).(*cxx . . $ std/unique_ptr#`operator*`(26c95836c1e8ca05).A);
76 LinearOperatorAdaptercxx . . $ ceres/internal/LinearOperatorAdapter# preconditionercxx . . $ ceres/internal/LinearOperatorAdapter#LinearOperatorAdapter(834cfd4de106c3a6).(identity);
77 Vectorcxx . . $ ceres/Vector#* scratch_array[4] = {
78 &scratch[0], &scratch[1], &scratch[2], &scratch[3]};
79 auto summary = ConjugateGradientsSolvercxx . . $ ceres/internal/ConjugateGradientsSolver(a72ef782e3f4c311).(
80 cg_options, lhs, b, preconditioner, scratch_array, x);
81
82 EXPECT_EQcxx . . $ `internal/ceres/gtest/gtest.h:11925:9`!(summary.termination_type, LinearSolverTerminationType::SUCCESS);
83 ASSERT_EQcxx . . $ `internal/ceres/gtest/gtest.h:11955:10`!(summary.num_iterations, 1);
84
85 ASSERT_DOUBLE_EQcxx . . $ `internal/ceres/gtest/gtest.h:12038:9`!(1, x(0));
86 ASSERT_DOUBLE_EQcxx . . $ `internal/ceres/gtest/gtest.h:12038:9`!(2, x(1));
87 ASSERT_DOUBLE_EQcxx . . $ `internal/ceres/gtest/gtest.h:12038:9`!(3, x(2));
88 }
89
90 TESTcxx . . $ `internal/ceres/gtest/gtest.h:12235:9`!(ConjuateGradientTest, Solves3x3SymmetricSystem) {
91 stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<TripletSparseMatrixcxx . . $ ceres/internal/TripletSparseMatrix#> Acxx . . $ std/unique_ptr#unique_ptr(c8c3025d73122d7d).(new TripletSparseMatrixcxx . . $ ceres/internal/TripletSparseMatrix#TripletSparseMatrix(b1146eedae1e1d0b).(3, 3, 9));
92 Vectorcxx . . $ ceres/Vector# bcxx . . $ Eigen/Matrix#Matrix(edcd8b2679e9bd1a).(3);
93 Vectorcxx . . $ ceres/Vector# xcxx . . $ Eigen/Matrix#Matrix(edcd8b2679e9bd1a).(3);
94
95 // | 2 -1 0|
96 // A = |-1 2 -1| is symmetric positive definite.
97 // | 0 -1 2|
98 int* Ai = A->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).mutable_rowscxx . . $ ceres/internal/TripletSparseMatrix#mutable_rows(a7741f82f5fc3640).();
99 int* Aj = A->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).mutable_colscxx . . $ ceres/internal/TripletSparseMatrix#mutable_cols(a7741f82f5fc3640).();
100 double* Ax = A->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).mutable_valuescxx . . $ ceres/internal/TripletSparseMatrix#mutable_values(78c35135705b60d1).();
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->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).set_num_nonzeroscxx . . $ ceres/internal/TripletSparseMatrix#set_num_nonzeros(d4f767463ce0a6b3).(9);
119
120 b(cxx . . $ Eigen/DenseCoeffsBase#`operator()`(1905f20a50b1238d).0) = -1;
121 b(cxx . . $ Eigen/DenseCoeffsBase#`operator()`(1905f20a50b1238d).1) = 0;
122 b(cxx . . $ Eigen/DenseCoeffsBase#`operator()`(1905f20a50b1238d).2) = 3;
123
124 x(cxx . . $ Eigen/DenseCoeffsBase#`operator()`(1905f20a50b1238d).0) = 1;
125 x(cxx . . $ Eigen/DenseCoeffsBase#`operator()`(1905f20a50b1238d).1) = 1;
126 x(cxx . . $ Eigen/DenseCoeffsBase#`operator()`(1905f20a50b1238d).2) = 1;
127
128 ConjugateGradientsSolverOptionscxx . . $ ceres/internal/ConjugateGradientsSolverOptions# cg_options;
129 cg_options.min_num_iterationscxx . . $ ceres/internal/ConjugateGradientsSolverOptions#min_num_iterations. = 1;
130 cg_options.max_num_iterationscxx . . $ ceres/internal/ConjugateGradientsSolverOptions#max_num_iterations. = 10;
131 cg_options.residual_reset_periodcxx . . $ ceres/internal/ConjugateGradientsSolverOptions#residual_reset_period. = 20;
132 cg_options.q_tolerancecxx . . $ ceres/internal/ConjugateGradientsSolverOptions#q_tolerance. = 0.0;
133 cg_options.r_tolerancecxx . . $ ceres/internal/ConjugateGradientsSolverOptions#r_tolerance. = 1e-9;
134
135 Vectorcxx . . $ ceres/Vector# scratchcxx . . $ Eigen/Matrix#Matrix(49f6e7a06ebc5aa8).[4];
136 for (int i = 0; i < 4; ++i) {
137 scratch[i] =cxx . . $ Eigen/Matrix#`operator=`(24f3321d7c59ffed). Vectorcxx . . $ ceres/Vector#::Zerocxx . . $ Eigen/DenseBase#Zero(b89831c754d9e743).(A->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).num_colscxx . . $ ceres/internal/TripletSparseMatrix#num_cols(455f465bc33b4cdf).());
138 }
139 Vectorcxx . . $ ceres/Vector#* scratch_array[4] = {
140 &scratch[0], &scratch[1], &scratch[2], &scratch[3]};
141 IdentityPreconditionercxx . . $ ceres/internal/IdentityPreconditioner# identitycxx . . $ ceres/internal/IdentityPreconditioner#IdentityPreconditioner(d4f767463ce0a6b3).(A->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).num_colscxx . . $ ceres/internal/TripletSparseMatrix#num_cols(455f465bc33b4cdf).());
142 LinearOperatorAdaptercxx . . $ ceres/internal/LinearOperatorAdapter# lhscxx . . $ ceres/internal/LinearOperatorAdapter#LinearOperatorAdapter(834cfd4de106c3a6).(*cxx . . $ std/unique_ptr#`operator*`(26c95836c1e8ca05).A);
143 LinearOperatorAdaptercxx . . $ ceres/internal/LinearOperatorAdapter# preconditionercxx . . $ ceres/internal/LinearOperatorAdapter#LinearOperatorAdapter(834cfd4de106c3a6).(identity);
144
145 auto summary = ConjugateGradientsSolvercxx . . $ ceres/internal/ConjugateGradientsSolver(a72ef782e3f4c311).(
146 cg_options, lhs, b, preconditioner, scratch_array, x);
147
148 EXPECT_EQcxx . . $ `internal/ceres/gtest/gtest.h:11925:9`!(summary.termination_type, LinearSolverTerminationType::SUCCESS);
149
150 ASSERT_DOUBLE_EQcxx . . $ `internal/ceres/gtest/gtest.h:12038:9`!(0, x(0));
151 ASSERT_DOUBLE_EQcxx . . $ `internal/ceres/gtest/gtest.h:12038:9`!(1, x(1));
152 ASSERT_DOUBLE_EQcxx . . $ `internal/ceres/gtest/gtest.h:12038:9`!(2, x(2));
153 }
154
155 } // namespace ceres::internal