| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  *  @file  testNonlinearFactor.cpp | 
					
						
							|  |  |  |  *  @brief Unit tests for Non-Linear Factor,  | 
					
						
							|  |  |  |  *  create a non linear factor graph and a configuration for it and | 
					
						
							|  |  |  |  *  calculate the error for the factor. | 
					
						
							|  |  |  |  *  @author Christian Potthast | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*STL/C++*/ | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Matrix.h"
 | 
					
						
							|  |  |  | #include "smallExample.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-25 04:01:47 +08:00
										 |  |  | #include "Simulated2DMeasurement.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | typedef boost::shared_ptr<NonlinearFactor<VectorConfig> > shared_nlf; | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 04:01:47 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:47:12 +08:00
										 |  |  | TEST( NonlinearFactor, equals ) | 
					
						
							| 
									
										
										
										
											2009-10-25 04:01:47 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	double sigma = 1.0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create two nonlinear2 factors
 | 
					
						
							|  |  |  | 	Vector z3(2); z3(0) = 0. ; z3(1) = -1.; | 
					
						
							|  |  |  | 	Simulated2DMeasurement f0(z3, sigma, "x1", "l1"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// measurement between x2 and l1
 | 
					
						
							|  |  |  | 	Vector z4(2); z4(0)= -1.5 ; z4(1) = -1.; | 
					
						
							|  |  |  | 	Simulated2DMeasurement f1(z4, sigma, "x2", "l1"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(assert_equal(f0,f0)); | 
					
						
							|  |  |  | 	CHECK(f0.equals(f0)); | 
					
						
							|  |  |  | 	CHECK(!f0.equals(f1)); | 
					
						
							|  |  |  | 	CHECK(!f1.equals(f0)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:47:12 +08:00
										 |  |  | TEST( NonlinearFactor, equals2 ) | 
					
						
							| 
									
										
										
										
											2009-10-25 04:01:47 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // create a non linear factor graph
 | 
					
						
							|  |  |  |   ExampleNonlinearFactorGraph fg = createNonlinearFactorGraph(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // get two factors
 | 
					
						
							|  |  |  |   shared_nlf f0 = fg[0], f1 = fg[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   CHECK(f0->equals(*f0)); | 
					
						
							|  |  |  |   CHECK(!f0->equals(*f1)); | 
					
						
							|  |  |  |   CHECK(!f1->equals(*f0)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:47:12 +08:00
										 |  |  | TEST( NonlinearFactor, NonlinearFactor ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // create a non linear factor graph
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  |   ExampleNonlinearFactorGraph fg = createNonlinearFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // create a configuration for the non linear factor graph
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   VectorConfig cfg = createNoisyConfig(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // get the factor "f1" from the factor graph
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  |   shared_nlf factor = fg[0]; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // calculate the error_vector from the factor "f1"
 | 
					
						
							|  |  |  |   Vector actual_e = factor->error_vector(cfg); | 
					
						
							|  |  |  |   Vector e(2); e(0) = -0.1;  e(1) = -0.1; | 
					
						
							|  |  |  |   CHECK(assert_equal(e,actual_e)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // the expected value for the error from the factor
 | 
					
						
							|  |  |  |   // error_vector / sigma = [0.1 0.1]/0.1 = [1;1]
 | 
					
						
							|  |  |  |   // error = 0.5 * [1 1] * [1;1] = 1
 | 
					
						
							|  |  |  |   double expected = 1.0;  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // calculate the error from the factor "f1"
 | 
					
						
							|  |  |  |   double actual = factor->error(cfg); | 
					
						
							|  |  |  |   DOUBLES_EQUAL(expected,actual,0.00000001); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:47:12 +08:00
										 |  |  | TEST( NonlinearFactor, linearize_f1 ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Grab a non-linear factor
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  |   ExampleNonlinearFactorGraph nfg = createNonlinearFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   boost::shared_ptr<NonlinearFactor1> nlf =  | 
					
						
							|  |  |  |     boost::static_pointer_cast<NonlinearFactor1>(nfg[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // We linearize at noisy config from SmallExample
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   VectorConfig c = createNoisyConfig(); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactor::shared_ptr actual = nlf->linearize(c); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph lfg = createGaussianFactorGraph(); | 
					
						
							|  |  |  |   GaussianFactor::shared_ptr expected = lfg[0]; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   CHECK(expected->equals(*actual)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:47:12 +08:00
										 |  |  | TEST( NonlinearFactor, linearize_f2 ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Grab a non-linear factor
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  |   ExampleNonlinearFactorGraph nfg = createNonlinearFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   boost::shared_ptr<NonlinearFactor1> nlf =  | 
					
						
							|  |  |  |     boost::static_pointer_cast<NonlinearFactor1>(nfg[1]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // We linearize at noisy config from SmallExample
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   VectorConfig c = createNoisyConfig(); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactor::shared_ptr actual = nlf->linearize(c); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph lfg = createGaussianFactorGraph(); | 
					
						
							|  |  |  |   GaussianFactor::shared_ptr expected = lfg[1]; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   CHECK(expected->equals(*actual)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:47:12 +08:00
										 |  |  | TEST( NonlinearFactor, linearize_f3 ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Grab a non-linear factor
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  |   ExampleNonlinearFactorGraph nfg = createNonlinearFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   boost::shared_ptr<NonlinearFactor1> nlf =  | 
					
						
							|  |  |  |     boost::static_pointer_cast<NonlinearFactor1>(nfg[2]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // We linearize at noisy config from SmallExample
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   VectorConfig c = createNoisyConfig(); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactor::shared_ptr actual = nlf->linearize(c); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph lfg = createGaussianFactorGraph(); | 
					
						
							|  |  |  |   GaussianFactor::shared_ptr expected = lfg[2]; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   CHECK(expected->equals(*actual)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:47:12 +08:00
										 |  |  | TEST( NonlinearFactor, linearize_f4 ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // Grab a non-linear factor
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  |   ExampleNonlinearFactorGraph nfg = createNonlinearFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   boost::shared_ptr<NonlinearFactor1> nlf =  | 
					
						
							|  |  |  |     boost::static_pointer_cast<NonlinearFactor1>(nfg[3]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // We linearize at noisy config from SmallExample
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   VectorConfig c = createNoisyConfig(); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactor::shared_ptr actual = nlf->linearize(c); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactorGraph lfg = createGaussianFactorGraph(); | 
					
						
							|  |  |  |   GaussianFactor::shared_ptr expected = lfg[3]; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   CHECK(expected->equals(*actual)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:47:12 +08:00
										 |  |  | TEST( NonlinearFactor, size ) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// create a non linear factor graph
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	ExampleNonlinearFactorGraph fg = createNonlinearFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	// create a configuration for the non linear factor graph
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | 	VectorConfig cfg = createNoisyConfig(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	// get some factors from the graph
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	shared_nlf factor1 = fg[0]; | 
					
						
							|  |  |  | 	shared_nlf factor2 = fg[1]; | 
					
						
							|  |  |  | 	shared_nlf factor3 = fg[2]; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	CHECK(factor1->size() == 1); | 
					
						
							|  |  |  | 	CHECK(factor2->size() == 2); | 
					
						
							|  |  |  | 	CHECK(factor3->size() == 2); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr);} | 
					
						
							|  |  |  | /* ************************************************************************* */ |