| 
									
										
										
										
											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 | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*STL/C++*/ | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <sstream>
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_BOOST_SERIALIZATION
 | 
					
						
							|  |  |  | #include <boost/archive/text_oarchive.hpp>
 | 
					
						
							|  |  |  | #include <boost/archive/text_iarchive.hpp>
 | 
					
						
							|  |  |  | #endif //HAVE_BOOST_SERIALIZATION
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | #define GTSAM_MAGIC_KEY
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include "Matrix.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | #include "GaussianConditional.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											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  | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +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
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   VectorConfig solution; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   solution.insert("x1", sx1); | 
					
						
							|  |  |  |   solution.insert("l1", sl1); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +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);} | 
					
						
							|  |  |  | /* ************************************************************************* */ |