| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    testBayesTree.cpp | 
					
						
							|  |  |  |  * @brief   Unit tests for Bayes Tree | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							| 
									
										
										
										
											2009-11-23 07:35:13 +08:00
										 |  |  |  * @author  Michael Kaess | 
					
						
							|  |  |  |  * @author  Viorela Ila | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  | #include <boost/assign/std/list.hpp> // for operator +=
 | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | #include "SymbolicBayesNet.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-21 11:38:13 +08:00
										 |  |  | #include "SymbolicFactorGraph.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | #include "Ordering.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-30 21:03:38 +08:00
										 |  |  | #include "BayesTree-inl.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | #include "smallExample.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | #include "IndexTable.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 02:33:14 +08:00
										 |  |  | typedef BayesTree<SymbolicConditional> SymbolicBayesTree; | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-24 04:11:10 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // SLAM example from RSS sqrtSAM paper
 | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | Symbol _x1_('x', 1), _x2_('x', 2), _x3_('x', 3), _l1_('l', 1), _l2_('l', 2); | 
					
						
							|  |  |  | SymbolicConditional::shared_ptr | 
					
						
							|  |  |  | 		x3(new SymbolicConditional(_x3_)), | 
					
						
							|  |  |  | 		x2(new SymbolicConditional(_x2_,_x3_)), | 
					
						
							|  |  |  | 		x1(new SymbolicConditional(_x1_,_x2_,_x3_)), | 
					
						
							|  |  |  | 		l1(new SymbolicConditional(_l1_,_x1_,_x2_)), | 
					
						
							|  |  |  | 		l2(new SymbolicConditional(_l2_,_x1_,_x3_)); | 
					
						
							| 
									
										
										
										
											2009-11-24 04:11:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Bayes Tree for sqrtSAM example
 | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | SymbolicBayesTree createSlamSymbolicBayesTree(){ | 
					
						
							| 
									
										
										
										
											2009-11-24 04:11:10 +08:00
										 |  |  | 	// Create using insert
 | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	Ordering slamOrdering; slamOrdering += _x3_, _x2_, _x1_, _l2_, _l1_; | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree bayesTree_slam; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	bayesTree_slam.insert(x3,slamOrdering); | 
					
						
							|  |  |  | 	bayesTree_slam.insert(x2,slamOrdering); | 
					
						
							|  |  |  | 	bayesTree_slam.insert(x1,slamOrdering); | 
					
						
							|  |  |  | 	bayesTree_slam.insert(l2,slamOrdering); | 
					
						
							|  |  |  | 	bayesTree_slam.insert(l1,slamOrdering); | 
					
						
							| 
									
										
										
										
											2009-11-24 04:11:10 +08:00
										 |  |  | 	return bayesTree_slam; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | // Conditionals for ASIA example from the tutorial with A and D evidence
 | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | Symbol _B_('B', 0), _L_('L', 0), _E_('E', 0), _S_('S', 0), _T_('T', 0), _X_('X',0); | 
					
						
							| 
									
										
										
										
											2009-11-23 02:12:29 +08:00
										 |  |  | SymbolicConditional::shared_ptr | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	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-23 02:12:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Bayes Tree for Asia example
 | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | SymbolicBayesTree createAsiaSymbolicBayesTree() { | 
					
						
							|  |  |  | 	SymbolicBayesTree bayesTree; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	Ordering asiaOrdering; asiaOrdering += _X_, _T_, _S_, _E_, _L_, _B_; | 
					
						
							|  |  |  | 	bayesTree.insert(B,asiaOrdering); | 
					
						
							|  |  |  | 	bayesTree.insert(L,asiaOrdering); | 
					
						
							|  |  |  | 	bayesTree.insert(E,asiaOrdering); | 
					
						
							|  |  |  | 	bayesTree.insert(S,asiaOrdering); | 
					
						
							|  |  |  | 	bayesTree.insert(T,asiaOrdering); | 
					
						
							|  |  |  | 	bayesTree.insert(X,asiaOrdering); | 
					
						
							| 
									
										
										
										
											2009-11-23 02:12:29 +08:00
										 |  |  | 	return bayesTree; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( BayesTree, Front ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-04 11:22:29 +08:00
										 |  |  | 	SymbolicBayesNet f1; | 
					
						
							| 
									
										
										
										
											2009-11-02 13:17:44 +08:00
										 |  |  | 	f1.push_back(B); | 
					
						
							|  |  |  | 	f1.push_back(L); | 
					
						
							| 
									
										
										
										
											2009-11-04 11:22:29 +08:00
										 |  |  | 	SymbolicBayesNet f2; | 
					
						
							| 
									
										
										
										
											2009-11-02 13:17:44 +08:00
										 |  |  | 	f2.push_back(L); | 
					
						
							|  |  |  | 	f2.push_back(B); | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 	CHECK(f1.equals(f1)); | 
					
						
							|  |  |  | 	CHECK(!f1.equals(f2)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-31 22:12:41 +08:00
										 |  |  | TEST( BayesTree, constructor ) | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-31 22:12:41 +08:00
										 |  |  | 	// Create using insert
 | 
					
						
							| 
									
										
										
										
											2009-11-23 02:12:29 +08:00
										 |  |  | 	SymbolicBayesTree bayesTree = createAsiaSymbolicBayesTree(); | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 22:12:41 +08:00
										 |  |  | 	// Check Size
 | 
					
						
							| 
									
										
										
										
											2009-11-19 02:05:12 +08:00
										 |  |  | 	LONGS_EQUAL(4,bayesTree.size()); | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check root
 | 
					
						
							| 
									
										
										
										
											2009-11-02 13:17:44 +08:00
										 |  |  | 	BayesNet<SymbolicConditional> expected_root; | 
					
						
							|  |  |  | 	expected_root.push_back(E); | 
					
						
							| 
									
										
										
										
											2009-11-03 14:29:56 +08:00
										 |  |  | 	expected_root.push_back(L); | 
					
						
							|  |  |  | 	expected_root.push_back(B); | 
					
						
							| 
									
										
										
										
											2009-11-13 02:33:14 +08:00
										 |  |  | 	boost::shared_ptr<SymbolicBayesNet> actual_root = bayesTree.root(); | 
					
						
							| 
									
										
										
										
											2009-11-05 13:29:47 +08:00
										 |  |  | 	CHECK(assert_equal(expected_root,*actual_root)); | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 22:12:41 +08:00
										 |  |  | 	// Create from symbolic Bayes chain in which we want to discover cliques
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | 	SymbolicBayesNet ASIA; | 
					
						
							| 
									
										
										
										
											2009-11-02 13:17:44 +08:00
										 |  |  | 	ASIA.push_back(X); | 
					
						
							|  |  |  | 	ASIA.push_back(T); | 
					
						
							|  |  |  | 	ASIA.push_back(S); | 
					
						
							|  |  |  | 	ASIA.push_back(E); | 
					
						
							|  |  |  | 	ASIA.push_back(L); | 
					
						
							|  |  |  | 	ASIA.push_back(B); | 
					
						
							| 
									
										
										
										
											2009-11-13 02:33:14 +08:00
										 |  |  | 	SymbolicBayesTree bayesTree2(ASIA); | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 22:12:41 +08:00
										 |  |  | 	// Check whether the same
 | 
					
						
							| 
									
										
										
										
											2009-11-01 00:57:36 +08:00
										 |  |  | 	CHECK(assert_equal(bayesTree,bayesTree2)); | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// CHECK findParentClique, should *not depend on order of parents*
 | 
					
						
							|  |  |  | 	Ordering ordering; ordering += _X_, _T_, _S_, _E_, _L_, _B_; | 
					
						
							|  |  |  | 	IndexTable<Symbol> index(ordering); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	list<Symbol> parents1; parents1 += _E_, _L_; | 
					
						
							|  |  |  | 	CHECK(assert_equal(_E_,bayesTree.findParentClique(parents1, index))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	list<Symbol> parents2; parents2 += _L_, _E_; | 
					
						
							|  |  |  | 	CHECK(assert_equal(_E_,bayesTree.findParentClique(parents2, index))); | 
					
						
							| 
									
										
										
										
											2010-02-10 05:32:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	list<Symbol> parents3; parents3 += _L_, _B_; | 
					
						
							|  |  |  | 	CHECK(assert_equal(_L_,bayesTree.findParentClique(parents3, index))); | 
					
						
							| 
									
										
										
										
											2009-11-01 00:57:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-20 12:23:35 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(BayesTree, clear) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  | //	SymbolicBayesTree bayesTree = createAsiaSymbolicBayesTree();
 | 
					
						
							|  |  |  | //	bayesTree.clear();
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	SymbolicBayesTree expected;
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	// Check whether cleared BayesTree is equal to a new BayesTree
 | 
					
						
							|  |  |  | //	CHECK(assert_equal(expected, bayesTree));
 | 
					
						
							| 
									
										
										
										
											2010-01-20 12:23:35 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-21 11:38:13 +08:00
										 |  |  | /* ************************************************************************* *
 | 
					
						
							| 
									
										
										
										
											2009-11-21 12:59:50 +08:00
										 |  |  | Bayes Tree for testing conversion to a forest of orphans needed for incremental. | 
					
						
							| 
									
										
										
										
											2009-11-21 11:38:13 +08:00
										 |  |  |        A,B | 
					
						
							|  |  |  |    C|A    E|B | 
					
						
							|  |  |  |    D|C    F|E | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( BayesTree, removePath ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	Symbol _A_('A', 0), _B_('B', 0), _C_('C', 0), _D_('D', 0), _E_('E', 0), _F_('F',0); | 
					
						
							| 
									
										
										
										
											2009-11-21 12:59:50 +08:00
										 |  |  | 	SymbolicConditional::shared_ptr | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 			A(new SymbolicConditional(_A_)), | 
					
						
							|  |  |  | 			B(new SymbolicConditional(_B_, _A_)), | 
					
						
							|  |  |  | 			C(new SymbolicConditional(_C_, _A_)), | 
					
						
							|  |  |  | 			D(new SymbolicConditional(_D_, _C_)), | 
					
						
							|  |  |  | 			E(new SymbolicConditional(_E_, _B_)), | 
					
						
							|  |  |  | 			F(new SymbolicConditional(_F_, _E_)); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree bayesTree; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	Ordering ord; ord += _A_,_B_,_C_,_D_,_E_,_F_; | 
					
						
							|  |  |  | 	bayesTree.insert(A,ord); | 
					
						
							|  |  |  | 	bayesTree.insert(B,ord); | 
					
						
							|  |  |  | 	bayesTree.insert(C,ord); | 
					
						
							|  |  |  | 	bayesTree.insert(D,ord); | 
					
						
							|  |  |  | 	bayesTree.insert(E,ord); | 
					
						
							|  |  |  | 	bayesTree.insert(F,ord); | 
					
						
							| 
									
										
										
										
											2009-11-21 11:38:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-21 12:59:50 +08:00
										 |  |  | 	// remove C, expected outcome: factor graph with ABC,
 | 
					
						
							|  |  |  | 	// Bayes Tree now contains two orphan trees: D|C and E|B,F|E
 | 
					
						
							| 
									
										
										
										
											2009-11-21 11:38:13 +08:00
										 |  |  | 	SymbolicFactorGraph expected; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	expected.push_factor(_A_,_B_); | 
					
						
							|  |  |  | 	expected.push_factor(_A_); | 
					
						
							|  |  |  | 	expected.push_factor(_A_,_C_); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques expectedOrphans; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  |   expectedOrphans += bayesTree[_D_], bayesTree[_E_]; | 
					
						
							| 
									
										
										
										
											2009-11-23 00:04:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  |   BayesNet<SymbolicConditional> bn; | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques orphans; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	bayesTree.removePath(bayesTree[_C_], bn, orphans); | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	FactorGraph<SymbolicFactor> factors(bn); | 
					
						
							| 
									
										
										
										
											2009-11-23 00:04:51 +08:00
										 |  |  |   CHECK(assert_equal((FactorGraph<SymbolicFactor>)expected, factors)); | 
					
						
							| 
									
										
										
										
											2009-11-23 01:40:24 +08:00
										 |  |  |   CHECK(assert_equal(expectedOrphans, orphans)); | 
					
						
							| 
									
										
										
										
											2009-11-21 11:38:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // remove E: factor graph with EB; E|B removed from second orphan tree
 | 
					
						
							| 
									
										
										
										
											2009-11-23 01:40:24 +08:00
										 |  |  | 	SymbolicFactorGraph expected2; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  |   expected2.push_factor(_B_,_E_); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  |   SymbolicBayesTree::Cliques expectedOrphans2; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  |   expectedOrphans2 += bayesTree[_F_]; | 
					
						
							| 
									
										
										
										
											2009-11-21 12:59:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  |   BayesNet<SymbolicConditional> bn2; | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques orphans2; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  |   bayesTree.removePath(bayesTree[_E_], bn2, orphans2); | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  |   FactorGraph<SymbolicFactor> factors2(bn2); | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  |   CHECK(assert_equal((FactorGraph<SymbolicFactor>)expected2, factors2)); | 
					
						
							|  |  |  |   CHECK(assert_equal(expectedOrphans2, orphans2)); | 
					
						
							| 
									
										
										
										
											2009-11-21 11:38:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 02:06:28 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( BayesTree, removePath2 ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree bayesTree = createAsiaSymbolicBayesTree(); | 
					
						
							| 
									
										
										
										
											2009-11-23 02:06:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 02:22:17 +08:00
										 |  |  | 	// Call remove-path with clique B
 | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	BayesNet<SymbolicConditional> bn; | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques orphans; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  |   bayesTree.removePath(bayesTree[_B_], bn, orphans); | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	FactorGraph<SymbolicFactor> factors(bn); | 
					
						
							| 
									
										
										
										
											2009-11-23 02:06:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check expected outcome
 | 
					
						
							|  |  |  | 	SymbolicFactorGraph expected; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	expected.push_factor(_B_,_L_,_E_); | 
					
						
							|  |  |  | 	expected.push_factor(_B_,_L_); | 
					
						
							|  |  |  | 	expected.push_factor(_B_); | 
					
						
							| 
									
										
										
										
											2009-11-23 02:06:28 +08:00
										 |  |  |   CHECK(assert_equal((FactorGraph<SymbolicFactor>)expected, factors)); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques expectedOrphans; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  |   expectedOrphans += bayesTree[_S_], bayesTree[_T_], bayesTree[_X_]; | 
					
						
							| 
									
										
										
										
											2009-11-23 02:06:28 +08:00
										 |  |  |   CHECK(assert_equal(expectedOrphans, orphans)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( BayesTree, removePath3 ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree bayesTree = createAsiaSymbolicBayesTree(); | 
					
						
							| 
									
										
										
										
											2009-11-23 02:06:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Call remove-path with clique S
 | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	BayesNet<SymbolicConditional> bn; | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques orphans; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  |   bayesTree.removePath(bayesTree[_S_], bn, orphans); | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	FactorGraph<SymbolicFactor> factors(bn); | 
					
						
							| 
									
										
										
										
											2009-11-23 02:06:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check expected outcome
 | 
					
						
							|  |  |  | 	SymbolicFactorGraph expected; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	expected.push_factor(_B_,_L_,_E_); | 
					
						
							|  |  |  | 	expected.push_factor(_B_,_L_); | 
					
						
							|  |  |  | 	expected.push_factor(_B_); | 
					
						
							|  |  |  | 	expected.push_factor(_L_,_B_,_S_); | 
					
						
							| 
									
										
										
										
											2009-11-23 02:06:28 +08:00
										 |  |  |   CHECK(assert_equal((FactorGraph<SymbolicFactor>)expected, factors)); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques expectedOrphans; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  |   expectedOrphans += bayesTree[_T_], bayesTree[_X_]; | 
					
						
							| 
									
										
										
										
											2009-11-23 02:06:28 +08:00
										 |  |  |   CHECK(assert_equal(expectedOrphans, orphans)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | TEST( BayesTree, removeTop ) | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree bayesTree = createAsiaSymbolicBayesTree(); | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create a new factor to be inserted
 | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	boost::shared_ptr<SymbolicFactor> newFactor(new SymbolicFactor(_B_,_S_)); | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Remove the contaminated part of the Bayes tree
 | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	BayesNet<SymbolicConditional> bn; | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques orphans; | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	bayesTree.removeTop(newFactor->keys(), bn, orphans); | 
					
						
							|  |  |  | 	FactorGraph<SymbolicFactor> factors(bn); | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check expected outcome
 | 
					
						
							|  |  |  | 	SymbolicFactorGraph expected; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	expected.push_factor(_B_,_L_,_E_); | 
					
						
							|  |  |  | 	expected.push_factor(_B_,_L_); | 
					
						
							|  |  |  | 	expected.push_factor(_B_); | 
					
						
							|  |  |  | 	expected.push_factor(_L_,_B_,_S_); | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  |   CHECK(assert_equal((FactorGraph<SymbolicFactor>)expected, factors)); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques expectedOrphans; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  |   expectedOrphans += bayesTree[_T_], bayesTree[_X_]; | 
					
						
							| 
									
										
										
										
											2009-11-23 02:22:17 +08:00
										 |  |  |   CHECK(assert_equal(expectedOrphans, orphans)); | 
					
						
							| 
									
										
										
										
											2009-11-23 06:59:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Try removeTop again with a factor that should not change a thing
 | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	boost::shared_ptr<SymbolicFactor> newFactor2(new SymbolicFactor(_B_)); | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	BayesNet<SymbolicConditional> bn2; | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques orphans2; | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	bayesTree.removeTop(newFactor2->keys(), bn2, orphans2); | 
					
						
							|  |  |  | 	FactorGraph<SymbolicFactor> factors2(bn2); | 
					
						
							| 
									
										
										
										
											2009-11-23 06:59:56 +08:00
										 |  |  | 	SymbolicFactorGraph expected2; | 
					
						
							| 
									
										
										
										
											2009-11-23 08:02:06 +08:00
										 |  |  |   CHECK(assert_equal((FactorGraph<SymbolicFactor>)expected2, factors2)); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques expectedOrphans2; | 
					
						
							| 
									
										
										
										
											2009-11-23 08:02:06 +08:00
										 |  |  |   CHECK(assert_equal(expectedOrphans2, orphans2)); | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 07:35:13 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | TEST( BayesTree, removeTop2 ) | 
					
						
							| 
									
										
										
										
											2009-11-23 07:35:13 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree bayesTree = createAsiaSymbolicBayesTree(); | 
					
						
							| 
									
										
										
										
											2009-11-23 07:35:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 07:50:01 +08:00
										 |  |  | 	// create two factors to be inserted
 | 
					
						
							|  |  |  | 	SymbolicFactorGraph newFactors; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	newFactors.push_factor(_B_); | 
					
						
							|  |  |  | 	newFactors.push_factor(_S_); | 
					
						
							| 
									
										
										
										
											2009-11-23 07:50:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Remove the contaminated part of the Bayes tree
 | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	BayesNet<SymbolicConditional> bn; | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques orphans; | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	bayesTree.removeTop(newFactors.keys(), bn, orphans); | 
					
						
							|  |  |  | 	FactorGraph<SymbolicFactor> factors(bn); | 
					
						
							| 
									
										
										
										
											2009-11-23 07:50:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check expected outcome
 | 
					
						
							|  |  |  | 	SymbolicFactorGraph expected; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	expected.push_factor(_B_,_L_,_E_); | 
					
						
							|  |  |  | 	expected.push_factor(_B_,_L_); | 
					
						
							|  |  |  | 	expected.push_factor(_B_); | 
					
						
							|  |  |  | 	expected.push_factor(_L_,_B_,_S_); | 
					
						
							| 
									
										
										
										
											2009-11-23 07:50:01 +08:00
										 |  |  |   CHECK(assert_equal((FactorGraph<SymbolicFactor>)expected, factors)); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques expectedOrphans; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  |   expectedOrphans += bayesTree[_T_], bayesTree[_X_]; | 
					
						
							| 
									
										
										
										
											2009-11-23 09:16:58 +08:00
										 |  |  | 	CHECK(assert_equal(expectedOrphans, orphans)); | 
					
						
							| 
									
										
										
										
											2009-11-23 07:50:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  | TEST( BayesTree, removeTop3 ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	Symbol _l5_('l', 5), _x4_('x', 4); | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  | 	// simple test case that failed after COLAMD was fixed/activated
 | 
					
						
							|  |  |  | 	SymbolicConditional::shared_ptr | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	X(new SymbolicConditional(_l5_)), | 
					
						
							|  |  |  | 	A(new SymbolicConditional(_x4_, _l5_)), | 
					
						
							|  |  |  | 	B(new SymbolicConditional(_x3_, _x4_)), | 
					
						
							|  |  |  | 	C(new SymbolicConditional(_x2_, _x3_)); | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	Ordering newOrdering; | 
					
						
							|  |  |  | 	newOrdering += _x3_, _x2_, _x1_, _l2_, _l1_, _x4_, _l5_; | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  | 	SymbolicBayesTree bayesTree; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	bayesTree.insert(X,newOrdering); | 
					
						
							|  |  |  | 	bayesTree.insert(A,newOrdering); | 
					
						
							|  |  |  | 	bayesTree.insert(B,newOrdering); | 
					
						
							|  |  |  | 	bayesTree.insert(C,newOrdering); | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// remove all
 | 
					
						
							|  |  |  | 	list<Symbol> keys; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	keys += _l5_, _x2_, _x3_, _x4_; | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	BayesNet<SymbolicConditional> bn; | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques orphans; | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 	bayesTree.removeTop(keys, bn, orphans); | 
					
						
							|  |  |  | 	FactorGraph<SymbolicFactor> factors(bn); | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(orphans.size() == 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-02-13 15:09:27 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  *  x2 - x3 - x4 - x5 | 
					
						
							|  |  |  |  *   |  /       \   | | 
					
						
							|  |  |  |  *  x1 /				 \ x6 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | TEST( BayesTree, insert ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// construct bayes tree by split the graph along the separator x3 - x4
 | 
					
						
							|  |  |  | 	Symbol _x4_('x', 4), _x5_('x', 5), _x6_('x', 6); | 
					
						
							|  |  |  | 	SymbolicFactorGraph fg1, fg2, fg3; | 
					
						
							|  |  |  | 	fg1.push_factor(_x3_, _x4_); | 
					
						
							|  |  |  | 	fg2.push_factor(_x1_, _x2_); | 
					
						
							|  |  |  | 	fg2.push_factor(_x2_, _x3_); | 
					
						
							|  |  |  | 	fg2.push_factor(_x1_, _x3_); | 
					
						
							|  |  |  | 	fg3.push_factor(_x4_, _x5_); | 
					
						
							|  |  |  | 	fg3.push_factor(_x5_, _x6_); | 
					
						
							|  |  |  | 	fg3.push_factor(_x4_, _x6_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Ordering ordering1; ordering1 += _x3_, _x4_; | 
					
						
							|  |  |  | 	Ordering ordering2; ordering2 += _x1_, _x2_; | 
					
						
							|  |  |  | 	Ordering ordering3; ordering3 += _x6_, _x5_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BayesNet<SymbolicConditional> bn1, bn2, bn3; | 
					
						
							|  |  |  | 	bn1 = fg1.eliminate(ordering1); | 
					
						
							|  |  |  | 	bn2 = fg2.eliminate(ordering2); | 
					
						
							|  |  |  | 	bn3 = fg3.eliminate(ordering3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// insert child cliques
 | 
					
						
							|  |  |  | 	SymbolicBayesTree actual; | 
					
						
							|  |  |  | 	list<SymbolicBayesTree::sharedClique> children; | 
					
						
							|  |  |  | 	SymbolicBayesTree::sharedClique r1 = actual.insert(bn2, children); | 
					
						
							|  |  |  | 	SymbolicBayesTree::sharedClique r2 = actual.insert(bn3, children); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// insert root clique
 | 
					
						
							|  |  |  | 	children.push_back(r1); | 
					
						
							|  |  |  | 	children.push_back(r2); | 
					
						
							|  |  |  | 	actual.insert(bn1, children, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// traditional way
 | 
					
						
							|  |  |  | 	SymbolicFactorGraph fg; | 
					
						
							|  |  |  | 	fg.push_factor(_x3_, _x4_); | 
					
						
							|  |  |  | 	fg.push_factor(_x1_, _x2_); | 
					
						
							|  |  |  | 	fg.push_factor(_x2_, _x3_); | 
					
						
							|  |  |  | 	fg.push_factor(_x1_, _x3_); | 
					
						
							|  |  |  | 	fg.push_factor(_x4_, _x5_); | 
					
						
							|  |  |  | 	fg.push_factor(_x5_, _x6_); | 
					
						
							|  |  |  | 	fg.push_factor(_x4_, _x6_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Ordering ordering;  ordering += _x1_, _x2_, _x6_, _x5_, _x3_, _x4_; | 
					
						
							|  |  |  | 	BayesNet<SymbolicConditional> bn; | 
					
						
							|  |  |  | 	bn = fg.eliminate(ordering); | 
					
						
							|  |  |  | 	SymbolicBayesTree expected(bn); | 
					
						
							|  |  |  | 	CHECK(assert_equal(expected, actual)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | int main() { | 
					
						
							|  |  |  | 	TestResult tr; | 
					
						
							|  |  |  | 	return TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ |