29 template<
class DERIVED,
class FACTORGRAPH>
31 const typename FactorGraphType::EliminationResult& eliminationResult)
33 conditional_ = eliminationResult.first;
37 template<
class DERIVED,
class FACTORGRAPH>
39 const DERIVED& other,
double tol)
const
41 return (!conditional_ && !other.conditional())
42 || conditional_->
equals(*other.conditional(), tol);
46 template<
class DERIVED,
class FACTORGRAPH>
51 KeySet indicesB(B->conditional()->begin(), B->conditional()->end());
53 std::set_difference(p_F_S_parents.begin(), p_F_S_parents.end(),
54 indicesB.begin(), indicesB.end(), std::back_inserter(S_setminus_B));
59 template<
class DERIVED,
class FACTORGRAPH>
61 const derived_ptr& B,
const FactorGraphType& p_Cp_B)
const
64 KeySet allKeys = p_Cp_B.keys();
65 KeySet indicesB(B->conditional()->begin(), B->conditional()->end());
69 std::set_intersection(S_setminus_B.begin(), S_setminus_B.end(),
70 allKeys.begin(), allKeys.end(), std::back_inserter(keep));
72 std::set_intersection(indicesB.begin(), indicesB.end(),
73 allKeys.begin(), allKeys.end(), std::back_inserter(keep));
78 template<
class DERIVED,
class FACTORGRAPH>
80 const std::string& s,
const KeyFormatter& keyFormatter)
const
82 conditional_->print(s, keyFormatter);
86 template<
class DERIVED,
class FACTORGRAPH>
89 for(
const derived_ptr& child: children)
90 size += child->treeSize();
95 template<
class DERIVED,
class FACTORGRAPH>
102 size_t subtree_count = 1;
103 for(
const derived_ptr& child: children)
104 subtree_count += child->numCachedSeparatorMarginals();
106 return subtree_count;
116 template <
class DERIVED,
class FACTORGRAPH>
117 typename BayesTreeCliqueBase<DERIVED, FACTORGRAPH>::BayesNetType
119 const derived_ptr& B, Eliminate function)
const {
120 gttic(BayesTreeCliqueBase_shortcut);
124 if (!parent_.expired() && !S_setminus_B.empty())
127 derived_ptr
parent(parent_.lock());
128 FactorGraphType p_Cp_B(
parent->shortcut(B, function));
129 p_Cp_B.push_back(
parent->conditional_);
135 std::shared_ptr<FactorGraphType> p_S_B = p_Cp_B.marginal(keep, function);
136 return *p_S_B->eliminatePartialSequential(S_setminus_B, function).first;
140 return BayesNetType();
150 template <
class DERIVED,
class FACTORGRAPH>
151 typename BayesTreeCliqueBase<DERIVED, FACTORGRAPH>::FactorGraphType
153 Eliminate function)
const {
154 gttic(BayesTreeCliqueBase_separatorMarginal);
159 std::vector<const This*> path;
160 std::vector<derived_ptr> keepAlive;
161 std::vector<std::unique_lock<std::mutex>> locks;
162 const This* clique =
this;
164 path.push_back(clique);
170 derived_ptr
parent = clique->parent_.lock();
178 for (
auto it = path.rbegin(); it != path.rend(); ++it) {
179 const This* clique = *it;
184 derived_ptr
parent = clique->parent_.lock();
186 FactorGraphType empty;
192 FactorGraphType p_Cp(*parent->cachedSeparatorMarginal_);
199 p_Cp.marginalMultifrontalBayesNet(
Ordering(indicesS), function);
203 return *cachedSeparatorMarginal_;
210 template <
class DERIVED,
class FACTORGRAPH>
211 typename BayesTreeCliqueBase<DERIVED, FACTORGRAPH>::FactorGraphType
213 Eliminate function)
const {
214 gttic(BayesTreeCliqueBase_marginal2);
218 p_C.push_back(std::shared_ptr<FactorType>(this->conditional_));
223 template<
class DERIVED,
class FACTORGRAPH>
232 for(derived_ptr& child: children) {
233 child->deleteCachedShortcuts();
Base class for cliques of a BayesTree.
Variable ordering for the elimination algorithm.
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:91
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:112
derived_ptr parent() const
Definition BayesTreeCliqueBase.h:161
size_t treeSize() const
The size of subtree rooted at this clique, i.e., nr of Cliques.
Definition BayesTreeCliqueBase-inst.h:87
std::mutex cachedSeparatorMarginalMutex_
This protects Cached seperator marginal P(S) from concurrent read/writes as many the functions which ...
Definition BayesTreeCliqueBase.h:109
FactorGraphType marginal2(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(C) of the clique, using marginal caching
Definition BayesTreeCliqueBase-inst.h:212
bool equals(const DERIVED &other, double tol=1e-9) const
check equality
Definition BayesTreeCliqueBase-inst.h:38
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(S) on the separator
Definition BayesTreeCliqueBase-inst.h:152
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:118
void deleteCachedShortcuts()
This deletes the cached shortcuts of all cliques (subtree) below this clique.
Definition BayesTreeCliqueBase-inst.h:224
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:60
const sharedConditional & conditional() const
Access the conditional.
Definition BayesTreeCliqueBase.h:140
KeyVector separator_setminus_B(const derived_ptr &B) const
Calculate set for shortcut calculations.
Definition BayesTreeCliqueBase-inst.h:48
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition BayesTreeCliqueBase-inst.h:79
size_t numCachedSeparatorMarginals() const
Collect number of cliques with cached separator marginals.
Definition BayesTreeCliqueBase-inst.h:96
std::optional< FactorGraphType > cachedSeparatorMarginal_
This stores the Cached separator marginal P(S).
Definition BayesTreeCliqueBase.h:104
void setEliminationResult(const typename FactorGraphType::EliminationResult &eliminationResult)
Fill the elimination result produced during elimination.
Definition BayesTreeCliqueBase-inst.h:30