gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SubgraphPreconditioner.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
18 #pragma once
19 
20 #include <gtsam/global_includes.h>
21 #include <gtsam/linear/Errors.h>
22 #include <gtsam/linear/IterativeSolver.h>
23 #include <gtsam/linear/Preconditioner.h>
25 #include <boost/shared_ptr.hpp>
26 
27 namespace gtsam {
28 
29  // Forward declarations
30  class GaussianBayesNet;
31  class GaussianFactorGraph;
32  class VectorValues;
33 
34  struct GTSAM_EXPORT SubgraphEdge {
35  size_t index_; /* edge id */
36  double weight_; /* edge weight */
37  SubgraphEdge() : index_(0), weight_(1.0) {}
38  SubgraphEdge(const SubgraphEdge &e) : index_(e.index()), weight_(e.weight()) {}
39  SubgraphEdge(const size_t index, const double weight = 1.0): index_(index), weight_(weight) {}
40  inline size_t index() const { return index_; }
41  inline double weight() const { return weight_; }
42  inline bool isUnitWeight() const { return (weight_ == 1.0); }
43  friend std::ostream &operator<<(std::ostream &os, const SubgraphEdge &edge);
44  private:
45  friend class boost::serialization::access;
46  template<class Archive>
47  void serialize(Archive & ar, const unsigned int version) {
48  ar & BOOST_SERIALIZATION_NVP(index_);
49  ar & BOOST_SERIALIZATION_NVP(weight_);
50  }
51  };
52 
53  /**************************************************************************/
54  class GTSAM_EXPORT Subgraph {
55  public:
56  typedef boost::shared_ptr<Subgraph> shared_ptr;
57  typedef std::vector<shared_ptr> vector_shared_ptr;
58  typedef std::vector<SubgraphEdge> Edges;
59  typedef std::vector<size_t> EdgeIndices;
60  typedef Edges::iterator iterator;
61  typedef Edges::const_iterator const_iterator;
62 
63  protected:
64  Edges edges_; /* index to the factors */
65 
66  public:
67  Subgraph() {}
68  Subgraph(const Subgraph &subgraph) : edges_(subgraph.edges()) {}
69  Subgraph(const Edges &edges) : edges_(edges) {}
70  Subgraph(const std::vector<size_t> &indices) ;
71 
72  inline const Edges& edges() const { return edges_; }
73  inline const size_t size() const { return edges_.size(); }
74  EdgeIndices edgeIndices() const;
75 
76  iterator begin() { return edges_.begin(); }
77  const_iterator begin() const { return edges_.begin(); }
78  iterator end() { return edges_.end(); }
79  const_iterator end() const { return edges_.end(); }
80 
81  void save(const std::string &fn) const;
82  static shared_ptr load(const std::string &fn);
83  friend std::ostream &operator<<(std::ostream &os, const Subgraph &subgraph);
84 
85  private:
86  friend class boost::serialization::access;
87  template<class Archive>
88  void serialize(Archive & ar, const unsigned int version) {
89  ar & BOOST_SERIALIZATION_NVP(edges_);
90  }
91  };
92 
93  /****************************************************************************/
94  struct GTSAM_EXPORT SubgraphBuilderParameters {
95  public:
96  typedef boost::shared_ptr<SubgraphBuilderParameters> shared_ptr;
97 
98  enum Skeleton {
99  /* augmented tree */
100  NATURALCHAIN = 0, /* natural ordering of the graph */
101  BFS, /* breadth-first search tree */
102  KRUSKAL, /* maximum weighted spanning tree */
103  } skeleton_ ;
104 
105  enum SkeletonWeight { /* how to weigh the graph edges */
106  EQUAL = 0, /* every block edge has equal weight */
107  RHS_2NORM, /* use the 2-norm of the rhs */
108  LHS_FNORM, /* use the frobenius norm of the lhs */
109  RANDOM, /* bounded random edge weight */
110  } skeletonWeight_ ;
111 
112  enum AugmentationWeight { /* how to weigh the graph edges */
113  SKELETON = 0, /* use the same weights in building the skeleton */
114 // STRETCH, /* stretch in the laplacian sense */
115 // GENERALIZED_STRETCH /* the generalized stretch defined in jian2013iros */
116  } augmentationWeight_ ;
117 
118  double complexity_;
119 
121  : skeleton_(KRUSKAL), skeletonWeight_(RANDOM), augmentationWeight_(SKELETON), complexity_(1.0) {}
122  virtual ~SubgraphBuilderParameters() {}
123 
124  /* for serialization */
125  void print() const ;
126  virtual void print(std::ostream &os) const ;
127  friend std::ostream& operator<<(std::ostream &os, const PreconditionerParameters &p);
128 
129  static Skeleton skeletonTranslator(const std::string &s);
130  static std::string skeletonTranslator(Skeleton w);
131  static SkeletonWeight skeletonWeightTranslator(const std::string &s);
132  static std::string skeletonWeightTranslator(SkeletonWeight w);
133  static AugmentationWeight augmentationWeightTranslator(const std::string &s);
134  static std::string augmentationWeightTranslator(AugmentationWeight w);
135  };
136 
137  /*****************************************************************************/
138  class GTSAM_EXPORT SubgraphBuilder {
139 
140  public:
141  typedef SubgraphBuilder Base;
142  typedef boost::shared_ptr<SubgraphBuilder> shared_ptr;
143  typedef std::vector<double> Weights;
144 
146  : parameters_(p) {}
147  virtual ~SubgraphBuilder() {}
148  virtual boost::shared_ptr<Subgraph> operator() (const GaussianFactorGraph &jfg) const ;
149 
150  private:
151  std::vector<size_t> buildTree(const GaussianFactorGraph &gfg, const FastMap<Key, size_t> &ordering, const std::vector<double> &weights) const ;
152  std::vector<size_t> unary(const GaussianFactorGraph &gfg) const ;
153  std::vector<size_t> natural_chain(const GaussianFactorGraph &gfg) const ;
154  std::vector<size_t> bfs(const GaussianFactorGraph &gfg) const ;
155  std::vector<size_t> kruskal(const GaussianFactorGraph &gfg, const FastMap<Key, size_t> &ordering, const std::vector<double> &w) const ;
156  std::vector<size_t> sample(const std::vector<double> &weights, const size_t t) const ;
157  Weights weights(const GaussianFactorGraph &gfg) const;
158  SubgraphBuilderParameters parameters_;
159 
160  };
161 
162  /*******************************************************************************************/
165  typedef boost::shared_ptr<SubgraphPreconditionerParameters> shared_ptr;
167  : Base(), builderParams_(p) {}
169  SubgraphBuilderParameters builderParams_;
170  };
171 
179  class GTSAM_EXPORT SubgraphPreconditioner : public Preconditioner {
180 
181  public:
182  typedef boost::shared_ptr<SubgraphPreconditioner> shared_ptr;
183  typedef boost::shared_ptr<const GaussianBayesNet> sharedBayesNet;
184  typedef boost::shared_ptr<const GaussianFactorGraph> sharedFG;
185  typedef boost::shared_ptr<const VectorValues> sharedValues;
186  typedef boost::shared_ptr<const Errors> sharedErrors;
187 
188  private:
189  sharedFG Ab2_;
190  sharedBayesNet Rc1_;
191  sharedValues xbar_;
192  sharedErrors b2bar_;
193 
194  KeyInfo keyInfo_;
196 
197  public:
198 
200 
207  SubgraphPreconditioner(const sharedFG& Ab2, const sharedBayesNet& Rc1, const sharedValues& xbar,
209 
210  virtual ~SubgraphPreconditioner() {}
211 
213  void print(const std::string& s = "SubgraphPreconditioner") const;
214 
216  const sharedFG& Ab2() const { return Ab2_; }
217 
219  const sharedBayesNet& Rc1() const { return Rc1_; }
220 
222  const sharedErrors b2bar() const { return b2bar_; }
223 
229  /* x = xbar + inv(R1)*y */
230  VectorValues x(const VectorValues& y) const;
231 
232  /* A zero VectorValues with the structure of xbar */
233  VectorValues zero() const {
234  VectorValues V(VectorValues::Zero(*xbar_));
235  return V ;
236  }
237 
243  void transposeMultiplyAdd2(double alpha, Errors::const_iterator begin,
244  Errors::const_iterator end, VectorValues& y) const;
245 
246  /* error, given y */
247  double error(const VectorValues& y) const;
248 
250  VectorValues gradient(const VectorValues& y) const;
251 
253  Errors operator*(const VectorValues& y) const;
254 
256  void multiplyInPlace(const VectorValues& y, Errors& e) const;
257 
259  VectorValues operator^(const Errors& e) const;
260 
265  void transposeMultiplyAdd(double alpha, const Errors& e, VectorValues& y) const;
266 
267  /*****************************************************************************/
268  /* implement virtual functions of Preconditioner */
269 
270  /* Computation Interfaces for Vector */
271  virtual void solve(const Vector& y, Vector &x) const;
272  virtual void transposeSolve(const Vector& y, Vector& x) const ;
273 
274  virtual void build(
275  const GaussianFactorGraph &gfg,
276  const KeyInfo &info,
277  const std::map<Key,Vector> &lambda
278  ) ;
279  /*****************************************************************************/
280  };
281 
282  /* get subvectors */
283  Vector getSubvector(const Vector &src, const KeyInfo &keyInfo, const FastVector<Key> &keys);
284 
285  /* set subvectors */
286  void setSubvector(const Vector &src, const KeyInfo &keyInfo, const FastVector<Key> &keys, Vector &dst);
287 
288 
289  /* build a factor subgraph, which is defined as a set of weighted edges (factors) */
290  boost::shared_ptr<GaussianFactorGraph>
291  buildFactorSubgraph(const GaussianFactorGraph &gfg, const Subgraph &subgraph, const bool clone);
292 
293 
294  /* sort the container and return permutation index with default comparator */
295  template <typename Container>
296  std::vector<size_t> sort_idx(const Container &src)
297  {
298  typedef typename Container::value_type T;
299  const size_t n = src.size() ;
300  std::vector<std::pair<size_t,T> > tmp;
301  tmp.reserve(n);
302  for ( size_t i = 0 ; i < n ; i++ )
303  tmp.push_back(std::make_pair(i, src[i]));
304 
305  /* sort */
306  std::stable_sort(tmp.begin(), tmp.end()) ;
307 
308  /* copy back */
309  std::vector<size_t> idx; idx.reserve(n);
310  for ( size_t i = 0 ; i < n ; i++ ) {
311  idx.push_back(tmp[i].first) ;
312  }
313  return idx;
314  }
315 
316 } // namespace gtsam
void transposeMultiplyAdd(double alpha, const Matrix &A, const Vector &e, Vector &x)
BLAS Level-2 style x <- x + alpha*A'*e.
Definition: Matrix.cpp:168
const sharedBayesNet & Rc1() const
Access Rc1.
Definition: SubgraphPreconditioner.h:219
Definition: SubgraphPreconditioner.h:34
void solve(Matrix &A, Matrix &B)
solve AX=B via in-place Lu factorization and backsubstitution After calling, A contains LU...
Definition: Matrix.cpp:283
void save(const Matrix &A, const string &s, const string &filename)
save a matrix to file, which can be loaded by matlab
Definition: Matrix.cpp:202
Factor Graph Values.
Definition: SubgraphPreconditioner.h:163
Included from all GTSAM files.
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:249
Definition: SubgraphPreconditioner.h:94
bool zero(const Vector &v)
check if all zero
Definition: Vector.cpp:39
const sharedFG & Ab2() const
Access Ab2.
Definition: SubgraphPreconditioner.h:216
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
static VectorValues Zero(const VectorValues &other)
Create a VectorValues with the same structure as other, but filled with zeros.
Definition: VectorValues.cpp:61
Definition: Preconditioner.h:23
Definition: IterativeSolver.h:115
Definition: SubgraphPreconditioner.h:138
Subgraph conditioner class, as explained in the RSS 2010 submission.
Definition: SubgraphPreconditioner.h:179
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:89
Vector operator^(const Matrix &A, const Vector &v)
overload ^ for trans(A)*v We transpose the vectors for speed.
Definition: Matrix.cpp:158
vector of errors
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition: GaussianFactorGraph.h:65
const sharedErrors b2bar() const
Access b2bar.
Definition: SubgraphPreconditioner.h:222
Definition: Preconditioner.h:62
Definition: SubgraphPreconditioner.h:54