| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    testJunctionTree.cpp | 
					
						
							| 
									
										
										
										
											2010-07-14 23:22:37 +08:00
										 |  |  |  * @brief   Unit tests for Junction Tree | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  |  * @author  Kai Ni | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/assign/std/list.hpp> // for operator +=
 | 
					
						
							|  |  |  | #include <boost/assign/std/set.hpp> // for operator +=
 | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/base/TestableAssertions.h>
 | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define GTSAM_MAGIC_KEY
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/nonlinear/Ordering.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/inference/SymbolicFactorGraph.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/JunctionTree.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/ClusterTree-inl.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/JunctionTree-inl.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/inference/Factor-inl.h>
 | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef JunctionTree<SymbolicFactorGraph> SymbolicJunctionTree; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | typedef BayesTree<Conditional> SymbolicBayesTree; | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | /* ************************************************************************* *
 | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  |  * x1 - x2 - x3 - x4 | 
					
						
							|  |  |  |  * x3 x4 | 
					
						
							|  |  |  |  *    x2 x1 : x3 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  |  ****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | TEST( JunctionTree, constructor ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   const Index x2=0, x1=1, x3=2, x4=3; | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 	SymbolicFactorGraph fg; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	fg.push_factor(x2,x1); | 
					
						
							|  |  |  | 	fg.push_factor(x2,x3); | 
					
						
							|  |  |  | 	fg.push_factor(x3,x4); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	SymbolicJunctionTree actual(fg); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	vector<Index> frontal1; frontal1 += x3, x4; | 
					
						
							|  |  |  | 	vector<Index> frontal2; frontal2 += x2, x1; | 
					
						
							|  |  |  | 	vector<Index> sep1; | 
					
						
							|  |  |  | 	vector<Index> sep2; sep2 += x3; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	CHECK(assert_equal(frontal1, actual.root()->frontal)); | 
					
						
							|  |  |  | 	CHECK(assert_equal(sep1,     actual.root()->separator)); | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 	LONGS_EQUAL(1,               actual.root()->size()); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	CHECK(assert_equal(frontal2, actual.root()->children().front()->frontal)); | 
					
						
							|  |  |  | 	CHECK(assert_equal(sep2,     actual.root()->children().front()->separator)); | 
					
						
							|  |  |  | 	LONGS_EQUAL(2,               actual.root()->children().front()->size()); | 
					
						
							|  |  |  | 	CHECK(assert_equal(*fg[2], *(*actual.root())[0])); | 
					
						
							|  |  |  |   CHECK(assert_equal(*fg[0], *(*actual.root()->children().front())[0])); | 
					
						
							|  |  |  |   CHECK(assert_equal(*fg[1], *(*actual.root()->children().front())[1])); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* *
 | 
					
						
							|  |  |  |  * x1 - x2 - x3 - x4 | 
					
						
							|  |  |  |  * x3 x4 | 
					
						
							|  |  |  |  *    x2 x1 : x3 | 
					
						
							|  |  |  |  ****************************************************************************/ | 
					
						
							|  |  |  | TEST( JunctionTree, eliminate) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   const Index x2=0, x1=1, x3=2, x4=3; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   SymbolicFactorGraph fg; | 
					
						
							|  |  |  |   fg.push_factor(x2,x1); | 
					
						
							|  |  |  |   fg.push_factor(x2,x3); | 
					
						
							|  |  |  |   fg.push_factor(x3,x4); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SymbolicJunctionTree jt(fg); | 
					
						
							|  |  |  |   SymbolicBayesTree::sharedClique actual = jt.eliminate(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   BayesNet<Conditional> bn = *Inference::Eliminate(fg); | 
					
						
							|  |  |  |   SymbolicBayesTree expected(bn); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  cout << "BT from JT:\n";
 | 
					
						
							|  |  |  | //  actual->printTree("");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   CHECK(assert_equal(*expected.root(), *actual)); | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  | 	TestResult tr; | 
					
						
							|  |  |  | 	return TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ |