183 lines
5.7 KiB
C++
183 lines
5.7 KiB
C++
/**
|
|
* @file testSymbolicFactorGraph.cpp
|
|
* @brief Unit tests for a symbolic Factor Graph
|
|
* @author Frank Dellaert
|
|
*/
|
|
|
|
#include <boost/assign/std/list.hpp> // for operator +=
|
|
using namespace boost::assign;
|
|
|
|
#include <gtsam/CppUnitLite/TestHarness.h>
|
|
|
|
#define GTSAM_MAGIC_KEY
|
|
|
|
#include <gtsam/slam/smallExample.h>
|
|
#include <gtsam/inference/SymbolicFactorGraph.h>
|
|
#include <gtsam/inference/Factor-inl.h>
|
|
#include <gtsam/inference/BayesNet-inl.h>
|
|
#include <gtsam/inference/FactorGraph-inl.h>
|
|
#include <gtsam/inference/inference-inl.h>
|
|
#include <gtsam/nonlinear/Ordering.h>
|
|
|
|
using namespace std;
|
|
using namespace gtsam;
|
|
using namespace example;
|
|
|
|
/* ************************************************************************* */
|
|
TEST( SymbolicFactorGraph, symbolicFactorGraph )
|
|
{
|
|
Ordering o; o += "x1","l1","x2";
|
|
// construct expected symbolic graph
|
|
SymbolicFactorGraph expected;
|
|
expected.push_factor(o["x1"]);
|
|
expected.push_factor(o["x1"],o["x2"]);
|
|
expected.push_factor(o["x1"],o["l1"]);
|
|
expected.push_factor(o["l1"],o["x2"]);
|
|
|
|
// construct it from the factor graph
|
|
GaussianFactorGraph factorGraph = createGaussianFactorGraph(o);
|
|
SymbolicFactorGraph actual(factorGraph);
|
|
|
|
CHECK(assert_equal(expected, actual));
|
|
}
|
|
|
|
///* ************************************************************************* */
|
|
//TEST( SymbolicFactorGraph, findAndRemoveFactors )
|
|
//{
|
|
// // construct it from the factor graph graph
|
|
// GaussianFactorGraph factorGraph = createGaussianFactorGraph();
|
|
// SymbolicFactorGraph actual(factorGraph);
|
|
// SymbolicFactor::shared_ptr f1 = actual[0];
|
|
// SymbolicFactor::shared_ptr f3 = actual[2];
|
|
// actual.findAndRemoveFactors("x2");
|
|
//
|
|
// // construct expected graph after find_factors_and_remove
|
|
// SymbolicFactorGraph expected;
|
|
// SymbolicFactor::shared_ptr null;
|
|
// expected.push_back(f1);
|
|
// expected.push_back(null);
|
|
// expected.push_back(f3);
|
|
// expected.push_back(null);
|
|
//
|
|
// CHECK(assert_equal(expected, actual));
|
|
//}
|
|
///* ************************************************************************* */
|
|
//TEST( SymbolicFactorGraph, factors)
|
|
//{
|
|
// // create a test graph
|
|
// GaussianFactorGraph factorGraph = createGaussianFactorGraph();
|
|
// SymbolicFactorGraph fg(factorGraph);
|
|
//
|
|
// // ask for all factor indices connected to x1
|
|
// list<size_t> x1_factors = fg.factors("x1");
|
|
// int x1_indices[] = { 0, 1, 2 };
|
|
// list<size_t> x1_expected(x1_indices, x1_indices + 3);
|
|
// CHECK(x1_factors==x1_expected);
|
|
//
|
|
// // ask for all factor indices connected to x2
|
|
// list<size_t> x2_factors = fg.factors("x2");
|
|
// int x2_indices[] = { 1, 3 };
|
|
// list<size_t> x2_expected(x2_indices, x2_indices + 2);
|
|
// CHECK(x2_factors==x2_expected);
|
|
//}
|
|
|
|
///* ************************************************************************* */
|
|
//TEST( SymbolicFactorGraph, removeAndCombineFactors )
|
|
//{
|
|
// // create a test graph
|
|
// GaussianFactorGraph factorGraph = createGaussianFactorGraph();
|
|
// SymbolicFactorGraph fg(factorGraph);
|
|
//
|
|
// // combine all factors connected to x1
|
|
// SymbolicFactor::shared_ptr actual = removeAndCombineFactors(fg,"x1");
|
|
//
|
|
// // check result
|
|
// SymbolicFactor expected("l1","x1","x2");
|
|
// CHECK(assert_equal(expected,*actual));
|
|
//}
|
|
|
|
/* ************************************************************************* */
|
|
TEST( SymbolicFactorGraph, eliminateOne )
|
|
{
|
|
Ordering o; o += "x1","l1","x2";
|
|
// create a test graph
|
|
GaussianFactorGraph factorGraph = createGaussianFactorGraph(o);
|
|
SymbolicFactorGraph fg(factorGraph);
|
|
|
|
// eliminate
|
|
VariableIndex<> varindex(fg);
|
|
Conditional::shared_ptr actual = Inference::EliminateOne(fg, varindex, o["x1"]);
|
|
|
|
// create expected symbolic Conditional
|
|
Conditional expected(o["x1"],o["l1"],o["x2"]);
|
|
|
|
CHECK(assert_equal(expected,*actual));
|
|
}
|
|
|
|
/* ************************************************************************* */
|
|
TEST( SymbolicFactorGraph, eliminate )
|
|
{
|
|
Ordering o; o += "x2","l1","x1";
|
|
|
|
// create expected Chordal bayes Net
|
|
Conditional::shared_ptr x2(new Conditional(o["x2"], o["l1"], o["x1"]));
|
|
Conditional::shared_ptr l1(new Conditional(o["l1"], o["x1"]));
|
|
Conditional::shared_ptr x1(new Conditional(o["x1"]));
|
|
|
|
SymbolicBayesNet expected;
|
|
expected.push_back(x2);
|
|
expected.push_back(l1);
|
|
expected.push_back(x1);
|
|
|
|
// create a test graph
|
|
GaussianFactorGraph factorGraph = createGaussianFactorGraph(o);
|
|
SymbolicFactorGraph fg(factorGraph);
|
|
|
|
// eliminate it
|
|
SymbolicBayesNet actual = *Inference::Eliminate(fg);
|
|
|
|
CHECK(assert_equal(expected,actual));
|
|
}
|
|
|
|
/* ************************************************************************* */
|
|
// Test to drive elimination tree development
|
|
// Uses example originally created in testEliminationTree
|
|
// graph: f(0,1) f(0,2) f(1,4) f(2,4) f(3,4)
|
|
/* ************************************************************************* */
|
|
TEST( SymbolicFactorGraph, eliminate2 )
|
|
{
|
|
// create expected Chordal bayes Net
|
|
Conditional::shared_ptr c0(new Conditional(0, 1, 2));
|
|
Conditional::shared_ptr c1(new Conditional(1, 2, 4));
|
|
Conditional::shared_ptr c2(new Conditional(2, 4));
|
|
Conditional::shared_ptr c3(new Conditional(3, 4));
|
|
Conditional::shared_ptr c4(new Conditional(4));
|
|
|
|
SymbolicBayesNet expected;
|
|
expected.push_back(c0);
|
|
expected.push_back(c1);
|
|
expected.push_back(c2);
|
|
expected.push_back(c3);
|
|
expected.push_back(c4);
|
|
|
|
// Create factor graph
|
|
SymbolicFactorGraph fg;
|
|
fg.push_factor(0, 1);
|
|
fg.push_factor(0, 2);
|
|
fg.push_factor(1, 4);
|
|
fg.push_factor(2, 4);
|
|
fg.push_factor(3, 4);
|
|
|
|
// eliminate it
|
|
SymbolicBayesNet actual = *Inference::Eliminate(fg);
|
|
|
|
CHECK(assert_equal(expected,actual));
|
|
}
|
|
|
|
/* ************************************************************************* */
|
|
int main() {
|
|
TestResult tr;
|
|
return TestRegistry::runAllTests(tr);
|
|
}
|
|
/* ************************************************************************* */
|