| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  |  * @file    testSymbolicBayesNet.cpp | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  |  * @brief   Unit tests for a symbolic Bayes chain | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | #include <boost/assign/list_inserter.hpp> // for 'insert()'
 | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  | #include <boost/assign/std/list.hpp> // for operator +=
 | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //#define GTSAM_MAGIC_KEY
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/inference/SymbolicFactorGraph.h>
 | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | static const Index _L_ = 0; | 
					
						
							|  |  |  | static const Index _A_ = 1; | 
					
						
							|  |  |  | static const Index _B_ = 2; | 
					
						
							|  |  |  | static const Index _C_ = 3; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Conditional::shared_ptr | 
					
						
							|  |  |  | 	B(new Conditional(_B_)), | 
					
						
							|  |  |  | 	L(new Conditional(_L_, _B_)); | 
					
						
							| 
									
										
										
										
											2010-07-07 14:11:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( SymbolicBayesNet, equals ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	BayesNet<Conditional> f1; | 
					
						
							| 
									
										
										
										
											2010-07-07 14:11:19 +08:00
										 |  |  | 	f1.push_back(B); | 
					
						
							|  |  |  | 	f1.push_back(L); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	BayesNet<Conditional> f2; | 
					
						
							| 
									
										
										
										
											2010-07-07 14:11:19 +08:00
										 |  |  | 	f2.push_back(L); | 
					
						
							|  |  |  | 	f2.push_back(B); | 
					
						
							|  |  |  | 	CHECK(f1.equals(f1)); | 
					
						
							|  |  |  | 	CHECK(!f1.equals(f2)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 10:49:18 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( SymbolicBayesNet, pop_front ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	Conditional::shared_ptr | 
					
						
							|  |  |  | 		A(new Conditional(_A_,_B_,_C_)), | 
					
						
							|  |  |  | 		B(new Conditional(_B_,_C_)), | 
					
						
							|  |  |  | 		C(new Conditional(_C_)); | 
					
						
							| 
									
										
										
										
											2009-11-08 10:49:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Expected after pop_front
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	BayesNet<Conditional> expected; | 
					
						
							| 
									
										
										
										
											2009-11-08 10:49:18 +08:00
										 |  |  | 	expected.push_back(B); | 
					
						
							|  |  |  | 	expected.push_back(C); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Actual
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	BayesNet<Conditional> actual; | 
					
						
							| 
									
										
										
										
											2009-11-08 10:49:18 +08:00
										 |  |  | 	actual.push_back(A); | 
					
						
							|  |  |  | 	actual.push_back(B); | 
					
						
							|  |  |  | 	actual.push_back(C); | 
					
						
							|  |  |  | 	actual.pop_front(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   CHECK(assert_equal(expected,actual)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( SymbolicBayesNet, combine ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	Conditional::shared_ptr | 
					
						
							|  |  |  | 		A(new Conditional(_A_,_B_,_C_)), | 
					
						
							|  |  |  | 		B(new Conditional(_B_,_C_)), | 
					
						
							|  |  |  | 		C(new Conditional(_C_)); | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// p(A|BC)
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	BayesNet<Conditional> p_ABC; | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | 	p_ABC.push_back(A); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// P(BC)=P(B|C)P(C)
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	BayesNet<Conditional> p_BC; | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | 	p_BC.push_back(B); | 
					
						
							|  |  |  | 	p_BC.push_back(C); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// P(ABC) = P(A|BC) P(BC)
 | 
					
						
							|  |  |  | 	p_ABC.push_back(p_BC); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	BayesNet<Conditional> expected; | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | 	expected.push_back(A); | 
					
						
							|  |  |  | 	expected.push_back(B); | 
					
						
							|  |  |  | 	expected.push_back(C); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   CHECK(assert_equal(expected,p_ABC)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  | 	TestResult tr; | 
					
						
							|  |  |  | 	return TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ |