gtsam  4.1.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 #include <mutex>
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 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 
77  BayesTreeCliqueBase(const sharedConditional& conditional) : conditional_(conditional), problemSize_(1) {}
78 
80  BayesTreeCliqueBase(const BayesTreeCliqueBase& c) : conditional_(c.conditional_), parent_(c.parent_), children(c.children), problemSize_(c.problemSize_), is_root(c.is_root) {}
81 
84  conditional_ = c.conditional_;
85  parent_ = c.parent_;
86  children = c.children;
87  problemSize_ = c.problemSize_;
88  is_root = c.is_root;
89  return *this;
90  }
91 
93 
95  mutable boost::optional<FactorGraphType> cachedSeparatorMarginal_;
100  mutable std::mutex cachedSeparatorMarginalMutex_;
101 
102  public:
103  sharedConditional conditional_;
104  derived_weak_ptr parent_;
105  FastVector<derived_ptr> children;
106  int problemSize_;
107 
108  bool is_root = false;
109 
113  void setEliminationResult(const typename FactorGraphType::EliminationResult& eliminationResult);
114 
117 
119  bool equals(const DERIVED& other, double tol = 1e-9) const;
120 
122  virtual void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
123 
127 
129  const sharedConditional& conditional() const { return conditional_; }
130 
132  inline bool isRoot() const { return parent_.expired(); }
133 
135  size_t treeSize() const;
136 
138  size_t numCachedSeparatorMarginals() const;
139 
141  derived_ptr parent() const { return parent_.lock(); }
142 
144  int problemSize() const { return problemSize_; }
145 
149 
151  BayesNetType shortcut(const derived_ptr& root, Eliminate function = EliminationTraitsType::DefaultEliminate) const;
152 
154  FactorGraphType separatorMarginal(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
155 
157  FactorGraphType marginal2(Eliminate function = EliminationTraitsType::DefaultEliminate) const;
158 
163  void deleteCachedShortcuts();
164 
165  const boost::optional<FactorGraphType>& cachedSeparatorMarginal() const {
166  std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
167  return cachedSeparatorMarginal_;
168  }
169 
170  friend class BayesTree<DerivedType>;
171 
172  protected:
173 
175  KeyVector separator_setminus_B(const derived_ptr& B) const;
176 
180  KeyVector shortcut_indices(const derived_ptr& B, const FactorGraphType& p_Cp_B) const;
181 
184  std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
185  cachedSeparatorMarginal_ = boost::none;
186  }
187 
188  private:
189 
191  friend class boost::serialization::access;
192  template<class ARCHIVE>
193  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
194  if(!parent_.lock()) {
195  is_root = true;
196  }
197  ar & BOOST_SERIALIZATION_NVP(is_root);
198  ar & BOOST_SERIALIZATION_NVP(conditional_);
199  if (!is_root) { // TODO(fan): Workaround for boost/serialization #119
200  ar & BOOST_SERIALIZATION_NVP(parent_);
201  }
202  ar & BOOST_SERIALIZATION_NVP(children);
203  }
204 
206 
207  };
208 
209 }
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
gtsam::BayesTreeCliqueBase::problemSize
int problemSize() const
Problem size (used for parallel traversal)
Definition: BayesTreeCliqueBase.h:144
gtsam::BayesTreeCliqueBase::shortcut
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
gtsam::BayesTreeCliqueBase::BayesTreeCliqueBase
BayesTreeCliqueBase(const sharedConditional &conditional)
Construct from a conditional, leaving parent and child pointers uninitialized.
Definition: BayesTreeCliqueBase.h:77
gtsam::BayesTreeCliqueBase::cachedSeparatorMarginalMutex_
std::mutex cachedSeparatorMarginalMutex_
This protects Cached seperator marginal P(S) from concurrent read/writes as many the functions which ...
Definition: BayesTreeCliqueBase.h:100
gtsam::BayesTreeCliqueBase
This is the base class for BayesTree cliques.
Definition: BayesTreeCliqueBase.h:50
types.h
Typedefs for easier changing of types.
gtsam::EliminateableFactorGraph< SymbolicFactorGraph >::EliminationResult
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
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::BayesTreeCliqueBase::deleteCachedShortcutsNonRecursive
void deleteCachedShortcutsNonRecursive()
Non-recursive delete cached shortcuts and marginals - internal only.
Definition: BayesTreeCliqueBase.h:183
gtsam::BayesTreeCliqueBase::separatorMarginal
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(S) on the separator
Definition: BayesTreeCliqueBase-inst.h:146
gtsam::BayesTreeCliqueBase::equals
bool equals(const DERIVED &other, double tol=1e-9) const
check equality
Definition: BayesTreeCliqueBase-inst.h:34
gtsam::BayesTreeCliqueBase::isRoot
bool isRoot() const
is this the root of a Bayes tree ?
Definition: BayesTreeCliqueBase.h:132
gtsam::BayesTreeCliqueBase::cachedSeparatorMarginal_
boost::optional< FactorGraphType > cachedSeparatorMarginal_
This stores the Cached separator marginal P(S)
Definition: BayesTreeCliqueBase.h:95
gtsam::BayesTreeCliqueBase::treeSize
size_t treeSize() const
The size of subtree rooted at this clique, i.e., nr of Cliques.
Definition: BayesTreeCliqueBase-inst.h:83
gtsam::BayesTreeCliqueBase::parent
derived_ptr parent() const
return a shared_ptr to the parent clique
Definition: BayesTreeCliqueBase.h:141
gtsam::BayesTreeCliqueBase::BayesTreeCliqueBase
BayesTreeCliqueBase(const BayesTreeCliqueBase &c)
Shallow copy constructor.
Definition: BayesTreeCliqueBase.h:80
Key.h
gtsam::BayesTreeCliqueBase::BayesTreeCliqueBase
BayesTreeCliqueBase()
Default constructor.
Definition: BayesTreeCliqueBase.h:74
gtsam::KeyFormatter
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:35
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
gtsam::BayesTreeCliqueBase::deleteCachedShortcuts
void deleteCachedShortcuts()
This deletes the cached shortcuts of all cliques (subtree) below this clique.
Definition: BayesTreeCliqueBase-inst.h:206
Ordering.h
Variable ordering for the elimination algorithm.
gtsam::BayesTreeCliqueBase::conditional
const sharedConditional & conditional() const
Access the conditional.
Definition: BayesTreeCliqueBase.h:129
gtsam::BayesTreeCliqueBase::separator_setminus_B
KeyVector separator_setminus_B(const derived_ptr &B) const
Calculate set for shortcut calculations.
Definition: BayesTreeCliqueBase-inst.h:44
gtsam::BayesTreeCliqueBase::setEliminationResult
void setEliminationResult(const typename FactorGraphType::EliminationResult &eliminationResult)
Fill the elimination result produced during elimination.
Definition: BayesTreeCliqueBase-inst.h:26
gtsam::BayesTreeCliqueBase::numCachedSeparatorMarginals
size_t numCachedSeparatorMarginals() const
Collect number of cliques with cached separator marginals.
Definition: BayesTreeCliqueBase-inst.h:92
gtsam::BayesTreeCliqueBase::marginal2
FactorGraphType marginal2(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(C) of the clique, using marginal caching
Definition: BayesTreeCliqueBase-inst.h:194
gtsam::EliminationTraits
Traits class for eliminateable factor graphs, specifies the types that result from elimination,...
Definition: EliminateableFactorGraph.h:36
gtsam::BayesTreeCliqueBase::shortcut_indices
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
gtsam::BayesTreeCliqueBase::print
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition: BayesTreeCliqueBase-inst.h:75
gtsam::BayesTreeCliqueBase::operator=
BayesTreeCliqueBase & operator=(const BayesTreeCliqueBase &c)
Shallow copy assignment constructor.
Definition: BayesTreeCliqueBase.h:83