| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * @file testNonlinearEquality.cpp | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define GTSAM_MAGIC_KEY
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-02 06:21:52 +08:00
										 |  |  | #include "Key.h"
 | 
					
						
							|  |  |  | #include "Pose2.h"
 | 
					
						
							| 
									
										
										
										
											2010-07-10 01:08:35 +08:00
										 |  |  | #include "Ordering.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | #include "VectorConfig.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | #include "NonlinearEquality.h"
 | 
					
						
							| 
									
										
										
										
											2010-07-10 01:08:35 +08:00
										 |  |  | #include "PriorFactor.h"
 | 
					
						
							|  |  |  | #include "NonlinearFactorGraph.h"
 | 
					
						
							|  |  |  | #include "NonlinearOptimizer-inl.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-02 06:21:52 +08:00
										 |  |  | #include "LieConfig-inl.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just  says something like
	if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
											
										 
											2010-01-14 06:25:03 +08:00
										 |  |  | typedef NonlinearEquality<VectorConfig,string,Vector> NLE; | 
					
						
							|  |  |  | typedef boost::shared_ptr<NLE> shared_nle; | 
					
						
							| 
									
										
										
										
											2010-07-09 05:30:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-02 06:21:52 +08:00
										 |  |  | typedef TypedSymbol<Pose2, 'x'> PoseKey; | 
					
						
							|  |  |  | typedef LieConfig<PoseKey, Pose2> PoseConfig; | 
					
						
							| 
									
										
										
										
											2010-07-10 01:08:35 +08:00
										 |  |  | typedef PriorFactor<PoseConfig, PoseKey, Pose2> PosePrior; | 
					
						
							| 
									
										
										
										
											2010-05-02 06:21:52 +08:00
										 |  |  | typedef NonlinearEquality<PoseConfig, PoseKey, Pose2> PoseNLE; | 
					
						
							|  |  |  | typedef boost::shared_ptr<PoseNLE> shared_poseNLE; | 
					
						
							| 
									
										
											  
											
												Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just  says something like
	if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
											
										 
											2010-01-14 06:25:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-10 01:08:35 +08:00
										 |  |  | typedef NonlinearFactorGraph<PoseConfig> PoseGraph; | 
					
						
							|  |  |  | typedef NonlinearOptimizer<PoseGraph,PoseConfig> PoseOptimizer; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just  says something like
	if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
											
										 
											2010-01-14 06:25:03 +08:00
										 |  |  | bool vector_compare(const Vector& a, const Vector& b) { | 
					
						
							|  |  |  | 	return equal_with_abs_tol(a, b, 1e-5); | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-28 01:59:03 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | TEST ( NonlinearEquality, linearization ) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	Symbol key = "x"; | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 	Vector value = Vector_(2, 1.0, 2.0); | 
					
						
							| 
									
										
											  
											
												Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just  says something like
	if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
											
										 
											2010-01-14 06:25:03 +08:00
										 |  |  | 	VectorConfig linearize; | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 	linearize.insert(key, value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create a nonlinear equality constraint
 | 
					
						
							| 
									
										
											  
											
												Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just  says something like
	if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
											
										 
											2010-01-14 06:25:03 +08:00
										 |  |  | 	shared_nle nle(new NLE(key, value,vector_compare)); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:47:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 	// check linearize
 | 
					
						
							| 
									
										
										
										
											2010-01-23 01:36:57 +08:00
										 |  |  | 	SharedDiagonal constraintModel = noiseModel::Constrained::All(2); | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  | 	GaussianFactor expLF(key, eye(2), zero(2), constraintModel); | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 	GaussianFactor::shared_ptr actualLF = nle->linearize(linearize); | 
					
						
							|  |  |  | 	CHECK(assert_equal(*actualLF, expLF)); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-02 06:21:52 +08:00
										 |  |  | /* ********************************************************************** */ | 
					
						
							|  |  |  | TEST ( NonlinearEquality, linearization_pose ) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PoseKey key(1); | 
					
						
							|  |  |  | 	Pose2 value; | 
					
						
							|  |  |  | 	PoseConfig config; | 
					
						
							|  |  |  | 	config.insert(key, value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create a nonlinear equality constraint
 | 
					
						
							|  |  |  | 	shared_poseNLE nle(new PoseNLE(key, value)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	GaussianFactor::shared_ptr actualLF = nle->linearize(config); | 
					
						
							|  |  |  | 	CHECK(true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just  says something like
	if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
											
										 
											2010-01-14 06:25:03 +08:00
										 |  |  | /* ********************************************************************** */ | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | TEST ( NonlinearEquality, linearization_fail ) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |   Symbol key = "x"; | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 	Vector value = Vector_(2, 1.0, 2.0); | 
					
						
							|  |  |  | 	Vector wrong = Vector_(2, 3.0, 4.0); | 
					
						
							| 
									
										
											  
											
												Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just  says something like
	if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
											
										 
											2010-01-14 06:25:03 +08:00
										 |  |  | 	VectorConfig bad_linearize; | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 	bad_linearize.insert(key, wrong); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create a nonlinear equality constraint
 | 
					
						
							| 
									
										
											  
											
												Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just  says something like
	if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
											
										 
											2010-01-14 06:25:03 +08:00
										 |  |  | 	shared_nle nle(new NLE(key, value,vector_compare)); | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// check linearize to ensure that it fails for bad linearization points
 | 
					
						
							|  |  |  | 	try { | 
					
						
							|  |  |  | 		GaussianFactor::shared_ptr actualLF = nle->linearize(bad_linearize); | 
					
						
							|  |  |  | 		CHECK(false); | 
					
						
							|  |  |  | 	} catch (std::invalid_argument) { | 
					
						
							|  |  |  | 		CHECK(true); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-02 06:21:52 +08:00
										 |  |  | /* ********************************************************************** */ | 
					
						
							|  |  |  | TEST ( NonlinearEquality, linearization_fail_pose ) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PoseKey key(1); | 
					
						
							|  |  |  | 	Pose2 value(2.0, 1.0, 2.0), | 
					
						
							|  |  |  | 		  wrong(2.0, 3.0, 4.0); | 
					
						
							|  |  |  | 	PoseConfig bad_linearize; | 
					
						
							|  |  |  | 	bad_linearize.insert(key, wrong); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create a nonlinear equality constraint
 | 
					
						
							|  |  |  | 	shared_poseNLE nle(new PoseNLE(key, value)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// check linearize to ensure that it fails for bad linearization points
 | 
					
						
							|  |  |  | 	try { | 
					
						
							|  |  |  | 		GaussianFactor::shared_ptr actualLF = nle->linearize(bad_linearize); | 
					
						
							|  |  |  | 		CHECK(false); | 
					
						
							|  |  |  | 	} catch (std::invalid_argument) { | 
					
						
							|  |  |  | 		CHECK(true); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ********************************************************************** */ | 
					
						
							|  |  |  | TEST ( NonlinearEquality, linearization_fail_pose_origin ) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PoseKey key(1); | 
					
						
							|  |  |  | 	Pose2 value, | 
					
						
							|  |  |  | 		  wrong(2.0, 3.0, 4.0); | 
					
						
							|  |  |  | 	PoseConfig bad_linearize; | 
					
						
							|  |  |  | 	bad_linearize.insert(key, wrong); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create a nonlinear equality constraint
 | 
					
						
							|  |  |  | 	shared_poseNLE nle(new PoseNLE(key, value)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// check linearize to ensure that it fails for bad linearization points
 | 
					
						
							|  |  |  | 	try { | 
					
						
							|  |  |  | 		GaussianFactor::shared_ptr actualLF = nle->linearize(bad_linearize); | 
					
						
							|  |  |  | 		CHECK(false); | 
					
						
							|  |  |  | 	} catch (std::invalid_argument) { | 
					
						
							|  |  |  | 		CHECK(true); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-28 01:59:03 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | TEST ( NonlinearEquality, error ) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |   Symbol key = "x"; | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 	Vector value = Vector_(2, 1.0, 2.0); | 
					
						
							|  |  |  | 	Vector wrong = Vector_(2, 3.0, 4.0); | 
					
						
							|  |  |  | 	VectorConfig feasible, bad_linearize; | 
					
						
							|  |  |  | 	feasible.insert(key, value); | 
					
						
							|  |  |  | 	bad_linearize.insert(key, wrong); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create a nonlinear equality constraint
 | 
					
						
							| 
									
										
											  
											
												Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just  says something like
	if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
											
										 
											2010-01-14 06:25:03 +08:00
										 |  |  | 	shared_nle nle(new NLE(key, value,vector_compare)); | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// check error function outputs
 | 
					
						
							| 
									
										
										
										
											2010-01-18 13:38:53 +08:00
										 |  |  | 	Vector actual = nle->unwhitenedError(feasible); | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 	CHECK(assert_equal(actual, zero(2))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 13:38:53 +08:00
										 |  |  | 	actual = nle->unwhitenedError(bad_linearize); | 
					
						
							| 
									
										
										
										
											2010-07-09 05:30:19 +08:00
										 |  |  | 	CHECK(assert_equal(actual, repeat(2, std::numeric_limits<double>::infinity()))); | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-28 01:59:03 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | TEST ( NonlinearEquality, equals ) { | 
					
						
							|  |  |  | 	string key1 = "x"; | 
					
						
							|  |  |  | 	Vector value1 = Vector_(2, 1.0, 2.0); | 
					
						
							|  |  |  | 	Vector value2 = Vector_(2, 3.0, 4.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create some constraints to compare
 | 
					
						
							| 
									
										
											  
											
												Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just  says something like
	if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
											
										 
											2010-01-14 06:25:03 +08:00
										 |  |  | 	shared_nle nle1(new NLE(key1, value1,vector_compare)); | 
					
						
							|  |  |  | 	shared_nle nle2(new NLE(key1, value1,vector_compare)); | 
					
						
							|  |  |  | 	shared_nle nle3(new NLE(key1, value2,vector_compare)); | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// verify
 | 
					
						
							| 
									
										
										
										
											2009-11-28 01:59:03 +08:00
										 |  |  | 	CHECK(nle1->equals(*nle2));  // basic equality = true
 | 
					
						
							|  |  |  | 	CHECK(nle2->equals(*nle1));  // test symmetry of equals()
 | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 	CHECK(!nle1->equals(*nle3)); // test config
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 05:30:19 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST ( NonlinearEquality, allow_error_vector ) { | 
					
						
							|  |  |  | 	Symbol key1 = "x"; | 
					
						
							|  |  |  | 	Vector feasible1 = Vector_(3, 1.0, 2.0, 3.0); | 
					
						
							|  |  |  | 	double error_gain = 500.0; | 
					
						
							|  |  |  | 	NLE nle(key1, feasible1, error_gain,vector_compare); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// the unwhitened error should provide logmap to the feasible state
 | 
					
						
							|  |  |  | 	Vector badPoint1 = Vector_(3, 0.0, 2.0, 3.0); | 
					
						
							|  |  |  | 	Vector actVec = nle.evaluateError(badPoint1); | 
					
						
							|  |  |  | 	Vector expVec = Vector_(3, 1.0, 0.0, 0.0); | 
					
						
							|  |  |  | 	CHECK(assert_equal(expVec, actVec)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// the actual error should have a gain on it
 | 
					
						
							|  |  |  | 	VectorConfig config; | 
					
						
							|  |  |  | 	config.insert(key1, badPoint1); | 
					
						
							|  |  |  | 	double actError = nle.error(config); | 
					
						
							|  |  |  | 	DOUBLES_EQUAL(500.0, actError, 1e-9); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// check linearization
 | 
					
						
							|  |  |  | 	GaussianFactor::shared_ptr actLinFactor = nle.linearize(config); | 
					
						
							|  |  |  | 	Matrix A1 = eye(3,3); | 
					
						
							| 
									
										
										
										
											2010-07-10 01:08:35 +08:00
										 |  |  | 	Vector b = expVec; | 
					
						
							| 
									
										
										
										
											2010-07-09 05:30:19 +08:00
										 |  |  | 	SharedDiagonal model = noiseModel::Constrained::All(3); | 
					
						
							|  |  |  | 	GaussianFactor::shared_ptr expLinFactor(new GaussianFactor(key1, A1, b, model)); | 
					
						
							|  |  |  | 	CHECK(assert_equal(*expLinFactor, *actLinFactor)); | 
					
						
							| 
									
										
										
										
											2010-07-10 01:08:35 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST ( NonlinearEquality, allow_error_pose ) { | 
					
						
							|  |  |  | 	PoseKey key1(1); | 
					
						
							|  |  |  | 	Pose2 feasible1(1.0, 2.0, 3.0); | 
					
						
							|  |  |  | 	double error_gain = 500.0; | 
					
						
							|  |  |  | 	PoseNLE nle(key1, feasible1, error_gain); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// the unwhitened error should provide logmap to the feasible state
 | 
					
						
							|  |  |  | 	Pose2 badPoint1(0.0, 2.0, 3.0); | 
					
						
							|  |  |  | 	Vector actVec = nle.evaluateError(badPoint1); | 
					
						
							|  |  |  | 	Vector expVec = Vector_(3, -0.989992, -0.14112, 0.0); | 
					
						
							|  |  |  | 	CHECK(assert_equal(expVec, actVec, 1e-5)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// the actual error should have a gain on it
 | 
					
						
							|  |  |  | 	PoseConfig config; | 
					
						
							|  |  |  | 	config.insert(key1, badPoint1); | 
					
						
							|  |  |  | 	double actError = nle.error(config); | 
					
						
							|  |  |  | 	DOUBLES_EQUAL(500.0, actError, 1e-9); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// check linearization
 | 
					
						
							|  |  |  | 	GaussianFactor::shared_ptr actLinFactor = nle.linearize(config); | 
					
						
							|  |  |  | 	Matrix A1 = eye(3,3); | 
					
						
							|  |  |  | 	Vector b = expVec; | 
					
						
							|  |  |  | 	SharedDiagonal model = noiseModel::Constrained::All(3); | 
					
						
							|  |  |  | 	GaussianFactor::shared_ptr expLinFactor(new GaussianFactor(key1, A1, b, model)); | 
					
						
							|  |  |  | 	CHECK(assert_equal(*expLinFactor, *actLinFactor, 1e-5)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST ( NonlinearEquality, allow_error_optimize ) { | 
					
						
							|  |  |  | 	PoseKey key1(1); | 
					
						
							|  |  |  | 	Pose2 feasible1(1.0, 2.0, 3.0); | 
					
						
							|  |  |  | 	double error_gain = 500.0; | 
					
						
							|  |  |  | 	PoseNLE nle(key1, feasible1, error_gain); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// add to a graph
 | 
					
						
							|  |  |  | 	boost::shared_ptr<PoseGraph> graph(new PoseGraph()); | 
					
						
							|  |  |  | 	graph->add(nle); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// initialize away from the ideal
 | 
					
						
							|  |  |  | 	Pose2 initPose(0.0, 2.0, 3.0); | 
					
						
							|  |  |  | 	boost::shared_ptr<PoseConfig> init(new PoseConfig()); | 
					
						
							|  |  |  | 	init->insert(key1, initPose); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// optimize
 | 
					
						
							|  |  |  | 	boost::shared_ptr<Ordering> ord(new Ordering()); | 
					
						
							|  |  |  | 	ord->push_back(key1); | 
					
						
							|  |  |  | 	PoseOptimizer::shared_solver solver(new PoseOptimizer::solver(ord)); | 
					
						
							|  |  |  | 	PoseOptimizer optimizer(graph, init, solver); | 
					
						
							|  |  |  | 	double relThresh = 1e-5, absThresh = 1e-5; | 
					
						
							|  |  |  | 	PoseOptimizer result = optimizer.levenbergMarquardt(relThresh, absThresh, PoseOptimizer::SILENT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// verify
 | 
					
						
							|  |  |  | 	PoseConfig expected; | 
					
						
							|  |  |  | 	expected.insert(key1, feasible1); | 
					
						
							|  |  |  | 	CHECK(assert_equal(expected, *result.config())); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-07-09 05:30:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-10 01:08:35 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST ( NonlinearEquality, allow_error_optimize_with_factors ) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create a hard constraint
 | 
					
						
							|  |  |  | 	PoseKey key1(1); | 
					
						
							|  |  |  | 	Pose2 feasible1(1.0, 2.0, 3.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// initialize away from the ideal
 | 
					
						
							|  |  |  | 	boost::shared_ptr<PoseConfig> init(new PoseConfig()); | 
					
						
							|  |  |  | 	Pose2 initPose(0.0, 2.0, 3.0); | 
					
						
							|  |  |  | 	init->insert(key1, initPose); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	double error_gain = 500.0; | 
					
						
							|  |  |  | 	PoseNLE nle(key1, feasible1, error_gain); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create a soft prior that conflicts
 | 
					
						
							|  |  |  | 	PosePrior prior(key1, initPose, noiseModel::Isotropic::Sigma(3, 0.1)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// add to a graph
 | 
					
						
							|  |  |  | 	boost::shared_ptr<PoseGraph> graph(new PoseGraph()); | 
					
						
							|  |  |  | 	graph->add(nle); | 
					
						
							|  |  |  | 	graph->add(prior); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// optimize
 | 
					
						
							|  |  |  | 	boost::shared_ptr<Ordering> ord(new Ordering()); | 
					
						
							|  |  |  | 	ord->push_back(key1); | 
					
						
							|  |  |  | 	PoseOptimizer::shared_solver solver(new PoseOptimizer::solver(ord)); | 
					
						
							|  |  |  | 	PoseOptimizer optimizer(graph, init, solver); | 
					
						
							|  |  |  | 	double relThresh = 1e-5, absThresh = 1e-5; | 
					
						
							|  |  |  | 	PoseOptimizer result = optimizer.levenbergMarquardt(relThresh, absThresh, PoseOptimizer::SILENT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// verify
 | 
					
						
							|  |  |  | 	PoseConfig expected; | 
					
						
							|  |  |  | 	expected.insert(key1, feasible1); | 
					
						
							|  |  |  | 	CHECK(assert_equal(expected, *result.config())); | 
					
						
							| 
									
										
										
										
											2010-07-09 05:30:19 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-11-13 10:06:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr); } | 
					
						
							|  |  |  | /* ************************************************************************* */ |