blob: 41cce4d8ecc92ed3d64af708274bf620d816977f [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
Keir Mierle7492b0d2015-03-17 22:30:16 -07002// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
Keir Mierle8ebb0732012-04-30 23:09:08 -07004//
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>
Keir Mierle8ebb0732012-04-30 23:09:08 -070038#include "Eigen/Dense"
39#include "ceres/block_random_access_dense_matrix.h"
40#include "ceres/block_sparse_matrix.h"
41#include "ceres/casts.h"
Sameer Agarwal0beab862012-08-13 15:12:01 -070042#include "ceres/internal/eigen.h"
43#include "ceres/internal/scoped_ptr.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070044#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"
Keir Mierle8ebb0732012-04-30 23:09:08 -070048#include "ceres/types.h"
Sameer Agarwal0beab862012-08-13 15:12:01 -070049#include "glog/logging.h"
50#include "gtest/gtest.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070051
52namespace ceres {
53namespace internal {
54
55using testing::AssertionResult;
56
57const double kEpsilon = 1e-14;
58
59class IterativeSchurComplementSolverTest : public ::testing::Test {
60 protected :
Sameer Agarwal0338f9a2013-09-02 22:28:40 -070061 void SetUpProblem(int problem_id) {
Keir Mierle8ebb0732012-04-30 23:09:08 -070062 scoped_ptr<LinearLeastSquaresProblem> problem(
Sameer Agarwal0338f9a2013-09-02 22:28:40 -070063 CreateLinearLeastSquaresProblemFromId(problem_id));
Keir Mierle8ebb0732012-04-30 23:09:08 -070064
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
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
Sameer Agarwal0c52f1e2012-09-17 11:30:14 -070092 options.elimination_groups.push_back(num_eliminate_blocks_);
Sameer Agarwal0338f9a2013-09-02 22:28:40 -070093 options.elimination_groups.push_back(0);
Keir Mierle8ebb0732012-04-30 23:09:08 -070094 options.max_num_iterations = num_cols_;
Sameer Agarwal0338f9a2013-09-02 22:28:40 -070095 options.preconditioner_type = SCHUR_JACOBI;
Keir Mierle8ebb0732012-04-30 23:09:08 -070096 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
Sameer Agarwal0338f9a2013-09-02 22:28:40 -0700119TEST_F(IterativeSchurComplementSolverTest, NormalProblem) {
120 SetUpProblem(2);
121 EXPECT_TRUE(TestSolver(NULL));
122 EXPECT_TRUE(TestSolver(D_.get()));
123}
124
125TEST_F(IterativeSchurComplementSolverTest, ProblemWithNoFBlocks) {
126 SetUpProblem(3);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700127 EXPECT_TRUE(TestSolver(NULL));
128 EXPECT_TRUE(TestSolver(D_.get()));
129}
130
131} // namespace internal
132} // namespace ceres