• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3 // http://code.google.com/p/ceres-solver/
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: sameeragarwal@google.com (Sameer Agarwal)
30 
31 #include <cstddef>
32 #include "ceres/block_sparse_matrix.h"
33 #include "ceres/block_structure.h"
34 #include "ceres/casts.h"
35 #include "ceres/internal/scoped_ptr.h"
36 #include "ceres/linear_least_squares_problems.h"
37 #include "ceres/linear_solver.h"
38 #include "ceres/schur_complement_solver.h"
39 #include "ceres/triplet_sparse_matrix.h"
40 #include "ceres/types.h"
41 #include "glog/logging.h"
42 #include "gtest/gtest.h"
43 
44 namespace ceres {
45 namespace internal {
46 
47 class SchurComplementSolverTest : public ::testing::Test {
48  protected:
SetUpFromProblemId(int problem_id)49   void SetUpFromProblemId(int problem_id) {
50     scoped_ptr<LinearLeastSquaresProblem> problem(
51         CreateLinearLeastSquaresProblemFromId(problem_id));
52 
53     CHECK_NOTNULL(problem.get());
54     A.reset(down_cast<BlockSparseMatrix*>(problem->A.release()));
55     b.reset(problem->b.release());
56     D.reset(problem->D.release());
57 
58     num_cols = A->num_cols();
59     num_rows = A->num_rows();
60     num_eliminate_blocks = problem->num_eliminate_blocks;
61 
62     x.reset(new double[num_cols]);
63     sol.reset(new double[num_cols]);
64     sol_d.reset(new double[num_cols]);
65 
66     LinearSolver::Options options;
67     options.type = DENSE_QR;
68 
69     scoped_ptr<LinearSolver> qr(LinearSolver::Create(options));
70 
71     TripletSparseMatrix triplet_A(A->num_rows(),
72                                   A->num_cols(),
73                                   A->num_nonzeros());
74     A->ToTripletSparseMatrix(&triplet_A);
75 
76     // Gold standard solutions using dense QR factorization.
77     DenseSparseMatrix dense_A(triplet_A);
78     LinearSolver::Summary summary1 =
79         qr->Solve(&dense_A,
80                   b.get(),
81                   LinearSolver::PerSolveOptions(),
82                   sol.get());
83 
84     // Gold standard solution with appended diagonal.
85     LinearSolver::PerSolveOptions per_solve_options;
86     per_solve_options.D = D.get();
87     LinearSolver::Summary summary2 =
88         qr->Solve(&dense_A,
89                   b.get(),
90                   per_solve_options,
91                   sol_d.get());
92   }
93 
ComputeAndCompareSolutions(int problem_id,bool regularization,ceres::LinearSolverType linear_solver_type,ceres::SparseLinearAlgebraLibraryType sparse_linear_algebra_library)94   void ComputeAndCompareSolutions(
95       int problem_id,
96       bool regularization,
97       ceres::LinearSolverType linear_solver_type,
98       ceres::SparseLinearAlgebraLibraryType sparse_linear_algebra_library) {
99     SetUpFromProblemId(problem_id);
100     LinearSolver::Options options;
101     options.elimination_groups.push_back(num_eliminate_blocks);
102     options.elimination_groups.push_back(
103         A->block_structure()->cols.size() - num_eliminate_blocks);
104     options.type = linear_solver_type;
105     options.sparse_linear_algebra_library = sparse_linear_algebra_library;
106 
107     scoped_ptr<LinearSolver> solver(LinearSolver::Create(options));
108 
109     LinearSolver::PerSolveOptions per_solve_options;
110     LinearSolver::Summary summary;
111     if (regularization) {
112       per_solve_options.D = D.get();
113     }
114 
115     summary = solver->Solve(A.get(), b.get(), per_solve_options, x.get());
116 
117     if (regularization) {
118       for (int i = 0; i < num_cols; ++i) {
119         ASSERT_NEAR(sol_d.get()[i], x[i], 1e-10);
120       }
121     } else {
122       for (int i = 0; i < num_cols; ++i) {
123         ASSERT_NEAR(sol.get()[i], x[i], 1e-10);
124       }
125     }
126   }
127 
128   int num_rows;
129   int num_cols;
130   int num_eliminate_blocks;
131 
132   scoped_ptr<BlockSparseMatrix> A;
133   scoped_array<double> b;
134   scoped_array<double> x;
135   scoped_array<double> D;
136   scoped_array<double> sol;
137   scoped_array<double> sol_d;
138 };
139 
140 #ifndef CERES_NO_SUITESPARSE
TEST_F(SchurComplementSolverTest,SparseSchurWithSuiteSparse)141 TEST_F(SchurComplementSolverTest, SparseSchurWithSuiteSparse) {
142   ComputeAndCompareSolutions(2, false, SPARSE_SCHUR, SUITE_SPARSE);
143   ComputeAndCompareSolutions(3, false, SPARSE_SCHUR, SUITE_SPARSE);
144   ComputeAndCompareSolutions(2, true, SPARSE_SCHUR, SUITE_SPARSE);
145   ComputeAndCompareSolutions(3, true, SPARSE_SCHUR, SUITE_SPARSE);
146 }
147 #endif  // CERES_NO_SUITESPARSE
148 
149 #ifndef CERES_NO_CXSPARSE
TEST_F(SchurComplementSolverTest,SparseSchurWithCXSparse)150 TEST_F(SchurComplementSolverTest, SparseSchurWithCXSparse) {
151   ComputeAndCompareSolutions(2, false, SPARSE_SCHUR, CX_SPARSE);
152   ComputeAndCompareSolutions(3, false, SPARSE_SCHUR, CX_SPARSE);
153   ComputeAndCompareSolutions(2, true, SPARSE_SCHUR, CX_SPARSE);
154   ComputeAndCompareSolutions(3, true, SPARSE_SCHUR, CX_SPARSE);
155 }
156 #endif  // CERES_NO_CXSPARSE
157 
TEST_F(SchurComplementSolverTest,DenseSchur)158 TEST_F(SchurComplementSolverTest, DenseSchur) {
159   // The sparse linear algebra library type is ignored for
160   // DENSE_SCHUR.
161   ComputeAndCompareSolutions(2, false, DENSE_SCHUR, SUITE_SPARSE);
162   ComputeAndCompareSolutions(3, false, DENSE_SCHUR, SUITE_SPARSE);
163   ComputeAndCompareSolutions(2, true, DENSE_SCHUR, SUITE_SPARSE);
164   ComputeAndCompareSolutions(3, true, DENSE_SCHUR, SUITE_SPARSE);
165 }
166 
167 }  // namespace internal
168 }  // namespace ceres
169