blob: 679c41f2431465995534602f4a29039c81901957 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// 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 Mierle8ebb0732012-04-30 23:09:08 -070036#include "Eigen/Dense"
37#include "ceres/block_sparse_matrix.h"
38#include "ceres/block_structure.h"
Keir Mierlef7898fb2012-05-05 20:55:08 -070039#include "ceres/conjugate_gradients_solver.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070040#include "ceres/implicit_schur_complement.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070041#include "ceres/internal/eigen.h"
42#include "ceres/internal/scoped_ptr.h"
Keir Mierlef7898fb2012-05-05 20:55:08 -070043#include "ceres/linear_solver.h"
44#include "ceres/triplet_sparse_matrix.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070045#include "ceres/types.h"
Keir Mierlef7898fb2012-05-05 20:55:08 -070046#include "ceres/visibility_based_preconditioner.h"
Sameer Agarwal0beab862012-08-13 15:12:01 -070047#include "glog/logging.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070048
49namespace ceres {
50namespace internal {
51
52IterativeSchurComplementSolver::IterativeSchurComplementSolver(
53 const LinearSolver::Options& options)
54 : options_(options) {
55}
56
57IterativeSchurComplementSolver::~IterativeSchurComplementSolver() {
58}
59
60LinearSolver::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 Agarwala9d8ef82012-05-14 02:28:05 -070068 if (schur_complement_ == NULL) {
Keir Mierle8ebb0732012-04-30 23:09:08 -070069 schur_complement_.reset(
70 new ImplicitSchurComplement(options_.num_eliminate_blocks,
Keir Mierle8ebb0732012-04-30 23:09:08 -070071 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 Mierle8ebb0732012-04-30 23:09:08 -070085 cg_options.max_num_iterations = options_.max_num_iterations;
Keir Mierlef7898fb2012-05-05 20:55:08 -070086 ConjugateGradientsSolver cg_solver(cg_options);
Keir Mierle8ebb0732012-04-30 23:09:08 -070087 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 Agarwala9d8ef82012-05-14 02:28:05 -0700117 visibility_based_preconditioner_->Update(*A, per_solve_options.D);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700118 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 Mierlef7898fb2012-05-05 20:55:08 -0700130 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 Mierle8ebb0732012-04-30 23:09:08 -0700134 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