1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2017 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: fredp@google.com (Fred Pighin)
30 //
31 // TODO(sameeragarwal): More comprehensive testing with larger and
32 // more badly conditioned problem.
33
34 #include "gtest/gtest.h"
35 #include "ceres/conjugate_gradients_solver.h"
36 #include "ceres/linear_solver.h"
37 #include "ceres/triplet_sparse_matrix.h"
38 #include "ceres/internal/eigen.h"
39 #include "ceres/internal/scoped_ptr.h"
40 #include "ceres/types.h"
41
42 namespace ceres {
43 namespace internal {
44
TEST(ConjugateGradientTest,Solves3x3IdentitySystem)45 TEST(ConjugateGradientTest, Solves3x3IdentitySystem) {
46 double diagonal[] = { 1.0, 1.0, 1.0 };
47 scoped_ptr<TripletSparseMatrix>
48 A(TripletSparseMatrix::CreateSparseDiagonalMatrix(diagonal, 3));
49 Vector b(3);
50 Vector x(3);
51
52 b(0) = 1.0;
53 b(1) = 2.0;
54 b(2) = 3.0;
55
56 x(0) = 1;
57 x(1) = 1;
58 x(2) = 1;
59
60 LinearSolver::Options options;
61 options.max_num_iterations = 10;
62
63 LinearSolver::PerSolveOptions per_solve_options;
64 per_solve_options.r_tolerance = 1e-9;
65
66 ConjugateGradientsSolver solver(options);
67 LinearSolver::Summary summary =
68 solver.Solve(A.get(), b.data(), per_solve_options, x.data());
69
70 EXPECT_EQ(summary.termination_type, LINEAR_SOLVER_SUCCESS);
71 ASSERT_EQ(summary.num_iterations, 1);
72
73 ASSERT_DOUBLE_EQ(1, x(0));
74 ASSERT_DOUBLE_EQ(2, x(1));
75 ASSERT_DOUBLE_EQ(3, x(2));
76 }
77
78
TEST(ConjuateGradientTest,Solves3x3SymmetricSystem)79 TEST(ConjuateGradientTest, Solves3x3SymmetricSystem) {
80 scoped_ptr<TripletSparseMatrix> A(new TripletSparseMatrix(3, 3, 9));
81 Vector b(3);
82 Vector x(3);
83
84 // | 2 -1 0|
85 // A = |-1 2 -1| is symmetric positive definite.
86 // | 0 -1 2|
87 int* Ai = A->mutable_rows();
88 int* Aj = A->mutable_cols();
89 double* Ax = A->mutable_values();
90 int counter = 0;
91 for (int i = 0; i < 3; ++i) {
92 for (int j = 0; j < 3; ++j) {
93 Ai[counter] = i;
94 Aj[counter] = j;
95 ++counter;
96 }
97 }
98 Ax[0] = 2.;
99 Ax[1] = -1.;
100 Ax[2] = 0;
101 Ax[3] = -1.;
102 Ax[4] = 2;
103 Ax[5] = -1;
104 Ax[6] = 0;
105 Ax[7] = -1;
106 Ax[8] = 2;
107 A->set_num_nonzeros(9);
108
109 b(0) = -1;
110 b(1) = 0;
111 b(2) = 3;
112
113 x(0) = 1;
114 x(1) = 1;
115 x(2) = 1;
116
117 LinearSolver::Options options;
118 options.max_num_iterations = 10;
119
120 LinearSolver::PerSolveOptions per_solve_options;
121 per_solve_options.r_tolerance = 1e-9;
122
123 ConjugateGradientsSolver solver(options);
124 LinearSolver::Summary summary =
125 solver.Solve(A.get(), b.data(), per_solve_options, x.data());
126
127 EXPECT_EQ(summary.termination_type, LINEAR_SOLVER_SUCCESS);
128
129 ASSERT_DOUBLE_EQ(0, x(0));
130 ASSERT_DOUBLE_EQ(1, x(1));
131 ASSERT_DOUBLE_EQ(2, x(2));
132 }
133
134 } // namespace internal
135 } // namespace ceres
136