gtsam/cpp/testIncremental.cpp

121 lines
3.6 KiB
C++
Raw Normal View History

/**
* @file testIncremental.cpp
* @brief Unit tests for graph-based iSAM
* @author Michael Kaess
* @author Viorela Ila
* @author Frank Dellaert
*/
2009-11-23 02:27:14 +08:00
#include <boost/tuple/tuple.hpp>
#include <boost/assign/std/list.hpp> // for operator +=
using namespace boost::assign;
#include <CppUnitLite/TestHarness.h>
#include "SymbolicBayesNet.h"
2009-11-19 01:03:19 +08:00
#include "SymbolicFactorGraph.h"
#include "GaussianBayesNet.h"
#include "Ordering.h"
#include "BayesTree-inl.h"
#include "smallExample.h"
using namespace std;
using namespace gtsam;
typedef BayesTree<SymbolicConditional> SymbolicBayesTree;
typedef BayesTree<GaussianConditional> GaussianBayesTree;
2009-11-19 00:31:19 +08:00
/* ************************************************************************* */
2009-11-23 06:39:32 +08:00
void update(SymbolicBayesTree& bayesTree, const FactorGraph<SymbolicFactor>& factorGraph) {
2009-11-19 01:03:19 +08:00
2009-11-23 06:39:32 +08:00
// Remove the contaminated part of the Bayes tree
2009-11-23 02:27:14 +08:00
FactorGraph<SymbolicFactor> factors;
SymbolicBayesTree::Cliques orphans;
2009-11-23 06:39:32 +08:00
BOOST_FOREACH(boost::shared_ptr<SymbolicFactor> factor, factorGraph) {
2009-11-19 02:22:08 +08:00
FactorGraph<SymbolicFactor> newFactors;
SymbolicBayesTree::Cliques newOrphans;
boost::tie(newFactors, newOrphans) = bayesTree.removeTop<SymbolicFactor>(factor);
2009-11-23 06:39:32 +08:00
factors.push_back(newFactors);
orphans.splice (orphans.begin(), newOrphans);
}
// create an ordering for the new and contaminated factors
2009-11-23 02:27:14 +08:00
Ordering ordering = factors.getOrdering();
2009-11-19 01:03:19 +08:00
// eliminate into a Bayes net
2009-11-23 02:27:14 +08:00
SymbolicBayesNet bayesNet = eliminate<SymbolicFactor,SymbolicConditional>(factors,ordering);
2009-11-19 01:03:19 +08:00
2009-11-23 06:39:32 +08:00
// insert conditionals back in, straight into the topless bayesTree
SymbolicBayesNet::const_reverse_iterator rit;
for ( rit=bayesNet.rbegin(); rit != bayesNet.rend(); ++rit )
bayesTree.insert(*rit);
2009-11-19 01:03:19 +08:00
2009-11-19 02:22:08 +08:00
// add orphans to the bottom of the new tree
BOOST_FOREACH(SymbolicBayesTree::sharedClique orphan, orphans) {
2009-11-23 06:39:32 +08:00
string key = orphan->separator_.front(); // todo: assumes there is a separator...
SymbolicBayesTree::sharedClique parent = bayesTree[key];
parent->children_ += orphan;
}
2009-11-19 00:31:19 +08:00
}
/* ************************************************************************* */
2009-11-18 23:37:19 +08:00
TEST( BayesTree, iSAM )
{
// 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"));
2009-11-18 23:37:19 +08:00
// Create using insert
SymbolicBayesTree bayesTree;
bayesTree.insert(B);
bayesTree.insert(L);
bayesTree.insert(E);
bayesTree.insert(S);
2009-11-18 23:37:19 +08:00
bayesTree.insert(T);
bayesTree.insert(X);
2009-11-19 00:31:19 +08:00
// Now we modify the Bayes tree by inserting a new factor over B and S
// New conditionals in modified top of the tree
SymbolicConditional::shared_ptr
S_(new SymbolicConditional("S")),
L_(new SymbolicConditional("L", "S")),
E_(new SymbolicConditional("E", "L", "S")),
B_(new SymbolicConditional("B", "E", "L", "S"));
2009-11-19 00:31:19 +08:00
// Create expected Bayes tree
SymbolicBayesTree expected;
expected.insert(S_);
expected.insert(L_);
expected.insert(E_);
expected.insert(B_);
2009-11-19 00:31:19 +08:00
expected.insert(T);
expected.insert(X);
// create new factors to be inserted
SymbolicFactorGraph factorGraph;
factorGraph.push_factor("B","S");
2009-11-19 00:31:19 +08:00
// do incremental inference
update(bayesTree, factorGraph);
2009-11-19 00:31:19 +08:00
// Check whether the same
CHECK(assert_equal(expected,bayesTree));
}
/* ************************************************************************* */
int main() {
TestResult tr;
return TestRegistry::runAllTests(tr);
}
/* ************************************************************************* */