From d4aaa5d114874c864b570e77b7d58d01e43d41fc Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Sun, 22 Nov 2009 18:06:28 +0000 Subject: [PATCH] Added two more removePath tests and fixed bug in removeTop. But orphan list is still incorrect. --- cpp/BayesTree-inl.h | 7 ++-- cpp/BayesTree.h | 2 +- cpp/testBayesTree.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/cpp/BayesTree-inl.h b/cpp/BayesTree-inl.h index 6ae445139..a7e377250 100644 --- a/cpp/BayesTree-inl.h +++ b/cpp/BayesTree-inl.h @@ -174,9 +174,9 @@ namespace gtsam { /* ************************************************************************* */ template void BayesTree::Cliques::print(const std::string& s) const { - cout << s << " Cliques: "; + cout << s << ":\n"; BOOST_FOREACH(sharedClique clique, *this) - clique->print(); + clique->printTree(); } /* ************************************************************************* */ @@ -388,6 +388,7 @@ namespace gtsam { } /* ************************************************************************* */ + template template pair, typename BayesTree::Cliques> @@ -406,7 +407,7 @@ namespace gtsam { // remove path above this clique FactorGraph factors1; Cliques orphans1; - boost::tie(factors1,orphans1) = removePath(clique->parent_); + boost::tie(factors1,orphans1) = removePath(clique); // add to global factors and orphans factors.push_back(factors1); diff --git a/cpp/BayesTree.h b/cpp/BayesTree.h index 9e2813de7..ae221aef0 100644 --- a/cpp/BayesTree.h +++ b/cpp/BayesTree.h @@ -65,7 +65,7 @@ namespace gtsam { size_t treeSize() const; /** print this node and entire subtree below it */ - void printTree(const std::string& indent) const; + void printTree(const std::string& indent="") const; /** return the conditional P(S|Root) on the separator given the root */ // TODO: create a cached version diff --git a/cpp/testBayesTree.cpp b/cpp/testBayesTree.cpp index ffc5942e1..c1ff98dab 100644 --- a/cpp/testBayesTree.cpp +++ b/cpp/testBayesTree.cpp @@ -351,6 +351,81 @@ TEST( BayesTree, removePath ) CHECK(assert_equal(expectedOrphans2, orphans)); } +/* ************************************************************************* */ +TEST( BayesTree, removePath2 ) +{ + // Conditionals for ASIA example from the tutorial with A and D evidence + SymbolicConditional::shared_ptr + B(new SymbolicConditional("B")), + L(new SymbolicConditional("L", "B")), + E(new SymbolicConditional("E", "B", "L")), + S(new SymbolicConditional("S", "L", "B")), + T(new SymbolicConditional("T", "E", "L")), + X(new SymbolicConditional("X", "E")); + + // Create using insert + SymbolicBayesTree bayesTree; + bayesTree.insert(B); + bayesTree.insert(L); + bayesTree.insert(E); + bayesTree.insert(S); + bayesTree.insert(T); + bayesTree.insert(X); + + // Call remove-path with clique S + FactorGraph factors; + SymbolicBayesTree::Cliques orphans; + boost::tie(factors,orphans) = bayesTree.removePath(bayesTree["B"]); + + // Check expected outcome + SymbolicFactorGraph expected; + expected.push_factor("B","L","E"); + expected.push_factor("B","L"); + expected.push_factor("B"); + CHECK(assert_equal((FactorGraph)expected, factors)); + SymbolicBayesTree::Cliques expectedOrphans; + expectedOrphans += bayesTree["S"], bayesTree["T"], bayesTree["X"]; + CHECK(assert_equal(expectedOrphans, orphans)); +} + +/* ************************************************************************* */ +TEST( BayesTree, removePath3 ) +{ + // Conditionals for ASIA example from the tutorial with A and D evidence + SymbolicConditional::shared_ptr + B(new SymbolicConditional("B")), + L(new SymbolicConditional("L", "B")), + E(new SymbolicConditional("E", "B", "L")), + S(new SymbolicConditional("S", "L", "B")), + T(new SymbolicConditional("T", "E", "L")), + X(new SymbolicConditional("X", "E")); + + // Create using insert + SymbolicBayesTree bayesTree; + bayesTree.insert(B); + bayesTree.insert(L); + bayesTree.insert(E); + bayesTree.insert(S); + bayesTree.insert(T); + bayesTree.insert(X); + + // Call remove-path with clique S + FactorGraph factors; + SymbolicBayesTree::Cliques orphans; + boost::tie(factors,orphans) = bayesTree.removePath(bayesTree["S"]); + + // Check expected outcome + SymbolicFactorGraph expected; + expected.push_factor("B","L","E"); + expected.push_factor("B","L"); + expected.push_factor("B"); + expected.push_factor("L","B","S"); + CHECK(assert_equal((FactorGraph)expected, factors)); + SymbolicBayesTree::Cliques expectedOrphans; + expectedOrphans += bayesTree["T"], bayesTree["X"]; + CHECK(assert_equal(expectedOrphans, orphans)); +} + /* ************************************************************************* */ TEST( BayesTree, removeTop ) { @@ -387,8 +462,12 @@ TEST( BayesTree, removeTop ) expected.push_factor("B","L","E"); expected.push_factor("B","L"); expected.push_factor("B"); + expected.push_factor("L","B","S"); expected.push_factor("B","S"); CHECK(assert_equal((FactorGraph)expected, factors)); + SymbolicBayesTree::Cliques expectedOrphans; + expectedOrphans += bayesTree["T"], bayesTree["X"]; + //CHECK(assert_equal(expectedOrphans, orphans)); } /* ************************************************************************* */