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 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 31 | #include "ceres/casts.h" |
| 32 | #include "ceres/compressed_row_sparse_matrix.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 33 | #include "ceres/internal/scoped_ptr.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 34 | #include "ceres/linear_least_squares_problems.h" |
| 35 | #include "ceres/linear_solver.h" |
| 36 | #include "ceres/triplet_sparse_matrix.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 37 | #include "ceres/types.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 38 | #include "glog/logging.h" |
| 39 | #include "gtest/gtest.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 40 | |
| 41 | |
| 42 | namespace ceres { |
| 43 | namespace internal { |
| 44 | |
| 45 | class UnsymmetricLinearSolverTest : public ::testing::Test { |
| 46 | protected : |
| 47 | virtual void SetUp() { |
| 48 | scoped_ptr<LinearLeastSquaresProblem> problem( |
| 49 | CreateLinearLeastSquaresProblemFromId(0)); |
| 50 | |
| 51 | CHECK_NOTNULL(problem.get()); |
| 52 | A_.reset(down_cast<TripletSparseMatrix*>(problem->A.release())); |
| 53 | b_.reset(problem->b.release()); |
| 54 | D_.reset(problem->D.release()); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 55 | sol_unregularized_.reset(problem->x.release()); |
| 56 | sol_regularized_.reset(problem->x_D.release()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 59 | void TestSolver( |
| 60 | LinearSolverType linear_solver_type, |
| 61 | SparseLinearAlgebraLibraryType sparse_linear_algebra_library) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 62 | LinearSolver::Options options; |
| 63 | options.type = linear_solver_type; |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 64 | options.sparse_linear_algebra_library = sparse_linear_algebra_library; |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 65 | options.use_block_amd = false; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 66 | scoped_ptr<LinearSolver> solver(LinearSolver::Create(options)); |
| 67 | |
| 68 | LinearSolver::PerSolveOptions per_solve_options; |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 69 | LinearSolver::Summary unregularized_solve_summary; |
| 70 | LinearSolver::Summary regularized_solve_summary; |
| 71 | Vector x_unregularized(A_->num_cols()); |
| 72 | Vector x_regularized(A_->num_cols()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 73 | |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 74 | scoped_ptr<SparseMatrix> transformed_A; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 75 | |
Sameer Agarwal | b9f15a5 | 2012-08-18 13:06:19 -0700 | [diff] [blame] | 76 | if (linear_solver_type == DENSE_QR || |
| 77 | linear_solver_type == DENSE_NORMAL_CHOLESKY) { |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 78 | transformed_A.reset(new DenseSparseMatrix(*A_)); |
| 79 | } else if (linear_solver_type == SPARSE_NORMAL_CHOLESKY) { |
| 80 | transformed_A.reset(new CompressedRowSparseMatrix(*A_)); |
| 81 | } else { |
| 82 | LOG(FATAL) << "Unknown linear solver : " << linear_solver_type; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 83 | } |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 84 | // Unregularized |
| 85 | unregularized_solve_summary = |
| 86 | solver->Solve(transformed_A.get(), |
| 87 | b_.get(), |
| 88 | per_solve_options, |
| 89 | x_unregularized.data()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 90 | |
| 91 | // Regularized solution |
| 92 | per_solve_options.D = D_.get(); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 93 | regularized_solve_summary = |
| 94 | solver->Solve(transformed_A.get(), |
| 95 | b_.get(), |
| 96 | per_solve_options, |
| 97 | x_regularized.data()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 98 | |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 99 | EXPECT_EQ(unregularized_solve_summary.termination_type, TOLERANCE); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 100 | |
| 101 | for (int i = 0; i < A_->num_cols(); ++i) { |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 102 | EXPECT_NEAR(sol_unregularized_[i], x_unregularized[i], 1e-8); |
| 103 | } |
| 104 | |
| 105 | EXPECT_EQ(regularized_solve_summary.termination_type, TOLERANCE); |
| 106 | for (int i = 0; i < A_->num_cols(); ++i) { |
| 107 | EXPECT_NEAR(sol_regularized_[i], x_regularized[i], 1e-8); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | scoped_ptr<TripletSparseMatrix> A_; |
| 112 | scoped_array<double> b_; |
| 113 | scoped_array<double> D_; |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 114 | scoped_array<double> sol_unregularized_; |
| 115 | scoped_array<double> sol_regularized_; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 118 | TEST_F(UnsymmetricLinearSolverTest, DenseQR) { |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 119 | TestSolver(DENSE_QR, SUITE_SPARSE); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Sameer Agarwal | b9f15a5 | 2012-08-18 13:06:19 -0700 | [diff] [blame] | 122 | TEST_F(UnsymmetricLinearSolverTest, DenseNormalCholesky) { |
| 123 | TestSolver(DENSE_NORMAL_CHOLESKY, SUITE_SPARSE); |
| 124 | } |
| 125 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 126 | #ifndef CERES_NO_SUITESPARSE |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 127 | TEST_F(UnsymmetricLinearSolverTest, SparseNormalCholeskyUsingSuiteSparse) { |
| 128 | TestSolver(SPARSE_NORMAL_CHOLESKY, SUITE_SPARSE); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 129 | } |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 130 | #endif |
| 131 | |
| 132 | #ifndef CERES_NO_CXSPARSE |
| 133 | TEST_F(UnsymmetricLinearSolverTest, SparseNormalCholeskyUsingCXSparse) { |
| 134 | TestSolver(SPARSE_NORMAL_CHOLESKY, CX_SPARSE); |
| 135 | } |
| 136 | #endif |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 137 | |
| 138 | } // namespace internal |
| 139 | } // namespace ceres |