| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |  * 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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @file    testGaussianISAM.cpp | 
					
						
							|  |  |  |  * @brief   Unit tests for GaussianISAM | 
					
						
							|  |  |  |  * @author  Michael Kaess | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <tests/smallExample.h>
 | 
					
						
							| 
									
										
										
										
											2013-08-19 23:32:16 +08:00
										 |  |  | #include <gtsam/inference/Symbol.h>
 | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  | #include <gtsam/linear/GaussianBayesTree.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianBayesNet.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianConditional.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianDensity.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/HessianFactor.h>
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | #include <gtsam/geometry/Rot2.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/assign/std/list.hpp> // for operator +=
 | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | using namespace example; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using symbol_shorthand::X; | 
					
						
							|  |  |  | using symbol_shorthand::L; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // Some numbers that should be consistent among all smoother tests
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static double sigmax1 = 0.786153, /*sigmax2 = 1.0/1.47292,*/ sigmax3 = 0.671512, sigmax4 = | 
					
						
							|  |  |  |     0.669534 /*, sigmax5 = sigmax3, sigmax6 = sigmax2*/, sigmax7 = sigmax1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const double tol = 1e-4; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* *
 | 
					
						
							|  |  |  |  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 | 
					
						
							|  |  |  | **************************************************************************** */ | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | TEST( GaussianBayesTree, linear_smoother_shortcuts ) | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Create smoother with 7 nodes
 | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianFactorGraph smoother = createSmoother(7); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesTree bayesTree = *smoother.eliminateMultifrontal(); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create the Bayes tree
 | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   LONGS_EQUAL(6, (long)bayesTree.size()); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Check the conditional P(Root|Root)
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   GaussianBayesNet empty; | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesTree::sharedClique R = bayesTree.roots().front(); | 
					
						
							|  |  |  |   GaussianBayesNet actual1 = R->shortcut(R); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(empty,actual1,tol)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check the conditional P(C2|Root)
 | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesTree::sharedClique C2 = bayesTree[X(5)]; | 
					
						
							|  |  |  |   GaussianBayesNet actual2 = C2->shortcut(R); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(empty,actual2,tol)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check the conditional P(C3|Root)
 | 
					
						
							|  |  |  |   double sigma3 = 0.61808; | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |   Matrix A56 = (Matrix(2,2) << -0.382022,0.,0.,-0.382022).finished(); | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   GaussianBayesNet expected3; | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   expected3 += GaussianConditional(X(5), Z_2x1, I_2x2/sigma3, X(6), A56/sigma3); | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesTree::sharedClique C3 = bayesTree[X(4)]; | 
					
						
							|  |  |  |   GaussianBayesNet actual3 = C3->shortcut(R); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(expected3,actual3,tol)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check the conditional P(C4|Root)
 | 
					
						
							|  |  |  |   double sigma4 = 0.661968; | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |   Matrix A46 = (Matrix(2,2) << -0.146067,0.,0.,-0.146067).finished(); | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   GaussianBayesNet expected4; | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   expected4 += GaussianConditional(X(4), Z_2x1, I_2x2/sigma4, X(6), A46/sigma4); | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesTree::sharedClique C4 = bayesTree[X(3)]; | 
					
						
							|  |  |  |   GaussianBayesNet actual4 = C4->shortcut(R); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(expected4,actual4,tol)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* *
 | 
					
						
							|  |  |  |  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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | TEST( GaussianBayesTree, balanced_smoother_marginals ) | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Create smoother with 7 nodes
 | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianFactorGraph smoother = createSmoother(7); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create the Bayes tree
 | 
					
						
							| 
									
										
										
										
											2013-08-07 02:04:37 +08:00
										 |  |  |   Ordering ordering; | 
					
						
							|  |  |  |   ordering += X(1),X(3),X(5),X(7),X(2),X(6),X(4); | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesTree bayesTree = *smoother.eliminateMultifrontal(ordering); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   VectorValues actualSolution = bayesTree.optimize(); | 
					
						
							|  |  |  |   VectorValues expectedSolution = VectorValues::Zero(actualSolution); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(expectedSolution,actualSolution,tol)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 08:26:18 +08:00
										 |  |  |   LONGS_EQUAL(4, (long)bayesTree.size()); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   double tol=1e-5; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check marginal on x1
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   JacobianFactor expected1 = GaussianDensity::FromMeanAndStddev(X(1), Z_2x1, sigmax1); | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   JacobianFactor actual1 = *bayesTree.marginalFactor(X(1)); | 
					
						
							| 
									
										
										
										
											2016-04-12 03:11:29 +08:00
										 |  |  |   Matrix expectedCovarianceX1 = I_2x2 * (sigmax1 * sigmax1); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   Matrix actualCovarianceX1; | 
					
						
							| 
									
										
										
										
											2013-08-07 02:04:37 +08:00
										 |  |  |   GaussianFactor::shared_ptr m = bayesTree.marginalFactor(X(1), EliminateCholesky); | 
					
						
							|  |  |  |   actualCovarianceX1 = bayesTree.marginalFactor(X(1), EliminateCholesky)->information().inverse(); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(expectedCovarianceX1, actualCovarianceX1, tol)); | 
					
						
							|  |  |  |   EXPECT(assert_equal(expected1,actual1,tol)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check marginal on x2
 | 
					
						
							|  |  |  |   double sigx2 = 0.68712938; // FIXME: this should be corrected analytically
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   JacobianFactor expected2 = GaussianDensity::FromMeanAndStddev(X(2), Z_2x1, sigx2); | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   JacobianFactor actual2 = *bayesTree.marginalFactor(X(2)); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(expected2,actual2,tol)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check marginal on x3
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   JacobianFactor expected3 = GaussianDensity::FromMeanAndStddev(X(3), Z_2x1, sigmax3); | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   JacobianFactor actual3 = *bayesTree.marginalFactor(X(3)); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(expected3,actual3,tol)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check marginal on x4
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   JacobianFactor expected4 = GaussianDensity::FromMeanAndStddev(X(4), Z_2x1, sigmax4); | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   JacobianFactor actual4 = *bayesTree.marginalFactor(X(4)); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(expected4,actual4,tol)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check marginal on x7 (should be equal to x1)
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   JacobianFactor expected7 = GaussianDensity::FromMeanAndStddev(X(7), Z_2x1, sigmax7); | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   JacobianFactor actual7 = *bayesTree.marginalFactor(X(7)); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(expected7,actual7,tol)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | TEST( GaussianBayesTree, balanced_smoother_shortcuts ) | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Create smoother with 7 nodes
 | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianFactorGraph smoother = createSmoother(7); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create the Bayes tree
 | 
					
						
							| 
									
										
										
										
											2013-08-07 02:04:37 +08:00
										 |  |  |   Ordering ordering; | 
					
						
							|  |  |  |   ordering += X(1),X(3),X(5),X(7),X(2),X(6),X(4); | 
					
						
							|  |  |  |   GaussianBayesTree bayesTree = *smoother.eliminateMultifrontal(ordering); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Check the conditional P(Root|Root)
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   GaussianBayesNet empty; | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesTree::sharedClique R = bayesTree.roots().front(); | 
					
						
							|  |  |  |   GaussianBayesNet actual1 = R->shortcut(R); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(empty,actual1,tol)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check the conditional P(C2|Root)
 | 
					
						
							| 
									
										
										
										
											2013-08-07 00:18:09 +08:00
										 |  |  |   GaussianBayesTree::sharedClique C2 = bayesTree[X(3)]; | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesNet actual2 = C2->shortcut(R); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(empty,actual2,tol)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check the conditional P(C3|Root), which should be equal to P(x2|x4)
 | 
					
						
							|  |  |  |   /** TODO: Note for multifrontal conditional:
 | 
					
						
							|  |  |  |    * p_x2_x4 is now an element conditional of the multifrontal conditional bayesTree[ordering[X(2)]]->conditional() | 
					
						
							|  |  |  |    * We don't know yet how to take it out. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //  GaussianConditional::shared_ptr p_x2_x4 = bayesTree[ordering[X(2)]]->conditional();
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | //  p_x2_x4->print("Conditional p_x2_x4: ");
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //  GaussianBayesNet expected3(p_x2_x4);
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | //  GaussianISAM::sharedClique C3 = isamTree[ordering[X(1)]];
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //  GaussianBayesNet actual3 = GaussianISAM::shortcut(C3,R);
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | //  EXPECT(assert_equal(expected3,actual3,tol));
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ///* ************************************************************************* */
 | 
					
						
							|  |  |  | //TEST( BayesTree, balanced_smoother_clique_marginals )
 | 
					
						
							|  |  |  | //{
 | 
					
						
							|  |  |  | //  // Create smoother with 7 nodes
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //  Ordering ordering;
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | //  ordering += X(1),X(3),X(5),X(7),X(2),X(6),X(4);
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //  GaussianFactorGraph smoother = createSmoother(7, ordering).first;
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  // Create the Bayes tree
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //  GaussianBayesNet chordalBayesNet = *GaussianSequentialSolver(smoother).eliminate();
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | //  GaussianISAM bayesTree(chordalBayesNet);
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  // Check the clique marginal P(C3)
 | 
					
						
							|  |  |  | //  double sigmax2_alt = 1/1.45533; // THIS NEEDS TO BE CHECKED!
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  | //  GaussianBayesNet expected = simpleGaussian(ordering[X(2)],Z_2x1,sigmax2_alt);
 | 
					
						
							|  |  |  | //  push_front(expected,ordering[X(1)], Z_2x1, eye(2)*sqrt(2), ordering[X(2)], -eye(2)*sqrt(2)/2, ones(2));
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | //  GaussianISAM::sharedClique R = bayesTree.root(), C3 = bayesTree[ordering[X(1)]];
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //  GaussianFactorGraph marginal = C3->marginal(R);
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | //  GaussianVariableIndex varIndex(marginal);
 | 
					
						
							|  |  |  | //  Permutation toFront(Permutation::PullToFront(C3->keys(), varIndex.size()));
 | 
					
						
							|  |  |  | //  Permutation toFrontInverse(*toFront.inverse());
 | 
					
						
							|  |  |  | //  varIndex.permute(toFront);
 | 
					
						
							| 
									
										
										
										
											2016-05-21 09:29:02 +08:00
										 |  |  | //  for(const GaussianFactor::shared_ptr& factor: marginal) {
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | //    factor->permuteWithInverse(toFrontInverse); }
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //  GaussianBayesNet actual = *inference::EliminateUntil(marginal, C3->keys().size(), varIndex);
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | //  actual.permuteWithInverse(toFront);
 | 
					
						
							|  |  |  | //  EXPECT(assert_equal(expected,actual,tol));
 | 
					
						
							|  |  |  | //}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | TEST( GaussianBayesTree, balanced_smoother_joint ) | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Create smoother with 7 nodes
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   Ordering ordering; | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   ordering += X(1),X(3),X(5),X(7),X(2),X(6),X(4); | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianFactorGraph smoother = createSmoother(7); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create the Bayes tree, expected to look like:
 | 
					
						
							|  |  |  |   //   x5 x6 x4
 | 
					
						
							|  |  |  |   //     x3 x2 : x4
 | 
					
						
							|  |  |  |   //       x1 : x2
 | 
					
						
							|  |  |  |   //     x7 : x6
 | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesTree bayesTree = *smoother.eliminateMultifrontal(ordering); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Conditional density elements reused by both tests
 | 
					
						
							| 
									
										
										
										
											2016-04-12 03:11:29 +08:00
										 |  |  |   const Matrix I = I_2x2, A = -0.00429185*I; | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Check the joint density P(x1,x7) factored as P(x1|x7)P(x7)
 | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesNet expected1 = list_of | 
					
						
							|  |  |  |     // Why does the sign get flipped on the prior?
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |     (GaussianConditional(X(1), Z_2x1, I/sigmax7, X(7), A/sigmax7)) | 
					
						
							|  |  |  |     (GaussianConditional(X(7), Z_2x1, -1*I/sigmax7)); | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesNet actual1 = *bayesTree.jointBayesNet(X(1),X(7)); | 
					
						
							|  |  |  |   EXPECT(assert_equal(expected1, actual1, tol)); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   //  // Check the joint density P(x7,x1) factored as P(x7|x1)P(x1)
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   //  GaussianBayesNet expected2;
 | 
					
						
							|  |  |  |   //  GaussianConditional::shared_ptr
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   //      parent2(new GaussianConditional(X(1), Z_2x1, -1*I/sigmax1, ones(2)));
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   //    expected2.push_front(parent2);
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   //  push_front(expected2,X(7), Z_2x1, I/sigmax1, X(1), A/sigmax1, sigma);
 | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   //  GaussianBayesNet actual2 = *bayesTree.jointBayesNet(X(7),X(1));
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   //  EXPECT(assert_equal(expected2,actual2,tol));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check the joint density P(x1,x4), i.e. with a root variable
 | 
					
						
							|  |  |  |   double sig14 = 0.784465; | 
					
						
							|  |  |  |   Matrix A14 = -0.0769231*I; | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesNet expected3 = list_of | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |     (GaussianConditional(X(1), Z_2x1, I/sig14, X(4), A14/sig14)) | 
					
						
							|  |  |  |     (GaussianConditional(X(4), Z_2x1, I/sigmax4)); | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   GaussianBayesNet actual3 = *bayesTree.jointBayesNet(X(1),X(4)); | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   EXPECT(assert_equal(expected3,actual3,tol)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //  // Check the joint density P(x4,x1), i.e. with a root variable, factored the other way
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   //  GaussianBayesNet expected4;
 | 
					
						
							|  |  |  |   //  GaussianConditional::shared_ptr
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   //      parent4(new GaussianConditional(X(1), Z_2x1, -1.0*I/sigmax1, ones(2)));
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   //    expected4.push_front(parent4);
 | 
					
						
							|  |  |  |   //  double sig41 = 0.668096;
 | 
					
						
							|  |  |  |   //  Matrix A41 = -0.055794*I;
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   //  push_front(expected4,X(4), Z_2x1, I/sig41, X(1), A41/sig41, sigma);
 | 
					
						
							| 
									
										
										
										
											2013-08-06 21:44:22 +08:00
										 |  |  |   //  GaussianBayesNet actual4 = *bayesTree.jointBayesNet(X(4),X(1));
 | 
					
						
							| 
									
										
										
										
											2012-10-09 06:40:40 +08:00
										 |  |  |   //  EXPECT(assert_equal(expected4,actual4,tol));
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-28 14:21:21 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | TEST(GaussianBayesTree, shortcut_overlapping_separator) | 
					
						
							| 
									
										
										
										
											2012-10-28 14:21:21 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Test computing shortcuts when the separator overlaps.  This previously
 | 
					
						
							|  |  |  |   // would have highlighted a problem where information was duplicated.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Create factor graph:
 | 
					
						
							|  |  |  |   // f(1,2,5)
 | 
					
						
							|  |  |  |   // f(3,4,5)
 | 
					
						
							|  |  |  |   // f(5,6)
 | 
					
						
							|  |  |  |   // f(6,7)
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   GaussianFactorGraph fg; | 
					
						
							| 
									
										
										
										
											2012-10-28 14:21:21 +08:00
										 |  |  |   noiseModel::Diagonal::shared_ptr model = noiseModel::Unit::Create(1); | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |   fg.add(1, (Matrix(1, 1) <<  1.0).finished(), 3, (Matrix(1, 1) <<  2.0).finished(), 5, (Matrix(1, 1) <<  3.0).finished(), (Vector(1) << 4.0).finished(), model); | 
					
						
							|  |  |  |   fg.add(1, (Matrix(1, 1) <<  5.0).finished(), (Vector(1) << 6.0).finished(), model); | 
					
						
							|  |  |  |   fg.add(2, (Matrix(1, 1) <<  7.0).finished(), 4, (Matrix(1, 1) <<  8.0).finished(), 5, (Matrix(1, 1) <<  9.0).finished(), (Vector(1) << 10.0).finished(), model); | 
					
						
							|  |  |  |   fg.add(2, (Matrix(1, 1) <<  11.0).finished(), (Vector(1) << 12.0).finished(), model); | 
					
						
							|  |  |  |   fg.add(5, (Matrix(1, 1) <<  13.0).finished(), 6, (Matrix(1, 1) <<  14.0).finished(), (Vector(1) << 15.0).finished(), model); | 
					
						
							|  |  |  |   fg.add(6, (Matrix(1, 1) <<  17.0).finished(), 7, (Matrix(1, 1) <<  18.0).finished(), (Vector(1) << 19.0).finished(), model); | 
					
						
							|  |  |  |   fg.add(7, (Matrix(1, 1) <<  20.0).finished(), (Vector(1) << 21.0).finished(), model); | 
					
						
							| 
									
										
										
										
											2012-10-28 14:21:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Eliminate into BayesTree
 | 
					
						
							|  |  |  |   // c(6,7)
 | 
					
						
							|  |  |  |   // c(5|6)
 | 
					
						
							|  |  |  |   //   c(1,2|5)
 | 
					
						
							|  |  |  |   //   c(3,4|5)
 | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  |   Ordering ordering(fg.keys()); | 
					
						
							|  |  |  |   GaussianBayesTree bt = *fg.eliminateMultifrontal(ordering); // eliminate in increasing key order, fg.keys() is sorted.
 | 
					
						
							| 
									
										
										
										
											2012-10-28 14:21:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   GaussianFactorGraph joint = *bt.joint(1,2, EliminateQR); | 
					
						
							| 
									
										
										
										
											2012-10-28 14:21:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Matrix expectedJointJ = (Matrix(2,3) << | 
					
						
							| 
									
										
										
										
											2013-08-07 02:04:37 +08:00
										 |  |  |     5, 0, 6, | 
					
						
							|  |  |  |     0, -11, -12 | 
					
						
							| 
									
										
										
										
											2012-10-28 14:21:21 +08:00
										 |  |  |     ).finished(); | 
					
						
							| 
									
										
										
										
											2018-09-28 09:19:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-28 14:21:21 +08:00
										 |  |  |   Matrix actualJointJ = joint.augmentedJacobian(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-28 15:47:17 +08:00
										 |  |  |   // PR 315: sign of rows in joint are immaterial
 | 
					
						
							|  |  |  |   if (signbit(expectedJointJ(0, 2)) != signbit(actualJointJ(0, 2))) | 
					
						
							| 
									
										
										
										
											2018-09-28 09:19:44 +08:00
										 |  |  |     expectedJointJ.row(0) = -expectedJointJ.row(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-28 15:47:17 +08:00
										 |  |  |   if (signbit(expectedJointJ(1, 2)) != signbit(actualJointJ(1, 2))) | 
					
						
							| 
									
										
										
										
											2018-09-28 09:19:44 +08:00
										 |  |  |     expectedJointJ.row(1) = -expectedJointJ.row(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-28 14:21:21 +08:00
										 |  |  |   EXPECT(assert_equal(expectedJointJ, actualJointJ)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-09 07:03:02 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr);} | 
					
						
							|  |  |  | /* ************************************************************************* */ |