From df2a6bfdee2731dbfba41733880ad24ba36d5833 Mon Sep 17 00:00:00 2001 From: Alex Cunningham Date: Fri, 21 Sep 2012 20:57:30 +0000 Subject: [PATCH] Added reporting functions for counting number of cached shortcuts/separatorMarginals in BayesTree --- gtsam.h | 4 +++ gtsam/inference/BayesTree-inl.h | 12 +++++++++ gtsam/inference/BayesTree.h | 6 +++++ gtsam/inference/BayesTreeCliqueBase-inl.h | 30 +++++++++++++++++++++-- gtsam/inference/BayesTreeCliqueBase.h | 6 +++++ gtsam/inference/tests/testBayesTree.cpp | 2 ++ 6 files changed, 58 insertions(+), 2 deletions(-) diff --git a/gtsam.h b/gtsam.h index 6777106b8..308ebdf7e 100644 --- a/gtsam.h +++ b/gtsam.h @@ -810,6 +810,8 @@ virtual class BayesTree { void clear(); void deleteCachedShorcuts(); void insert(const CLIQUE* subtree); + size_t numCachedShortcuts() const; + size_t numCachedSeparatorMarginals() const; }; template @@ -822,6 +824,8 @@ virtual class BayesTreeClique { void print(string s) const; void printTree() const; // Default indent of "" void printTree(string indent) const; + size_t numCachedShortcuts() const; + size_t numCachedSeparatorMarginals() const; CONDITIONAL* conditional() const; bool isRoot() const; diff --git a/gtsam/inference/BayesTree-inl.h b/gtsam/inference/BayesTree-inl.h index e09fdc83a..bf3b7f37c 100644 --- a/gtsam/inference/BayesTree-inl.h +++ b/gtsam/inference/BayesTree-inl.h @@ -56,6 +56,18 @@ namespace gtsam { } } + /* ************************************************************************* */ + template + size_t BayesTree::numCachedShortcuts() const { + return (root_) ? root_->numCachedShortcuts() : 0; + } + + /* ************************************************************************* */ + template + size_t BayesTree::numCachedSeparatorMarginals() const { + return (root_) ? root_->numCachedSeparatorMarginals() : 0; + } + /* ************************************************************************* */ template void BayesTree::saveGraph(const std::string &s, const IndexFormatter& indexFormatter) const { diff --git a/gtsam/inference/BayesTree.h b/gtsam/inference/BayesTree.h index 54e738559..5753c14c4 100644 --- a/gtsam/inference/BayesTree.h +++ b/gtsam/inference/BayesTree.h @@ -176,6 +176,12 @@ namespace gtsam { /** Gather data on all cliques */ CliqueData getCliqueData() const; + /** Collect number of cliques with cached shortcuts */ + size_t numCachedShortcuts() const; + + /** Collect number of cliques with cached separator marginals */ + size_t numCachedSeparatorMarginals() const; + /** return marginal on any variable */ typename FactorType::shared_ptr marginalFactor(Index j, Eliminate function) const; diff --git a/gtsam/inference/BayesTreeCliqueBase-inl.h b/gtsam/inference/BayesTreeCliqueBase-inl.h index bc98c28b5..639e27202 100644 --- a/gtsam/inference/BayesTreeCliqueBase-inl.h +++ b/gtsam/inference/BayesTreeCliqueBase-inl.h @@ -90,14 +90,40 @@ namespace gtsam { size_t BayesTreeCliqueBase::treeSize() const { size_t size = 1; BOOST_FOREACH(const derived_ptr& child, children_) - size += child->treeSize(); + size += child->treeSize(); return size; } + /* ************************************************************************* */ + template + size_t BayesTreeCliqueBase::numCachedShortcuts() const { + if (!cachedShortcut_) + return 0; + + size_t subtree_count = 1; + BOOST_FOREACH(const derived_ptr& child, children_) + subtree_count += child->numCachedShortcuts(); + + return subtree_count; + } + + /* ************************************************************************* */ + template + size_t BayesTreeCliqueBase::numCachedSeparatorMarginals() const { + if (!cachedSeparatorMarginal_) + return 0; + + size_t subtree_count = 1; + BOOST_FOREACH(const derived_ptr& child, children_) + subtree_count += child->numCachedSeparatorMarginals(); + + return subtree_count; + } + /* ************************************************************************* */ template void BayesTreeCliqueBase::printTree( - const std::string& indent, const IndexFormatter& indexFormatter) const { + const std::string& indent, const IndexFormatter& indexFormatter) const { asDerived(this)->print(indent, indexFormatter); BOOST_FOREACH(const derived_ptr& child, children_) child->printTree(indent + " ", indexFormatter); diff --git a/gtsam/inference/BayesTreeCliqueBase.h b/gtsam/inference/BayesTreeCliqueBase.h index 7038ac228..19065ff8f 100644 --- a/gtsam/inference/BayesTreeCliqueBase.h +++ b/gtsam/inference/BayesTreeCliqueBase.h @@ -125,6 +125,12 @@ namespace gtsam { /** The size of subtree rooted at this clique, i.e., nr of Cliques */ size_t treeSize() const; + /** Collect number of cliques with cached shortcuts in subtree */ + size_t numCachedShortcuts() const; + + /** Collect number of cliques with cached separator marginals */ + size_t numCachedSeparatorMarginals() const; + /** The arrow operator accesses the conditional */ const ConditionalType* operator->() const { return conditional_.get(); diff --git a/gtsam/inference/tests/testBayesTree.cpp b/gtsam/inference/tests/testBayesTree.cpp index b99364092..b0fe5e670 100644 --- a/gtsam/inference/tests/testBayesTree.cpp +++ b/gtsam/inference/tests/testBayesTree.cpp @@ -293,6 +293,8 @@ TEST( BayesTree, shortcutCheck ) bool notCleared = clique->cachedShortcut(); CHECK( notCleared == false); } + EXPECT_LONGS_EQUAL(0, rootClique->numCachedShortcuts()); + EXPECT_LONGS_EQUAL(0, rootClique->numCachedSeparatorMarginals()); // BOOST_FOREACH(SymbolicBayesTree::sharedClique& clique, allCliques) { // clique->print("Clique#");