| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |  *  @file   testGaussianFactorGraph.cpp | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  *  @brief  Unit tests for Linear Factor Graph | 
					
						
							|  |  |  |  *  @author Christian Potthast | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | #include <boost/foreach.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include <boost/tuple/tuple.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  | #include <boost/assign/std/list.hpp> // for operator +=
 | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include "Matrix.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | #include "Ordering.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include "smallExample.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | #include "GaussianBayesNet.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  | #include "inference-inl.h" // needed for eliminate and marginals
 | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  | double tol=1e-4; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | /* unit test for equals (GaussianFactorGraph1 == GaussianFactorGraph2)           */ | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, equals ){ | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							|  |  |  |   GaussianFactorGraph fg2 = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  |   CHECK(fg.equals(fg2)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, error ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   VectorConfig cfg = createZeroDelta(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // note the error is the same as in testNonlinearFactorGraph as a
 | 
					
						
							|  |  |  |   // zero delta config in the linear graph is equivalent to noisy in
 | 
					
						
							|  |  |  |   // non-linear, which is really linear under the hood
 | 
					
						
							|  |  |  |   double actual = fg.error(cfg); | 
					
						
							|  |  |  |   DOUBLES_EQUAL( 5.625, actual, 1e-9 ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-10 12:36:07 +08:00
										 |  |  | /* unit test for find seperator                                              */ | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, find_separator ) | 
					
						
							| 
									
										
										
										
											2009-11-10 12:36:07 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   set<string> separator = fg.find_separator("x2"); | 
					
						
							|  |  |  |   set<string> expected; | 
					
						
							|  |  |  |   expected.insert("x1"); | 
					
						
							|  |  |  |   expected.insert("l1"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   CHECK(separator.size()==expected.size()); | 
					
						
							|  |  |  |   set<string>::iterator it1 = separator.begin(), it2 = expected.begin(); | 
					
						
							|  |  |  |   for(; it1!=separator.end(); it1++, it2++) | 
					
						
							|  |  |  |     CHECK(*it1 == *it2); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, combine_factors_x1 ) | 
					
						
							| 
									
										
										
										
											2009-11-10 12:36:07 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   // create a small example for a linear factor graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   // create sigmas
 | 
					
						
							|  |  |  |   double sigma1 = 0.1; | 
					
						
							|  |  |  |   double sigma2 = 0.1; | 
					
						
							|  |  |  |   double sigma3 = 0.2; | 
					
						
							|  |  |  |   Vector sigmas = Vector_(6, sigma1, sigma1, sigma2, sigma2, sigma3, sigma3); | 
					
						
							| 
									
										
										
										
											2009-11-10 12:36:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // combine all factors
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactor::shared_ptr actual = removeAndCombineFactors(fg,"x1"); | 
					
						
							| 
									
										
										
										
											2009-10-15 05:06:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   // the expected linear factor
 | 
					
						
							|  |  |  |   Matrix Al1 = Matrix_(6,2, | 
					
						
							|  |  |  | 			 0., 0., | 
					
						
							|  |  |  | 			 0., 0., | 
					
						
							|  |  |  | 			 0., 0., | 
					
						
							|  |  |  | 			 0., 0., | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 			 1., 0., | 
					
						
							|  |  |  | 			 0., 1. | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 			 ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Matrix Ax1 = Matrix_(6,2, | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 			 1.,   0., | 
					
						
							|  |  |  | 			 0.00, 1., | 
					
						
							|  |  |  | 			 -1.,  0., | 
					
						
							|  |  |  | 			 0.00,-1., | 
					
						
							|  |  |  | 			 -1.,   0., | 
					
						
							|  |  |  | 			 00.,  -1. | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 			 ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Matrix Ax2 = Matrix_(6,2, | 
					
						
							|  |  |  | 			 0., 0., | 
					
						
							|  |  |  | 			 0., 0., | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 			 1., 0., | 
					
						
							|  |  |  | 			 +0.,1., | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 			 0., 0., | 
					
						
							|  |  |  | 			 0., 0. | 
					
						
							|  |  |  | 			 ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // the expected RHS vector
 | 
					
						
							|  |  |  |   Vector b(6); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   b(0) = -1*sigma1; | 
					
						
							|  |  |  |   b(1) = -1*sigma1; | 
					
						
							|  |  |  |   b(2) =  2*sigma2; | 
					
						
							|  |  |  |   b(3) = -1*sigma2; | 
					
						
							|  |  |  |   b(4) =  0*sigma3; | 
					
						
							|  |  |  |   b(5) =  1*sigma3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   vector<pair<string, Matrix> > meas; | 
					
						
							|  |  |  |   meas.push_back(make_pair("l1", Al1)); | 
					
						
							|  |  |  |   meas.push_back(make_pair("x1", Ax1)); | 
					
						
							|  |  |  |   meas.push_back(make_pair("x2", Ax2)); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactor expected(meas, b, sigmas); | 
					
						
							|  |  |  |   //GaussianFactor expected("l1", Al1, "x1", Ax1, "x2", Ax2, b);
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // check if the two factors are the same
 | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  |   CHECK(assert_equal(expected,*actual)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, combine_factors_x2 ) | 
					
						
							| 
									
										
										
										
											2009-11-10 12:36:07 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  // create a small example for a linear factor graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   // determine sigmas
 | 
					
						
							|  |  |  |   double sigma1 = 0.1; | 
					
						
							|  |  |  |   double sigma2 = 0.2; | 
					
						
							|  |  |  |   Vector sigmas = Vector_(4, sigma1, sigma1, sigma2, sigma2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   // combine all factors
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactor::shared_ptr actual = removeAndCombineFactors(fg,"x2"); | 
					
						
							| 
									
										
										
										
											2009-10-15 05:06:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   // the expected linear factor
 | 
					
						
							|  |  |  |   Matrix Al1 = Matrix_(4,2, | 
					
						
							|  |  |  | 			 // l1
 | 
					
						
							|  |  |  | 			 0., 0., | 
					
						
							|  |  |  | 			 0., 0., | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 			 1., 0., | 
					
						
							|  |  |  | 			 0., 1. | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 			 ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Matrix Ax1 = Matrix_(4,2, | 
					
						
							|  |  |  |                          // x1
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 			 -1.,  0.,  // f2
 | 
					
						
							|  |  |  | 			 0.00,-1.,  // f2
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 			 0.00,  0., // f4
 | 
					
						
							|  |  |  | 			 0.00,  0.  // f4
 | 
					
						
							|  |  |  | 			 ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Matrix Ax2 = Matrix_(4,2, | 
					
						
							|  |  |  | 			 // x2
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 			 1., 0., | 
					
						
							|  |  |  | 			 +0.,1., | 
					
						
							|  |  |  | 			 -1., 0., | 
					
						
							|  |  |  | 			 +0.,-1. | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 			 ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // the expected RHS vector
 | 
					
						
							|  |  |  |   Vector b(4); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   b(0) = 2*sigma1; | 
					
						
							|  |  |  |   b(1) = -1*sigma1; | 
					
						
							|  |  |  |   b(2) = -1*sigma2; | 
					
						
							|  |  |  |   b(3) = 1.5*sigma2; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   vector<pair<string, Matrix> > meas; | 
					
						
							|  |  |  |   meas.push_back(make_pair("l1", Al1)); | 
					
						
							|  |  |  |   meas.push_back(make_pair("x1", Ax1)); | 
					
						
							|  |  |  |   meas.push_back(make_pair("x2", Ax2)); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactor expected(meas, b, sigmas); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // check if the two factors are the same
 | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  |   CHECK(assert_equal(expected,*actual)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, eliminateOne_x1 ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |   GaussianConditional::shared_ptr actual = fg.eliminateOne("x1"); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // create expected Conditional Gaussian
 | 
					
						
							| 
									
										
										
										
											2009-11-12 14:09:03 +08:00
										 |  |  |   Matrix I = eye(2), R11 = I, S12 = -0.111111*I, S13 = -0.444444*I; | 
					
						
							|  |  |  |   Vector d = Vector_(2, -0.133333, -0.0222222), sigma = repeat(2, 1./15); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |   GaussianConditional expected("x1",d,R11,"l1",S12,"x2",S13,sigma); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  |   CHECK(assert_equal(expected,*actual,tol)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-10 12:36:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, eliminateOne_x2 ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |   GaussianConditional::shared_ptr actual = fg.eliminateOne("x2"); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // create expected Conditional Gaussian
 | 
					
						
							| 
									
										
										
										
											2009-11-12 14:09:03 +08:00
										 |  |  |   Matrix I = eye(2), R11 = I, S12 = -0.2*I, S13 = -0.8*I; | 
					
						
							|  |  |  |   Vector d = Vector_(2, 0.2, -0.14), sigma = repeat(2, 0.0894427); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |   GaussianConditional expected("x2",d,R11,"l1",S12,"x1",S13,sigma); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  |   CHECK(assert_equal(expected,*actual,tol)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, eliminateOne_l1 ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |   GaussianConditional::shared_ptr actual = fg.eliminateOne("l1"); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // create expected Conditional Gaussian
 | 
					
						
							| 
									
										
										
										
											2009-11-12 14:09:03 +08:00
										 |  |  |   Matrix I = eye(2), R11 = I, S12 = -0.5*I, S13 = -0.5*I; | 
					
						
							|  |  |  |   Vector d = Vector_(2, -0.1, 0.25), sigma = repeat(2, 0.141421); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |   GaussianConditional expected("l1",d,R11,"x1",S12,"x2",S13,sigma); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  |   CHECK(assert_equal(expected,*actual,tol)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, eliminateAll ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // create expected Chordal bayes Net
 | 
					
						
							| 
									
										
										
										
											2009-11-12 14:09:03 +08:00
										 |  |  |   Matrix I = eye(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Vector d1 = Vector_(2, -0.1,-0.1); | 
					
						
							|  |  |  |   GaussianBayesNet expected = simpleGaussian("x1",d1,0.1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Vector d2 = Vector_(2, 0.0, 0.2), sigma2 = repeat(2,0.149071); | 
					
						
							|  |  |  |   push_front(expected,"l1",d2, I,"x1", (-1)*I,sigma2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Vector d3 = Vector_(2, 0.2, -0.14), sigma3 = repeat(2,0.0894427); | 
					
						
							|  |  |  |   push_front(expected,"x2",d3, I,"l1", (-0.2)*I, "x1", (-0.8)*I, sigma3); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   // Check one ordering
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg1 = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-12 14:09:03 +08:00
										 |  |  |   Ordering ordering; | 
					
						
							|  |  |  |   ordering += "x2","l1","x1"; | 
					
						
							|  |  |  |   GaussianBayesNet actual = fg1.eliminate(ordering); | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  |   CHECK(assert_equal(expected,actual,tol)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, add_priors ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							|  |  |  |   GaussianFactorGraph actual = fg.add_priors(3); | 
					
						
							|  |  |  |   GaussianFactorGraph expected = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   Matrix A = eye(2); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   Vector b = zero(2); | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  |   double sigma = 3.0; | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   expected.push_back(GaussianFactor::shared_ptr(new GaussianFactor("l1",A,b,sigma))); | 
					
						
							|  |  |  |   expected.push_back(GaussianFactor::shared_ptr(new GaussianFactor("x1",A,b,sigma))); | 
					
						
							|  |  |  |   expected.push_back(GaussianFactor::shared_ptr(new GaussianFactor("x2",A,b,sigma))); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   CHECK(assert_equal(expected,actual)); // Fails
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, copying ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Create a graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph actual = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Copy the graph !
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph copy = actual; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // now eliminate the copy
 | 
					
						
							|  |  |  |   Ordering ord1; | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  |   ord1 += "x2","l1","x1"; | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  |   GaussianBayesNet actual1 = copy.eliminate(ord1); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create the same graph, but not by copying
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph expected = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // and check that original is still the same graph
 | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  |   CHECK(assert_equal(expected,actual)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, matrix ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Create a graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // render with a given ordering
 | 
					
						
							|  |  |  |   Ordering ord; | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  |   ord += "x2","l1","x1"; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Matrix A; Vector b; | 
					
						
							|  |  |  |   boost::tie(A,b) = fg.matrix(ord); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Matrix A1 = Matrix_(2*4,3*2, | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 		     +0.,  0.,  0.,  0., 10.,  0., // unary factor on x1 (prior)
 | 
					
						
							|  |  |  | 		     +0.,  0.,  0.,  0.,  0., 10., | 
					
						
							|  |  |  | 		     10.,  0.,  0.,  0.,-10.,  0., // binary factor on x2,x1 (odometry)
 | 
					
						
							|  |  |  | 		     +0., 10.,  0.,  0.,  0.,-10., | 
					
						
							|  |  |  | 		     +0.,  0.,  5.,  0., -5.,  0., // binary factor on l1,x1 (z1)
 | 
					
						
							|  |  |  | 		     +0.,  0.,  0.,  5.,  0., -5., | 
					
						
							|  |  |  | 		     -5.,  0.,  5.,  0.,  0.,  0., // binary factor on x2,l1 (z2)
 | 
					
						
							|  |  |  | 		     +0., -5.,  0.,  5.,  0.,  0. | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  |   Vector b1 = Vector_(8,-1., -1., 2., -1., 0., 1., -1., 1.5); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  |   EQUALITY(A,A1); | 
					
						
							|  |  |  |   CHECK(b==b1); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, sparse ) | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// create a small linear factor graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// render with a given ordering
 | 
					
						
							|  |  |  | 	Ordering ord; | 
					
						
							|  |  |  |   ord += "x2","l1","x1"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Matrix ijs = fg.sparse(ord); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	EQUALITY(ijs, Matrix_(3, 14, | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 		// f(x1)   f(x2,x1)            f(l1,x1)         f(x2,l1)
 | 
					
						
							|  |  |  | 		+1., 2.,   3., 4.,  3.,  4.,   5.,6., 5., 6.,   7., 8.,7.,8., | 
					
						
							|  |  |  | 		+5., 6.,   1., 2.,  5.,  6.,   3.,4., 5., 6.,   1., 2.,3.,4., | 
					
						
							|  |  |  | 		10.,10.,  10.,10.,-10.,-10.,   5.,5.,-5.,-5.,  -5.,-5.,5.,5.)); | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, CONSTRUCTOR_GaussianBayesNet ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // render with a given ordering
 | 
					
						
							|  |  |  |   Ordering ord; | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  |   ord += "x2","l1","x1"; | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  |   GaussianBayesNet CBN = fg.eliminate(ord); | 
					
						
							| 
									
										
										
										
											2009-11-05 12:56:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   // True GaussianFactorGraph
 | 
					
						
							|  |  |  |   GaussianFactorGraph fg2(CBN); | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  |   GaussianBayesNet CBN2 = fg2.eliminate(ord); | 
					
						
							|  |  |  |   CHECK(assert_equal(CBN,CBN2)); | 
					
						
							| 
									
										
										
										
											2009-11-05 12:56:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Base FactorGraph only
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   FactorGraph<GaussianFactor> fg3(CBN); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |   GaussianBayesNet CBN3 = gtsam::eliminate<GaussianFactor,GaussianConditional>(fg3,ord); | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  |   CHECK(assert_equal(CBN,CBN3)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, GET_ORDERING) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  |   Ordering expected; | 
					
						
							|  |  |  |   expected += "l1","x1","x2"; | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  |   Ordering actual = fg.getOrdering(); | 
					
						
							|  |  |  |   CHECK(assert_equal(expected,actual)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, OPTIMIZE ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// create a graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create an ordering
 | 
					
						
							|  |  |  | 	Ordering ord = fg.getOrdering(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// optimize the graph
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | 	VectorConfig actual = fg.optimize(ord); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// verify
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | 	VectorConfig expected = createCorrectDelta(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  |   CHECK(assert_equal(expected,actual)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, COMBINE_GRAPHS_INPLACE) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 	// create a test graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg1 = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create another factor graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg2 = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// get sizes
 | 
					
						
							|  |  |  | 	int size1 = fg1.size(); | 
					
						
							|  |  |  | 	int size2 = fg2.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// combine them
 | 
					
						
							|  |  |  | 	fg1.combine(fg2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(size1+size2 == fg1.size()); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, COMBINE_GRAPHS) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 	// create a test graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg1 = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create another factor graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg2 = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// get sizes
 | 
					
						
							|  |  |  | 	int size1 = fg1.size(); | 
					
						
							|  |  |  | 	int size2 = fg2.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// combine them
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg3 = GaussianFactorGraph::combine2(fg1, fg2); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(size1+size2 == fg3.size()); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-10-22 13:02:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // print a vector of ints if needed for debugging
 | 
					
						
							|  |  |  | void print(vector<int> v) { | 
					
						
							|  |  |  | 	for (int k = 0; k < v.size(); k++) | 
					
						
							|  |  |  | 		cout << v[k] << " "; | 
					
						
							|  |  |  | 	cout << endl; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, factor_lookup) | 
					
						
							| 
									
										
										
										
											2009-10-22 13:02:31 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// create a test graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-10-22 13:02:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// ask for all factor indices connected to x1
 | 
					
						
							| 
									
										
										
										
											2009-10-23 05:33:00 +08:00
										 |  |  | 	list<int> x1_factors = fg.factors("x1"); | 
					
						
							| 
									
										
										
										
											2009-10-22 13:02:31 +08:00
										 |  |  | 	int x1_indices[] = { 0, 1, 2 }; | 
					
						
							| 
									
										
										
										
											2009-10-23 05:33:00 +08:00
										 |  |  | 	list<int> x1_expected(x1_indices, x1_indices + 3); | 
					
						
							| 
									
										
										
										
											2009-10-22 13:02:31 +08:00
										 |  |  | 	CHECK(x1_factors==x1_expected); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// ask for all factor indices connected to x2
 | 
					
						
							| 
									
										
										
										
											2009-10-23 05:33:00 +08:00
										 |  |  | 	list<int> x2_factors = fg.factors("x2"); | 
					
						
							| 
									
										
										
										
											2009-10-22 13:02:31 +08:00
										 |  |  | 	int x2_indices[] = { 1, 3 }; | 
					
						
							| 
									
										
										
										
											2009-10-23 05:33:00 +08:00
										 |  |  | 	list<int> x2_expected(x2_indices, x2_indices + 2); | 
					
						
							| 
									
										
										
										
											2009-10-22 13:02:31 +08:00
										 |  |  | 	CHECK(x2_factors==x2_expected); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-22 21:28:37 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, findAndRemoveFactors ) | 
					
						
							| 
									
										
										
										
											2009-10-22 21:28:37 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// create the graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-10-22 21:28:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // We expect to remove these three factors: 0, 1, 2
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactor::shared_ptr f0 = fg[0]; | 
					
						
							|  |  |  |   GaussianFactor::shared_ptr f1 = fg[1]; | 
					
						
							|  |  |  |   GaussianFactor::shared_ptr f2 = fg[2]; | 
					
						
							| 
									
										
										
										
											2009-10-22 21:28:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // call the function
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   vector<GaussianFactor::shared_ptr> factors = fg.findAndRemoveFactors("x1"); | 
					
						
							| 
									
										
										
										
											2009-10-22 21:28:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Check the factors
 | 
					
						
							|  |  |  |   CHECK(f0==factors[0]); | 
					
						
							|  |  |  |   CHECK(f1==factors[1]); | 
					
						
							|  |  |  |   CHECK(f2==factors[2]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // CHECK if the factors are deleted from the factor graph
 | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  |   LONGS_EQUAL(1,fg.nrFactors()); | 
					
						
							| 
									
										
										
										
											2009-10-22 21:28:37 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-23 08:56:40 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, findAndRemoveFactors_twice ) | 
					
						
							| 
									
										
										
										
											2009-10-23 08:56:40 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// create the graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-10-23 08:56:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // We expect to remove these three factors: 0, 1, 2
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactor::shared_ptr f0 = fg[0]; | 
					
						
							|  |  |  |   GaussianFactor::shared_ptr f1 = fg[1]; | 
					
						
							|  |  |  |   GaussianFactor::shared_ptr f2 = fg[2]; | 
					
						
							| 
									
										
										
										
											2009-10-23 08:56:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // call the function
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   vector<GaussianFactor::shared_ptr> factors = fg.findAndRemoveFactors("x1"); | 
					
						
							| 
									
										
										
										
											2009-10-23 08:56:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Check the factors
 | 
					
						
							|  |  |  |   CHECK(f0==factors[0]); | 
					
						
							|  |  |  |   CHECK(f1==factors[1]); | 
					
						
							|  |  |  |   CHECK(f2==factors[2]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 13:39:13 +08:00
										 |  |  |   factors = fg.findAndRemoveFactors("x1"); | 
					
						
							| 
									
										
										
										
											2009-10-23 08:56:40 +08:00
										 |  |  |   CHECK(factors.size() == 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // CHECK if the factors are deleted from the factor graph
 | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  |   LONGS_EQUAL(1,fg.nrFactors()); | 
					
						
							| 
									
										
										
										
											2009-10-23 08:56:40 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST(GaussianFactorGraph, createSmoother) | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg1 = createSmoother(2); | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | 	LONGS_EQUAL(3,fg1.size()); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg2 = createSmoother(3); | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | 	LONGS_EQUAL(5,fg2.size()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, variables ) | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  |   Dimensions expected; | 
					
						
							|  |  |  |   insert(expected)("l1", 2)("x1", 2)("x2", 2); | 
					
						
							|  |  |  |   Dimensions actual = fg.dimensions(); | 
					
						
							|  |  |  |   CHECK(expected==actual); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, keys ) | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  |   Ordering expected; | 
					
						
							|  |  |  |   expected += "l1","x1","x2"; | 
					
						
							|  |  |  |   CHECK(assert_equal(expected,fg.keys())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 13:14:34 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( GaussianFactorGraph, involves ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GaussianFactorGraph fg = createGaussianFactorGraph(); | 
					
						
							|  |  |  |   CHECK(fg.involves("l1")); | 
					
						
							|  |  |  |   CHECK(fg.involves("x1")); | 
					
						
							|  |  |  |   CHECK(fg.involves("x2")); | 
					
						
							|  |  |  |   CHECK(!fg.involves("x3")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 05:34:20 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | // Tests ported from ConstrainedGaussianFactorGraph
 | 
					
						
							| 
									
										
										
										
											2009-11-10 12:36:07 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-10 05:34:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 12:36:07 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, constrained_simple ) | 
					
						
							| 
									
										
										
										
											2009-11-10 12:36:07 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// get a graph with a constraint in it
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg = createSimpleConstraintGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-10 12:36:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// eliminate and solve
 | 
					
						
							|  |  |  | 	Ordering ord; | 
					
						
							|  |  |  | 	ord += "x", "y"; | 
					
						
							|  |  |  | 	VectorConfig actual = fg.optimize(ord); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// verify
 | 
					
						
							|  |  |  | 	VectorConfig expected = createSimpleConstraintConfig(); | 
					
						
							|  |  |  | 	CHECK(assert_equal(actual, expected)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-11 22:42:09 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, constrained_single ) | 
					
						
							| 
									
										
										
										
											2009-11-11 22:42:09 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// get a graph with a constraint in it
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg = createSingleConstraintGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-11 22:42:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// eliminate and solve
 | 
					
						
							|  |  |  | 	Ordering ord; | 
					
						
							|  |  |  | 	ord += "x", "y"; | 
					
						
							|  |  |  | 	VectorConfig actual = fg.optimize(ord); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// verify
 | 
					
						
							|  |  |  | 	VectorConfig expected = createSingleConstraintConfig(); | 
					
						
							|  |  |  | 	CHECK(assert_equal(actual, expected)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, constrained_single2 ) | 
					
						
							| 
									
										
										
										
											2009-11-11 22:42:09 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// get a graph with a constraint in it
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg = createSingleConstraintGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-11 22:42:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// eliminate and solve
 | 
					
						
							|  |  |  | 	Ordering ord; | 
					
						
							|  |  |  | 	ord += "y", "x"; | 
					
						
							|  |  |  | 	VectorConfig actual = fg.optimize(ord); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// verify
 | 
					
						
							|  |  |  | 	VectorConfig expected = createSingleConstraintConfig(); | 
					
						
							|  |  |  | 	CHECK(assert_equal(actual, expected)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, constrained_multi1 ) | 
					
						
							| 
									
										
										
										
											2009-11-11 22:42:09 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// get a graph with a constraint in it
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg = createMultiConstraintGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-11 22:42:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// eliminate and solve
 | 
					
						
							|  |  |  | 	Ordering ord; | 
					
						
							|  |  |  | 	ord += "x", "y", "z"; | 
					
						
							|  |  |  | 	VectorConfig actual = fg.optimize(ord); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// verify
 | 
					
						
							|  |  |  | 	VectorConfig expected = createMultiConstraintConfig(); | 
					
						
							|  |  |  | 	CHECK(assert_equal(actual, expected)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-11-10 05:34:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-12 03:00:39 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST( GaussianFactorGraph, constrained_multi2 ) | 
					
						
							| 
									
										
										
										
											2009-11-12 03:00:39 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// get a graph with a constraint in it
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg = createMultiConstraintGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-12 03:00:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// eliminate and solve
 | 
					
						
							|  |  |  | 	Ordering ord; | 
					
						
							|  |  |  | 	ord += "z", "x", "y"; | 
					
						
							|  |  |  | 	VectorConfig actual = fg.optimize(ord); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// verify
 | 
					
						
							|  |  |  | 	VectorConfig expected = createMultiConstraintConfig(); | 
					
						
							|  |  |  | 	CHECK(assert_equal(actual, expected)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr);} | 
					
						
							|  |  |  | /* ************************************************************************* */ |