23 #include <unordered_map>
24 #include <unordered_set>
29 #include "gtsam_unstable/dllexport.h"
36 template <
typename BayesTree>
66 static std::unordered_set<Key>
70 const bool debug =
ISDEBUG(
"BayesTreeMarginalizationHelper");
72 std::unordered_set<const Clique*> additionalCliques =
73 gatherAdditionalCliquesToReEliminate(bayesTree, marginalizableKeys);
75 std::unordered_set<Key> additionalKeys;
76 for (
const Clique* clique : additionalCliques) {
77 addCliqueToKeySet(clique, &additionalKeys);
81 std::cout <<
"BayesTreeMarginalizationHelper: Additional keys to re-eliminate: ";
82 for (
const Key&
key : additionalKeys) {
85 std::cout << std::endl;
88 return additionalKeys;
97 static std::unordered_set<const Clique*>
101 std::unordered_set<const Clique*> additionalCliques;
102 std::unordered_set<Key> marginalizableKeySet(
103 marginalizableKeys.begin(), marginalizableKeys.end());
107 for (
const Clique* clique :
108 getCliquesContainingKeys(bayesTree, marginalizableKeySet)) {
109 if (additionalCliques.count(clique)) {
116 if (needsReelimination(clique, marginalizableKeySet, &cachedSearch)) {
118 additionalCliques.insert(clique);
121 gatherDependentCliques(clique, marginalizableKeySet, &additionalCliques,
125 return additionalCliques;
137 const std::unordered_set<Key>& keysOfInterest) {
138 std::unordered_set<const Clique*> cliques;
139 for (
const Key&
key : keysOfInterest) {
140 cliques.insert(bayesTree[
key].
get());
160 const std::unordered_set<Key>& marginalizableKeys,
167 for (
Key key : clique->conditional()->frontals()) {
168 if (!marginalizableKeys.count(
key)) {
185 const std::unordered_set<Key>& marginalizableKeys,
192 if (isWholeCliqueMarginalizable(subtree, marginalizableKeys, cache)) {
194 if (!isWholeSubtreeMarginalizable(child.get(), marginalizableKeys, cache)) {
217 const std::unordered_set<Key>& marginalizableKeys,
219 bool hasNonMarginalizableAhead =
false;
222 for (
Key key : clique->conditional()->frontals()) {
223 if (marginalizableKeys.count(
key)) {
226 if (hasNonMarginalizableAhead) {
233 if (hasDependency(child.get(),
key) &&
234 !isWholeSubtreeMarginalizable(child.get(), marginalizableKeys, cache)) {
239 hasNonMarginalizableAhead =
true;
254 const std::unordered_set<Key>& marginalizableKeys,
255 std::unordered_set<const Clique*>* additionalCliques,
257 std::vector<const Clique*> dependentChildren;
258 dependentChildren.reserve(rootClique->children.size());
259 for (
const sharedClique& child : rootClique->children) {
260 if (additionalCliques->count(child.get())) {
266 if (hasDependency(child.get(), marginalizableKeys)) {
267 dependentChildren.push_back(child.get());
270 gatherDependentCliquesFromChildren(
271 dependentChildren, marginalizableKeys, additionalCliques, cache);
278 const std::vector<const Clique*>& dependentChildren,
279 const std::unordered_set<Key>& marginalizableKeys,
280 std::unordered_set<const Clique*>* additionalCliques,
282 std::deque<const Clique*> descendants(
283 dependentChildren.begin(), dependentChildren.end());
284 while (!descendants.empty()) {
285 const Clique* descendant = descendants.front();
286 descendants.pop_front();
291 if (!isWholeSubtreeMarginalizable(descendant, marginalizableKeys, cache)) {
292 additionalCliques->insert(descendant);
295 for (
const sharedClique& child : descendant->children) {
296 if (additionalCliques->count(child.get())) {
300 descendants.push_back(child.get());
315 std::unordered_set<Key>* additionalKeys) {
316 for (
Key key : clique->conditional()->frontals()) {
317 additionalKeys->insert(
key);
330 auto& conditional = clique->conditional();
331 if (std::find(conditional->beginParents(),
332 conditional->endParents(),
key)
333 != conditional->endParents()) {
344 const Clique* clique,
const std::unordered_set<Key>&
keys) {
345 auto& conditional = clique->conditional();
346 for (
auto it = conditional->beginParents();
347 it != conditional->endParents(); ++it) {
348 if (
keys.count(*it)) {