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/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are met:
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.
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.
29
// Author: sameeragarwal@google.com (Sameer Agarwal)
31
#include "ceres/iterative_schur_complement_solver.h"
37
#include <glog/logging.h>
38
#include "Eigen/Dense"
39
#include "ceres/block_sparse_matrix.h"
40
#include "ceres/block_structure.h"
41
#include "ceres/conjugate_gradients_solver.h"
42
#include "ceres/implicit_schur_complement.h"
43
#include "ceres/internal/eigen.h"
44
#include "ceres/internal/scoped_ptr.h"
45
#include "ceres/linear_solver.h"
46
#include "ceres/triplet_sparse_matrix.h"
47
#include "ceres/types.h"
48
#include "ceres/visibility_based_preconditioner.h"
53
IterativeSchurComplementSolver::IterativeSchurComplementSolver(
54
const LinearSolver::Options& options)
58
IterativeSchurComplementSolver::~IterativeSchurComplementSolver() {
61
LinearSolver::Summary IterativeSchurComplementSolver::SolveImpl(
62
BlockSparseMatrixBase* A,
64
const LinearSolver::PerSolveOptions& per_solve_options,
66
CHECK_NOTNULL(A->block_structure());
68
// Initialize a ImplicitSchurComplement object.
69
if (schur_complement_ == NULL) {
70
schur_complement_.reset(
71
new ImplicitSchurComplement(options_.num_eliminate_blocks,
72
options_.preconditioner_type == JACOBI));
74
schur_complement_->Init(*A, per_solve_options.D, b);
76
// Initialize the solution to the Schur complement system to zero.
78
// TODO(sameeragarwal): There maybe a better initialization than an
79
// all zeros solution. Explore other cheap starting points.
80
reduced_linear_system_solution_.resize(schur_complement_->num_rows());
81
reduced_linear_system_solution_.setZero();
83
// Instantiate a conjugate gradient solver that runs on the Schur complement
84
// matrix with the block diagonal of the matrix F'F as the preconditioner.
85
LinearSolver::Options cg_options;
86
cg_options.max_num_iterations = options_.max_num_iterations;
87
ConjugateGradientsSolver cg_solver(cg_options);
88
LinearSolver::PerSolveOptions cg_per_solve_options;
90
cg_per_solve_options.r_tolerance = per_solve_options.r_tolerance;
91
cg_per_solve_options.q_tolerance = per_solve_options.q_tolerance;
93
bool is_preconditioner_good = false;
94
switch (options_.preconditioner_type) {
96
is_preconditioner_good = true;
99
// We need to strip the constness of the block_diagonal_FtF_inverse
100
// matrix here because the only other way to initialize the struct
101
// cg_solve_options would be to add a constructor to it. We know
102
// that the only method ever called on the preconditioner is the
103
// RightMultiply which is a const method so we don't need to worry
104
// about the object getting modified.
105
cg_per_solve_options.preconditioner =
106
const_cast<BlockSparseMatrix*>(
107
schur_complement_->block_diagonal_FtF_inverse());
108
is_preconditioner_good = true;
112
case CLUSTER_TRIDIAGONAL:
113
if (visibility_based_preconditioner_.get() == NULL) {
114
visibility_based_preconditioner_.reset(
115
new VisibilityBasedPreconditioner(*A->block_structure(), options_));
117
is_preconditioner_good =
118
visibility_based_preconditioner_->Update(*A, per_solve_options.D);
119
cg_per_solve_options.preconditioner =
120
visibility_based_preconditioner_.get();
123
LOG(FATAL) << "Unknown Preconditioner Type";
126
LinearSolver::Summary cg_summary;
127
cg_summary.num_iterations = 0;
128
cg_summary.termination_type = FAILURE;
130
if (is_preconditioner_good) {
131
cg_summary = cg_solver.Solve(schur_complement_.get(),
132
schur_complement_->rhs().data(),
133
cg_per_solve_options,
134
reduced_linear_system_solution_.data());
135
if (cg_summary.termination_type != FAILURE) {
136
schur_complement_->BackSubstitute(
137
reduced_linear_system_solution_.data(), x);
141
VLOG(2) << "CG Iterations : " << cg_summary.num_iterations;
145
} // namespace internal