Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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> |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 42 | #include "ceres/internal/eigen.h" |
| 43 | #include "ceres/internal/scoped_ptr.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 48 | #include "ceres/types.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 49 | #include "glog/logging.h" |
| 50 | #include "gtest/gtest.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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 : |
| 61 | virtual void SetUp() { |
| 62 | scoped_ptr<LinearLeastSquaresProblem> problem( |
| 63 | CreateLinearLeastSquaresProblemFromId(2)); |
| 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 | |
| 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.num_eliminate_blocks = num_eliminate_blocks_; |
| 93 | options.max_num_iterations = num_cols_; |
| 94 | IterativeSchurComplementSolver isc(options); |
| 95 | |
| 96 | Vector isc_sol(num_cols_); |
| 97 | per_solve_options.r_tolerance = 1e-12; |
| 98 | isc.Solve(A_.get(), b_.get(), per_solve_options, isc_sol.data()); |
| 99 | double diff = (isc_sol - reference_solution).norm(); |
| 100 | if (diff < kEpsilon) { |
| 101 | return testing::AssertionSuccess(); |
| 102 | } else { |
| 103 | return testing::AssertionFailure() |
| 104 | << "The reference solution differs from the ITERATIVE_SCHUR" |
| 105 | << " solution by " << diff << " which is more than " << kEpsilon; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | int num_rows_; |
| 110 | int num_cols_; |
| 111 | int num_eliminate_blocks_; |
| 112 | scoped_ptr<BlockSparseMatrix> A_; |
| 113 | scoped_array<double> b_; |
| 114 | scoped_array<double> D_; |
| 115 | }; |
| 116 | |
| 117 | TEST_F(IterativeSchurComplementSolverTest, SolverTest) { |
| 118 | EXPECT_TRUE(TestSolver(NULL)); |
| 119 | EXPECT_TRUE(TestSolver(D_.get())); |
| 120 | } |
| 121 | |
| 122 | } // namespace internal |
| 123 | } // namespace ceres |