| 
									
										
										
										
											2010-10-14 12:54:38 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation,  | 
					
						
							|  |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2012-03-15 10:10:37 +08:00
										 |  |  |  * @file    testSymbolicFactorGraphB.cpp | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  |  * @brief   Unit tests for a symbolic Factor Graph | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-10 04:15:44 +08:00
										 |  |  | #include <tests/smallExample.h>
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:05:38 +08:00
										 |  |  | #include <gtsam/nonlinear/Ordering.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/Symbol.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/inference/SymbolicFactorGraph.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/inference/BayesNet-inl.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | #include <gtsam/inference/SymbolicSequentialSolver.h>
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:05:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/assign/std/list.hpp> // for operator +=
 | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | using symbol_shorthand::X; | 
					
						
							|  |  |  | using symbol_shorthand::L; | 
					
						
							| 
									
										
										
										
											2012-02-21 08:53:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( SymbolicFactorGraph, symbolicFactorGraph ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  |   Ordering o; o += X(1),L(1),X(2); | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  | 	// construct expected symbolic graph
 | 
					
						
							|  |  |  | 	SymbolicFactorGraph expected; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	expected.push_factor(o[X(1)]); | 
					
						
							|  |  |  | 	expected.push_factor(o[X(1)],o[X(2)]); | 
					
						
							|  |  |  | 	expected.push_factor(o[X(1)],o[L(1)]); | 
					
						
							|  |  |  | 	expected.push_factor(o[X(2)],o[L(1)]); | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// construct it from the factor graph
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:05:38 +08:00
										 |  |  | 	GaussianFactorGraph factorGraph = example::createGaussianFactorGraph(o); | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  | 	SymbolicFactorGraph actual(factorGraph); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(assert_equal(expected, actual)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | ///* ************************************************************************* */
 | 
					
						
							|  |  |  | //TEST( SymbolicFactorGraph, findAndRemoveFactors )
 | 
					
						
							|  |  |  | //{
 | 
					
						
							|  |  |  | //	// construct it from the factor graph graph
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:05:38 +08:00
										 |  |  | //	GaussianFactorGraph factorGraph = example::createGaussianFactorGraph();
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	SymbolicFactorGraph actual(factorGraph);
 | 
					
						
							|  |  |  | //  SymbolicFactor::shared_ptr f1 = actual[0];
 | 
					
						
							|  |  |  | //  SymbolicFactor::shared_ptr f3 = actual[2];
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //	actual.findAndRemoveFactors(X(2));
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //	// 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
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:05:38 +08:00
										 |  |  | //	GaussianFactorGraph factorGraph = example::createGaussianFactorGraph();
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	SymbolicFactorGraph fg(factorGraph);
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	// ask for all factor indices connected to x1
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //	list<size_t> x1_factors = fg.factors(X(1));
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	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
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //	list<size_t> x2_factors = fg.factors(X(2));
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	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
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:05:38 +08:00
										 |  |  | //	GaussianFactorGraph factorGraph = example::createGaussianFactorGraph();
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	SymbolicFactorGraph fg(factorGraph);
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  // combine all factors connected to x1
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //  SymbolicFactor::shared_ptr actual = removeAndCombineFactors(fg,X(1));
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  // check result
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //  SymbolicFactor expected(L(1),X(1),X(2));
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //  CHECK(assert_equal(expected,*actual));
 | 
					
						
							|  |  |  | //}
 | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | ///* ************************************************************************* */
 | 
					
						
							|  |  |  | //TEST( SymbolicFactorGraph, eliminateOne )
 | 
					
						
							|  |  |  | //{
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //  Ordering o; o += X(1),L(1),X(2);
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | //	// create a test graph
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:05:38 +08:00
										 |  |  | //	GaussianFactorGraph factorGraph = example::createGaussianFactorGraph(o);
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | //	SymbolicFactorGraph fg(factorGraph);
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	// eliminate
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //	IndexConditional::shared_ptr actual = GaussianSequentialSolver::EliminateUntil(fg, o[X(1)]+1);
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  // create expected symbolic IndexConditional
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //  IndexConditional expected(o[X(1)],o[L(1)],o[X(2)]);
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  CHECK(assert_equal(expected,*actual));
 | 
					
						
							|  |  |  | //}
 | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-12 08:15:26 +08:00
										 |  |  | TEST( SymbolicFactorGraph, eliminate ) | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  |   Ordering o; o += X(2),L(1),X(1); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  |   // create expected Chordal bayes Net
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  |   IndexConditional::shared_ptr x2(new IndexConditional(o[X(2)], o[L(1)], o[X(1)])); | 
					
						
							|  |  |  |   IndexConditional::shared_ptr l1(new IndexConditional(o[L(1)], o[X(1)])); | 
					
						
							|  |  |  |   IndexConditional::shared_ptr x1(new IndexConditional(o[X(1)])); | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   SymbolicBayesNet expected; | 
					
						
							|  |  |  |   expected.push_back(x2); | 
					
						
							|  |  |  |   expected.push_back(l1); | 
					
						
							|  |  |  |   expected.push_back(x1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // create a test graph
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:05:38 +08:00
										 |  |  | 	GaussianFactorGraph factorGraph = example::createGaussianFactorGraph(o); | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  | 	SymbolicFactorGraph fg(factorGraph); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// eliminate it
 | 
					
						
							| 
									
										
										
										
											2011-03-25 03:27:12 +08:00
										 |  |  |   SymbolicBayesNet actual = *SymbolicSequentialSolver(fg).eliminate(&EliminateSymbolic); | 
					
						
							| 
									
										
										
										
											2010-07-12 15:16:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   CHECK(assert_equal(expected,actual)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  | 	TestResult tr; | 
					
						
							|  |  |  | 	return TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ |