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 <gtsam/inference/Key.h>
22 #include <gtsam/base/types.h>
23 #include <gtsam/base/FastVector.h>
24 #include <boost/optional.hpp>
25 
26 #include <string>
27 
28 namespace gtsam {
29 
30  // Forward declarations
31  template<class CLIQUE> class BayesTree;
32  template<class GRAPH> struct EliminationTraits;
33 
47  template<class DERIVED, class FACTORGRAPH>
49  {
50  private:
52  typedef DERIVED DerivedType;
54  typedef boost::shared_ptr<This> shared_ptr;
55  typedef boost::weak_ptr<This> weak_ptr;
56  typedef boost::shared_ptr<DerivedType> derived_ptr;
57  typedef boost::weak_ptr<DerivedType> derived_weak_ptr;
58 
59  public:
60  typedef FACTORGRAPH FactorGraphType;
61  typedef typename EliminationTraitsType::BayesNetType BayesNetType;
62  typedef typename BayesNetType::ConditionalType ConditionalType;
63  typedef boost::shared_ptr<ConditionalType> sharedConditional;
64  typedef typename FactorGraphType::FactorType FactorType;
65  typedef typename FactorGraphType::Eliminate Eliminate;
66 
67  protected:
68 
71 
73  BayesTreeCliqueBase() : problemSize_(1) {}
74 
76  BayesTreeCliqueBase(const sharedConditional& conditional) : conditional_(conditional), problemSize_(1) {}
77 
79 
81  mutable boost::optional<FactorGraphType> cachedSeparatorMarginal_;
82 
83  public:
84  sharedConditional conditional_;
85  derived_weak_ptr parent_;
86  FastVector<derived_ptr> children;
87  int problemSize_;
88 
92  void setEliminationResult(const typename FactorGraphType::EliminationResult& eliminationResult);
93 
96 
98  bool equals(const DERIVED& other, double tol = 1e-9) const;
99 
101  void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
102 
106 
108  const sharedConditional& conditional() const { return conditional_; }
109 
111  inline bool isRoot() const { return parent_.expired(); }
112 
114  size_t treeSize() const;
115 
117  size_t numCachedSeparatorMarginals() const;
118 
120  derived_ptr parent() const { return parent_.lock(); }
121 
123  int problemSize() const { return problemSize_; }
124 
128 
130  BayesNetType shortcut(const derived_ptr& root, Eliminate function = EliminationTraitsType::DefaultEliminate) const;
131 
133  FactorGraphType separatorMarginal(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
134 
136  FactorGraphType marginal2(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
137 
142  void deleteCachedShortcuts();
143 
144  const boost::optional<FactorGraphType>& cachedSeparatorMarginal() const {
145  return cachedSeparatorMarginal_; }
146 
147  friend class BayesTree<DerivedType>;
148 
149  protected:
150 
152  KeyVector separator_setminus_B(const derived_ptr& B) const;
153 
157  KeyVector shortcut_indices(const derived_ptr& B, const FactorGraphType& p_Cp_B) const;
158 
161 
162  private:
163 
165  friend class boost::serialization::access;
166  template<class ARCHIVE>
167  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
168  ar & BOOST_SERIALIZATION_NVP(conditional_);
169  ar & BOOST_SERIALIZATION_NVP(parent_);
170  ar & BOOST_SERIALIZATION_NVP(children);
171  }
172 
174 
175  };
176 
177 }
FactorGraphType marginal2(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(C) of the clique, using marginal caching
Definition: BayesTreeCliqueBase-inst.h:188
BayesTreeCliqueBase(const sharedConditional &conditional)
Construct from a conditional, leaving parent and child pointers uninitialized.
Definition: BayesTreeCliqueBase.h:76
A thin wrapper around std::vector that uses a custom allocator.
int problemSize() const
Problem size (used for parallel traversal)
Definition: BayesTreeCliqueBase.h:123
bool isRoot() const
is this the root of a Bayes tree ?
Definition: BayesTreeCliqueBase.h:111
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
Variable ordering for the elimination algorithm.
This is the base class for BayesTree cliques.
Definition: BayesTreeCliqueBase.h:48
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
BayesTreeCliqueBase()
Default constructor.
Definition: BayesTreeCliqueBase.h:73
derived_ptr parent() const
return a shared_ptr to the parent clique
Definition: BayesTreeCliqueBase.h:120
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:56
size_t numCachedSeparatorMarginals() const
Collect number of cliques with cached separator marginals.
Definition: BayesTreeCliqueBase-inst.h:92
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
void deleteCachedShortcuts()
This deletes the cached shortcuts of all cliques (subtree) below this clique.
Definition: BayesTreeCliqueBase-inst.h:200
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
bool equals(const DERIVED &other, double tol=1e-9) const
check equality
Definition: BayesTreeCliqueBase-inst.h:34
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition: BayesTreeCliqueBase-inst.h:75
void deleteCachedShortcutsNonRecursive()
Non-recursive delete cached shortcuts and marginals - internal only.
Definition: BayesTreeCliqueBase.h:160
KeyVector separator_setminus_B(const derived_ptr &B) const
Calculate set for shortcut calculations.
Definition: BayesTreeCliqueBase-inst.h:44
const sharedConditional & conditional() const
Access the conditional.
Definition: BayesTreeCliqueBase.h:108
KeyVector 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
Traits class for eliminateable factor graphs, specifies the types that result from elimination,...
Definition: BayesTreeCliqueBase.h:32
void setEliminationResult(const typename FactorGraphType::EliminationResult &eliminationResult)
Fill the elimination result produced during elimination.
Definition: BayesTreeCliqueBase-inst.h:26
boost::optional< FactorGraphType > cachedSeparatorMarginal_
This stores the Cached separator margnal P(S)
Definition: BayesTreeCliqueBase.h:81
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(S) on the separator
Definition: BayesTreeCliqueBase-inst.h:145
Typedefs for easier changing of types.
size_t treeSize() const
The size of subtree rooted at this clique, i.e., nr of Cliques.
Definition: BayesTreeCliqueBase-inst.h:83