GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
BayesTree.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 // \callgraph
19 
20 #pragma once
21 
22 #include <memory>
23 
24 #include <gtsam/inference/Key.h>
25 #include <gtsam/base/FastList.h>
26 #include <gtsam/base/ConcurrentMap.h>
27 #include <gtsam/base/FastVector.h>
28 
29 #include <string>
30 
31 namespace gtsam {
32 
33  // Forward declarations
34  template<class FACTOR> class FactorGraph;
35  template<class BAYESTREE, class GRAPH> class EliminatableClusterTree;
36 
37  /* ************************************************************************* */
39  struct GTSAM_EXPORT BayesTreeCliqueStats {
40  double avgConditionalSize;
41  std::size_t maxConditionalSize;
42  double avgSeparatorSize;
43  std::size_t maxSeparatorSize;
44  void print(const std::string& s = "") const ;
45  };
46 
48  struct GTSAM_EXPORT BayesTreeCliqueData {
49  FastVector<std::size_t> conditionalSizes;
50  FastVector<std::size_t> separatorSizes;
51  BayesTreeCliqueStats getStats() const;
52  };
53 
54  /* ************************************************************************* */
65  template<class CLIQUE>
66  class BayesTree
67  {
68  protected:
69  typedef BayesTree<CLIQUE> This;
70  typedef std::shared_ptr<This> shared_ptr;
71 
72  public:
73  typedef CLIQUE Clique;
74  typedef std::shared_ptr<Clique> sharedClique;
75  typedef Clique Node;
76  typedef sharedClique sharedNode;
77  typedef typename CLIQUE::ConditionalType ConditionalType;
78  typedef std::shared_ptr<ConditionalType> sharedConditional;
79  typedef typename CLIQUE::BayesNetType BayesNetType;
80  typedef std::shared_ptr<BayesNetType> sharedBayesNet;
81  typedef typename CLIQUE::FactorType FactorType;
82  typedef std::shared_ptr<FactorType> sharedFactor;
83  typedef typename CLIQUE::FactorGraphType FactorGraphType;
84  typedef std::shared_ptr<FactorGraphType> sharedFactorGraph;
85  typedef typename FactorGraphType::Eliminate Eliminate;
86  typedef typename CLIQUE::EliminationTraitsType EliminationTraitsType;
87 
90 
93 
96 
97  protected:
98 
100  Nodes nodes_;
101 
103  Roots roots_;
104 
107 
110 
112  BayesTree(const This& other);
113 
115 
117  ~BayesTree();
118 
120  This& operator=(const This& other);
121 
124 
126  bool equals(const This& other, double tol = 1e-9) const;
127 
128  public:
130  void print(const std::string& s = "",
131  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
133 
136 
138  size_t size() const;
139 
141  inline bool empty() const {
142  return nodes_.empty();
143  }
144 
146  const Nodes& nodes() const { return nodes_; }
147 
149  sharedClique operator[](Key j) const { return nodes_.at(j); }
150 
152  const Roots& roots() const { return roots_; }
153 
155  const sharedClique& clique(Key j) const {
156  typename Nodes::const_iterator c = nodes_.find(j);
157  if(c == nodes_.end())
158  throw std::out_of_range("Requested the BayesTree clique for a key that is not in the BayesTree");
159  else
160  return c->second;
161  }
162 
164  BayesTreeCliqueData getCliqueData() const;
165 
167  size_t numCachedSeparatorMarginals() const;
168 
174  sharedConditional marginalFactor(Key j, const Eliminate& function = EliminationTraitsType::DefaultEliminate) const;
175 
180  sharedFactorGraph joint(Key j1, Key j2, const Eliminate& function = EliminationTraitsType::DefaultEliminate) const;
181 
186  sharedBayesNet jointBayesNet(Key j1, Key j2, const Eliminate& function = EliminationTraitsType::DefaultEliminate) const;
187 
190 
192  void dot(std::ostream& os, const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
193 
195  std::string dot(
196  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
197 
199  void saveGraph(const std::string& filename,
200  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
201 
205 
210  template<class CONTAINER>
211  Key findParentClique(const CONTAINER& parents) const;
212 
214  void clear();
215 
217  void deleteCachedShortcuts();
218 
223  void removePath(sharedClique clique, BayesNetType* bn, Cliques* orphans);
224 
229  void removeTop(const KeyVector& keys, BayesNetType* bn, Cliques* orphans);
230 
233  Cliques removeSubtree(const sharedClique& subtree);
234 
238  void insertRoot(const sharedClique& subtree);
239 
241  void addClique(const sharedClique& clique, const sharedClique& parent_clique = sharedClique());
242 
244  void addFactorsToGraph(FactorGraph<FactorType>* graph) const;
245 
246  protected:
247 
249  void dot(std::ostream &s, sharedClique clique, const KeyFormatter& keyFormatter,
250  int parentnum = 0) const;
251 
253  void getCliqueData(sharedClique clique, BayesTreeCliqueData* stats) const;
254 
256  void removeClique(sharedClique clique);
257 
259  void fillNodesIndex(const sharedClique& subtree);
260 
261  // Friend JunctionTree because it directly fills roots and nodes index.
262  template<class BAYESTREE, class GRAPH> friend class EliminatableClusterTree;
263 
264  private:
265 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
266 
267  friend class boost::serialization::access;
268  template<class ARCHIVE>
269  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
270  ar & BOOST_SERIALIZATION_NVP(nodes_);
271  ar & BOOST_SERIALIZATION_NVP(roots_);
272  }
273 #endif
274 
276 
277  }; // BayesTree
278 
279  /* ************************************************************************* */
280  template <class CLIQUE, typename = void>
281  class BayesTreeOrphanWrapper : public CLIQUE::ConditionalType {
282  public:
283  typedef CLIQUE CliqueType;
284  typedef typename CLIQUE::ConditionalType Base;
285 
286  std::shared_ptr<CliqueType> clique;
287 
298  BayesTreeOrphanWrapper(const std::shared_ptr<CliqueType>& clique)
299  : clique(clique) {
300  this->keys_.assign(clique->conditional()->beginParents(),
301  clique->conditional()->endParents());
302  }
303 
304  void print(
305  const std::string& s = "",
306  const KeyFormatter& formatter = DefaultKeyFormatter) const override {
307  clique->print(s + "stored clique", formatter);
308  }
309  };
310 
311 }
sharedClique sharedNode
Synonym for sharedClique (TODO: remove)
Definition: BayesTree.h:76
A thin wrapper around std::list that uses boost&#39;s fast_pool_allocator.
double dot(const V1 &a, const V2 &b)
Definition: Vector.h:195
Nodes nodes_
Definition: BayesTree.h:100
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:113
const Nodes & nodes() const
Definition: BayesTree.h:146
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
Definition: BayesTree.h:34
Clique Node
Synonym for Clique (TODO: remove)
Definition: BayesTree.h:75
Definition: BayesTree.h:281
std::shared_ptr< Clique > sharedClique
Shared pointer to a clique.
Definition: BayesTree.h:74
sharedClique operator[](Key j) const
Definition: BayesTree.h:149
Definition: BayesTree.h:66
Definition: Testable.h:112
Definition: BayesTree.h:35
BayesTreeOrphanWrapper(const std::shared_ptr< CliqueType > &clique)
Construct a new Bayes Tree Orphan Wrapper object.
Definition: BayesTree.h:298
CLIQUE Clique
The clique type, normally BayesTreeClique.
Definition: BayesTree.h:73
GTSAM_EXPORT void print(const Matrix &A, const std::string &s, std::ostream &stream)
ConcurrentMap< Key, sharedClique > Nodes
Definition: BayesTree.h:92
BayesTree()
Definition: BayesTree.h:109
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
Definition: FastList.h:43
A thin wrapper around std::vector that uses a custom allocator.
Definition: chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
const sharedClique & clique(Key j) const
Definition: BayesTree.h:155
bool empty() const
Definition: BayesTree.h:141
const Roots & roots() const
Definition: BayesTree.h:152
Roots roots_
Definition: BayesTree.h:103
FastList< sharedClique > Cliques
Definition: BayesTree.h:89
FastVector< sharedClique > Roots
Definition: BayesTree.h:95
Definition: BayesTree.h:48
Definition: BayesTree.h:39
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102