Blame internal/ceres/schur_complement_solver.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_SCHUR_COMPLEMENT_SOLVER_H_
Packit ea1746
#define CERES_INTERNAL_SCHUR_COMPLEMENT_SOLVER_H_
Packit ea1746
Packit ea1746
#include <set>
Packit ea1746
#include <utility>
Packit ea1746
#include <vector>
Packit ea1746
Packit ea1746
#include "ceres/block_random_access_diagonal_matrix.h"
Packit ea1746
#include "ceres/block_random_access_matrix.h"
Packit ea1746
#include "ceres/block_sparse_matrix.h"
Packit ea1746
#include "ceres/block_structure.h"
Packit ea1746
#include "ceres/internal/port.h"
Packit ea1746
#include "ceres/internal/scoped_ptr.h"
Packit ea1746
#include "ceres/linear_solver.h"
Packit ea1746
#include "ceres/schur_eliminator.h"
Packit ea1746
#include "ceres/types.h"
Packit ea1746
Packit ea1746
#ifdef CERES_USE_EIGEN_SPARSE
Packit ea1746
#include "Eigen/SparseCholesky"
Packit ea1746
#include "Eigen/OrderingMethods"
Packit ea1746
#endif
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
class BlockSparseMatrix;
Packit ea1746
class SparseCholesky;
Packit ea1746
Packit ea1746
// Base class for Schur complement based linear least squares
Packit ea1746
// solvers. It assumes that the input linear system Ax = b can be
Packit ea1746
// partitioned into
Packit ea1746
//
Packit ea1746
//  E y + F z = b
Packit ea1746
//
Packit ea1746
// Where x = [y;z] is a partition of the variables.  The paritioning
Packit ea1746
// of the variables is such that, E'E is a block diagonal
Packit ea1746
// matrix. Further, the rows of A are ordered so that for every
Packit ea1746
// variable block in y, all the rows containing that variable block
Packit ea1746
// occur as a vertically contiguous block. i.e the matrix A looks like
Packit ea1746
//
Packit ea1746
//              E                 F
Packit ea1746
//  A = [ y1   0   0   0 |  z1    0    0   0    z5]
Packit ea1746
//      [ y1   0   0   0 |  z1   z2    0   0     0]
Packit ea1746
//      [  0  y2   0   0 |   0    0   z3   0     0]
Packit ea1746
//      [  0   0  y3   0 |  z1   z2   z3  z4    z5]
Packit ea1746
//      [  0   0  y3   0 |  z1    0    0   0    z5]
Packit ea1746
//      [  0   0   0  y4 |   0    0    0   0    z5]
Packit ea1746
//      [  0   0   0  y4 |   0   z2    0   0     0]
Packit ea1746
//      [  0   0   0  y4 |   0    0    0   0     0]
Packit ea1746
//      [  0   0   0   0 |  z1    0    0   0     0]
Packit ea1746
//      [  0   0   0   0 |   0    0   z3  z4    z5]
Packit ea1746
//
Packit ea1746
// This structure should be reflected in the corresponding
Packit ea1746
// CompressedRowBlockStructure object associated with A. The linear
Packit ea1746
// system Ax = b should either be well posed or the array D below
Packit ea1746
// should be non-null and the diagonal matrix corresponding to it
Packit ea1746
// should be non-singular.
Packit ea1746
//
Packit ea1746
// SchurComplementSolver has two sub-classes.
Packit ea1746
//
Packit ea1746
// DenseSchurComplementSolver: For problems where the Schur complement
Packit ea1746
// matrix is small and dense, or if CHOLMOD/SuiteSparse is not
Packit ea1746
// installed. For structure from motion problems, this is solver can
Packit ea1746
// be used for problems with upto a few hundred cameras.
Packit ea1746
//
Packit ea1746
// SparseSchurComplementSolver: For problems where the Schur
Packit ea1746
// complement matrix is large and sparse. It requires that Ceres be
Packit ea1746
// build with at least one sparse linear algebra library, as it
Packit ea1746
// computes a sparse Cholesky factorization of the Schur complement.
Packit ea1746
//
Packit ea1746
// This solver can be used for solving structure from motion problems
Packit ea1746
// with tens of thousands of cameras, though depending on the exact
Packit ea1746
// sparsity structure, it maybe better to use an iterative solver.
Packit ea1746
//
Packit ea1746
// The two solvers can be instantiated by calling
Packit ea1746
// LinearSolver::CreateLinearSolver with LinearSolver::Options::type
Packit ea1746
// set to DENSE_SCHUR and SPARSE_SCHUR
Packit ea1746
// respectively. LinearSolver::Options::elimination_groups[0] should
Packit ea1746
// be at least 1.
Packit ea1746
class SchurComplementSolver : public BlockSparseMatrixSolver {
Packit ea1746
 public:
Packit ea1746
  explicit SchurComplementSolver(const LinearSolver::Options& options)
Packit ea1746
      : options_(options) {
Packit ea1746
    CHECK_GT(options.elimination_groups.size(), 1);
Packit ea1746
    CHECK_GT(options.elimination_groups[0], 0);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // LinearSolver methods
Packit ea1746
  virtual ~SchurComplementSolver() {}
Packit ea1746
  virtual LinearSolver::Summary SolveImpl(
Packit ea1746
      BlockSparseMatrix* A,
Packit ea1746
      const double* b,
Packit ea1746
      const LinearSolver::PerSolveOptions& per_solve_options,
Packit ea1746
      double* x);
Packit ea1746
Packit ea1746
 protected:
Packit ea1746
  const LinearSolver::Options& options() const { return options_; }
Packit ea1746
Packit ea1746
  const BlockRandomAccessMatrix* lhs() const { return lhs_.get(); }
Packit ea1746
  void set_lhs(BlockRandomAccessMatrix* lhs) { lhs_.reset(lhs); }
Packit ea1746
  const double* rhs() const { return rhs_.get(); }
Packit ea1746
  void set_rhs(double* rhs) { rhs_.reset(rhs); }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  virtual void InitStorage(const CompressedRowBlockStructure* bs) = 0;
Packit ea1746
  virtual LinearSolver::Summary SolveReducedLinearSystem(
Packit ea1746
      const LinearSolver::PerSolveOptions& per_solve_options,
Packit ea1746
      double* solution) = 0;
Packit ea1746
Packit ea1746
  LinearSolver::Options options_;
Packit ea1746
Packit ea1746
  scoped_ptr<SchurEliminatorBase> eliminator_;
Packit ea1746
  scoped_ptr<BlockRandomAccessMatrix> lhs_;
Packit ea1746
  scoped_array<double> rhs_;
Packit ea1746
Packit ea1746
  CERES_DISALLOW_COPY_AND_ASSIGN(SchurComplementSolver);
Packit ea1746
};
Packit ea1746
Packit ea1746
// Dense Cholesky factorization based solver.
Packit ea1746
class DenseSchurComplementSolver : public SchurComplementSolver {
Packit ea1746
 public:
Packit ea1746
  explicit DenseSchurComplementSolver(const LinearSolver::Options& options)
Packit ea1746
      : SchurComplementSolver(options) {}
Packit ea1746
  virtual ~DenseSchurComplementSolver() {}
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  virtual void InitStorage(const CompressedRowBlockStructure* bs);
Packit ea1746
  virtual LinearSolver::Summary SolveReducedLinearSystem(
Packit ea1746
      const LinearSolver::PerSolveOptions& per_solve_options,
Packit ea1746
      double* solution);
Packit ea1746
Packit ea1746
  CERES_DISALLOW_COPY_AND_ASSIGN(DenseSchurComplementSolver);
Packit ea1746
};
Packit ea1746
Packit ea1746
// Sparse Cholesky factorization based solver.
Packit ea1746
class SparseSchurComplementSolver : public SchurComplementSolver {
Packit ea1746
 public:
Packit ea1746
  explicit SparseSchurComplementSolver(const LinearSolver::Options& options);
Packit ea1746
  virtual ~SparseSchurComplementSolver();
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  virtual void InitStorage(const CompressedRowBlockStructure* bs);
Packit ea1746
  virtual LinearSolver::Summary SolveReducedLinearSystem(
Packit ea1746
      const LinearSolver::PerSolveOptions& per_solve_options,
Packit ea1746
      double* solution);
Packit ea1746
  LinearSolver::Summary SolveReducedLinearSystemUsingConjugateGradients(
Packit ea1746
      const LinearSolver::PerSolveOptions& per_solve_options,
Packit ea1746
      double* solution);
Packit ea1746
Packit ea1746
  // Size of the blocks in the Schur complement.
Packit ea1746
  std::vector<int> blocks_;
Packit ea1746
  scoped_ptr<SparseCholesky> sparse_cholesky_;
Packit ea1746
  scoped_ptr<BlockRandomAccessDiagonalMatrix> preconditioner_;
Packit ea1746
  CERES_DISALLOW_COPY_AND_ASSIGN(SparseSchurComplementSolver);
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_SCHUR_COMPLEMENT_SOLVER_H_