| 
									
										
										
										
											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-12-10 03:39:25 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  |  * @file    testGaussianISAM.cpp | 
					
						
							|  |  |  |  * @brief   Unit tests for GaussianISAM | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  |  * @author  Michael Kaess | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-10 04:15:44 +08:00
										 |  |  | #include <tests/smallExample.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/nonlinear/Ordering.h>
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:05:38 +08:00
										 |  |  | #include <gtsam/nonlinear/Symbol.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/linear/GaussianBayesNet.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianISAM.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | #include <gtsam/linear/GaussianSequentialSolver.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-23 06:53:33 +08:00
										 |  |  | #include <gtsam/linear/GaussianMultifrontalSolver.h>
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:05:38 +08:00
										 |  |  | #include <gtsam/inference/ISAM.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Rot2.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | #include <boost/assign/std/list.hpp> // for operator +=
 | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | using namespace example; | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | using symbol_shorthand::X; | 
					
						
							|  |  |  | using symbol_shorthand::L; | 
					
						
							| 
									
										
										
										
											2012-02-21 08:53:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // Some numbers that should be consistent among all smoother tests
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-27 12:39:35 +08:00
										 |  |  | double sigmax1 = 0.786153, sigmax2 = 1.0/1.47292, sigmax3 = 0.671512, sigmax4 = | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  | 		0.669534, sigmax5 = sigmax3, sigmax6 = sigmax2, sigmax7 = sigmax1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-27 12:39:35 +08:00
										 |  |  | const double tol = 1e-4; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | TEST_UNSAFE( ISAM, iSAM_smoother ) | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   Ordering ordering; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  |   for (int t = 1; t <= 7; t++) ordering += X(t); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create smoother with 7 nodes
 | 
					
						
							|  |  |  | 	GaussianFactorGraph smoother = createSmoother(7, ordering).first; | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// run iSAM for every factor
 | 
					
						
							|  |  |  | 	GaussianISAM actual; | 
					
						
							|  |  |  | 	BOOST_FOREACH(boost::shared_ptr<GaussianFactor> factor, smoother) { | 
					
						
							|  |  |  | 		GaussianFactorGraph factorGraph; | 
					
						
							|  |  |  | 		factorGraph.push_back(factor); | 
					
						
							|  |  |  | 		actual.update(factorGraph); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	BayesTree<GaussianConditional>::shared_ptr bayesTree = GaussianMultifrontalSolver(smoother).eliminate(); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	// Create expected Bayes Tree by solving smoother with "natural" ordering
 | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	GaussianISAM expected(*bayesTree); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check whether BayesTree is correct
 | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | 	EXPECT(assert_equal(expected, actual)); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// obtain solution
 | 
					
						
							| 
									
										
										
										
											2011-10-30 02:53:08 +08:00
										 |  |  | 	VectorValues e(VectorValues::Zero(7,2)); // expected solution
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 	VectorValues optimized = optimize(actual); // actual solution
 | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | 	EXPECT(assert_equal(e, optimized)); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | // SL-FIX TEST( ISAM, iSAM_smoother2 )
 | 
					
						
							|  |  |  | //{
 | 
					
						
							|  |  |  | //	// Create smoother with 7 nodes
 | 
					
						
							|  |  |  | //	GaussianFactorGraph smoother = createSmoother(7);
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	// Create initial tree from first 4 timestamps in reverse order !
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //	Ordering ord; ord += X(4),X(3),X(2),X(1);
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	GaussianFactorGraph factors1;
 | 
					
						
							|  |  |  | //	for (int i=0;i<7;i++) factors1.push_back(smoother[i]);
 | 
					
						
							| 
									
										
										
										
											2012-03-04 04:23:03 +08:00
										 |  |  | //	GaussianISAM actual(*inference::Eliminate(factors1));
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //	// run iSAM with remaining factors
 | 
					
						
							|  |  |  | //	GaussianFactorGraph factors2;
 | 
					
						
							|  |  |  | //	for (int i=7;i<13;i++) factors2.push_back(smoother[i]);
 | 
					
						
							|  |  |  | //	actual.update(factors2);
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	// Create expected Bayes Tree by solving smoother with "natural" ordering
 | 
					
						
							|  |  |  | //	Ordering ordering;
 | 
					
						
							|  |  |  | //	for (int t = 1; t <= 7; t++) ordering += symbol('x', t);
 | 
					
						
							|  |  |  | //	GaussianISAM expected(smoother.eliminate(ordering));
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | //	EXPECT(assert_equal(expected, actual));
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //}
 | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  | /* ************************************************************************* *
 | 
					
						
							|  |  |  |  Bayes tree for smoother with "natural" ordering: | 
					
						
							|  |  |  | C1 x6 x7 | 
					
						
							|  |  |  | C2   x5 : x6 | 
					
						
							|  |  |  | C3     x4 : x5 | 
					
						
							|  |  |  | C4       x3 : x4 | 
					
						
							|  |  |  | C5         x2 : x3 | 
					
						
							|  |  |  | C6           x1 : x2 | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | **************************************************************************** */ | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | TEST_UNSAFE( BayesTree, linear_smoother_shortcuts ) | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// Create smoother with 7 nodes
 | 
					
						
							|  |  |  |   Ordering ordering; | 
					
						
							|  |  |  | 	GaussianFactorGraph smoother; | 
					
						
							|  |  |  | 	boost::tie(smoother, ordering) = createSmoother(7); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	BayesTree<GaussianConditional> bayesTree = *GaussianMultifrontalSolver(smoother).eliminate(); | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Create the Bayes tree
 | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	GaussianISAM isamTree(bayesTree); | 
					
						
							|  |  |  | 	LONGS_EQUAL(6,isamTree.size()); | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check the conditional P(Root|Root)
 | 
					
						
							|  |  |  | 	GaussianBayesNet empty; | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	GaussianISAM::sharedClique R = isamTree.root(); | 
					
						
							| 
									
										
										
										
											2011-03-25 03:27:12 +08:00
										 |  |  | 	GaussianBayesNet actual1 = GaussianISAM::shortcut(R,R); | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | 	EXPECT(assert_equal(empty,actual1,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check the conditional P(C2|Root)
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	GaussianISAM::sharedClique C2 = isamTree[ordering[X(5)]]; | 
					
						
							| 
									
										
										
										
											2011-03-25 03:27:12 +08:00
										 |  |  | 	GaussianBayesNet actual2 = GaussianISAM::shortcut(C2,R); | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | 	EXPECT(assert_equal(empty,actual2,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check the conditional P(C3|Root)
 | 
					
						
							|  |  |  | 	double sigma3 = 0.61808; | 
					
						
							|  |  |  | 	Matrix A56 = Matrix_(2,2,-0.382022,0.,0.,-0.382022); | 
					
						
							|  |  |  | 	GaussianBayesNet expected3; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	push_front(expected3,ordering[X(5)], zero(2), eye(2)/sigma3, ordering[X(6)], A56/sigma3, ones(2)); | 
					
						
							|  |  |  | 	GaussianISAM::sharedClique C3 = isamTree[ordering[X(4)]]; | 
					
						
							| 
									
										
										
										
											2011-03-25 03:27:12 +08:00
										 |  |  | 	GaussianBayesNet actual3 = GaussianISAM::shortcut(C3,R); | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | 	EXPECT(assert_equal(expected3,actual3,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check the conditional P(C4|Root)
 | 
					
						
							|  |  |  | 	double sigma4 = 0.661968; | 
					
						
							|  |  |  | 	Matrix A46 = Matrix_(2,2,-0.146067,0.,0.,-0.146067); | 
					
						
							|  |  |  | 	GaussianBayesNet expected4; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	push_front(expected4, ordering[X(4)], zero(2), eye(2)/sigma4, ordering[X(6)], A46/sigma4, ones(2)); | 
					
						
							|  |  |  | 	GaussianISAM::sharedClique C4 = isamTree[ordering[X(3)]]; | 
					
						
							| 
									
										
										
										
											2011-03-25 03:27:12 +08:00
										 |  |  | 	GaussianBayesNet actual4 = GaussianISAM::shortcut(C4,R); | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | 	EXPECT(assert_equal(expected4,actual4,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* *
 | 
					
						
							|  |  |  |  Bayes tree for smoother with "nested dissection" ordering: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	 Node[x1] P(x1 | x2) | 
					
						
							|  |  |  | 	 Node[x3] P(x3 | x2 x4) | 
					
						
							|  |  |  | 	 Node[x5] P(x5 | x4 x6) | 
					
						
							|  |  |  | 	 Node[x7] P(x7 | x6) | 
					
						
							|  |  |  | 	 Node[x2] P(x2 | x4) | 
					
						
							|  |  |  | 	 Node[x6] P(x6 | x4) | 
					
						
							|  |  |  | 	 Node[x4] P(x4) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  becomes | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	 C1		 x5 x6 x4 | 
					
						
							|  |  |  | 	 C2		  x3 x2 : x4 | 
					
						
							|  |  |  | 	 C3		    x1 : x2 | 
					
						
							|  |  |  | 	 C4		  x7 : x6 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | TEST_UNSAFE( BayesTree, balanced_smoother_marginals ) | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Create smoother with 7 nodes
 | 
					
						
							|  |  |  |   Ordering ordering; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  |   ordering += X(1),X(3),X(5),X(7),X(2),X(6),X(4); | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  |   GaussianFactorGraph smoother = createSmoother(7, ordering).first; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Create the Bayes tree
 | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  |   BayesTree<GaussianConditional> chordalBayesNet = *GaussianMultifrontalSolver(smoother).eliminate(); | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-30 02:53:08 +08:00
										 |  |  | 	VectorValues expectedSolution(VectorValues::Zero(7,2)); | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 	VectorValues actualSolution = optimize(chordalBayesNet); | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | 	EXPECT(assert_equal(expectedSolution,actualSolution,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Create the Bayes tree
 | 
					
						
							|  |  |  | 	GaussianISAM bayesTree(chordalBayesNet); | 
					
						
							|  |  |  | 	LONGS_EQUAL(4,bayesTree.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	double tol=1e-5; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check marginal on x1
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	GaussianBayesNet expected1 = simpleGaussian(ordering[X(1)], zero(2), sigmax1); | 
					
						
							|  |  |  | 	GaussianBayesNet actual1 = *bayesTree.marginalBayesNet(ordering[X(1)]); | 
					
						
							| 
									
										
										
										
											2011-02-12 06:08:07 +08:00
										 |  |  | 	Matrix expectedCovarianceX1 = eye(2,2) * (sigmax1 * sigmax1); | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	Matrix actualCovarianceX1; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	actualCovarianceX1 = bayesTree.marginalCovariance(ordering[X(1)]); | 
					
						
							| 
									
										
										
										
											2011-02-12 06:08:07 +08:00
										 |  |  | 	EXPECT(assert_equal(expectedCovarianceX1, actualCovarianceX1, tol)); | 
					
						
							|  |  |  | 	EXPECT(assert_equal(expected1,actual1,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check marginal on x2
 | 
					
						
							|  |  |  | 	double sigx2 = 0.68712938; // FIXME: this should be corrected analytically
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	GaussianBayesNet expected2 = simpleGaussian(ordering[X(2)], zero(2), sigx2); | 
					
						
							|  |  |  | 	GaussianBayesNet actual2 = *bayesTree.marginalBayesNet(ordering[X(2)]); | 
					
						
							| 
									
										
										
										
											2011-02-12 06:08:07 +08:00
										 |  |  | 	Matrix expectedCovarianceX2 = eye(2,2) * (sigx2 * sigx2); | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	Matrix actualCovarianceX2; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	actualCovarianceX2 = bayesTree.marginalCovariance(ordering[X(2)]); | 
					
						
							| 
									
										
										
										
											2011-02-12 06:08:07 +08:00
										 |  |  | 	EXPECT(assert_equal(expectedCovarianceX2, actualCovarianceX2, tol)); | 
					
						
							|  |  |  | 	EXPECT(assert_equal(expected2,actual2,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check marginal on x3
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	GaussianBayesNet expected3 = simpleGaussian(ordering[X(3)], zero(2), sigmax3); | 
					
						
							|  |  |  | 	GaussianBayesNet actual3 = *bayesTree.marginalBayesNet(ordering[X(3)]); | 
					
						
							| 
									
										
										
										
											2011-02-12 06:08:07 +08:00
										 |  |  | 	Matrix expectedCovarianceX3 = eye(2,2) * (sigmax3 * sigmax3); | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	Matrix actualCovarianceX3; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	actualCovarianceX3 = bayesTree.marginalCovariance(ordering[X(3)]); | 
					
						
							| 
									
										
										
										
											2011-02-12 06:08:07 +08:00
										 |  |  | 	EXPECT(assert_equal(expectedCovarianceX3, actualCovarianceX3, tol)); | 
					
						
							|  |  |  | 	EXPECT(assert_equal(expected3,actual3,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check marginal on x4
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	GaussianBayesNet expected4 = simpleGaussian(ordering[X(4)], zero(2), sigmax4); | 
					
						
							|  |  |  | 	GaussianBayesNet actual4 = *bayesTree.marginalBayesNet(ordering[X(4)]); | 
					
						
							| 
									
										
										
										
											2011-02-12 06:08:07 +08:00
										 |  |  | 	Matrix expectedCovarianceX4 = eye(2,2) * (sigmax4 * sigmax4); | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	Matrix actualCovarianceX4; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	actualCovarianceX4 = bayesTree.marginalCovariance(ordering[X(4)]); | 
					
						
							| 
									
										
										
										
											2011-02-12 06:08:07 +08:00
										 |  |  | 	EXPECT(assert_equal(expectedCovarianceX4, actualCovarianceX4, tol)); | 
					
						
							|  |  |  | 	EXPECT(assert_equal(expected4,actual4,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check marginal on x7 (should be equal to x1)
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	GaussianBayesNet expected7 = simpleGaussian(ordering[X(7)], zero(2), sigmax7); | 
					
						
							|  |  |  | 	GaussianBayesNet actual7 = *bayesTree.marginalBayesNet(ordering[X(7)]); | 
					
						
							| 
									
										
										
										
											2011-02-12 06:08:07 +08:00
										 |  |  | 	Matrix expectedCovarianceX7 = eye(2,2) * (sigmax7 * sigmax7); | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	Matrix actualCovarianceX7; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	actualCovarianceX7 = bayesTree.marginalCovariance(ordering[X(7)]); | 
					
						
							| 
									
										
										
										
											2011-02-12 06:08:07 +08:00
										 |  |  | 	EXPECT(assert_equal(expectedCovarianceX7, actualCovarianceX7, tol)); | 
					
						
							|  |  |  | 	EXPECT(assert_equal(expected7,actual7,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | TEST_UNSAFE( BayesTree, balanced_smoother_shortcuts ) | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// Create smoother with 7 nodes
 | 
					
						
							|  |  |  |   Ordering ordering; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  |   ordering += X(1),X(3),X(5),X(7),X(2),X(6),X(4); | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | 	GaussianFactorGraph smoother = createSmoother(7, ordering).first; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Create the Bayes tree
 | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	BayesTree<GaussianConditional> bayesTree = *GaussianMultifrontalSolver(smoother).eliminate(); | 
					
						
							|  |  |  | 	GaussianISAM isamTree(bayesTree); | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check the conditional P(Root|Root)
 | 
					
						
							|  |  |  | 	GaussianBayesNet empty; | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	GaussianISAM::sharedClique R = isamTree.root(); | 
					
						
							| 
									
										
										
										
											2011-03-25 03:27:12 +08:00
										 |  |  | 	GaussianBayesNet actual1 = GaussianISAM::shortcut(R,R); | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | 	EXPECT(assert_equal(empty,actual1,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check the conditional P(C2|Root)
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	GaussianISAM::sharedClique C2 = isamTree[ordering[X(3)]]; | 
					
						
							| 
									
										
										
										
											2011-03-25 03:27:12 +08:00
										 |  |  | 	GaussianBayesNet actual2 = GaussianISAM::shortcut(C2,R); | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | 	EXPECT(assert_equal(empty,actual2,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check the conditional P(C3|Root), which should be equal to P(x2|x4)
 | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	/** TODO: Note for multifrontal conditional:
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	 * p_x2_x4 is now an element conditional of the multifrontal conditional bayesTree[ordering[X(2)]]->conditional() | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	 * We don't know yet how to take it out. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //	GaussianConditional::shared_ptr p_x2_x4 = bayesTree[ordering[X(2)]]->conditional();
 | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | //	p_x2_x4->print("Conditional p_x2_x4: ");
 | 
					
						
							|  |  |  | //	GaussianBayesNet expected3(p_x2_x4);
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //	GaussianISAM::sharedClique C3 = isamTree[ordering[X(1)]];
 | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | //	GaussianBayesNet actual3 = GaussianISAM::shortcut(C3,R);
 | 
					
						
							|  |  |  | //	EXPECT(assert_equal(expected3,actual3,tol));
 | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | ///* ************************************************************************* */
 | 
					
						
							|  |  |  | //TEST( BayesTree, balanced_smoother_clique_marginals )
 | 
					
						
							|  |  |  | //{
 | 
					
						
							|  |  |  | //  // Create smoother with 7 nodes
 | 
					
						
							|  |  |  | //  Ordering ordering;
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //  ordering += X(1),X(3),X(5),X(7),X(2),X(6),X(4);
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | //  GaussianFactorGraph smoother = createSmoother(7, ordering).first;
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  // Create the Bayes tree
 | 
					
						
							|  |  |  | //  GaussianBayesNet chordalBayesNet = *GaussianSequentialSolver(smoother).eliminate();
 | 
					
						
							|  |  |  | //  GaussianISAM bayesTree(chordalBayesNet);
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	// Check the clique marginal P(C3)
 | 
					
						
							|  |  |  | //	double sigmax2_alt = 1/1.45533; // THIS NEEDS TO BE CHECKED!
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //	GaussianBayesNet expected = simpleGaussian(ordering[X(2)],zero(2),sigmax2_alt);
 | 
					
						
							|  |  |  | //	push_front(expected,ordering[X(1)], zero(2), eye(2)*sqrt(2), ordering[X(2)], -eye(2)*sqrt(2)/2, ones(2));
 | 
					
						
							|  |  |  | //	GaussianISAM::sharedClique R = bayesTree.root(), C3 = bayesTree[ordering[X(1)]];
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | //	GaussianFactorGraph marginal = C3->marginal(R);
 | 
					
						
							| 
									
										
										
										
											2010-10-23 02:02:55 +08:00
										 |  |  | //	GaussianVariableIndex varIndex(marginal);
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | //	Permutation toFront(Permutation::PullToFront(C3->keys(), varIndex.size()));
 | 
					
						
							|  |  |  | //	Permutation toFrontInverse(*toFront.inverse());
 | 
					
						
							|  |  |  | //	varIndex.permute(toFront);
 | 
					
						
							|  |  |  | //	BOOST_FOREACH(const GaussianFactor::shared_ptr& factor, marginal) {
 | 
					
						
							|  |  |  | //	  factor->permuteWithInverse(toFrontInverse); }
 | 
					
						
							| 
									
										
										
										
											2012-03-04 04:23:03 +08:00
										 |  |  | //	GaussianBayesNet actual = *inference::EliminateUntil(marginal, C3->keys().size(), varIndex);
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | //	actual.permuteWithInverse(toFront);
 | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | //	EXPECT(assert_equal(expected,actual,tol));
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | //}
 | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | TEST_UNSAFE( BayesTree, balanced_smoother_joint ) | 
					
						
							| 
									
										
										
										
											2010-10-23 06:11:23 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// Create smoother with 7 nodes
 | 
					
						
							|  |  |  | 	Ordering ordering; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	ordering += X(1),X(3),X(5),X(7),X(2),X(6),X(4); | 
					
						
							| 
									
										
										
										
											2010-10-23 06:11:23 +08:00
										 |  |  | 	GaussianFactorGraph smoother = createSmoother(7, ordering).first; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Create the Bayes tree, expected to look like:
 | 
					
						
							|  |  |  | 	//	 x5 x6 x4
 | 
					
						
							|  |  |  | 	//	   x3 x2 : x4
 | 
					
						
							|  |  |  | 	//	     x1 : x2
 | 
					
						
							|  |  |  | 	//	   x7 : x6
 | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | 	BayesTree<GaussianConditional> chordalBayesNet = *GaussianMultifrontalSolver(smoother).eliminate(); | 
					
						
							| 
									
										
										
										
											2010-10-23 06:11:23 +08:00
										 |  |  | 	GaussianISAM bayesTree(chordalBayesNet); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Conditional density elements reused by both tests
 | 
					
						
							|  |  |  | 	const Vector sigma = ones(2); | 
					
						
							|  |  |  | 	const Matrix I = eye(2), A = -0.00429185*I; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check the joint density P(x1,x7) factored as P(x1|x7)P(x7)
 | 
					
						
							|  |  |  | 	GaussianBayesNet expected1; | 
					
						
							|  |  |  | 	// Why does the sign get flipped on the prior?
 | 
					
						
							|  |  |  | 	GaussianConditional::shared_ptr | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 		parent1(new GaussianConditional(ordering[X(7)], zero(2), -1*I/sigmax7, ones(2))); | 
					
						
							| 
									
										
										
										
											2010-10-23 06:11:23 +08:00
										 |  |  | 	expected1.push_front(parent1); | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	push_front(expected1,ordering[X(1)], zero(2), I/sigmax7, ordering[X(7)], A/sigmax7, sigma); | 
					
						
							|  |  |  | 	GaussianBayesNet actual1 = *bayesTree.jointBayesNet(ordering[X(1)],ordering[X(7)]); | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | 	EXPECT(assert_equal(expected1,actual1,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-23 06:11:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	// Check the joint density P(x7,x1) factored as P(x7|x1)P(x1)
 | 
					
						
							|  |  |  | //	GaussianBayesNet expected2;
 | 
					
						
							|  |  |  | //	GaussianConditional::shared_ptr
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //			parent2(new GaussianConditional(ordering[X(1)], zero(2), -1*I/sigmax1, ones(2)));
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //		expected2.push_front(parent2);
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //	push_front(expected2,ordering[X(7)], zero(2), I/sigmax1, ordering[X(1)], A/sigmax1, sigma);
 | 
					
						
							|  |  |  | //	GaussianBayesNet actual2 = *bayesTree.jointBayesNet(ordering[X(7)],ordering[X(1)]);
 | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | //	EXPECT(assert_equal(expected2,actual2,tol));
 | 
					
						
							| 
									
										
										
										
											2010-10-23 06:11:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check the joint density P(x1,x4), i.e. with a root variable
 | 
					
						
							|  |  |  | 	GaussianBayesNet expected3; | 
					
						
							|  |  |  | 	GaussianConditional::shared_ptr | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 			parent3(new GaussianConditional(ordering[X(4)], zero(2), I/sigmax4, ones(2))); | 
					
						
							| 
									
										
										
										
											2010-10-23 06:11:23 +08:00
										 |  |  | 		expected3.push_front(parent3); | 
					
						
							|  |  |  | 	double sig14 = 0.784465; | 
					
						
							|  |  |  | 	Matrix A14 = -0.0769231*I; | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | 	push_front(expected3,ordering[X(1)], zero(2), I/sig14, ordering[X(4)], A14/sig14, sigma); | 
					
						
							|  |  |  | 	GaussianBayesNet actual3 = *bayesTree.jointBayesNet(ordering[X(1)],ordering[X(4)]); | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | 	EXPECT(assert_equal(expected3,actual3,tol)); | 
					
						
							| 
									
										
										
										
											2010-10-23 06:11:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	// Check the joint density P(x4,x1), i.e. with a root variable, factored the other way
 | 
					
						
							|  |  |  | //	GaussianBayesNet expected4;
 | 
					
						
							|  |  |  | //	GaussianConditional::shared_ptr
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //			parent4(new GaussianConditional(ordering[X(1)], zero(2), -1.0*I/sigmax1, ones(2)));
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //		expected4.push_front(parent4);
 | 
					
						
							|  |  |  | //	double sig41 = 0.668096;
 | 
					
						
							|  |  |  | //	Matrix A41 = -0.055794*I;
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:28:21 +08:00
										 |  |  | //	push_front(expected4,ordering[X(4)], zero(2), I/sig41, ordering[X(1)], A41/sig41, sigma);
 | 
					
						
							|  |  |  | //	GaussianBayesNet actual4 = *bayesTree.jointBayesNet(ordering[X(4)],ordering[X(1)]);
 | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  | //	EXPECT(assert_equal(expected4,actual4,tol));
 | 
					
						
							| 
									
										
										
										
											2010-10-23 06:11:23 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-23 06:53:33 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  | TEST_UNSAFE(BayesTree, simpleMarginal) | 
					
						
							| 
									
										
										
										
											2010-10-23 06:53:33 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   GaussianFactorGraph gfg; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Matrix A12 = Rot2::fromDegrees(45.0).matrix(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   gfg.add(0, eye(2), zero(2), sharedSigma(2, 1.0)); | 
					
						
							|  |  |  |   gfg.add(0, -eye(2), 1, eye(2), ones(2), sharedSigma(2, 1.0)); | 
					
						
							|  |  |  |   gfg.add(1, -eye(2), 2, A12, ones(2), sharedSigma(2, 1.0)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-14 00:55:31 +08:00
										 |  |  |   Matrix expected(GaussianSequentialSolver(gfg).marginalCovariance(2)); | 
					
						
							|  |  |  |   Matrix actual(GaussianMultifrontalSolver(gfg).marginalCovariance(2)); | 
					
						
							| 
									
										
										
										
											2010-10-23 06:53:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-20 21:52:08 +08:00
										 |  |  |   EXPECT(assert_equal(expected, actual)); | 
					
						
							| 
									
										
										
										
											2010-10-23 06:53:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-10 03:39:25 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr);} | 
					
						
							|  |  |  | /* ************************************************************************* */ |