Changed order of conditionals created during elimination. Added unit test for disconnected graphs
parent
9bd7af848c
commit
7dfd999673
|
|
@ -175,11 +175,7 @@ namespace gtsam {
|
||||||
EliminationTreeUnordered<BAYESNET,GRAPH>::eliminate(Eliminate function) const
|
EliminationTreeUnordered<BAYESNET,GRAPH>::eliminate(Eliminate function) const
|
||||||
{
|
{
|
||||||
// Stack for eliminating nodes. We use this stack instead of recursive function calls to
|
// Stack for eliminating nodes. We use this stack instead of recursive function calls to
|
||||||
// avoid call stack overflow due to very long trees that arise from chain-like graphs. We use
|
// avoid call stack overflow due to very long trees that arise from chain-like graphs.
|
||||||
// an std::vector for storage here since we do not want frequent reallocations and do not care
|
|
||||||
// about the vector growing to be very large once and not being deallocated until this
|
|
||||||
// function exits, because in the worst case we only store one pointer in this stack for each
|
|
||||||
// variable in the system.
|
|
||||||
// TODO: Check whether this is faster as a vector (then use indices instead of parent pointers).
|
// TODO: Check whether this is faster as a vector (then use indices instead of parent pointers).
|
||||||
typedef EliminationNode<This> EliminationNode;
|
typedef EliminationNode<This> EliminationNode;
|
||||||
std::stack<EliminationNode, FastList<EliminationNode> > eliminationStack;
|
std::stack<EliminationNode, FastList<EliminationNode> > eliminationStack;
|
||||||
|
|
@ -192,8 +188,9 @@ namespace gtsam {
|
||||||
boost::shared_ptr<FactorGraphType> remainingFactors =
|
boost::shared_ptr<FactorGraphType> remainingFactors =
|
||||||
boost::make_shared<FactorGraphType>(remainingFactors_.begin(), remainingFactors_.end());
|
boost::make_shared<FactorGraphType>(remainingFactors_.begin(), remainingFactors_.end());
|
||||||
|
|
||||||
// Add roots to the stack
|
// Add roots to the stack (use reverse foreach so conditionals to appear in elimination order -
|
||||||
BOOST_FOREACH(const sharedNode& root, roots_) {
|
// doesn't matter for computation but can make printouts easier to interpret by hand)
|
||||||
|
BOOST_REVERSE_FOREACH(const sharedNode& root, roots_) {
|
||||||
eliminationStack.push(
|
eliminationStack.push(
|
||||||
EliminationNode(root.get(), root->factors.size() + root->subTrees.size(),
|
EliminationNode(root.get(), root->factors.size() + root->subTrees.size(),
|
||||||
root->factors.begin(), root->factors.end(), 0)); }
|
root->factors.begin(), root->factors.end(), 0)); }
|
||||||
|
|
@ -221,9 +218,11 @@ namespace gtsam {
|
||||||
// Remove from stack
|
// Remove from stack
|
||||||
eliminationStack.pop();
|
eliminationStack.pop();
|
||||||
} else {
|
} else {
|
||||||
// Expand children and mark as expanded
|
// Expand children and mark as expanded (use reverse foreach so conditionals to appear in
|
||||||
|
// elimination order - doesn't matter for computation but can make printouts easier to
|
||||||
|
// interpret by hand)
|
||||||
node.expanded = true;
|
node.expanded = true;
|
||||||
BOOST_FOREACH(const sharedNode& child, node.treeNode->subTrees) {
|
BOOST_REVERSE_FOREACH(const sharedNode& child, node.treeNode->subTrees) {
|
||||||
eliminationStack.push(
|
eliminationStack.push(
|
||||||
EliminationNode(child.get(), child->factors.size() + child->subTrees.size(),
|
EliminationNode(child.get(), child->factors.size() + child->subTrees.size(),
|
||||||
child->factors.begin(), child->factors.end(), &node)); }
|
child->factors.begin(), child->factors.end(), &node)); }
|
||||||
|
|
|
||||||
|
|
@ -90,11 +90,11 @@ TEST_UNSAFE(EliminationTree, eliminate )
|
||||||
{
|
{
|
||||||
// create expected Chordal bayes Net
|
// create expected Chordal bayes Net
|
||||||
SymbolicBayesNetUnordered expected;
|
SymbolicBayesNetUnordered expected;
|
||||||
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(4));
|
|
||||||
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(3,4));
|
|
||||||
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(2,4));
|
|
||||||
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(1,2,4));
|
|
||||||
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(0,1,2));
|
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(0,1,2));
|
||||||
|
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(1,2,4));
|
||||||
|
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(2,4));
|
||||||
|
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(3,4));
|
||||||
|
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(4));
|
||||||
|
|
||||||
// Create factor graph
|
// Create factor graph
|
||||||
SymbolicFactorGraphUnordered fg;
|
SymbolicFactorGraphUnordered fg;
|
||||||
|
|
@ -109,19 +109,31 @@ TEST_UNSAFE(EliminationTree, eliminate )
|
||||||
order += 0,1,2,3,4;
|
order += 0,1,2,3,4;
|
||||||
SymbolicBayesNetUnordered actual = *SymbolicEliminationTreeUnordered(fg,order).eliminate(EliminateSymbolicUnordered).first;
|
SymbolicBayesNetUnordered actual = *SymbolicEliminationTreeUnordered(fg,order).eliminate(EliminateSymbolicUnordered).first;
|
||||||
|
|
||||||
CHECK(assert_equal(expected,actual));
|
EXPECT(assert_equal(expected,actual));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
//TEST(EliminationTree, disconnected_graph) {
|
TEST(EliminationTree, disconnected_graph) {
|
||||||
// SymbolicFactorGraph fg;
|
SymbolicFactorGraphUnordered fg;
|
||||||
// fg.push_factor(0, 1);
|
fg.push_factor(0, 1);
|
||||||
// fg.push_factor(0, 2);
|
fg.push_factor(0, 2);
|
||||||
// fg.push_factor(1, 2);
|
fg.push_factor(1, 2);
|
||||||
// fg.push_factor(3, 4);
|
fg.push_factor(3, 4);
|
||||||
//
|
|
||||||
// CHECK_EXCEPTION(SymbolicEliminationTree::Create(fg), DisconnectedGraphException);
|
// create expected Chordal bayes Net
|
||||||
//}
|
SymbolicBayesNetUnordered expected;
|
||||||
|
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(0,1,2));
|
||||||
|
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(1,2));
|
||||||
|
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(2));
|
||||||
|
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(3,4));
|
||||||
|
expected.push_back(boost::make_shared<SymbolicConditionalUnordered>(4));
|
||||||
|
|
||||||
|
vector<size_t> order;
|
||||||
|
order += 0,1,2,3,4;
|
||||||
|
SymbolicBayesNetUnordered actual = *SymbolicEliminationTreeUnordered(fg, order).eliminate(EliminateSymbolicUnordered).first;
|
||||||
|
|
||||||
|
EXPECT(assert_equal(expected,actual));
|
||||||
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
int main() {
|
int main() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue