gtsam 4.1.1
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>
24#include <boost/optional.hpp>
25
26#include <string>
27#include <mutex>
28
29namespace gtsam {
30
31 // Forward declarations
32 template<class CLIQUE> class BayesTree;
33 template<class GRAPH> struct EliminationTraits;
34
48 template<class DERIVED, class FACTORGRAPH>
50 {
51 private:
53 typedef DERIVED DerivedType;
55 typedef boost::shared_ptr<This> shared_ptr;
56 typedef boost::weak_ptr<This> weak_ptr;
57 typedef boost::shared_ptr<DerivedType> derived_ptr;
58 typedef boost::weak_ptr<DerivedType> derived_weak_ptr;
59
60 public:
61 typedef FACTORGRAPH FactorGraphType;
62 typedef typename EliminationTraitsType::BayesNetType BayesNetType;
63 typedef typename BayesNetType::ConditionalType ConditionalType;
64 typedef boost::shared_ptr<ConditionalType> sharedConditional;
65 typedef typename FactorGraphType::FactorType FactorType;
66 typedef typename FactorGraphType::Eliminate Eliminate;
67
68 protected:
69
72
74 BayesTreeCliqueBase() : problemSize_(1) {}
75
78 BayesTreeCliqueBase(const sharedConditional& conditional)
79 : conditional_(conditional), problemSize_(1) {}
80
83 : conditional_(c.conditional_),
84 parent_(c.parent_),
85 children(c.children),
86 problemSize_(c.problemSize_),
87 is_root(c.is_root) {}
88
91 conditional_ = c.conditional_;
92 parent_ = c.parent_;
93 children = c.children;
94 problemSize_ = c.problemSize_;
95 is_root = c.is_root;
96 return *this;
97 }
98
99 // Virtual destructor.
100 virtual ~BayesTreeCliqueBase() {}
101
103
105 mutable boost::optional<FactorGraphType> cachedSeparatorMarginal_;
111
112 public:
113 sharedConditional conditional_;
114 derived_weak_ptr parent_;
115 FastVector<derived_ptr> children;
116 int problemSize_;
117
118 bool is_root = false;
119
123 void setEliminationResult(const typename FactorGraphType::EliminationResult& eliminationResult);
124
127
129 bool equals(const DERIVED& other, double tol = 1e-9) const;
130
132 virtual void print(
133 const std::string& s = "",
134 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
135
139
141 const sharedConditional& conditional() const { return conditional_; }
142
144 inline bool isRoot() const { return parent_.expired(); }
145
147 size_t treeSize() const;
148
150 size_t numCachedSeparatorMarginals() const;
151
153 derived_ptr parent() const { return parent_.lock(); }
154
156 int problemSize() const { return problemSize_; }
157
161
163 BayesNetType shortcut(const derived_ptr& root, Eliminate function = EliminationTraitsType::DefaultEliminate) const;
164
166 FactorGraphType separatorMarginal(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
167
169 FactorGraphType marginal2(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
170
176
177 const boost::optional<FactorGraphType>& cachedSeparatorMarginal() const {
178 std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
180 }
181
182 friend class BayesTree<DerivedType>;
183
184 protected:
185
187 KeyVector separator_setminus_B(const derived_ptr& B) const;
188
192 KeyVector shortcut_indices(const derived_ptr& B, const FactorGraphType& p_Cp_B) const;
193
196 std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
197 cachedSeparatorMarginal_ = boost::none;
198 }
199
200 private:
201
204 template<class ARCHIVE>
205 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
206 if(!parent_.lock()) {
207 is_root = true;
208 }
209 ar & BOOST_SERIALIZATION_NVP(is_root);
210 ar & BOOST_SERIALIZATION_NVP(conditional_);
211 if (!is_root) { // TODO(fan): Workaround for boost/serialization #119
212 ar & BOOST_SERIALIZATION_NVP(parent_);
213 }
214 ar & BOOST_SERIALIZATION_NVP(children);
215 }
216
218
219 };
220
221}
A thin wrapper around std::vector that uses a custom allocator.
Typedefs for easier changing of types.
Variable ordering for the elimination algorithm.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
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
Traits class for eliminateable factor graphs, specifies the types that result from elimination,...
Definition: EliminateableFactorGraph.h:36
This is the base class for BayesTree cliques.
Definition: BayesTreeCliqueBase.h:50
BayesTreeCliqueBase & operator=(const BayesTreeCliqueBase &c)
Shallow copy assignment constructor.
Definition: BayesTreeCliqueBase.h:90
void deleteCachedShortcutsNonRecursive()
Non-recursive delete cached shortcuts and marginals - internal only.
Definition: BayesTreeCliqueBase.h:195
int problemSize() const
Problem size (used for parallel traversal)
Definition: BayesTreeCliqueBase.h:156
BayesTreeCliqueBase()
Default constructor.
Definition: BayesTreeCliqueBase.h:74
derived_ptr parent() const
return a shared_ptr to the parent clique
Definition: BayesTreeCliqueBase.h:153
size_t treeSize() const
The size of subtree rooted at this clique, i.e., nr of Cliques.
Definition: BayesTreeCliqueBase-inst.h:83
BayesTreeCliqueBase(const sharedConditional &conditional)
Construct from a conditional, leaving parent and child pointers uninitialized.
Definition: BayesTreeCliqueBase.h:78
std::mutex cachedSeparatorMarginalMutex_
This protects Cached seperator marginal P(S) from concurrent read/writes as many the functions which ...
Definition: BayesTreeCliqueBase.h:110
bool isRoot() const
is this the root of a Bayes tree ?
Definition: BayesTreeCliqueBase.h:144
BayesTreeCliqueBase(const BayesTreeCliqueBase &c)
Shallow copy constructor.
Definition: BayesTreeCliqueBase.h:82
FactorGraphType marginal2(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(C) of the clique, using marginal caching
Definition: BayesTreeCliqueBase-inst.h:194
bool equals(const DERIVED &other, double tol=1e-9) const
check equality
Definition: BayesTreeCliqueBase-inst.h:34
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(S) on the separator
Definition: BayesTreeCliqueBase-inst.h:146
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:112
void deleteCachedShortcuts()
This deletes the cached shortcuts of all cliques (subtree) below this clique.
Definition: BayesTreeCliqueBase-inst.h:206
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
const sharedConditional & conditional() const
Access the conditional.
Definition: BayesTreeCliqueBase.h:141
friend class boost::serialization::access
Serialization function.
Definition: BayesTreeCliqueBase.h:203
KeyVector separator_setminus_B(const derived_ptr &B) const
Calculate set for shortcut calculations.
Definition: BayesTreeCliqueBase-inst.h:44
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition: BayesTreeCliqueBase-inst.h:75
size_t numCachedSeparatorMarginals() const
Collect number of cliques with cached separator marginals.
Definition: BayesTreeCliqueBase-inst.h:92
boost::optional< FactorGraphType > cachedSeparatorMarginal_
This stores the Cached separator marginal P(S)
Definition: BayesTreeCliqueBase.h:105
void setEliminationResult(const typename FactorGraphType::EliminationResult &eliminationResult)
Fill the elimination result produced during elimination.
Definition: BayesTreeCliqueBase-inst.h:26