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;
64  typedef boost::shared_ptr<ConditionalType> sharedConditional;
67 
68  protected:
69 
72 
75 
77  BayesTreeCliqueBase(const sharedConditional& conditional) : conditional_(conditional), problemSize_(1) {}
78 
81 
85  parent_ = c.parent_;
86  children = c.children;
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_;
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 }
Typedefs for easier changing of types.
KeyVector shortcut_indices(const derived_ptr &B, const FactorGraphType &p_Cp_B) const
FactorGraphType::Eliminate Eliminate
BayesTreeCliqueBase(const BayesTreeCliqueBase &c)
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
EliminationTraitsType::BayesNetType BayesNetType
BayesNetType shortcut(const derived_ptr &root, Eliminate function=EliminationTraitsType::DefaultEliminate) const
const boost::optional< FactorGraphType > & cachedSeparatorMarginal() const
BayesTreeCliqueBase(const sharedConditional &conditional)
const mpreal root(const mpreal &x, unsigned long int k, mp_rnd_t r=mpreal::get_default_rnd())
Definition: mpreal.h:2194
boost::shared_ptr< ConditionalType > sharedConditional
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
boost::shared_ptr< This > shared_ptr
void setEliminationResult(const typename FactorGraphType::EliminationResult &eliminationResult)
boost::optional< FactorGraphType > cachedSeparatorMarginal_
This stores the Cached separator marginal P(S)
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
FactorGraphType marginal2(Eliminate function=EliminationTraitsType::DefaultEliminate) const
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
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
void serialize(ARCHIVE &ar, const unsigned int)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
boost::weak_ptr< DerivedType > derived_weak_ptr
FastVector< derived_ptr > children
EliminationTraits< FACTORGRAPH > EliminationTraitsType
boost::shared_ptr< DerivedType > derived_ptr
A thin wrapper around std::vector that uses a custom allocator.
traits
Definition: chartTesting.h:28
bool equals(const DERIVED &other, double tol=1e-9) const
KeyVector separator_setminus_B(const derived_ptr &B) const
Calculate set for shortcut calculations.
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
boost::function< EliminationResult(const FactorGraphType &, const Ordering &)> Eliminate
The function type that does a single dense elimination step on a subgraph.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
BayesTreeCliqueBase & operator=(const BayesTreeCliqueBase &c)
BayesTreeCliqueBase< DERIVED, FACTORGRAPH > This
const G double tol
Definition: Group.h:83
std::pair< boost::shared_ptr< ConditionalType >, boost::shared_ptr< _FactorType > > EliminationResult
boost::weak_ptr< This > weak_ptr
const sharedConditional & conditional() const
BayesNetType::ConditionalType ConditionalType
FactorGraphType::FactorType FactorType


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:41