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>
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 {
44  void print(const std::string& s = "") const ;
45  };
46 
48  struct GTSAM_EXPORT BayesTreeCliqueData {
51  BayesTreeCliqueStats getStats() const;
52  };
53 
54  /* ************************************************************************* */
65  template<class CLIQUE>
66  class BayesTree
67  {
68  protected:
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 }
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
std::shared_ptr< FactorGraphType > sharedFactorGraph
Definition: BayesTree.h:84
void print(const std::string &s="", const KeyFormatter &formatter=DefaultKeyFormatter) const override
Definition: BayesTree.h:304
BayesTree< CLIQUE > This
Definition: BayesTree.h:69
FactorGraphType::Eliminate Eliminate
Definition: BayesTree.h:85
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
std::string serialize(const T &input)
serializes to a string
const Nodes & nodes() const
Definition: BayesTree.h:146
std::size_t maxSeparatorSize
Definition: BayesTree.h:43
std::shared_ptr< CliqueType > clique
Definition: BayesTree.h:286
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
std::shared_ptr< BayesNetType > sharedBayesNet
Definition: BayesTree.h:80
std::shared_ptr< ConditionalType > sharedConditional
Definition: BayesTree.h:78
NonlinearFactorGraph graph
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
Clique Node
Synonym for Clique (TODO: remove)
Definition: BayesTree.h:75
bool stats
std::shared_ptr< Clique > sharedClique
Shared pointer to a clique.
Definition: BayesTree.h:74
std::shared_ptr< FactorType > sharedFactor
Definition: BayesTree.h:82
const KeyFormatter & formatter
CLIQUE::EliminationTraitsType EliminationTraitsType
Definition: BayesTree.h:86
sharedClique operator[](Key j) const
Definition: BayesTree.h:149
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
BayesTreeOrphanWrapper(const std::shared_ptr< CliqueType > &clique)
Construct a new Bayes Tree Orphan Wrapper object.
Definition: BayesTree.h:298
FastVector< std::size_t > conditionalSizes
Definition: BayesTree.h:49
CLIQUE Clique
The clique type, normally BayesTreeClique.
Definition: BayesTree.h:73
CLIQUE::ConditionalType ConditionalType
Definition: BayesTree.h:77
FastVector< std::size_t > separatorSizes
Definition: BayesTree.h:50
ConcurrentMap< Key, sharedClique > Nodes
Definition: BayesTree.h:92
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
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
CLIQUE::FactorGraphType FactorGraphType
Definition: BayesTree.h:83
A thin wrapper around std::vector that uses a custom allocator.
traits
Definition: chartTesting.h:28
const sharedClique & clique(Key j) const
Definition: BayesTree.h:155
ofstream os("timeSchurFactors.csv")
bool empty() const
Definition: BayesTree.h:141
const Roots & roots() const
Definition: BayesTree.h:152
FastList< sharedClique > Cliques
Definition: BayesTree.h:89
FastVector< sharedClique > Roots
Definition: BayesTree.h:95
CLIQUE::BayesNetType BayesNetType
Definition: BayesTree.h:79
std::shared_ptr< This > shared_ptr
Definition: BayesTree.h:70
CLIQUE::ConditionalType Base
Definition: BayesTree.h:284
const G double tol
Definition: Group.h:86
const KeyVector keys
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
std::size_t maxConditionalSize
Definition: BayesTree.h:41
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
std::ptrdiff_t j
CLIQUE::FactorType FactorType
Definition: BayesTree.h:81


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:33:57