BayesTreeCliqueBase-inst.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 
17 #pragma once
18 
21 #include <gtsam/base/timing.h>
22 
23 namespace gtsam {
24 
25  /* ************************************************************************* */
26  template<class DERIVED, class FACTORGRAPH>
28  const typename FactorGraphType::EliminationResult& eliminationResult)
29  {
30  conditional_ = eliminationResult.first;
31  }
32 
33  /* ************************************************************************* */
34  template<class DERIVED, class FACTORGRAPH>
36  const DERIVED& other, double tol) const
37  {
38  return (!conditional_ && !other.conditional())
39  || conditional_->equals(*other.conditional(), tol);
40  }
41 
42  /* ************************************************************************* */
43  template<class DERIVED, class FACTORGRAPH>
44  KeyVector
46  {
47  KeySet p_F_S_parents(this->conditional()->beginParents(), this->conditional()->endParents());
48  KeySet indicesB(B->conditional()->begin(), B->conditional()->end());
49  KeyVector S_setminus_B;
50  std::set_difference(p_F_S_parents.begin(), p_F_S_parents.end(),
51  indicesB.begin(), indicesB.end(), back_inserter(S_setminus_B));
52  return S_setminus_B;
53  }
54 
55  /* ************************************************************************* */
56  template<class DERIVED, class FACTORGRAPH>
58  const derived_ptr& B, const FactorGraphType& p_Cp_B) const
59  {
60  gttic(shortcut_indices);
61  KeySet allKeys = p_Cp_B.keys();
62  KeySet indicesB(B->conditional()->begin(), B->conditional()->end());
63  KeyVector S_setminus_B = separator_setminus_B(B);
64  KeyVector keep;
65  // keep = S\B intersect allKeys (S_setminus_B is already sorted)
66  std::set_intersection(S_setminus_B.begin(), S_setminus_B.end(), //
67  allKeys.begin(), allKeys.end(), back_inserter(keep));
68  // keep += B intersect allKeys
69  std::set_intersection(indicesB.begin(), indicesB.end(), //
70  allKeys.begin(), allKeys.end(), back_inserter(keep));
71  return keep;
72  }
73 
74  /* ************************************************************************* */
75  template<class DERIVED, class FACTORGRAPH>
77  const std::string& s, const KeyFormatter& keyFormatter) const
78  {
79  conditional_->print(s, keyFormatter);
80  }
81 
82  /* ************************************************************************* */
83  template<class DERIVED, class FACTORGRAPH>
85  size_t size = 1;
86  for(const derived_ptr& child: children)
87  size += child->treeSize();
88  return size;
89  }
90 
91  /* ************************************************************************* */
92  template<class DERIVED, class FACTORGRAPH>
94  {
95  std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
96  if (!cachedSeparatorMarginal_)
97  return 0;
98 
99  size_t subtree_count = 1;
100  for(const derived_ptr& child: children)
101  subtree_count += child->numCachedSeparatorMarginals();
102 
103  return subtree_count;
104  }
105 
106  /* ************************************************************************* */
107  // The shortcut density is a conditional P(S|B) of the separator of this
108  // clique on the root or common ancestor B. We can compute it recursively from
109  // the parent shortcut P(Sp|B) as \int P(Fp|Sp) P(Sp|B), where Fp are the
110  // frontal nodes in the parent p, and Sp the separator of the parent.
111  /* *************************************************************************
112  */
113  template <class DERIVED, class FACTORGRAPH>
116  const derived_ptr& B, Eliminate function) const {
117  gttic(BayesTreeCliqueBase_shortcut);
118  // We only calculate the shortcut when this clique is not B
119  // and when the S\B is not empty
120  KeyVector S_setminus_B = separator_setminus_B(B);
121  if (!parent_.expired() /*(if we're not the root)*/ && !S_setminus_B.empty())
122  {
123  // Obtain P(Cp||B) = P(Fp|Sp) * P(Sp||B) as a factor graph
124  derived_ptr parent(parent_.lock());
125  FactorGraphType p_Cp_B(parent->shortcut(B, function)); // P(Sp||B)
126  p_Cp_B.push_back(parent->conditional_); // P(Fp|Sp)
127 
128  // Determine the variables we want to keep, S union B
129  KeyVector keep = shortcut_indices(B, p_Cp_B);
130 
131  // Marginalize out everything except S union B
132  std::shared_ptr<FactorGraphType> p_S_B = p_Cp_B.marginal(keep, function);
133  return *p_S_B->eliminatePartialSequential(S_setminus_B, function).first;
134  }
135  else
136  {
137  return BayesNetType();
138  }
139  }
140 
141  /* *********************************************************************** */
142  // Separator marginal, uses separator marginal of parent recursively
143  // Calculates P(S) = \int P(Cp) = \int P(Fp|Sp) P(Sp)
144  // if P(Sp) is not cached, it will call separatorMarginal on the parent.
145  // Here again, Fp and Sp are the frontal nodes and separator in the parent p.
146  /* *********************************************************************** */
147  template <class DERIVED, class FACTORGRAPH>
150  Eliminate function) const {
151  std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
152  gttic(BayesTreeCliqueBase_separatorMarginal);
153  // Check if the Separator marginal was already calculated
154  if (!cachedSeparatorMarginal_) {
155  // If this is the root, there is no separator
156  if (parent_.expired() /*(if we're the root)*/) {
157  // we are root, return empty
159  cachedSeparatorMarginal_ = empty;
160  } else {
161  // Obtain P(S) = \int P(Cp) = \int P(Fp|Sp) P(Sp)
162  // initialize P(Cp) with the parent separator marginal
163  derived_ptr parent(parent_.lock());
164  FactorGraphType p_Cp(
165  parent->separatorMarginal(function)); // recursive P(Sp)
166 
167  // now add the parent conditional
168  p_Cp.push_back(parent->conditional_); // P(Fp|Sp)
169 
170  // The variables we want to keep are exactly the ones in S
171  KeyVector indicesS(this->conditional()->beginParents(),
172  this->conditional()->endParents());
173  auto separatorMarginal =
174  p_Cp.marginalMultifrontalBayesNet(Ordering(indicesS), function);
175  cachedSeparatorMarginal_ = *separatorMarginal;
176  }
177  }
178 
179  // return the shortcut P(S||B)
180  return *cachedSeparatorMarginal_; // return the cached version
181  }
182 
183  /* *********************************************************************** */
184  // marginal2, uses separator marginal of parent
185  // P(C) = P(F|S) P(S)
186  /* *********************************************************************** */
187  template <class DERIVED, class FACTORGRAPH>
190  Eliminate function) const {
191  gttic(BayesTreeCliqueBase_marginal2);
192  // initialize with separator marginal P(S)
193  FactorGraphType p_C = this->separatorMarginal(function);
194  // add the conditional P(F|S)
195  p_C.push_back(std::shared_ptr<FactorType>(this->conditional_));
196  return p_C;
197  }
198 
199  /* ************************************************************************* */
200  template<class DERIVED, class FACTORGRAPH>
202 
203  // When a shortcut is requested, all of the shortcuts between it and the
204  // root are also generated. So, if this clique's cached shortcut is set,
205  // recursively call over all child cliques. Otherwise, it is unnecessary.
206 
207  std::lock_guard<std::mutex> marginalLock(cachedSeparatorMarginalMutex_);
208  if (cachedSeparatorMarginal_) {
209  for(derived_ptr& child: children) {
210  child->deleteCachedShortcuts();
211  }
212 
213  //Delete CachedShortcut for this clique
214  cachedSeparatorMarginal_ = {};
215  }
216 
217  }
218 
219 }
timing.h
Timing utilities.
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::BayesTreeCliqueBase::setEliminationResult
void setEliminationResult(const typename FactorGraphType::EliminationResult &eliminationResult)
Definition: BayesTreeCliqueBase-inst.h:27
B
Definition: test_numpy_dtypes.cpp:301
gtsam::EliminateableFactorGraph::marginalMultifrontalBayesNet
std::shared_ptr< BayesNetType > marginalMultifrontalBayesNet(const Ordering &variables, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Definition: EliminateableFactorGraph-inst.h:230
gtsam::BayesTreeCliqueBase::separatorMarginal
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
Definition: BayesTreeCliqueBase-inst.h:149
gtsam::BayesTreeCliqueBase::shortcut
BayesNetType shortcut(const derived_ptr &root, Eliminate function=EliminationTraitsType::DefaultEliminate) const
Definition: BayesTreeCliqueBase-inst.h:115
BayesTreeCliqueBase.h
Base class for cliques of a BayesTree.
gtsam::EliminateableFactorGraph::marginal
std::shared_ptr< FactorGraphType > marginal(const KeyVector &variables, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Definition: EliminateableFactorGraph-inst.h:440
gtsam::FastSet< Key >
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
FactorGraph-inst.h
Factor Graph Base Class.
gtsam::BayesTreeCliqueBase< SymbolicBayesTreeClique, SymbolicFactorGraph >::derived_ptr
std::shared_ptr< DerivedType > derived_ptr
Definition: BayesTreeCliqueBase.h:57
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
gtsam::EliminateableFactorGraph< SymbolicFactorGraph >::EliminationResult
std::pair< std::shared_ptr< ConditionalType >, std::shared_ptr< _FactorType > > EliminationResult
Definition: EliminateableFactorGraph.h:85
gtsam::BayesTreeCliqueBase< SymbolicBayesTreeClique, SymbolicFactorGraph >::Eliminate
FactorGraphType::Eliminate Eliminate
Definition: BayesTreeCliqueBase.h:66
gtsam::BayesTreeCliqueBase::marginal2
FactorGraphType marginal2(Eliminate function=EliminationTraitsType::DefaultEliminate) const
Definition: BayesTreeCliqueBase-inst.h:189
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::FactorGraph::keys
KeySet keys() const
Definition: FactorGraph-inst.h:85
gtsam::BayesTreeCliqueBase::treeSize
size_t treeSize() const
Definition: BayesTreeCliqueBase-inst.h:84
gtsam::BayesTreeCliqueBase::print
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: BayesTreeCliqueBase-inst.h:76
gtsam::BayesTreeCliqueBase::numCachedSeparatorMarginals
size_t numCachedSeparatorMarginals() const
Definition: BayesTreeCliqueBase-inst.h:93
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:201
empty
Definition: test_copy_move.cpp:19
gtsam
traits
Definition: SFMdata.h:40
gtsam::FactorGraph::push_back
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:147
gtsam::BayesTreeCliqueBase::shortcut_indices
KeyVector shortcut_indices(const derived_ptr &B, const FactorGraphType &p_Cp_B) const
Definition: BayesTreeCliqueBase-inst.h:57
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::BayesTreeCliqueBase::FactorGraphType
FACTORGRAPH FactorGraphType
Definition: BayesTreeCliqueBase.h:61
gtsam::SymbolicBayesNet
Definition: SymbolicBayesNet.h:32
gtsam::Ordering
Definition: inference/Ordering.h:33
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
gttic
#define gttic(label)
Definition: timing.h:326


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:18