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 // TODO(sameeragarwal): Add support for larger, more complicated and
32 // poorly conditioned problems both for correctness testing as well as
33 // benchmarking.
34
35 #include "ceres/iterative_schur_complement_solver.h"
36
37 #include <cstddef>
38 #include "Eigen/Dense"
39 #include "ceres/block_random_access_dense_matrix.h"
40 #include "ceres/block_sparse_matrix.h"
41 #include "ceres/casts.h"
42 #include "ceres/internal/eigen.h"
43 #include "ceres/internal/scoped_ptr.h"
44 #include "ceres/linear_least_squares_problems.h"
45 #include "ceres/linear_solver.h"
46 #include "ceres/schur_eliminator.h"
47 #include "ceres/triplet_sparse_matrix.h"
48 #include "ceres/types.h"
49 #include "glog/logging.h"
50 #include "gtest/gtest.h"
51
52 namespace ceres {
53 namespace internal {
54
55 using testing::AssertionResult;
56
57 const double kEpsilon = 1e-14;
58
59 class IterativeSchurComplementSolverTest : public ::testing::Test {
60 protected :
SetUpProblem(int problem_id)61 void SetUpProblem(int problem_id) {
62 scoped_ptr<LinearLeastSquaresProblem> problem(
63 CreateLinearLeastSquaresProblemFromId(problem_id));
64
65 CHECK_NOTNULL(problem.get());
66 A_.reset(down_cast<BlockSparseMatrix*>(problem->A.release()));
67 b_.reset(problem->b.release());
68 D_.reset(problem->D.release());
69
70 num_cols_ = A_->num_cols();
71 num_rows_ = A_->num_rows();
72 num_eliminate_blocks_ = problem->num_eliminate_blocks;
73 }
74
TestSolver(double * D)75 AssertionResult TestSolver(double* D) {
76 TripletSparseMatrix triplet_A(A_->num_rows(),
77 A_->num_cols(),
78 A_->num_nonzeros());
79 A_->ToTripletSparseMatrix(&triplet_A);
80
81 DenseSparseMatrix dense_A(triplet_A);
82
83 LinearSolver::Options options;
84 options.type = DENSE_QR;
85 scoped_ptr<LinearSolver> qr(LinearSolver::Create(options));
86
87 LinearSolver::PerSolveOptions per_solve_options;
88 per_solve_options.D = D;
89 Vector reference_solution(num_cols_);
90 qr->Solve(&dense_A, b_.get(), per_solve_options, reference_solution.data());
91
92 options.elimination_groups.push_back(num_eliminate_blocks_);
93 options.elimination_groups.push_back(0);
94 options.max_num_iterations = num_cols_;
95 options.preconditioner_type = SCHUR_JACOBI;
96 IterativeSchurComplementSolver isc(options);
97
98 Vector isc_sol(num_cols_);
99 per_solve_options.r_tolerance = 1e-12;
100 isc.Solve(A_.get(), b_.get(), per_solve_options, isc_sol.data());
101 double diff = (isc_sol - reference_solution).norm();
102 if (diff < kEpsilon) {
103 return testing::AssertionSuccess();
104 } else {
105 return testing::AssertionFailure()
106 << "The reference solution differs from the ITERATIVE_SCHUR"
107 << " solution by " << diff << " which is more than " << kEpsilon;
108 }
109 }
110
111 int num_rows_;
112 int num_cols_;
113 int num_eliminate_blocks_;
114 scoped_ptr<BlockSparseMatrix> A_;
115 scoped_array<double> b_;
116 scoped_array<double> D_;
117 };
118
TEST_F(IterativeSchurComplementSolverTest,NormalProblem)119 TEST_F(IterativeSchurComplementSolverTest, NormalProblem) {
120 SetUpProblem(2);
121 EXPECT_TRUE(TestSolver(NULL));
122 EXPECT_TRUE(TestSolver(D_.get()));
123 }
124
TEST_F(IterativeSchurComplementSolverTest,ProblemWithNoFBlocks)125 TEST_F(IterativeSchurComplementSolverTest, ProblemWithNoFBlocks) {
126 SetUpProblem(3);
127 EXPECT_TRUE(TestSolver(NULL));
128 EXPECT_TRUE(TestSolver(D_.get()));
129 }
130
131 } // namespace internal
132 } // namespace ceres
133