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 
25 #include <string>
26 #include <mutex>
27 #include <optional>
28 
29 namespace 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 std::shared_ptr<This> shared_ptr;
56  typedef std::weak_ptr<This> weak_ptr;
57  typedef std::shared_ptr<DerivedType> derived_ptr;
58  typedef std::weak_ptr<DerivedType> derived_weak_ptr;
59 
60  public:
61  typedef FACTORGRAPH FactorGraphType;
64  typedef std::shared_ptr<ConditionalType> sharedConditional;
67 
68  protected:
69 
72 
75 
80 
84  parent_(c.parent_),
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 std::optional<FactorGraphType> cachedSeparatorMarginal_;
110  mutable std::mutex cachedSeparatorMarginalMutex_;
111 
112  public:
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 
145 
147  inline bool isRoot() const { return parent_.expired(); }
148 
150  size_t nrChildren() const { return children.size(); }
151 
153  const derived_ptr operator[](size_t i) const { return children.at(i); }
154 
156  size_t treeSize() const;
157 
159  size_t numCachedSeparatorMarginals() const;
160 
162  derived_ptr parent() const { return parent_.lock(); }
163 
165  int problemSize() const { return problemSize_; }
166 
170 
172  BayesNetType shortcut(const derived_ptr& root, Eliminate function = EliminationTraitsType::DefaultEliminate) const;
173 
175  FactorGraphType separatorMarginal(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
176 
178  FactorGraphType marginal2(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
179 
184  void deleteCachedShortcuts();
185 
186  const std::optional<FactorGraphType>& cachedSeparatorMarginal() const {
187  std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
188  return cachedSeparatorMarginal_;
189  }
190 
191  friend class BayesTree<DerivedType>;
192 
193  protected:
194 
197 
201  KeyVector shortcut_indices(const derived_ptr& B, const FactorGraphType& p_Cp_B) const;
202 
205  std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
207  }
208 
209  private:
210 
211 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
212 
213  friend class boost::serialization::access;
214  template<class ARCHIVE>
215  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
216  if(!parent_.lock()) {
217  is_root = true;
218  }
219  ar & BOOST_SERIALIZATION_NVP(is_root);
220  ar & BOOST_SERIALIZATION_NVP(conditional_);
221  if (!is_root) { // TODO(fan): Workaround for boost/serialization #119
222  ar & BOOST_SERIALIZATION_NVP(parent_);
223  }
224  ar & BOOST_SERIALIZATION_NVP(children);
225  }
226 #endif
227 
229 
230  };
231 
232 }
gtsam::BayesTreeCliqueBase::This
BayesTreeCliqueBase< DERIVED, FACTORGRAPH > This
Definition: BayesTreeCliqueBase.h:52
gtsam::BayesTreeCliqueBase::~BayesTreeCliqueBase
virtual ~BayesTreeCliqueBase()
Definition: BayesTreeCliqueBase.h:100
gtsam::BayesTreeCliqueBase::parent_
derived_weak_ptr parent_
Definition: BayesTreeCliqueBase.h:114
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
types.h
Typedefs for easier changing of types.
gtsam::BayesTreeCliqueBase::conditional
const sharedConditional & conditional() const
Definition: BayesTreeCliqueBase.h:141
gtsam::BayesTreeCliqueBase::BayesTreeCliqueBase
BayesTreeCliqueBase(const sharedConditional &conditional)
Definition: BayesTreeCliqueBase.h:78
gtsam::BayesTreeCliqueBase::setEliminationResult
void setEliminationResult(const typename FactorGraphType::EliminationResult &eliminationResult)
Definition: BayesTreeCliqueBase-inst.h:27
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
gtsam::BayesTreeCliqueBase::DerivedType
DERIVED DerivedType
Definition: BayesTreeCliqueBase.h:53
gtsam::BayesTreeCliqueBase::isRoot
bool isRoot() const
Return true if this clique is the root of a Bayes tree.
Definition: BayesTreeCliqueBase.h:147
gtsam::BayesTreeCliqueBase::deleteCachedShortcutsNonRecursive
void deleteCachedShortcutsNonRecursive()
Definition: BayesTreeCliqueBase.h:204
gtsam::BayesTreeCliqueBase::conditional
sharedConditional & conditional()
Definition: BayesTreeCliqueBase.h:144
gtsam::BayesTreeCliqueBase::problemSize
int problemSize() const
Definition: BayesTreeCliqueBase.h:165
B
Definition: test_numpy_dtypes.cpp:299
gtsam::BayesTreeCliqueBase::derived_weak_ptr
std::weak_ptr< DerivedType > derived_weak_ptr
Definition: BayesTreeCliqueBase.h:58
gtsam::BayesTreeCliqueBase::separatorMarginal
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
Definition: BayesTreeCliqueBase-inst.h:147
gtsam::BayesTreeCliqueBase::shortcut
BayesNetType shortcut(const derived_ptr &root, Eliminate function=EliminationTraitsType::DefaultEliminate) const
Definition: BayesTreeCliqueBase-inst.h:113
gtsam::BayesTreeCliqueBase::weak_ptr
std::weak_ptr< This > weak_ptr
Definition: BayesTreeCliqueBase.h:56
gtsam::BayesTreeCliqueBase::ConditionalType
BayesNetType::ConditionalType ConditionalType
Definition: BayesTreeCliqueBase.h:63
Ordering.h
Variable ordering for the elimination algorithm.
gtsam::BayesTreeCliqueBase::separator_setminus_B
KeyVector separator_setminus_B(const derived_ptr &B) const
Calculate set for shortcut calculations.
Definition: BayesTreeCliqueBase-inst.h:45
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:92
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::BayesTreeCliqueBase::derived_ptr
std::shared_ptr< DerivedType > derived_ptr
Definition: BayesTreeCliqueBase.h:57
gtsam::BayesTreeCliqueBase::operator=
BayesTreeCliqueBase & operator=(const BayesTreeCliqueBase &c)
Shallow copy assignment constructor.
Definition: BayesTreeCliqueBase.h:90
Key.h
gtsam::EliminateableFactorGraph< SymbolicFactorGraph >::EliminationResult
std::pair< std::shared_ptr< ConditionalType >, std::shared_ptr< _FactorType > > EliminationResult
Definition: EliminateableFactorGraph.h:85
gtsam::BayesTreeCliqueBase::is_root
bool is_root
Definition: BayesTreeCliqueBase.h:118
gtsam::BayesTreeCliqueBase::Eliminate
FactorGraphType::Eliminate Eliminate
Definition: BayesTreeCliqueBase.h:66
gtsam::BayesTreeCliqueBase::shared_ptr
std::shared_ptr< This > shared_ptr
Definition: BayesTreeCliqueBase.h:55
gtsam::BayesTreeCliqueBase::operator[]
const derived_ptr operator[](size_t i) const
Return the child at index i.
Definition: BayesTreeCliqueBase.h:153
gtsam::BayesTreeCliqueBase::sharedConditional
std::shared_ptr< ConditionalType > sharedConditional
Definition: BayesTreeCliqueBase.h:64
gtsam::BayesTreeCliqueBase
Definition: BayesTreeCliqueBase.h:49
gtsam::BayesTreeCliqueBase::marginal2
FactorGraphType marginal2(Eliminate function=EliminationTraitsType::DefaultEliminate) const
Definition: BayesTreeCliqueBase-inst.h:195
gtsam::KeyFormatter
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
gtsam::SymbolicFactorGraph
Definition: SymbolicFactorGraph.h:61
gtsam::BayesTreeCliqueBase::BayesTreeCliqueBase
BayesTreeCliqueBase()
Default constructor.
Definition: BayesTreeCliqueBase.h:74
gtsam::BayesTreeCliqueBase::cachedSeparatorMarginal
const std::optional< FactorGraphType > & cachedSeparatorMarginal() const
Definition: BayesTreeCliqueBase.h:186
gtsam::BayesTreeCliqueBase::FactorType
FactorGraphType::FactorType FactorType
Definition: BayesTreeCliqueBase.h:65
gtsam::BayesTreeCliqueBase::treeSize
size_t treeSize() const
Definition: BayesTreeCliqueBase-inst.h:84
gtsam::BayesTreeCliqueBase::children
FastVector< derived_ptr > children
Definition: BayesTreeCliqueBase.h:115
gtsam::BayesTreeCliqueBase::print
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: BayesTreeCliqueBase-inst.h:76
gtsam::BayesTreeCliqueBase::conditional_
sharedConditional conditional_
Definition: BayesTreeCliqueBase.h:113
gtsam::BayesTreeCliqueBase::numCachedSeparatorMarginals
size_t numCachedSeparatorMarginals() const
Definition: BayesTreeCliqueBase-inst.h:93
gtsam::EliminationTraits
Definition: BayesTreeCliqueBase.h:33
gtsam::BayesTreeCliqueBase::BayesNetType
EliminationTraitsType::BayesNetType BayesNetType
Definition: BayesTreeCliqueBase.h:62
gtsam::BayesTreeCliqueBase::equals
bool equals(const DERIVED &other, double tol=1e-9) const
Definition: BayesTreeCliqueBase-inst.h:35
gtsam::BayesTreeCliqueBase::deleteCachedShortcuts
void deleteCachedShortcuts()
Definition: BayesTreeCliqueBase-inst.h:207
gtsam::BayesTreeCliqueBase::cachedSeparatorMarginalMutex_
std::mutex cachedSeparatorMarginalMutex_
Definition: BayesTreeCliqueBase.h:110
gtsam::SymbolicConditional
Definition: SymbolicConditional.h:36
gtsam::SymbolicBayesTreeClique
A clique in a SymbolicBayesTree.
Definition: SymbolicBayesTree.h:33
gtsam::BayesTreeCliqueBase::parent
derived_ptr parent() const
Definition: BayesTreeCliqueBase.h:162
gtsam
traits
Definition: SFMdata.h:40
gtsam::BayesTree
Definition: BayesTree.h:66
gtsam::BayesTreeCliqueBase::BayesTreeCliqueBase
BayesTreeCliqueBase(const BayesTreeCliqueBase &c)
Shallow copy constructor.
Definition: BayesTreeCliqueBase.h:82
gtsam::BayesTreeCliqueBase::EliminationTraitsType
EliminationTraits< FACTORGRAPH > EliminationTraitsType
Definition: BayesTreeCliqueBase.h:54
gtsam::SymbolicFactor
Definition: SymbolicFactor.h:38
gtsam::BayesTreeCliqueBase::shortcut_indices
KeyVector shortcut_indices(const derived_ptr &B, const FactorGraphType &p_Cp_B) const
Definition: BayesTreeCliqueBase-inst.h:57
gtsam::EliminateableFactorGraph< SymbolicFactorGraph >::Eliminate
std::function< EliminationResult(const FactorGraphType &, const Ordering &)> Eliminate
The function type that does a single dense elimination step on a subgraph.
Definition: EliminateableFactorGraph.h:88
gtsam::BayesTreeCliqueBase::cachedSeparatorMarginal_
std::optional< FactorGraphType > cachedSeparatorMarginal_
This stores the Cached separator marginal P(S)
Definition: BayesTreeCliqueBase.h:105
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::BayesTreeCliqueBase::problemSize_
int problemSize_
Definition: BayesTreeCliqueBase.h:116
gtsam::BayesTreeCliqueBase::FactorGraphType
FACTORGRAPH FactorGraphType
Definition: BayesTreeCliqueBase.h:61
gtsam::SymbolicBayesNet
Definition: SymbolicBayesNet.h:32
gtsam::BayesTreeCliqueBase::nrChildren
size_t nrChildren() const
Return the number of children.
Definition: BayesTreeCliqueBase.h:150
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:11