21 #include <boost/foreach.hpp>
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>
47 FastSet<Key> p_F_S_parents(this->conditional()->beginParents(), this->conditional()->endParents());
48 FastSet<Key> 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
60 gttic(shortcut_indices);
62 FastSet<Key> 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 BOOST_FOREACH(
const derived_ptr& child, children)
87 size += child->treeSize();
92 template<
class DERIVED,
class FACTORGRAPH>
95 if (!cachedSeparatorMarginal_)
98 size_t subtree_count = 1;
99 BOOST_FOREACH(
const derived_ptr& child, children)
100 subtree_count += child->numCachedSeparatorMarginals();
102 return subtree_count;
110 template<
class DERIVED,
class FACTORGRAPH>
111 typename BayesTreeCliqueBase<DERIVED, FACTORGRAPH>::BayesNetType
114 gttic(BayesTreeCliqueBase_shortcut);
118 if (!parent_.expired() && !S_setminus_B.empty())
121 derived_ptr parent(parent_.lock());
122 gttoc(BayesTreeCliqueBase_shortcut);
123 FactorGraphType p_Cp_B(parent->shortcut(B,
function));
124 gttic(BayesTreeCliqueBase_shortcut);
125 p_Cp_B += parent->conditional_;
131 boost::shared_ptr<FactorGraphType> p_S_B = p_Cp_B.marginal(keep,
function);
132 return *p_S_B->eliminatePartialSequential(S_setminus_B,
function).first;
136 return BayesNetType();
144 template<
class DERIVED,
class FACTORGRAPH>
145 typename BayesTreeCliqueBase<DERIVED, FACTORGRAPH>::FactorGraphType
148 gttic(BayesTreeCliqueBase_separatorMarginal);
150 if (!cachedSeparatorMarginal_)
152 gttic(BayesTreeCliqueBase_separatorMarginal_cachemiss);
154 if (parent_.expired() )
157 FactorGraphType empty;
158 cachedSeparatorMarginal_ = empty;
164 derived_ptr parent(parent_.lock());
165 gttoc(BayesTreeCliqueBase_separatorMarginal_cachemiss);
166 gttoc(BayesTreeCliqueBase_separatorMarginal);
167 FactorGraphType p_Cp(parent->separatorMarginal(
function));
168 gttic(BayesTreeCliqueBase_separatorMarginal);
169 gttic(BayesTreeCliqueBase_separatorMarginal_cachemiss);
171 p_Cp += parent->conditional_;
174 FastVector<Key> indicesS(this->conditional()->beginParents(), this->conditional()->endParents());
175 cachedSeparatorMarginal_ = *p_Cp.marginalMultifrontalBayesNet(
Ordering(indicesS), boost::none,
function);
180 return *cachedSeparatorMarginal_;
187 template<
class DERIVED,
class FACTORGRAPH>
188 typename BayesTreeCliqueBase<DERIVED, FACTORGRAPH>::FactorGraphType
191 gttic(BayesTreeCliqueBase_marginal2);
193 FactorGraphType p_C = this->separatorMarginal(
function);
195 p_C += boost::shared_ptr<FactorType>(this->conditional_);
200 template<
class DERIVED,
class FACTORGRAPH>
206 if (cachedSeparatorMarginal_) {
207 BOOST_FOREACH(derived_ptr& child, children) {
208 child->deleteCachedShortcuts();
212 cachedSeparatorMarginal_ = boost::none;
void setEliminationResult(const typename FactorGraphType::EliminationResult &eliminationResult)
Fill the elimination result produced during elimination.
Definition: BayesTreeCliqueBase-inst.h:27
void deleteCachedShortcuts()
This deletes the cached shortcuts of all cliques (subtree) below this clique.
Definition: BayesTreeCliqueBase-inst.h:201
FastVector< Key > 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
bool equals(const DERIVED &other, double tol=1e-9) const
check equality
Definition: BayesTreeCliqueBase-inst.h:35
Base class for cliques of a BayesTree.
size_t treeSize() const
The size of subtree rooted at this clique, i.e., nr of Cliques.
Definition: BayesTreeCliqueBase-inst.h:84
FastVector< Key > separator_setminus_B(const derived_ptr &B) const
Calculate set for shortcut calculations.
Definition: BayesTreeCliqueBase-inst.h:45
FactorGraphType marginal2(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(C) of the clique, using marginal caching
Definition: BayesTreeCliqueBase-inst.h:189
size_t numCachedSeparatorMarginals() const
Collect number of cliques with cached separator marginals.
Definition: BayesTreeCliqueBase-inst.h:93
Definition: Ordering.h:30
FactorGraphType separatorMarginal(Eliminate function=EliminationTraitsType::DefaultEliminate) const
return the marginal P(S) on the separator
Definition: BayesTreeCliqueBase-inst.h:146
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition: BayesTreeCliqueBase-inst.h:76
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62
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