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 | #include "ceres/iterative_schur_complement_solver.h" |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <cstring> |
| 35 | #include <vector> |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 36 | #include "Eigen/Dense" |
| 37 | #include "ceres/block_sparse_matrix.h" |
| 38 | #include "ceres/block_structure.h" |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 39 | #include "ceres/conjugate_gradients_solver.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 40 | #include "ceres/implicit_schur_complement.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 41 | #include "ceres/internal/eigen.h" |
| 42 | #include "ceres/internal/scoped_ptr.h" |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 43 | #include "ceres/linear_solver.h" |
| 44 | #include "ceres/triplet_sparse_matrix.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 45 | #include "ceres/types.h" |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 46 | #include "ceres/visibility_based_preconditioner.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 47 | #include "glog/logging.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 48 | |
| 49 | namespace ceres { |
| 50 | namespace internal { |
| 51 | |
| 52 | IterativeSchurComplementSolver::IterativeSchurComplementSolver( |
| 53 | const LinearSolver::Options& options) |
| 54 | : options_(options) { |
| 55 | } |
| 56 | |
| 57 | IterativeSchurComplementSolver::~IterativeSchurComplementSolver() { |
| 58 | } |
| 59 | |
| 60 | LinearSolver::Summary IterativeSchurComplementSolver::SolveImpl( |
| 61 | BlockSparseMatrixBase* A, |
| 62 | const double* b, |
| 63 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 64 | double* x) { |
| 65 | CHECK_NOTNULL(A->block_structure()); |
| 66 | |
| 67 | // Initialize a ImplicitSchurComplement object. |
Sameer Agarwal | a9d8ef8 | 2012-05-14 02:28:05 -0700 | [diff] [blame] | 68 | if (schur_complement_ == NULL) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 69 | schur_complement_.reset( |
| 70 | new ImplicitSchurComplement(options_.num_eliminate_blocks, |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 71 | options_.preconditioner_type == JACOBI)); |
| 72 | } |
| 73 | schur_complement_->Init(*A, per_solve_options.D, b); |
| 74 | |
| 75 | // Initialize the solution to the Schur complement system to zero. |
| 76 | // |
| 77 | // TODO(sameeragarwal): There maybe a better initialization than an |
| 78 | // all zeros solution. Explore other cheap starting points. |
| 79 | reduced_linear_system_solution_.resize(schur_complement_->num_rows()); |
| 80 | reduced_linear_system_solution_.setZero(); |
| 81 | |
| 82 | // Instantiate a conjugate gradient solver that runs on the Schur complement |
| 83 | // matrix with the block diagonal of the matrix F'F as the preconditioner. |
| 84 | LinearSolver::Options cg_options; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 85 | cg_options.max_num_iterations = options_.max_num_iterations; |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 86 | ConjugateGradientsSolver cg_solver(cg_options); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 87 | LinearSolver::PerSolveOptions cg_per_solve_options; |
| 88 | |
| 89 | cg_per_solve_options.r_tolerance = per_solve_options.r_tolerance; |
| 90 | cg_per_solve_options.q_tolerance = per_solve_options.q_tolerance; |
| 91 | |
| 92 | bool is_preconditioner_good = false; |
| 93 | switch (options_.preconditioner_type) { |
| 94 | case IDENTITY: |
| 95 | is_preconditioner_good = true; |
| 96 | break; |
| 97 | case JACOBI: |
| 98 | // We need to strip the constness of the block_diagonal_FtF_inverse |
| 99 | // matrix here because the only other way to initialize the struct |
| 100 | // cg_solve_options would be to add a constructor to it. We know |
| 101 | // that the only method ever called on the preconditioner is the |
| 102 | // RightMultiply which is a const method so we don't need to worry |
| 103 | // about the object getting modified. |
| 104 | cg_per_solve_options.preconditioner = |
| 105 | const_cast<BlockSparseMatrix*>( |
| 106 | schur_complement_->block_diagonal_FtF_inverse()); |
| 107 | is_preconditioner_good = true; |
| 108 | break; |
| 109 | case SCHUR_JACOBI: |
| 110 | case CLUSTER_JACOBI: |
| 111 | case CLUSTER_TRIDIAGONAL: |
| 112 | if (visibility_based_preconditioner_.get() == NULL) { |
| 113 | visibility_based_preconditioner_.reset( |
| 114 | new VisibilityBasedPreconditioner(*A->block_structure(), options_)); |
| 115 | } |
| 116 | is_preconditioner_good = |
Sameer Agarwal | a9d8ef8 | 2012-05-14 02:28:05 -0700 | [diff] [blame] | 117 | visibility_based_preconditioner_->Update(*A, per_solve_options.D); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 118 | cg_per_solve_options.preconditioner = |
| 119 | visibility_based_preconditioner_.get(); |
| 120 | break; |
| 121 | default: |
| 122 | LOG(FATAL) << "Unknown Preconditioner Type"; |
| 123 | } |
| 124 | |
| 125 | LinearSolver::Summary cg_summary; |
| 126 | cg_summary.num_iterations = 0; |
| 127 | cg_summary.termination_type = FAILURE; |
| 128 | |
| 129 | if (is_preconditioner_good) { |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 130 | cg_summary = cg_solver.Solve(schur_complement_.get(), |
| 131 | schur_complement_->rhs().data(), |
| 132 | cg_per_solve_options, |
| 133 | reduced_linear_system_solution_.data()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 134 | if (cg_summary.termination_type != FAILURE) { |
| 135 | schur_complement_->BackSubstitute( |
| 136 | reduced_linear_system_solution_.data(), x); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | VLOG(2) << "CG Iterations : " << cg_summary.num_iterations; |
| 141 | return cg_summary; |
| 142 | } |
| 143 | |
| 144 | } // namespace internal |
| 145 | } // namespace ceres |