g2o
linear_solver_pcg.h
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef G2O_LINEAR_SOLVER_PCG_H
28 #define G2O_LINEAR_SOLVER_PCG_H
29 
30 #include "g2o/core/linear_solver.h"
31 #include "g2o/core/batch_stats.h"
32 
33 #include <vector>
34 #include <utility>
35 #include<Eigen/Core>
36 //#ifndef EIGEN_USE_NEW_STDVECTOR
37 //#define EIGEN_USE_NEW_STDVECTOR
38 //#endif
39 #include<Eigen/StdVector>
40 
41 namespace g2o {
42 
46  template <typename MatrixType>
47  class LinearSolverPCG : public LinearSolver<MatrixType>
48  {
49  public:
51  LinearSolver<MatrixType>()
52  {
53  _tolerance = 1e-6;
54  _verbose = false;
56  _residual = -1.0;
57  _maxIter = -1;
58  }
59 
60  virtual ~LinearSolverPCG()
61  {
62  }
63 
64  virtual bool init()
65  {
66  _residual = -1.0;
67  _indices.clear();
68  _sparseMat.clear();
69  return true;
70  }
71 
72  bool solve(const SparseBlockMatrix<MatrixType>& A, double* x, double* b);
73 
75  double tolerance() const { return _tolerance;}
77 
78  int maxIterations() const { return _maxIter;}
79  void setMaxIterations(int maxIter) { _maxIter = maxIter;}
80 
81  bool absoluteTolerance() const { return _absoluteTolerance;}
83 
84  bool verbose() const { return _verbose;}
86 
87  protected:
88  typedef std::vector< MatrixType, Eigen::aligned_allocator<MatrixType> > MatrixVector;
89  typedef std::vector< const MatrixType* > MatrixPtrVector;
90 
91  double _tolerance;
92  double _residual;
94  bool _verbose;
95  int _maxIter;
96 
97  MatrixPtrVector _diag;
98  MatrixVector _J;
99 
100  std::vector<std::pair<int, int> > _indices;
101  MatrixPtrVector _sparseMat;
102 
103  void multDiag(const std::vector<int>& colBlockIndices, MatrixVector& A, const VectorXD& src, VectorXD& dest);
104  void multDiag(const std::vector<int>& colBlockIndices, MatrixPtrVector& A, const VectorXD& src, VectorXD& dest);
105  void mult(const std::vector<int>& colBlockIndices, const VectorXD& src, VectorXD& dest);
106  };
107 
108 #include "linear_solver_pcg.hpp"
109 
110 }// end namespace
111 
112 #endif
MatrixPtrVector _diag
double tolerance() const
return the tolerance for terminating PCG before convergence
void setMaxIterations(int maxIter)
void multDiag(const std::vector< int > &colBlockIndices, MatrixVector &A, const VectorXD &src, VectorXD &dest)
std::vector< MatrixType, Eigen::aligned_allocator< MatrixType > > MatrixVector
void mult(const std::vector< int > &colBlockIndices, const VectorXD &src, VectorXD &dest)
bool absoluteTolerance() const
void setTolerance(double tolerance)
std::vector< const MatrixType * > MatrixPtrVector
linear solver using PCG, pre-conditioner is block Jacobi
void setAbsoluteTolerance(bool absoluteTolerance)
basic solver for Ax = b
Definition: linear_solver.h:41
std::vector< std::pair< int, int > > _indices
void setVerbose(bool verbose)
Eigen::Matrix< double, Eigen::Dynamic, 1, Eigen::ColMajor > VectorXD
Definition: eigen_types.h:48
bool solve(const SparseBlockMatrix< MatrixType > &A, double *x, double *b)
MatrixPtrVector _sparseMat
int maxIterations() const
Sparse matrix which uses blocks.