~ubuntu-branches/ubuntu/raring/ceres-solver/raring

« back to all changes in this revision

Viewing changes to internal/ceres/iterative_schur_complement_solver.cc

  • Committer: Package Import Robot
  • Author(s): Koichi Akabe
  • Date: 2012-06-04 07:15:43 UTC
  • Revision ID: package-import@ubuntu.com-20120604071543-zx6uthupvmtqn3k2
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
36
 
 
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"
 
49
 
 
50
namespace ceres {
 
51
namespace internal {
 
52
 
 
53
IterativeSchurComplementSolver::IterativeSchurComplementSolver(
 
54
    const LinearSolver::Options& options)
 
55
    : options_(options) {
 
56
}
 
57
 
 
58
IterativeSchurComplementSolver::~IterativeSchurComplementSolver() {
 
59
}
 
60
 
 
61
LinearSolver::Summary IterativeSchurComplementSolver::SolveImpl(
 
62
    BlockSparseMatrixBase* A,
 
63
    const double* b,
 
64
    const LinearSolver::PerSolveOptions& per_solve_options,
 
65
    double* x) {
 
66
  CHECK_NOTNULL(A->block_structure());
 
67
 
 
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));
 
73
  }
 
74
  schur_complement_->Init(*A, per_solve_options.D, b);
 
75
 
 
76
  // Initialize the solution to the Schur complement system to zero.
 
77
  //
 
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();
 
82
 
 
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;
 
89
 
 
90
  cg_per_solve_options.r_tolerance = per_solve_options.r_tolerance;
 
91
  cg_per_solve_options.q_tolerance = per_solve_options.q_tolerance;
 
92
 
 
93
  bool is_preconditioner_good = false;
 
94
  switch (options_.preconditioner_type) {
 
95
    case IDENTITY:
 
96
      is_preconditioner_good = true;
 
97
      break;
 
98
    case JACOBI:
 
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;
 
109
      break;
 
110
    case SCHUR_JACOBI:
 
111
    case CLUSTER_JACOBI:
 
112
    case CLUSTER_TRIDIAGONAL:
 
113
      if (visibility_based_preconditioner_.get() == NULL) {
 
114
        visibility_based_preconditioner_.reset(
 
115
            new VisibilityBasedPreconditioner(*A->block_structure(), options_));
 
116
      }
 
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();
 
121
      break;
 
122
    default:
 
123
      LOG(FATAL) << "Unknown Preconditioner Type";
 
124
  }
 
125
 
 
126
  LinearSolver::Summary cg_summary;
 
127
  cg_summary.num_iterations = 0;
 
128
  cg_summary.termination_type = FAILURE;
 
129
 
 
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);
 
138
    }
 
139
  }
 
140
 
 
141
  VLOG(2) << "CG Iterations : " << cg_summary.num_iterations;
 
142
  return cg_summary;
 
143
}
 
144
 
 
145
}  // namespace internal
 
146
}  // namespace ceres