gtsam 4.2.0
gtsam
Loading...
Searching...
No Matches
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;
54 typedef EliminationTraits<FACTORGRAPH> EliminationTraitsType;
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_;
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 nrChildren() const { return children.size(); }
148
150 const derived_ptr operator[](size_t i) const { return children[i]; }
151
153 size_t treeSize() const;
154
156 size_t numCachedSeparatorMarginals() const;
157
159 derived_ptr parent() const { return parent_.lock(); }
160
162 int problemSize() const { return problemSize_; }
163
167
169 BayesNetType shortcut(const derived_ptr& root, Eliminate function = EliminationTraitsType::DefaultEliminate) const;
170
172 FactorGraphType separatorMarginal(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
173
175 FactorGraphType marginal2(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
176
182
183 const boost::optional<FactorGraphType>& cachedSeparatorMarginal() const {
184 std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
186 }
187
188 friend class BayesTree<DerivedType>;
189
190 protected:
191
193 KeyVector separator_setminus_B(const derived_ptr& B) const;
194
198 KeyVector shortcut_indices(const derived_ptr& B, const FactorGraphType& p_Cp_B) const;
199
202 std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
203 cachedSeparatorMarginal_ = boost::none;
204 }
205
206 private:
207
210 template<class ARCHIVE>
211 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
212 if(!parent_.lock()) {
213 is_root = true;
214 }
215 ar & BOOST_SERIALIZATION_NVP(is_root);
216 ar & BOOST_SERIALIZATION_NVP(conditional_);
217 if (!is_root) { // TODO(fan): Workaround for boost/serialization #119
218 ar & BOOST_SERIALIZATION_NVP(parent_);
219 }
220 ar & BOOST_SERIALIZATION_NVP(children);
221 }
222
224
225 };
226
227}
Typedefs for easier changing of types.
A thin wrapper around std::vector that uses a custom allocator.
Variable ordering for the elimination algorithm.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:34
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
Template to create a binary predicate.
Definition Testable.h:111
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
const derived_ptr operator[](size_t i) const
Return the child at index i.
Definition BayesTreeCliqueBase.h:150
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:201
size_t nrChildren() const
Return the number of children.
Definition BayesTreeCliqueBase.h:147
int problemSize() const
Problem size (used for parallel traversal)
Definition BayesTreeCliqueBase.h:162
BayesTreeCliqueBase()
Default constructor.
Definition BayesTreeCliqueBase.h:74
derived_ptr parent() const
return a shared_ptr to the parent clique
Definition BayesTreeCliqueBase.h:159
size_t treeSize() const
The size of subtree rooted at this clique, i.e., nr of Cliques.
Definition BayesTreeCliqueBase-inst.h:84
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
Return true if this clique is 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:195
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(S) on the separator
Definition BayesTreeCliqueBase-inst.h:147
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:113
void deleteCachedShortcuts()
This deletes the cached shortcuts of all cliques (subtree) below this clique.
Definition BayesTreeCliqueBase-inst.h:207
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:57
const sharedConditional & conditional() const
Access the conditional.
Definition BayesTreeCliqueBase.h:141
friend class boost::serialization::access
Serialization function.
Definition BayesTreeCliqueBase.h:209
KeyVector separator_setminus_B(const derived_ptr &B) const
Calculate set for shortcut calculations.
Definition BayesTreeCliqueBase-inst.h:45
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition BayesTreeCliqueBase-inst.h:76
size_t numCachedSeparatorMarginals() const
Collect number of cliques with cached separator marginals.
Definition BayesTreeCliqueBase-inst.h:93
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:27