| 
									
										
										
										
											2010-10-14 12:54:38 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation,  | 
					
						
							|  |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  |  * testGaussianJunctionTree.cpp | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  |  * Created on: Jul 8, 2010 | 
					
						
							|  |  |  |  * @author Kai Ni | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <boost/assign/list_of.hpp>
 | 
					
						
							|  |  |  | #include <boost/assign/std/list.hpp> // for operator +=
 | 
					
						
							|  |  |  | #include <boost/assign/std/set.hpp> // for operator +=
 | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/linear/GaussianJunctionTree.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/BayesTree-inl.h>
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | static const Index x2=0, x1=1, x3=2, x4=3; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-19 04:23:23 +08:00
										 |  |  | GaussianFactorGraph createChain() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	typedef GaussianFactorGraph::sharedFactor Factor; | 
					
						
							|  |  |  | 	SharedDiagonal model(Vector_(1, 0.5)); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	Factor factor1(new GaussianFactor(x2, Matrix_(1,1,1.), x1, Matrix_(1,1,1.), Vector_(1,1.),  model)); | 
					
						
							|  |  |  | 	Factor factor2(new GaussianFactor(x2, Matrix_(1,1,1.), x3, Matrix_(1,1,1.), Vector_(1,1.),  model)); | 
					
						
							|  |  |  | 	Factor factor3(new GaussianFactor(x3, Matrix_(1,1,1.), x4, Matrix_(1,1,1.), Vector_(1,1.),  model)); | 
					
						
							|  |  |  | 	Factor factor4(new GaussianFactor(x4, Matrix_(1,1,1.), Vector_(1,1.),  model)); | 
					
						
							| 
									
										
										
										
											2010-07-19 04:23:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	GaussianFactorGraph fg; | 
					
						
							|  |  |  | 	fg.push_back(factor1); | 
					
						
							|  |  |  | 	fg.push_back(factor2); | 
					
						
							|  |  |  | 	fg.push_back(factor3); | 
					
						
							|  |  |  | 	fg.push_back(factor4); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return fg; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 17:06:58 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-07-11 15:30:27 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * x1 - x2 - x3 - x4 | 
					
						
							|  |  |  |  * x3 x4 | 
					
						
							|  |  |  |  *    x2 x1 : x3 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * x2 x1 x3 x4  b | 
					
						
							|  |  |  |  *  1  1        1 | 
					
						
							|  |  |  |  *  1     1     1 | 
					
						
							|  |  |  |  *        1  1  1 | 
					
						
							|  |  |  |  *           1  1 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  1  0  0  1 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | TEST( GaussianJunctionTree, eliminate ) | 
					
						
							| 
									
										
										
										
											2010-07-11 15:30:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-07-19 04:23:23 +08:00
										 |  |  | 	GaussianFactorGraph fg = createChain(); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	GaussianJunctionTree junctionTree(fg); | 
					
						
							|  |  |  | 	BayesTree<GaussianConditional>::sharedClique rootClique = junctionTree.eliminate(); | 
					
						
							| 
									
										
										
										
											2010-07-11 15:30:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	typedef BayesTree<GaussianConditional>::sharedConditional sharedConditional; | 
					
						
							|  |  |  | 	Matrix two = Matrix_(1,1,2.); | 
					
						
							|  |  |  | 	Matrix one = Matrix_(1,1,1.); | 
					
						
							|  |  |  | 	BayesTree<GaussianConditional> bayesTree_expected; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	bayesTree_expected.insert(sharedConditional(new GaussianConditional(x4, Vector_(1,2.), two, Vector_(1,1.)))); | 
					
						
							|  |  |  | 	bayesTree_expected.insert(sharedConditional(new GaussianConditional(x3, Vector_(1,2.), two, x4, two, Vector_(1,1.)))); | 
					
						
							|  |  |  | 	bayesTree_expected.insert(sharedConditional(new GaussianConditional(x1, Vector_(1,0.), one*(-1), x3, one, Vector_(1,1.)))); | 
					
						
							|  |  |  | 	bayesTree_expected.insert(sharedConditional(new GaussianConditional(x2, Vector_(1,2.), two, x1, one, x3, one, Vector_(1,1.)))); | 
					
						
							| 
									
										
										
										
											2010-07-31 15:19:03 +08:00
										 |  |  | 	CHECK(assert_equal(*bayesTree_expected.root(), *rootClique)); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	CHECK(assert_equal(*(bayesTree_expected.root()->children().front()), *(rootClique->children().front()))); | 
					
						
							| 
									
										
										
										
											2010-07-31 15:19:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-11 15:30:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-19 04:23:23 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( GaussianJunctionTree, optimizeMultiFrontal ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	GaussianFactorGraph fg = createChain(); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	GaussianJunctionTree tree(fg); | 
					
						
							| 
									
										
										
										
											2010-07-19 04:23:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 	VectorValues actual = tree.optimize(); | 
					
						
							|  |  |  | 	VectorValues expected(vector<size_t>(4,1)); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	expected[x1] = Vector_(1, 0.); | 
					
						
							|  |  |  | 	expected[x2] = Vector_(1, 1.); | 
					
						
							|  |  |  | 	expected[x3] = Vector_(1, 0.); | 
					
						
							|  |  |  | 	expected[x4] = Vector_(1, 1.); | 
					
						
							| 
									
										
										
										
											2010-07-19 04:23:23 +08:00
										 |  |  | 	CHECK(assert_equal(expected,actual)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(GaussianJunctionTree, complexExample) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr);} | 
					
						
							|  |  |  | /* ************************************************************************* */ |