26 template<
class DERIVED,
class FACTORGRAPH>
28 const typename FactorGraphType::EliminationResult& eliminationResult)
30 conditional_ = eliminationResult.first;
34 template<
class DERIVED,
class FACTORGRAPH>
36 const DERIVED& other,
double tol)
const
38 return (!conditional_ && !other.conditional())
39 || conditional_->
equals(*other.conditional(), tol);
43 template<
class DERIVED,
class FACTORGRAPH>
48 KeySet indicesB(B->conditional()->begin(), B->conditional()->end());
50 std::set_difference(p_F_S_parents.begin(), p_F_S_parents.end(),
51 indicesB.begin(), indicesB.end(), back_inserter(S_setminus_B));
56 template<
class DERIVED,
class FACTORGRAPH>
58 const derived_ptr& B,
const FactorGraphType& p_Cp_B)
const
61 KeySet allKeys = p_Cp_B.keys();
62 KeySet indicesB(B->conditional()->begin(), B->conditional()->end());
66 std::set_intersection(S_setminus_B.begin(), S_setminus_B.end(),
67 allKeys.begin(), allKeys.end(), back_inserter(keep));
69 std::set_intersection(indicesB.begin(), indicesB.end(),
70 allKeys.begin(), allKeys.end(), back_inserter(keep));
75 template<
class DERIVED,
class FACTORGRAPH>
77 const std::string& s,
const KeyFormatter& keyFormatter)
const
79 conditional_->print(s, keyFormatter);
83 template<
class DERIVED,
class FACTORGRAPH>
86 for(
const derived_ptr& child: children)
87 size += child->treeSize();
92 template<
class DERIVED,
class FACTORGRAPH>
99 size_t subtree_count = 1;
100 for(
const derived_ptr& child: children)
101 subtree_count += child->numCachedSeparatorMarginals();
103 return subtree_count;
111 template<
class DERIVED,
class FACTORGRAPH>
112 typename BayesTreeCliqueBase<DERIVED, FACTORGRAPH>::BayesNetType
115 gttic(BayesTreeCliqueBase_shortcut);
119 if (!parent_.expired() && !S_setminus_B.empty())
122 derived_ptr
parent(parent_.lock());
123 gttoc(BayesTreeCliqueBase_shortcut);
124 FactorGraphType p_Cp_B(
parent->shortcut(B, function));
125 gttic(BayesTreeCliqueBase_shortcut);
126 p_Cp_B +=
parent->conditional_;
132 boost::shared_ptr<FactorGraphType> p_S_B = p_Cp_B.marginal(keep, function);
133 return *p_S_B->eliminatePartialSequential(S_setminus_B, function).first;
137 return BayesNetType();
145 template <
class DERIVED,
class FACTORGRAPH>
146 typename BayesTreeCliqueBase<DERIVED, FACTORGRAPH>::FactorGraphType
148 Eliminate function)
const {
150 gttic(BayesTreeCliqueBase_separatorMarginal);
153 gttic(BayesTreeCliqueBase_separatorMarginal_cachemiss);
156 if (parent_.expired() ) {
158 FactorGraphType empty;
162 gttoc(BayesTreeCliqueBase_separatorMarginal_cachemiss);
163 gttoc(BayesTreeCliqueBase_separatorMarginal);
167 derived_ptr parent(parent_.lock());
168 FactorGraphType p_Cp(parent->separatorMarginal(function));
170 gttic(BayesTreeCliqueBase_separatorMarginal);
171 gttic(BayesTreeCliqueBase_separatorMarginal_cachemiss);
174 p_Cp +=
parent->conditional_;
180 p_Cp.marginalMultifrontalBayesNet(
Ordering(indicesS), function);
186 return *cachedSeparatorMarginal_;
193 template <
class DERIVED,
class FACTORGRAPH>
194 typename BayesTreeCliqueBase<DERIVED, FACTORGRAPH>::FactorGraphType
196 Eliminate function)
const {
197 gttic(BayesTreeCliqueBase_marginal2);
201 p_C += boost::shared_ptr<FactorType>(this->conditional_);
206 template<
class DERIVED,
class FACTORGRAPH>
215 for(derived_ptr& child: children) {
216 child->deleteCachedShortcuts();
Base class for cliques of a BayesTree.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:86
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
Template to create a binary predicate.
Definition Testable.h:111
derived_ptr parent() const
return a shared_ptr to the parent clique
Definition BayesTreeCliqueBase.h:159
size_t treeSize() const
The size of subtree rooted at this clique, i.e., nr of Cliques.
Definition BayesTreeCliqueBase-inst.h:84
std::mutex cachedSeparatorMarginalMutex_
This protects Cached seperator marginal P(S) from concurrent read/writes as many the functions which ...
Definition BayesTreeCliqueBase.h:110
FactorGraphType marginal2(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(C) of the clique, using marginal caching
Definition BayesTreeCliqueBase-inst.h:195
bool equals(const DERIVED &other, double tol=1e-9) const
check equality
Definition BayesTreeCliqueBase-inst.h:35
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(S) on the separator
Definition BayesTreeCliqueBase-inst.h:147
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:113
void deleteCachedShortcuts()
This deletes the cached shortcuts of all cliques (subtree) below this clique.
Definition BayesTreeCliqueBase-inst.h:207
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:57
const sharedConditional & conditional() const
Access the conditional.
Definition BayesTreeCliqueBase.h:141
KeyVector separator_setminus_B(const derived_ptr &B) const
Calculate set for shortcut calculations.
Definition BayesTreeCliqueBase-inst.h:45
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition BayesTreeCliqueBase-inst.h:76
size_t numCachedSeparatorMarginals() const
Collect number of cliques with cached separator marginals.
Definition BayesTreeCliqueBase-inst.h:93
boost::optional< FactorGraphType > cachedSeparatorMarginal_
This stores the Cached separator marginal P(S).
Definition BayesTreeCliqueBase.h:105
void setEliminationResult(const typename FactorGraphType::EliminationResult &eliminationResult)
Fill the elimination result produced during elimination.
Definition BayesTreeCliqueBase-inst.h:27