gtsam  4.0.0
gtsam
BayesTreeCliqueBase.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 <boost/optional.hpp>
21 #include <gtsam/base/types.h>
22 #include <gtsam/base/FastVector.h>
23 
24 namespace gtsam {
25 
26  // Forward declarations
27  template<class CLIQUE> class BayesTree;
28  template<class GRAPH> struct EliminationTraits;
29 
43  template<class DERIVED, class FACTORGRAPH>
45  {
46  private:
48  typedef DERIVED DerivedType;
50  typedef boost::shared_ptr<This> shared_ptr;
51  typedef boost::weak_ptr<This> weak_ptr;
52  typedef boost::shared_ptr<DerivedType> derived_ptr;
53  typedef boost::weak_ptr<DerivedType> derived_weak_ptr;
54 
55  public:
56  typedef FACTORGRAPH FactorGraphType;
57  typedef typename EliminationTraitsType::BayesNetType BayesNetType;
58  typedef typename BayesNetType::ConditionalType ConditionalType;
59  typedef boost::shared_ptr<ConditionalType> sharedConditional;
60  typedef typename FactorGraphType::FactorType FactorType;
61  typedef typename FactorGraphType::Eliminate Eliminate;
62 
63  protected:
64 
67 
69  BayesTreeCliqueBase() : problemSize_(1) {}
70 
72  BayesTreeCliqueBase(const sharedConditional& conditional) : conditional_(conditional), problemSize_(1) {}
73 
75 
77  mutable boost::optional<FactorGraphType> cachedSeparatorMarginal_;
78 
79  public:
80  sharedConditional conditional_;
81  derived_weak_ptr parent_;
82  FastVector<derived_ptr> children;
83  int problemSize_;
84 
88  void setEliminationResult(const typename FactorGraphType::EliminationResult& eliminationResult);
89 
92 
94  bool equals(const DERIVED& other, double tol = 1e-9) const;
95 
97  void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
98 
102 
104  const sharedConditional& conditional() const { return conditional_; }
105 
107  inline bool isRoot() const { return parent_.expired(); }
108 
110  size_t treeSize() const;
111 
113  size_t numCachedSeparatorMarginals() const;
114 
116  derived_ptr parent() const { return parent_.lock(); }
117 
119  int problemSize() const { return problemSize_; }
120 
124 
126  BayesNetType shortcut(const derived_ptr& root, Eliminate function = EliminationTraitsType::DefaultEliminate) const;
127 
129  FactorGraphType separatorMarginal(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
130 
132  FactorGraphType marginal2(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
133 
138  void deleteCachedShortcuts();
139 
140  const boost::optional<FactorGraphType>& cachedSeparatorMarginal() const {
141  return cachedSeparatorMarginal_; }
142 
143  friend class BayesTree<DerivedType>;
144 
145  protected:
146 
148  FastVector<Key> separator_setminus_B(const derived_ptr& B) const;
149 
153  FastVector<Key> shortcut_indices(const derived_ptr& B, const FactorGraphType& p_Cp_B) const;
154 
156  void deleteCachedShortcutsNonRecursive() { cachedSeparatorMarginal_ = boost::none; }
157 
158  private:
159 
161  friend class boost::serialization::access;
162  template<class ARCHIVE>
163  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
164  ar & BOOST_SERIALIZATION_NVP(conditional_);
165  ar & BOOST_SERIALIZATION_NVP(parent_);
166  ar & BOOST_SERIALIZATION_NVP(children);
167  }
168 
170 
171  };
172 
173 }
void deleteCachedShortcuts()
This deletes the cached shortcuts of all cliques (subtree) below this clique.
Definition: BayesTreeCliqueBase-inst.h:200
FastVector< Key > shortcut_indices(const derived_ptr &B, const FactorGraphType &p_Cp_B) const
Determine variable indices to keep in recursive separator shortcut calculation The factor graph p_Cp_...
Definition: BayesTreeCliqueBase-inst.h:56
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(S) on the separator
Definition: BayesTreeCliqueBase-inst.h:145
std::pair< boost::shared_ptr< ConditionalType >, boost::shared_ptr< _FactorType > > EliminationResult
The pair of conditional and remaining factor produced by a single dense elimination step on a subgrap...
Definition: EliminateableFactorGraph.h:86
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition: BayesTreeCliqueBase-inst.h:75
size_t treeSize() const
The size of subtree rooted at this clique, i.e., nr of Cliques.
Definition: BayesTreeCliqueBase-inst.h:83
BayesNetType shortcut(const derived_ptr &root, Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the conditional P(S|Root) on the separator given the root
Definition: BayesTreeCliqueBase-inst.h:111
Typedefs for easier changing of types.
BayesTreeCliqueBase()
Default constructor.
Definition: BayesTreeCliqueBase.h:69
Definition: BayesTree.h:64
Traits class for eliminateable factor graphs, specifies the types that result from elimination...
Definition: BayesTreeCliqueBase.h:28
const sharedConditional & conditional() const
Access the conditional.
Definition: BayesTreeCliqueBase.h:104
size_t numCachedSeparatorMarginals() const
Collect number of cliques with cached separator marginals.
Definition: BayesTreeCliqueBase-inst.h:92
derived_ptr parent() const
return a shared_ptr to the parent clique
Definition: BayesTreeCliqueBase.h:116
boost::optional< FactorGraphType > cachedSeparatorMarginal_
This stores the Cached separator margnal P(S)
Definition: BayesTreeCliqueBase.h:77
BayesTreeCliqueBase(const sharedConditional &conditional)
Construct from a conditional, leaving parent and child pointers uninitialized.
Definition: BayesTreeCliqueBase.h:72
bool equals(const DERIVED &other, double tol=1e-9) const
check equality
Definition: BayesTreeCliqueBase-inst.h:34
void setEliminationResult(const typename FactorGraphType::EliminationResult &eliminationResult)
Fill the elimination result produced during elimination.
Definition: BayesTreeCliqueBase-inst.h:26
void deleteCachedShortcutsNonRecursive()
Non-recursive delete cached shortcuts and marginals - internal only.
Definition: BayesTreeCliqueBase.h:156
A thin wrapper around std::vector that uses boost&#39;s pool_allocator.
int problemSize() const
Problem size (used for parallel traversal)
Definition: BayesTreeCliqueBase.h:119
FastVector< Key > separator_setminus_B(const derived_ptr &B) const
Calculate set for shortcut calculations.
Definition: BayesTreeCliqueBase-inst.h:44
This is the base class for BayesTree cliques.
Definition: BayesTreeCliqueBase.h:44
bool isRoot() const
is this the root of a Bayes tree ?
Definition: BayesTreeCliqueBase.h:107
FactorGraphType marginal2(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(C) of the clique, using marginal caching
Definition: BayesTreeCliqueBase-inst.h:188
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33