| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | /** 
 | 
					
						
							|  |  |  |  * @file    testNonlinearOptimizer.cpp | 
					
						
							|  |  |  |  * @brief   Unit tests for NonlinearOptimizer class | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  | #include <boost/assign/std/list.hpp> // for operator +=
 | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Matrix.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | #include "Ordering.h"
 | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | #include "smallExample.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-13 12:13:03 +08:00
										 |  |  | // template definitions
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | #include "NonlinearFactorGraph-inl.h"
 | 
					
						
							| 
									
										
										
										
											2009-09-13 12:13:03 +08:00
										 |  |  | #include "NonlinearOptimizer-inl.h"
 | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | typedef NonlinearOptimizer<ExampleNonlinearFactorGraph,VectorConfig> Optimizer; | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | TEST( NonlinearOptimizer, delta ) | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	ExampleNonlinearFactorGraph fg = createNonlinearFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 	Optimizer::shared_config initial = sharedNoisyConfig(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Expected configuration is the difference between the noisy config
 | 
					
						
							|  |  |  | 	// and the ground-truth config. One step only because it's linear !
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | 	VectorConfig expected; | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 	Vector dl1(2); | 
					
						
							|  |  |  | 	dl1(0) = -0.1; | 
					
						
							|  |  |  | 	dl1(1) = 0.1; | 
					
						
							|  |  |  | 	expected.insert("l1", dl1); | 
					
						
							|  |  |  | 	Vector dx1(2); | 
					
						
							|  |  |  | 	dx1(0) = -0.1; | 
					
						
							|  |  |  | 	dx1(1) = -0.1; | 
					
						
							|  |  |  | 	expected.insert("x1", dx1); | 
					
						
							|  |  |  | 	Vector dx2(2); | 
					
						
							|  |  |  | 	dx2(0) = 0.1; | 
					
						
							|  |  |  | 	dx2(1) = -0.2; | 
					
						
							|  |  |  | 	expected.insert("x2", dx2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check one ordering
 | 
					
						
							|  |  |  | 	Ordering ord1; | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  | 	ord1 += "x2","l1","x1"; | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 	Optimizer optimizer1(fg, ord1, initial); | 
					
						
							| 
									
										
										
										
											2009-10-20 03:12:48 +08:00
										 |  |  | 	VectorConfig actual1 = optimizer1.linearizeAndOptimizeForDelta(); | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 	CHECK(assert_equal(actual1,expected)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check another
 | 
					
						
							|  |  |  | 	Ordering ord2; | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  | 	ord2 += "x1","x2","l1"; | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 	Optimizer optimizer2(fg, ord2, initial); | 
					
						
							| 
									
										
										
										
											2009-10-20 03:12:48 +08:00
										 |  |  | 	VectorConfig actual2 = optimizer2.linearizeAndOptimizeForDelta(); | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 	CHECK(assert_equal(actual2,expected)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// And yet another...
 | 
					
						
							|  |  |  | 	Ordering ord3; | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  | 	ord3 += "l1","x1","x2"; | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 	Optimizer optimizer3(fg, ord3, initial); | 
					
						
							| 
									
										
										
										
											2009-10-20 03:12:48 +08:00
										 |  |  | 	VectorConfig actual3 = optimizer3.linearizeAndOptimizeForDelta(); | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 	CHECK(assert_equal(actual3,expected)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | TEST( NonlinearOptimizer, iterateLM ) | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// really non-linear factor graph
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	ExampleNonlinearFactorGraph fg = createReallyNonlinearFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// config far from minimum
 | 
					
						
							|  |  |  | 	Vector x0 = Vector_(1, 3.0); | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | 	boost::shared_ptr<VectorConfig> config(new VectorConfig); | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 	config->insert("x", x0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// ordering
 | 
					
						
							|  |  |  | 	Ordering ord; | 
					
						
							|  |  |  | 	ord.push_back("x"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create initial optimization state, with lambda=0
 | 
					
						
							|  |  |  | 	Optimizer optimizer(fg, ord, config, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// normal iterate
 | 
					
						
							|  |  |  | 	Optimizer iterated1 = optimizer.iterate(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// LM iterate with lambda 0 should be the same
 | 
					
						
							|  |  |  | 	Optimizer iterated2 = optimizer.iterateLM(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	CHECK(assert_equal(*iterated1.config(), *iterated2.config(), 1e-9)); | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | TEST( NonlinearOptimizer, optimize ) | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	ExampleNonlinearFactorGraph fg = createReallyNonlinearFactorGraph(); | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// test error at minimum
 | 
					
						
							|  |  |  | 	Vector xstar = Vector_(1, 0.0); | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | 	VectorConfig cstar; | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 	cstar.insert("x", xstar); | 
					
						
							|  |  |  | 	DOUBLES_EQUAL(0.0,fg.error(cstar),0.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// test error at initial = [(1-cos(3))^2 + (sin(3))^2]*50 =
 | 
					
						
							|  |  |  | 	Vector x0 = Vector_(1, 3.0); | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | 	boost::shared_ptr<VectorConfig> c0(new VectorConfig); | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 	c0->insert("x", x0); | 
					
						
							|  |  |  | 	DOUBLES_EQUAL(199.0,fg.error(*c0),1e-3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// optimize parameters
 | 
					
						
							|  |  |  | 	Ordering ord; | 
					
						
							|  |  |  | 	ord.push_back("x"); | 
					
						
							|  |  |  | 	double relativeThreshold = 1e-5; | 
					
						
							|  |  |  | 	double absoluteThreshold = 1e-5; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// initial optimization state is the same in both cases tested
 | 
					
						
							|  |  |  | 	Optimizer optimizer(fg, ord, c0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Gauss-Newton
 | 
					
						
							|  |  |  | 	Optimizer actual1 = optimizer.gaussNewton(relativeThreshold, | 
					
						
							|  |  |  | 			absoluteThreshold); | 
					
						
							| 
									
										
										
										
											2009-09-11 06:08:47 +08:00
										 |  |  | 	CHECK(assert_equal(*(actual1.config()),cstar)); | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Levenberg-Marquardt
 | 
					
						
							|  |  |  | 	Optimizer actual2 = optimizer.levenbergMarquardt(relativeThreshold, | 
					
						
							|  |  |  | 			absoluteThreshold, Optimizer::SILENT); | 
					
						
							| 
									
										
										
										
											2009-09-11 06:08:47 +08:00
										 |  |  | 	CHECK(assert_equal(*(actual2.config()),cstar)); | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  | 	TestResult tr; | 
					
						
							|  |  |  | 	return TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ |