| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/inference/SymbolicFactorGraph.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/BayesTree-inl.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | #include <gtsam/inference/IndexFactor.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/inference/inference-inl.h>
 | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | typedef BayesTree<IndexConditional> SymbolicBayesTree; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ///* ************************************************************************* */
 | 
					
						
							|  |  |  | //// SLAM example from RSS sqrtSAM paper
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | static const Index _x3_=0, _x2_=1, _x1_=2, _l2_=3, _l1_=4; | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | //IndexConditional::shared_ptr
 | 
					
						
							|  |  |  | //		x3(new IndexConditional(_x3_)),
 | 
					
						
							|  |  |  | //		x2(new IndexConditional(_x2_,_x3_)),
 | 
					
						
							|  |  |  | //		x1(new IndexConditional(_x1_,_x2_,_x3_)),
 | 
					
						
							|  |  |  | //		l1(new IndexConditional(_l1_,_x1_,_x2_)),
 | 
					
						
							|  |  |  | //		l2(new IndexConditional(_l2_,_x1_,_x3_));
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //// Bayes Tree for sqrtSAM example
 | 
					
						
							|  |  |  | //SymbolicBayesTree createSlamSymbolicBayesTree(){
 | 
					
						
							|  |  |  | //	// Create using insert
 | 
					
						
							|  |  |  | ////	Ordering slamOrdering; slamOrdering += _x3_, _x2_, _x1_, _l2_, _l1_;
 | 
					
						
							|  |  |  | //	SymbolicBayesTree bayesTree_slam;
 | 
					
						
							|  |  |  | //	bayesTree_slam.insert(x3);
 | 
					
						
							|  |  |  | //	bayesTree_slam.insert(x2);
 | 
					
						
							|  |  |  | //	bayesTree_slam.insert(x1);
 | 
					
						
							|  |  |  | //	bayesTree_slam.insert(l2);
 | 
					
						
							|  |  |  | //	bayesTree_slam.insert(l1);
 | 
					
						
							|  |  |  | //	return bayesTree_slam;
 | 
					
						
							|  |  |  | //}
 | 
					
						
							| 
									
										
										
										
											2009-11-24 04:11:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | // Conditionals for ASIA example from the tutorial with A and D evidence
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | static const Index _X_=0, _T_=1, _S_=2, _E_=3, _L_=4, _B_=5; | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | IndexConditional::shared_ptr | 
					
						
							|  |  |  | 	B(new IndexConditional(_B_)), | 
					
						
							|  |  |  | 	L(new IndexConditional(_L_, _B_)), | 
					
						
							|  |  |  | 	E(new IndexConditional(_E_, _L_, _B_)), | 
					
						
							|  |  |  | 	S(new IndexConditional(_S_, _L_, _B_)), | 
					
						
							|  |  |  | 	T(new IndexConditional(_T_, _E_, _L_)), | 
					
						
							|  |  |  | 	X(new IndexConditional(_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-10-09 06:04:47 +08:00
										 |  |  | //	Ordering asiaOrdering; asiaOrdering += _X_, _T_, _S_, _E_, _L_, _B_;
 | 
					
						
							|  |  |  | 	bayesTree.insert(B); | 
					
						
							|  |  |  | 	bayesTree.insert(L); | 
					
						
							|  |  |  | 	bayesTree.insert(E); | 
					
						
							|  |  |  | 	bayesTree.insert(S); | 
					
						
							|  |  |  | 	bayesTree.insert(T); | 
					
						
							|  |  |  | 	bayesTree.insert(X); | 
					
						
							| 
									
										
										
										
											2009-11-23 02:12:29 +08:00
										 |  |  | 	return bayesTree; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | 	BayesNet<IndexConditional> expected_root; | 
					
						
							| 
									
										
										
										
											2009-11-02 13:17:44 +08:00
										 |  |  | 	expected_root.push_back(E); | 
					
						
							| 
									
										
										
										
											2009-11-03 14:29:56 +08:00
										 |  |  | 	expected_root.push_back(L); | 
					
						
							|  |  |  | 	expected_root.push_back(B); | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | 	boost::shared_ptr<BayesNet<IndexConditional> > 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
 | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | 	BayesNet<IndexConditional> 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*
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	Ordering ordering; ordering += _X_, _T_, _S_, _E_, _L_, _B_;
 | 
					
						
							|  |  |  | //	IndexTable<Symbol> index(ordering);
 | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	list<Index> parents1; parents1 += _E_, _L_; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	CHECK(assert_equal(_E_, bayesTree.findParentClique(parents1))); | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	list<Index> parents2; parents2 += _L_, _E_; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	CHECK(assert_equal(_E_, bayesTree.findParentClique(parents2))); | 
					
						
							| 
									
										
										
										
											2010-02-10 05:32:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	list<Index> parents3; parents3 += _L_, _B_; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	CHECK(assert_equal(_L_, bayesTree.findParentClique(parents3))); | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2009-11-21 11:38:13 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( BayesTree, removePath ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   const Index _A_=5, _B_=4, _C_=3, _D_=2, _E_=1, _F_=0; | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | 	IndexConditional::shared_ptr | 
					
						
							|  |  |  | 			A(new IndexConditional(_A_)), | 
					
						
							|  |  |  | 			B(new IndexConditional(_B_, _A_)), | 
					
						
							|  |  |  | 			C(new IndexConditional(_C_, _A_)), | 
					
						
							|  |  |  | 			D(new IndexConditional(_D_, _C_)), | 
					
						
							|  |  |  | 			E(new IndexConditional(_E_, _B_)), | 
					
						
							|  |  |  | 			F(new IndexConditional(_F_, _E_)); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree bayesTree; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	Ordering ord; ord += _A_,_B_,_C_,_D_,_E_,_F_;
 | 
					
						
							|  |  |  | 	bayesTree.insert(A); | 
					
						
							|  |  |  | 	bayesTree.insert(B); | 
					
						
							|  |  |  | 	bayesTree.insert(C); | 
					
						
							|  |  |  | 	bayesTree.insert(D); | 
					
						
							|  |  |  | 	bayesTree.insert(E); | 
					
						
							|  |  |  | 	bayesTree.insert(F); | 
					
						
							| 
									
										
										
										
											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-10-09 06:04:47 +08:00
										 |  |  | 	expected.push_factor(_B_,_A_); | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	expected.push_factor(_A_); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	expected.push_factor(_C_,_A_); | 
					
						
							| 
									
										
										
										
											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-10-20 05:31:13 +08:00
										 |  |  |   BayesNet<IndexConditional> 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-10-09 06:04:47 +08:00
										 |  |  | 	SymbolicFactorGraph factors(bn); | 
					
						
							|  |  |  |   CHECK(assert_equal((SymbolicFactorGraph)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-10-09 06:04:47 +08:00
										 |  |  |   expected2.push_factor(_E_,_B_); | 
					
						
							| 
									
										
										
										
											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-10-20 05:31:13 +08:00
										 |  |  |   BayesNet<IndexConditional> 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-10-09 06:04:47 +08:00
										 |  |  |   SymbolicFactorGraph factors2(bn2); | 
					
						
							|  |  |  |   CHECK(assert_equal((SymbolicFactorGraph)expected2, factors2)); | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  |   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-10-20 05:31:13 +08:00
										 |  |  | 	BayesNet<IndexConditional> 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-10-09 06:04:47 +08:00
										 |  |  | 	SymbolicFactorGraph factors(bn); | 
					
						
							| 
									
										
										
										
											2009-11-23 02:06:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check expected outcome
 | 
					
						
							|  |  |  | 	SymbolicFactorGraph expected; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	expected.push_factor(_E_,_L_,_B_); | 
					
						
							|  |  |  | 	expected.push_factor(_L_,_B_); | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	expected.push_factor(_B_); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   CHECK(assert_equal(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-10-20 05:31:13 +08:00
										 |  |  | 	BayesNet<IndexConditional> 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-10-09 06:04:47 +08:00
										 |  |  | 	SymbolicFactorGraph factors(bn); | 
					
						
							| 
									
										
										
										
											2009-11-23 02:06:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check expected outcome
 | 
					
						
							|  |  |  | 	SymbolicFactorGraph expected; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	expected.push_factor(_E_,_L_,_B_); | 
					
						
							|  |  |  | 	expected.push_factor(_L_,_B_); | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	expected.push_factor(_B_); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	expected.push_factor(_S_,_L_,_B_); | 
					
						
							|  |  |  |   CHECK(assert_equal(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-10-20 05:31:13 +08:00
										 |  |  | 	boost::shared_ptr<IndexFactor> newFactor(new IndexFactor(_S_,_B_)); | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Remove the contaminated part of the Bayes tree
 | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | 	BayesNet<IndexConditional> bn; | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques orphans; | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	list<Index> keys; keys += _B_,_S_; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	bayesTree.removeTop(keys, bn, orphans); | 
					
						
							|  |  |  | 	SymbolicFactorGraph factors(bn); | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check expected outcome
 | 
					
						
							|  |  |  | 	SymbolicFactorGraph expected; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	expected.push_factor(_E_,_L_,_B_); | 
					
						
							|  |  |  | 	expected.push_factor(_L_,_B_); | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	expected.push_factor(_B_); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	expected.push_factor(_S_,_L_,_B_); | 
					
						
							|  |  |  |   CHECK(assert_equal(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-10-20 05:31:13 +08:00
										 |  |  | 	boost::shared_ptr<IndexFactor> newFactor2(new IndexFactor(_B_)); | 
					
						
							|  |  |  | 	BayesNet<IndexConditional> bn2; | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques orphans2; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	keys.clear(); keys += _B_; | 
					
						
							|  |  |  | 	bayesTree.removeTop(keys, bn2, orphans2); | 
					
						
							|  |  |  | 	SymbolicFactorGraph factors2(bn2); | 
					
						
							| 
									
										
										
										
											2009-11-23 06:59:56 +08:00
										 |  |  | 	SymbolicFactorGraph expected2; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   CHECK(assert_equal(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-10-20 05:31:13 +08:00
										 |  |  | 	BayesNet<IndexConditional> bn; | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	SymbolicBayesTree::Cliques orphans; | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   list<Index> keys; keys += _B_,_S_; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	bayesTree.removeTop(keys, bn, orphans); | 
					
						
							|  |  |  | 	SymbolicFactorGraph factors(bn); | 
					
						
							| 
									
										
										
										
											2009-11-23 07:50:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check expected outcome
 | 
					
						
							|  |  |  | 	SymbolicFactorGraph expected; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	expected.push_factor(_E_,_L_,_B_); | 
					
						
							|  |  |  | 	expected.push_factor(_L_,_B_); | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	expected.push_factor(_B_); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	expected.push_factor(_S_,_L_,_B_); | 
					
						
							|  |  |  |   CHECK(assert_equal(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-10-12 05:14:35 +08:00
										 |  |  |   const Index _x4_=5, _l5_=6; | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  | 	// simple test case that failed after COLAMD was fixed/activated
 | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | 	IndexConditional::shared_ptr | 
					
						
							|  |  |  | 	X(new IndexConditional(_l5_)), | 
					
						
							|  |  |  | 	A(new IndexConditional(_x4_, _l5_)), | 
					
						
							|  |  |  | 	B(new IndexConditional(_x2_, _x4_)), | 
					
						
							|  |  |  | 	C(new IndexConditional(_x3_, _x2_)); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | //	Ordering newOrdering;
 | 
					
						
							|  |  |  | //	newOrdering += _x3_, _x2_, _x1_, _l2_, _l1_, _x4_, _l5_;
 | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  | 	SymbolicBayesTree bayesTree; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	bayesTree.insert(X); | 
					
						
							|  |  |  | 	bayesTree.insert(A); | 
					
						
							|  |  |  | 	bayesTree.insert(B); | 
					
						
							|  |  |  | 	bayesTree.insert(C); | 
					
						
							| 
									
										
										
										
											2010-01-20 09:24:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// remove all
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	list<Index> keys; | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 	keys += _l5_, _x2_, _x3_, _x4_; | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | 	BayesNet<IndexConditional> 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); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	SymbolicFactorGraph 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
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	const Index _x1_=0, _x2_=1, _x6_=2, _x5_=3, _x3_=4, _x4_=5; | 
					
						
							| 
									
										
										
										
											2010-02-13 15:09:27 +08:00
										 |  |  | 	SymbolicFactorGraph fg1, fg2, fg3; | 
					
						
							|  |  |  | 	fg1.push_factor(_x3_, _x4_); | 
					
						
							|  |  |  | 	fg2.push_factor(_x1_, _x2_); | 
					
						
							|  |  |  | 	fg2.push_factor(_x2_, _x3_); | 
					
						
							|  |  |  | 	fg2.push_factor(_x1_, _x3_); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	fg3.push_factor(_x5_, _x4_); | 
					
						
							|  |  |  | 	fg3.push_factor(_x6_, _x5_); | 
					
						
							|  |  |  | 	fg3.push_factor(_x6_, _x4_); | 
					
						
							| 
									
										
										
										
											2010-02-13 15:09:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	Ordering ordering1; ordering1 += _x3_, _x4_;
 | 
					
						
							|  |  |  | //	Ordering ordering2; ordering2 += _x1_, _x2_;
 | 
					
						
							|  |  |  | //	Ordering ordering3; ordering3 += _x6_, _x5_;
 | 
					
						
							| 
									
										
										
										
											2010-02-13 15:09:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | 	BayesNet<IndexConditional> bn1, bn2, bn3; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	bn1 = *Inference::EliminateUntil(fg1, _x4_+1); | 
					
						
							|  |  |  | 	bn2 = *Inference::EliminateUntil(fg2, _x2_+1); | 
					
						
							|  |  |  | 	bn3 = *Inference::EliminateUntil(fg3, _x5_+1); | 
					
						
							| 
									
										
										
										
											2010-02-13 15:09:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// 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_); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   fg.push_factor(_x5_, _x4_); | 
					
						
							|  |  |  |   fg.push_factor(_x6_, _x5_); | 
					
						
							|  |  |  |   fg.push_factor(_x6_, _x4_); | 
					
						
							| 
									
										
										
										
											2010-02-13 15:09:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	Ordering ordering;  ordering += _x1_, _x2_, _x6_, _x5_, _x3_, _x4_;
 | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | 	BayesNet<IndexConditional> bn; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	bn = *Inference::Eliminate(fg); | 
					
						
							| 
									
										
										
										
											2010-02-13 15:09:27 +08:00
										 |  |  | 	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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ |