| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  |  * @file    testGaussianBayesNet.cpp | 
					
						
							|  |  |  |  * @brief   Unit tests for GaussianBayesNet | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // STL/C++
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <sstream>
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | #include <boost/tuple/tuple.hpp>
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | #include <boost/assign/std/list.hpp> // for operator +=
 | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | #include "GaussianBayesNet.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  | #include "BayesNet.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include "smallExample.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | #include "Ordering.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | TEST( GaussianBayesNet, constructor ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // small Bayes Net x <- y
 | 
					
						
							|  |  |  |   // x y d
 | 
					
						
							|  |  |  |   // 1 1 9
 | 
					
						
							|  |  |  |   //   1 5
 | 
					
						
							|  |  |  |   Matrix R11 = Matrix_(1,1,1.0), S12 = Matrix_(1,1,1.0); | 
					
						
							|  |  |  |   Matrix                         R22 = Matrix_(1,1,1.0); | 
					
						
							|  |  |  |   Vector d1(1), d2(1); | 
					
						
							|  |  |  |   d1(0) = 9; d2(0) = 5; | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  |   Vector sigmas(1); | 
					
						
							|  |  |  |   sigmas(0) = 1.; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    | 
					
						
							|  |  |  |   // define nodes and specify in reverse topological sort (i.e. parents last)
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |   GaussianConditional x("x",d1,R11,"y",S12, sigmas), y("y",d2,R22, sigmas); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // check small example which uses constructor
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  |   GaussianBayesNet cbn = createSmallGaussianBayesNet(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   CHECK( x.equals(*cbn["x"]) ); | 
					
						
							|  |  |  |   CHECK( y.equals(*cbn["y"]) ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | TEST( GaussianBayesNet, matrix ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Create a test graph
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  |   GaussianBayesNet cbn = createSmallGaussianBayesNet(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Matrix R; Vector d; | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  |   boost::tie(R,d) = matrix(cbn); // find matrix and RHS
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Matrix R1 = Matrix_(2,2, | 
					
						
							|  |  |  | 		      1.0, 1.0, | 
					
						
							|  |  |  | 		      0.0, 1.0 | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   Vector d1 = Vector_(2, 9.0, 5.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   EQUALITY(R,R1); | 
					
						
							|  |  |  |   CHECK(d==d1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | TEST( GaussianBayesNet, optimize ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  |   GaussianBayesNet cbn = createSmallGaussianBayesNet(); | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  |   VectorConfig actual = optimize(cbn); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   VectorConfig expected; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   Vector x(1), y(1); | 
					
						
							|  |  |  |   x(0) = 4; y(0) = 5; | 
					
						
							|  |  |  |   expected.insert("x",x); | 
					
						
							|  |  |  |   expected.insert("y",y); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  |   CHECK(assert_equal(expected,actual)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | #ifdef HAVE_BOOST_SERIALIZATION
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | TEST( GaussianBayesNet, serialize ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	//create a starting CBN
 | 
					
						
							|  |  |  | 	GaussianBayesNet cbn = createSmallGaussianBayesNet(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//serialize the CBN
 | 
					
						
							|  |  |  | 	ostringstream in_archive_stream; | 
					
						
							|  |  |  | 	boost::archive::text_oarchive in_archive(in_archive_stream); | 
					
						
							|  |  |  | 	in_archive << cbn; | 
					
						
							|  |  |  | 	string serialized = in_archive_stream.str(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//DEBUG
 | 
					
						
							|  |  |  | 	cout << "CBN Raw string: [" << serialized << "]" << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//remove newlines/carriage returns
 | 
					
						
							|  |  |  | 	string clean; | 
					
						
							|  |  |  | 	BOOST_FOREACH(char s, serialized) { | 
					
						
							|  |  |  | 		if (s != '\n') { | 
					
						
							|  |  |  | 			//copy in character
 | 
					
						
							|  |  |  | 			clean.append(string(1,s)); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			cout << "   Newline character found!" << endl; | 
					
						
							|  |  |  | 			//replace with an identifiable string
 | 
					
						
							|  |  |  | 			clean.append(string(1,' ')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cout << "Cleaned CBN String: [" << clean << "]" << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//deserialize the CBN
 | 
					
						
							|  |  |  | 	istringstream out_archive_stream(clean); | 
					
						
							|  |  |  | 	boost::archive::text_iarchive out_archive(out_archive_stream); | 
					
						
							|  |  |  | 	GaussianBayesNet output; | 
					
						
							|  |  |  | 	out_archive >> output; | 
					
						
							|  |  |  | 	CHECK(cbn.equals(output)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | #endif //HAVE_BOOST_SERIALIZATION
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr);} | 
					
						
							|  |  |  | /* ************************************************************************* */ |