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
17
18#pragma once
19
20#include <gtsam/inference/Key.h>
21#include <gtsam/base/types.h>
23
24#include <string>
25#include <mutex>
26#include <optional>
27
28namespace 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;
53 typedef EliminationTraits<FACTORGRAPH> EliminationTraitsType;
54 typedef std::shared_ptr<This> shared_ptr;
55 typedef std::weak_ptr<This> weak_ptr;
56 typedef std::shared_ptr<DerivedType> derived_ptr;
57 typedef std::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 std::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
77 BayesTreeCliqueBase(const sharedConditional& conditional)
78 : conditional_(conditional), problemSize_(1) {}
79
82 : conditional_(c.conditional_),
83 parent_(c.parent_),
84 children(c.children),
85 problemSize_(c.problemSize_),
86 is_root(c.is_root) {}
87
90 conditional_ = c.conditional_;
91 parent_ = c.parent_;
92 children = c.children;
93 problemSize_ = c.problemSize_;
94 is_root = c.is_root;
95 return *this;
96 }
97
98 // Virtual destructor.
99 virtual ~BayesTreeCliqueBase() {}
100
102
104 mutable std::optional<FactorGraphType> cachedSeparatorMarginal_;
110
111 public:
112 sharedConditional conditional_;
113 derived_weak_ptr parent_;
115 int problemSize_;
116
117 bool is_root = false;
118
122 void setEliminationResult(const typename FactorGraphType::EliminationResult& eliminationResult);
123
126
128 bool equals(const DERIVED& other, double tol = 1e-9) const;
129
131 virtual void print(
132 const std::string& s = "",
133 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
134
138
140 const sharedConditional& conditional() const { return conditional_; }
141
143 sharedConditional& conditional() { return conditional_; }
144
146 inline bool isRoot() const { return parent_.expired(); }
147
149 size_t nrChildren() const { return children.size(); }
150
152 const derived_ptr operator[](size_t i) const { return children.at(i); }
153
155 size_t treeSize() const;
156
159
161 derived_ptr parent() const { return parent_.lock(); }
162
164 int problemSize() const { return problemSize_; }
165
169
171 BayesNetType shortcut(const derived_ptr& root, Eliminate function = EliminationTraitsType::DefaultEliminate) const;
172
174 FactorGraphType separatorMarginal(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
175
177 FactorGraphType marginal2(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
178
184
185 const std::optional<FactorGraphType>& cachedSeparatorMarginal() const {
186 std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
188 }
189
190 friend class BayesTree<DerivedType>;
191
193 KeyVector separator_setminus_B(const derived_ptr& B) const;
194
195 protected:
196
200 KeyVector shortcut_indices(const derived_ptr& B, const FactorGraphType& p_Cp_B) const;
201
204 std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
206 }
207
208 private:
209
210#if GTSAM_ENABLE_BOOST_SERIALIZATION
212 friend class boost::serialization::access;
213 template<class ARCHIVE>
214 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
215 if(!parent_.lock()) {
216 is_root = true;
217 }
218 ar & BOOST_SERIALIZATION_NVP(is_root);
219 ar & BOOST_SERIALIZATION_NVP(conditional_);
220 if (!is_root) { // TODO(fan): Workaround for boost/serialization #119
221 ar & BOOST_SERIALIZATION_NVP(parent_);
222 }
223 ar & BOOST_SERIALIZATION_NVP(children);
224 }
225#endif
226
228
229 };
230
231}
A thin wrapper around std::vector that uses a custom allocator.
Typedefs for easier changing of types.
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:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
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
static std::pair< std::shared_ptr< ConditionalType >, std::shared_ptr< FactorType > > DefaultEliminate(const FactorGraphType &factors, const Ordering &keys)
The default dense elimination function.
Definition DiscreteFactorGraph.h:81
Bayes tree.
Definition BayesTree.h:77
Traits class for eliminateable factor graphs, specifies the types that result from elimination,...
Definition EliminateableFactorGraph.h:38
This is the base class for BayesTree cliques.
Definition BayesTreeCliqueBase.h:49
const derived_ptr operator[](size_t i) const
Return the child at index i.
Definition BayesTreeCliqueBase.h:152
BayesTreeCliqueBase & operator=(const BayesTreeCliqueBase &c)
Shallow copy assignment constructor.
Definition BayesTreeCliqueBase.h:89
void deleteCachedShortcutsNonRecursive()
Non-recursive delete cached shortcuts and marginals - internal only.
Definition BayesTreeCliqueBase.h:203
size_t nrChildren() const
Return the number of children.
Definition BayesTreeCliqueBase.h:149
int problemSize() const
Problem size (used for parallel traversal).
Definition BayesTreeCliqueBase.h:164
BayesTreeCliqueBase()
Default constructor.
Definition BayesTreeCliqueBase.h:73
derived_ptr parent() const
return a shared_ptr to the parent clique
Definition BayesTreeCliqueBase.h:161
size_t treeSize() const
The size of subtree rooted at this clique, i.e., nr of Cliques.
Definition BayesTreeCliqueBase-inst.h:87
BayesTreeCliqueBase(const sharedConditional &conditional)
Construct from a conditional, leaving parent and child pointers uninitialized.
Definition BayesTreeCliqueBase.h:77
std::mutex cachedSeparatorMarginalMutex_
Definition BayesTreeCliqueBase.h:109
bool isRoot() const
Return true if this clique is the root of a Bayes tree.
Definition BayesTreeCliqueBase.h:146
sharedConditional & conditional()
Write access to the conditional.
Definition BayesTreeCliqueBase.h:143
BayesTreeCliqueBase(const BayesTreeCliqueBase &c)
Shallow copy constructor.
Definition BayesTreeCliqueBase.h:81
FactorGraphType marginal2(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(C) of the clique, using marginal caching
Definition BayesTreeCliqueBase-inst.h:212
bool equals(const DERIVED &other, double tol=1e-9) const
check equality
Definition BayesTreeCliqueBase-inst.h:38
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(S) on the separator
Definition BayesTreeCliqueBase-inst.h:152
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:118
void deleteCachedShortcuts()
This deletes the cached shortcuts of all cliques (subtree) below this clique.
Definition BayesTreeCliqueBase-inst.h:224
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:60
const sharedConditional & conditional() const
Definition BayesTreeCliqueBase.h:140
KeyVector separator_setminus_B(const derived_ptr &B) const
Calculate set for shortcut calculations.
Definition BayesTreeCliqueBase-inst.h:48
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition BayesTreeCliqueBase-inst.h:79
size_t numCachedSeparatorMarginals() const
Collect number of cliques with cached separator marginals.
Definition BayesTreeCliqueBase-inst.h:96
std::optional< FactorGraphType > cachedSeparatorMarginal_
Definition BayesTreeCliqueBase.h:104
void setEliminationResult(const typename FactorGraphType::EliminationResult &eliminationResult)
Fill the elimination result produced during elimination.
Definition BayesTreeCliqueBase-inst.h:30
std::pair< std::shared_ptr< ConditionalType >, std::shared_ptr< _FactorType > > EliminationResult
Definition EliminateableFactorGraph.h:88