| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |  *  @file   testGaussianConditional.cpp | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  *  @brief  Unit tests for Conditional gaussian | 
					
						
							|  |  |  |  *  @author Christian Potthast | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/base/TestableAssertions.h>
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_BOOST_SERIALIZATION
 | 
					
						
							|  |  |  | #include <boost/archive/text_oarchive.hpp>
 | 
					
						
							|  |  |  | #include <boost/archive/text_iarchive.hpp>
 | 
					
						
							|  |  |  | #endif //HAVE_BOOST_SERIALIZATION
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //#define GTSAM_MAGIC_KEY
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/base/Matrix.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianConditional.h>
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <sstream>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include <boost/assign/std/list.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | static const Index _x_=0, _x1_=1, _l1_=2; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(GaussianConditional, constructor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   Matrix R = Matrix_(2,2, | 
					
						
							|  |  |  |       -12.1244,  -5.1962, | 
					
						
							|  |  |  |             0.,   4.6904); | 
					
						
							|  |  |  |   Matrix S1 = Matrix_(2,2, | 
					
						
							|  |  |  |       -5.2786,  -8.6603, | 
					
						
							|  |  |  |       5.0254,   5.5432); | 
					
						
							|  |  |  |   Matrix S2 = Matrix_(2,2, | 
					
						
							|  |  |  |       -10.5573,  -5.9385, | 
					
						
							|  |  |  |       5.5737,   3.0153); | 
					
						
							|  |  |  |   Matrix S3 = Matrix_(2,2, | 
					
						
							|  |  |  |       -11.3820,  -7.2581, | 
					
						
							|  |  |  |       -3.0153,  -3.5635); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Vector d = Vector_(2, 1.0, 2.0); | 
					
						
							|  |  |  |   Vector s = Vector_(2, 3.0, 4.0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   list<pair<Index, Matrix> > terms; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   terms += | 
					
						
							|  |  |  |       make_pair(3, S1), | 
					
						
							|  |  |  |       make_pair(5, S2), | 
					
						
							|  |  |  |       make_pair(7, S3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   GaussianConditional actual(1, d, R, terms, s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   GaussianConditional::const_iterator it = actual.beginFrontals(); | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   CHECK(assert_equal(Index(1), *it)); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   CHECK(assert_equal(R, actual.get_R())); | 
					
						
							|  |  |  |   ++ it; | 
					
						
							|  |  |  |   CHECK(it == actual.endFrontals()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it = actual.beginParents(); | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   CHECK(assert_equal(Index(3), *it)); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   CHECK(assert_equal(S1, actual.get_S(it))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ++ it; | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   CHECK(assert_equal(Index(5), *it)); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   CHECK(assert_equal(S2, actual.get_S(it))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ++ it; | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   CHECK(assert_equal(Index(7), *it)); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   CHECK(assert_equal(S3, actual.get_S(it))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ++it; | 
					
						
							|  |  |  |   CHECK(it == actual.endParents()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   CHECK(assert_equal(d, actual.get_d())); | 
					
						
							|  |  |  |   CHECK(assert_equal(s, actual.get_sigmas())); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | /* unit test for equals                                                     */ | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | TEST( GaussianConditional, equals ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   // create a conditional gaussian node
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   Matrix A1(2,2); | 
					
						
							|  |  |  |   A1(0,0) = 1 ; A1(1,0) = 2; | 
					
						
							|  |  |  |   A1(0,1) = 3 ; A1(1,1) = 4; | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Matrix A2(2,2); | 
					
						
							|  |  |  |   A2(0,0) = 6 ; A2(1,0) = 0.2; | 
					
						
							|  |  |  |   A2(0,1) = 8 ; A2(1,1) = 0.4; | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Matrix R(2,2); | 
					
						
							|  |  |  |   R(0,0) = 0.1 ; R(1,0) = 0.3; | 
					
						
							|  |  |  |   R(0,1) = 0.0 ; R(1,1) = 0.34; | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   Vector tau(2); | 
					
						
							|  |  |  |   tau(0) = 1.0; | 
					
						
							|  |  |  |   tau(1) = 0.34; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   Vector d(2); | 
					
						
							|  |  |  |   d(0) = 0.2; d(1) = 0.5; | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |   GaussianConditional  | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     expected(_x_,d, R, _x1_, A1, _l1_, A2, tau), | 
					
						
							|  |  |  |     actual(_x_,d, R, _x1_, A1, _l1_, A2, tau); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    | 
					
						
							|  |  |  |   CHECK( expected.equals(actual) ); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | /* unit test for solve                                                      */ | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | TEST( GaussianConditional, solve ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   //expected solution
 | 
					
						
							|  |  |  |   Vector expected(2); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   expected(0) = 20-3-11 ; expected(1) = 40-7-15; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    | 
					
						
							|  |  |  |   // create a conditional gaussion node
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   Matrix R = Matrix_(2,2,   1., 0., | 
					
						
							|  |  |  |                             0., 1.); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   Matrix A1 = Matrix_(2,2,  1., 2., | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |                             3., 4.); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   Matrix A2 = Matrix_(2,2,  5., 6., | 
					
						
							|  |  |  |                             7., 8.); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    | 
					
						
							|  |  |  |   Vector d(2); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   d(0) = 20.0; d(1) = 40.0; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   Vector tau = ones(2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   GaussianConditional cg(_x_,d, R, _x1_, A1, _l1_, A2, tau); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    | 
					
						
							|  |  |  |   Vector sx1(2); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   sx1(0) = 1.0; sx1(1) = 1.0; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    | 
					
						
							|  |  |  |   Vector sl1(2); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   sl1(0) = 1.0; sl1(1) = 1.0; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  |   VectorValues solution(vector<size_t>(3, 2)); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   solution[_x1_] = sx1; | 
					
						
							|  |  |  |   solution[_l1_] = sl1; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    | 
					
						
							|  |  |  |   Vector result = cg.solve(solution); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   CHECK(assert_equal(expected , result, 0.0001)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | /* unit test for serialization                                               */  | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | #ifdef HAVE_BOOST_SERIALIZATION
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | TEST( GaussianConditional, serialize ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	 // create a conditional gaussion node
 | 
					
						
							|  |  |  | 	 Matrix A1(2,2); | 
					
						
							|  |  |  | 	 A1(0,0) = 1 ; A1(1,0) = 2; | 
					
						
							|  |  |  | 	 A1(0,1) = 3 ; A1(1,1) = 4; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	 Matrix A2(2,2); | 
					
						
							|  |  |  | 	 A2(0,0) = 6 ; A2(1,0) = 0.2; | 
					
						
							|  |  |  | 	 A2(0,1) = 8 ; A2(1,1) = 0.4; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	 Matrix R(2,2); | 
					
						
							|  |  |  | 	 R(0,0) = 0.1 ; R(1,0) = 0.3; | 
					
						
							|  |  |  | 	 R(0,1) = 0.0 ; R(1,1) = 0.34; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	 Vector d(2); | 
					
						
							|  |  |  | 	 d(0) = 0.2; d(1) = 0.5; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	 GaussianConditional cg(_x2_, d, R, _x1_, A1, _l1_, A2); | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	 //serialize the CG
 | 
					
						
							|  |  |  | 	 std::ostringstream in_archive_stream; | 
					
						
							|  |  |  | 	 boost::archive::text_oarchive in_archive(in_archive_stream); | 
					
						
							|  |  |  | 	 in_archive << cg; | 
					
						
							|  |  |  | 	 std::string serialized = in_archive_stream.str(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	 //deserialize the CGg
 | 
					
						
							|  |  |  | 	 std::istringstream out_archive_stream(serialized); | 
					
						
							|  |  |  | 	 boost::archive::text_iarchive out_archive(out_archive_stream); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	 GaussianConditional output; | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	 out_archive >> output; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	 //check for equality
 | 
					
						
							|  |  |  | 	 CHECK(cg.equals(output)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | #endif //HAVE_BOOST_SERIALIZATION
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr);} | 
					
						
							|  |  |  | /* ************************************************************************* */ |