| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * @file testSQP.cpp | 
					
						
							|  |  |  |  * @brief demos of SQP using existing gtsam components | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <cmath>
 | 
					
						
							|  |  |  | #include <boost/assign/std/list.hpp> // for operator +=
 | 
					
						
							|  |  |  | #include <boost/assign/std/map.hpp> // for insert
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 13:38:53 +08:00
										 |  |  | // TODO: DANGEROUS, create shared pointers
 | 
					
						
							| 
									
										
										
										
											2010-01-23 01:36:57 +08:00
										 |  |  | #define GTSAM_DANGEROUS_GAUSSIAN 2
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | #define GTSAM_MAGIC_KEY
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | #include <Pose3.h>
 | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | #include <GaussianFactorGraph.h>
 | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | #include <NonlinearOptimizer.h>
 | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | #include <SQPOptimizer.h>
 | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | #include <simulated2D.h>
 | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | #include <Ordering.h>
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | #include <visualSLAM.h>
 | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | // templated implementations
 | 
					
						
							|  |  |  | #include <NonlinearFactorGraph-inl.h>
 | 
					
						
							| 
									
										
										
										
											2009-11-20 13:13:32 +08:00
										 |  |  | #include <NonlinearConstraint-inl.h>
 | 
					
						
							|  |  |  | #include <NonlinearOptimizer-inl.h>
 | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | #include <SQPOptimizer-inl.h>
 | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | using namespace boost; | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  | // Models to use
 | 
					
						
							| 
									
										
										
										
											2010-01-23 01:36:57 +08:00
										 |  |  | SharedDiagonal probModel1 = sharedSigma(1,1.0); | 
					
						
							|  |  |  | SharedDiagonal probModel2 = sharedSigma(2,1.0); | 
					
						
							|  |  |  | SharedDiagonal constraintModel1 = noiseModel::Constrained::All(1); | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | // trick from some reading group
 | 
					
						
							|  |  |  | #define FOREACH_PAIR( KEY, VAL, COL) BOOST_FOREACH (boost::tie(KEY,VAL),COL)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | /* *********************************************************************
 | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  |  * This example uses a nonlinear objective function and | 
					
						
							|  |  |  |  * nonlinear equality constraint.  The formulation is actually | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  |  * the Cholesky form that creates the full Hessian explicitly, | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  |  * which should really be avoided with our QR-based machinery. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Note: the update equation used here has a fixed step size | 
					
						
							|  |  |  |  * and gain that is rather arbitrarily chosen, and as such, | 
					
						
							|  |  |  |  * will take a silly number of iterations. | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | TEST (SQP, problem1_cholesky ) { | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 	bool verbose = false; | 
					
						
							|  |  |  | 	// use a nonlinear function of f(x) = x^2+y^2
 | 
					
						
							|  |  |  | 	// nonlinear equality constraint: g(x) = x^2-5-y=0
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 	// Lagrangian: f(x) + \lambda*g(x)
 | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 	// state structure: [x y \lambda]
 | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 	VectorConfig init, state; | 
					
						
							|  |  |  | 	init.insert("x", Vector_(1, 1.0)); | 
					
						
							|  |  |  | 	init.insert("y", Vector_(1, 1.0)); | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	init.insert("L", Vector_(1, 1.0)); | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 	state = init; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (verbose) init.print("Initial State"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// loop until convergence
 | 
					
						
							| 
									
										
										
										
											2009-11-22 03:05:24 +08:00
										 |  |  | 	int maxIt = 10; | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 	for (int i = 0; i<maxIt; ++i) { | 
					
						
							|  |  |  | 		if (verbose) cout << "\n******************************\nIteration: " << i+1 << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// extract the states
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 		double x, y, lambda; | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 		x = state["x"](0); | 
					
						
							|  |  |  | 		y = state["y"](0); | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		lambda = state["L"](0); | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// calculate the components
 | 
					
						
							|  |  |  | 		Matrix H1, H2, gradG; | 
					
						
							|  |  |  | 		Vector gradL, gx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// hessian of lagrangian function, in two columns:
 | 
					
						
							|  |  |  | 		H1 = Matrix_(2,1, | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 				2.0+2.0*lambda, | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 				0.0); | 
					
						
							|  |  |  | 		H2 = Matrix_(2,1, | 
					
						
							|  |  |  | 				0.0, | 
					
						
							|  |  |  | 				2.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// deriviative of lagrangian function
 | 
					
						
							|  |  |  | 		gradL = Vector_(2, | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 				2.0*x*(1+lambda), | 
					
						
							|  |  |  | 				2.0*y-lambda); | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// constraint derivatives
 | 
					
						
							|  |  |  | 		gradG = Matrix_(2,1, | 
					
						
							|  |  |  | 				2.0*x, | 
					
						
							|  |  |  | 				0.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// constraint value
 | 
					
						
							|  |  |  | 		gx = Vector_(1, | 
					
						
							|  |  |  | 				x*x-5-y); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// create a factor for the states
 | 
					
						
							|  |  |  | 		GaussianFactor::shared_ptr f1(new | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  | 				GaussianFactor("x", H1, "y", H2, "L", gradG, gradL, probModel2)); | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// create a factor for the lagrange multiplier
 | 
					
						
							|  |  |  | 		GaussianFactor::shared_ptr f2(new | 
					
						
							|  |  |  | 				GaussianFactor("x", -sub(gradG, 0, 1, 0, 1), | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  | 							   "y", -sub(gradG, 1, 2, 0, 1), -gx, constraintModel1)); | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// construct graph
 | 
					
						
							|  |  |  | 		GaussianFactorGraph fg; | 
					
						
							|  |  |  | 		fg.push_back(f1); | 
					
						
							|  |  |  | 		fg.push_back(f2); | 
					
						
							|  |  |  | 		if (verbose) fg.print("Graph"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// solve
 | 
					
						
							|  |  |  | 		Ordering ord; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		ord += "x", "y", "L"; | 
					
						
							| 
									
										
										
										
											2009-11-22 03:05:24 +08:00
										 |  |  | 		VectorConfig delta = fg.optimize(ord).scale(-1.0); | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 		if (verbose) delta.print("Delta"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// update initial estimate
 | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | 		VectorConfig newState = expmap(state, delta); | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 		state = newState; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (verbose) state.print("Updated State"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// verify that it converges to the nearest optimal point
 | 
					
						
							|  |  |  | 	VectorConfig expected; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	expected.insert("L", Vector_(1, -1.0)); | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 	expected.insert("x", Vector_(1, 2.12)); | 
					
						
							|  |  |  | 	expected.insert("y", Vector_(1, -0.5)); | 
					
						
							| 
									
										
										
										
											2009-11-21 12:22:59 +08:00
										 |  |  | 	CHECK(assert_equal(expected,state, 1e-2)); | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | /* *********************************************************************
 | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  |  * This example uses a nonlinear objective function and | 
					
						
							|  |  |  |  * nonlinear equality constraint.  This formulation splits | 
					
						
							|  |  |  |  * the constraint into a factor and a linear constraint. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This example uses the same silly number of iterations as the | 
					
						
							|  |  |  |  * previous example. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | TEST (SQP, problem1_sqp ) { | 
					
						
							|  |  |  | 	bool verbose = false; | 
					
						
							|  |  |  | 	// use a nonlinear function of f(x) = x^2+y^2
 | 
					
						
							|  |  |  | 	// nonlinear equality constraint: g(x) = x^2-5-y=0
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 	// Lagrangian: f(x) + \lambda*g(x)
 | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 	// state structure: [x y \lambda]
 | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 	VectorConfig init, state; | 
					
						
							|  |  |  | 	init.insert("x", Vector_(1, 1.0)); | 
					
						
							|  |  |  | 	init.insert("y", Vector_(1, 1.0)); | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	init.insert("L", Vector_(1, 1.0)); | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 	state = init; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (verbose) init.print("Initial State"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// loop until convergence
 | 
					
						
							| 
									
										
										
										
											2009-11-19 04:23:07 +08:00
										 |  |  | 	int maxIt = 5; | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 	for (int i = 0; i<maxIt; ++i) { | 
					
						
							|  |  |  | 		if (verbose) cout << "\n******************************\nIteration: " << i+1 << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// extract the states
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 		double x, y, lambda; | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 		x = state["x"](0); | 
					
						
							|  |  |  | 		y = state["y"](0); | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		lambda = state["L"](0); | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/** create the linear factor
 | 
					
						
							|  |  |  | 		 * ||h(x)-z||^2 => ||Ax-b||^2 | 
					
						
							|  |  |  | 		 *  where: | 
					
						
							|  |  |  | 		 *		h(x) simply returns the inputs | 
					
						
							|  |  |  | 		 *		z    zeros(2) | 
					
						
							|  |  |  | 		 *		A 	 identity | 
					
						
							|  |  |  | 		 *		b	 linearization point | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 		Matrix A = eye(2); | 
					
						
							|  |  |  | 		Vector b = Vector_(2, x, y); | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 		GaussianFactor::shared_ptr f1( | 
					
						
							|  |  |  | 						new GaussianFactor("x", sub(A, 0,2, 0,1), // A(:,1)
 | 
					
						
							|  |  |  | 										   "y", sub(A, 0,2, 1,2), // A(:,2)
 | 
					
						
							|  |  |  | 										   b,                     // rhs of f(x)
 | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  | 										   probModel2));          // arbitrary sigma
 | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/** create the constraint-linear factor
 | 
					
						
							|  |  |  | 		 * Provides a mechanism to use variable gain to force the constraint | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 		 * \lambda*gradG*dx + d\lambda = zero | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 		 * formulated in matrix form as: | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 		 * [\lambda*gradG eye(1)] [dx; d\lambda] = zero | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 		Matrix gradG = Matrix_(1, 2,2*x, -1.0); | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 		GaussianFactor::shared_ptr f2( | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 				new GaussianFactor("x", lambda*sub(gradG, 0,1, 0,1), // scaled gradG(:,1)
 | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  | 								   "y", lambda*sub(gradG, 0,1, 1,2), // scaled gradG(:,2)
 | 
					
						
							|  |  |  | 								   "L", eye(1),                      // dlambda term
 | 
					
						
							|  |  |  | 								   Vector_(1, 0.0),                  // rhs is zero
 | 
					
						
							|  |  |  | 								   probModel1));                     // arbitrary sigma
 | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// create the actual constraint
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 		// [gradG] [x; y] - g = 0
 | 
					
						
							|  |  |  | 		Vector g = Vector_(1,x*x-y-5); | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 		GaussianFactor::shared_ptr c1( | 
					
						
							|  |  |  | 				new GaussianFactor("x", sub(gradG, 0,1, 0,1),   // slice first part of gradG
 | 
					
						
							|  |  |  | 								   "y", sub(gradG, 0,1, 1,2),   // slice second part of gradG
 | 
					
						
							|  |  |  | 								   g,                           // value of constraint function
 | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  | 								   constraintModel1));          // force to constraint
 | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 		// construct graph
 | 
					
						
							|  |  |  | 		GaussianFactorGraph fg; | 
					
						
							|  |  |  | 		fg.push_back(f1); | 
					
						
							|  |  |  | 		fg.push_back(f2); | 
					
						
							|  |  |  | 		fg.push_back(c1); | 
					
						
							|  |  |  | 		if (verbose) fg.print("Graph"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// solve
 | 
					
						
							|  |  |  | 		Ordering ord; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		ord += "x", "y", "L"; | 
					
						
							| 
									
										
										
										
											2009-11-29 05:00:09 +08:00
										 |  |  | 		VectorConfig delta = fg.optimize(ord); | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 		if (verbose) delta.print("Delta"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// update initial estimate
 | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | 		VectorConfig newState = expmap(state, delta.scale(-1.0)); | 
					
						
							| 
									
										
										
										
											2009-11-19 04:23:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// set the state to the updated state
 | 
					
						
							| 
									
										
										
										
											2009-11-19 03:42:14 +08:00
										 |  |  | 		state = newState; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (verbose) state.print("Updated State"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// verify that it converges to the nearest optimal point
 | 
					
						
							|  |  |  | 	VectorConfig expected; | 
					
						
							|  |  |  | 	expected.insert("x", Vector_(1, 2.12)); | 
					
						
							|  |  |  | 	expected.insert("y", Vector_(1, -0.5)); | 
					
						
							|  |  |  | 	CHECK(assert_equal(state["x"], expected["x"], 1e-2)); | 
					
						
							|  |  |  | 	CHECK(assert_equal(state["y"], expected["y"], 1e-2)); | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | /* ********************************************************************* */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | typedef simulated2D::Config Config2D; | 
					
						
							|  |  |  | typedef NonlinearFactorGraph<Config2D> NLGraph; | 
					
						
							|  |  |  | typedef NonlinearEquality<Config2D, simulated2D::PoseKey, Point2> NLE; | 
					
						
							|  |  |  | typedef boost::shared_ptr<simulated2D::Measurement > shared; | 
					
						
							|  |  |  | typedef NonlinearOptimizer<NLGraph, Config2D> Optimizer; | 
					
						
							| 
									
										
											  
											
												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-01-19 13:33:44 +08:00
										 |  |  | typedef TypedSymbol<Vector, 'L'> LamKey; | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  |  * Determining a ground truth linear system | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  |  * with two poses seeing one landmark, with each pose | 
					
						
							|  |  |  |  * constrained to a particular value | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | TEST (SQP, two_pose_truth ) { | 
					
						
							| 
									
										
										
										
											2009-11-22 03:05:24 +08:00
										 |  |  | 	bool verbose = false; | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	// create a graph
 | 
					
						
							|  |  |  | 	shared_ptr<NLGraph> graph(new NLGraph); | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	// add the constraints on the ends
 | 
					
						
							|  |  |  | 	// position (1, 1) constraint for x1
 | 
					
						
							|  |  |  | 	// position (5, 6) constraint for x2
 | 
					
						
							|  |  |  | 	simulated2D::PoseKey x1(1), x2(2); | 
					
						
							|  |  |  | 	simulated2D::PointKey l1(1); | 
					
						
							|  |  |  | 	Point2 pt_x1(1.0, 1.0), | 
					
						
							|  |  |  | 		   pt_x2(5.0, 6.0); | 
					
						
							|  |  |  | 	shared_ptr<NLE> ef1(new NLE(x1, pt_x1)), | 
					
						
							|  |  |  | 			        ef2(new NLE(x2, pt_x2)); | 
					
						
							|  |  |  | 	graph->push_back(ef1); | 
					
						
							|  |  |  | 	graph->push_back(ef2); | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// measurement from x1 to l1
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	Point2 z1(0.0, 5.0); | 
					
						
							| 
									
										
										
										
											2010-01-23 01:36:57 +08:00
										 |  |  | 	SharedGaussian sigma(noiseModel::Isotropic::Sigma(2, 0.1)); | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	shared f1(new simulated2D::Measurement(z1, sigma, x1,l1)); | 
					
						
							|  |  |  | 	graph->push_back(f1); | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// measurement from x2 to l1
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	Point2 z2(-4.0, 0.0); | 
					
						
							|  |  |  | 	shared f2(new simulated2D::Measurement(z2, sigma, x2,l1)); | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | 	graph->push_back(f2); | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create an initial estimate
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	Point2 pt_l1( | 
					
						
							|  |  |  | 			1.0, 6.0 // ground truth
 | 
					
						
							|  |  |  | 		  //1.2, 5.6 // small error
 | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 	shared_ptr<Config2D> initialEstimate(new Config2D); | 
					
						
							|  |  |  | 	initialEstimate->insert(l1, pt_l1); | 
					
						
							|  |  |  | 	initialEstimate->insert(x1, pt_x1); | 
					
						
							|  |  |  | 	initialEstimate->insert(x2, pt_x2); | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// optimize the graph
 | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | 	shared_ptr<Ordering> ordering(new Ordering()); | 
					
						
							|  |  |  | 	*ordering += "x1", "x2", "l1"; | 
					
						
							| 
									
										
										
										
											2010-01-20 10:28:23 +08:00
										 |  |  | 	Optimizer::shared_solver solver(new Optimizer::solver(ordering)); | 
					
						
							|  |  |  | 	Optimizer optimizer(graph, initialEstimate, solver); | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// display solution
 | 
					
						
							|  |  |  | 	double relativeThreshold = 1e-5; | 
					
						
							|  |  |  | 	double absoluteThreshold = 1e-5; | 
					
						
							|  |  |  | 	Optimizer act_opt = optimizer.gaussNewton(relativeThreshold, absoluteThreshold); | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	boost::shared_ptr<const Config2D> actual = act_opt.config(); | 
					
						
							| 
									
										
										
										
											2009-11-22 03:05:24 +08:00
										 |  |  | 	if (verbose) actual->print("Configuration after optimization"); | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// verify
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	Config2D expected; | 
					
						
							|  |  |  | 	expected.insert(x1, pt_x1); | 
					
						
							|  |  |  | 	expected.insert(x2, pt_x2); | 
					
						
							|  |  |  | 	expected.insert(l1, Point2(1.0, 6.0)); | 
					
						
							| 
									
										
										
										
											2009-11-19 05:57:59 +08:00
										 |  |  | 	CHECK(assert_equal(expected, *actual, 1e-5)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | /* ********************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-20 13:13:32 +08:00
										 |  |  | namespace sqp_test1 { | 
					
						
							| 
									
										
										
										
											2009-11-20 00:50:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 	// binary constraint between landmarks
 | 
					
						
							|  |  |  | 	/** g(x) = x-y = 0 */ | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	Vector g(const Config2D& config, const list<Symbol>& keys) { | 
					
						
							|  |  |  | 		Point2 pt1, pt2; | 
					
						
							|  |  |  | 		pt1 = config[simulated2D::PointKey(keys.front().index())]; | 
					
						
							|  |  |  | 		pt2 = config[simulated2D::PointKey(keys.back().index())]; | 
					
						
							|  |  |  | 		return Vector_(2, pt1.x() - pt2.x(), pt1.y() - pt2.y()); | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 11:05:47 +08:00
										 |  |  | 	/** jacobian at l1 */ | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	Matrix G1(const Config2D& config, const list<Symbol>& keys) { | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 		return eye(2); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 11:05:47 +08:00
										 |  |  | 	/** jacobian at l2 */ | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	Matrix G2(const Config2D& config, const list<Symbol>& keys) { | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 		return -1 * eye(2); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-11-20 13:13:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // \namespace sqp_test1
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | //namespace sqp_test2 {
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	// Unary Constraint on x1
 | 
					
						
							|  |  |  | //	/** g(x) = x -[1;1] = 0 */
 | 
					
						
							|  |  |  | //	Vector g(const Config2D& config, const list<Symbol>& keys) {
 | 
					
						
							|  |  |  | //		return config[keys.front()] - Vector_(2, 1.0, 1.0);
 | 
					
						
							|  |  |  | //	}
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	/** jacobian at x1 */
 | 
					
						
							|  |  |  | //	Matrix G(const Config2D& config, const list<Symbol>& keys) {
 | 
					
						
							|  |  |  | //		return eye(2);
 | 
					
						
							|  |  |  | //	}
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //} // \namespace sqp_test2
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef NonlinearConstraint2< | 
					
						
							|  |  |  | 	Config2D, simulated2D::PointKey, Point2, simulated2D::PointKey, Point2> NLC2; | 
					
						
							| 
									
										
										
										
											2009-11-22 03:05:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | /* *********************************************************************
 | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  |  *  Version that actually uses nonlinear equality constraints | 
					
						
							|  |  |  |  *  to to perform optimization.  Same as above, but no | 
					
						
							|  |  |  |  *  equality constraint on x2, and two landmarks that | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  |  *  should be the same. Note that this is a linear system, | 
					
						
							|  |  |  |  *  so it will converge in one step. | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | TEST (SQP, two_pose ) { | 
					
						
							| 
									
										
										
										
											2009-11-20 22:29:11 +08:00
										 |  |  | 	bool verbose = false; | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	// create the graph
 | 
					
						
							|  |  |  | 	shared_ptr<NLGraph> graph(new NLGraph); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// add the constraints on the ends
 | 
					
						
							|  |  |  | 	// position (1, 1) constraint for x1
 | 
					
						
							|  |  |  | 	// position (5, 6) constraint for x2
 | 
					
						
							|  |  |  | 	simulated2D::PoseKey x1(1), x2(2); | 
					
						
							|  |  |  | 	simulated2D::PointKey l1(1), l2(2); | 
					
						
							|  |  |  | 	Point2 pt_x1(1.0, 1.0), | 
					
						
							|  |  |  | 		   pt_x2(5.0, 6.0); | 
					
						
							|  |  |  | 	shared_ptr<NLE> ef1(new NLE(x1, pt_x1)); | 
					
						
							|  |  |  | 	graph->push_back(ef1); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// measurement from x1 to l1
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	Point2 z1(0.0, 5.0); | 
					
						
							| 
									
										
										
										
											2010-01-23 01:36:57 +08:00
										 |  |  | 	SharedGaussian sigma(noiseModel::Isotropic::Sigma(2, 0.1)); | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	shared f1(new simulated2D::Measurement(z1, sigma, x1,l1)); | 
					
						
							|  |  |  | 	graph->push_back(f1); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// measurement from x2 to l2
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	Point2 z2(-4.0, 0.0); | 
					
						
							|  |  |  | 	shared f2(new simulated2D::Measurement(z2, sigma, x2,l2)); | 
					
						
							|  |  |  | 	graph->push_back(f2); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// equality constraint between l1 and l2
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	list<Symbol> keys2; keys2 += "l1", "l2"; | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	boost::shared_ptr<NLC2 > c2(new NLC2( | 
					
						
							| 
									
										
										
										
											2009-12-18 10:39:02 +08:00
										 |  |  | 					boost::bind(sqp_test1::g, _1, keys2), | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 					l1, boost::bind(sqp_test1::G1, _1, keys2), | 
					
						
							|  |  |  | 					l2, boost::bind(sqp_test1::G2, _1, keys2), | 
					
						
							|  |  |  | 					2, "L1")); | 
					
						
							|  |  |  | 	graph->push_back(c2); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create an initial estimate
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	shared_ptr<Config2D> initialEstimate(new Config2D); | 
					
						
							|  |  |  | 	initialEstimate->insert(x1, pt_x1); | 
					
						
							|  |  |  | 	initialEstimate->insert(x2, Point2()); | 
					
						
							|  |  |  | 	initialEstimate->insert(l1, Point2(1.0, 6.0)); // ground truth
 | 
					
						
							|  |  |  | 	initialEstimate->insert(l2, Point2(-4.0, 0.0)); // starting with a separate reference frame
 | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create an initial estimate for the lagrange multiplier
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	shared_ptr<VectorConfig> initLagrange(new VectorConfig); | 
					
						
							|  |  |  | 	initLagrange->insert(LamKey(1), Vector_(2, 1.0, 1.0)); // connect the landmarks
 | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create state config variables and initialize them
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	Config2D state(*initialEstimate); | 
					
						
							|  |  |  | 	VectorConfig state_lambda(*initLagrange); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// optimization loop
 | 
					
						
							|  |  |  | 	int maxIt = 1; | 
					
						
							|  |  |  | 	for (int i = 0; i<maxIt; ++i) { | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  |  		// linearize the graph
 | 
					
						
							|  |  |  | 		GaussianFactorGraph fg; | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 		typedef FactorGraph<NonlinearFactor<Config2D> >::const_iterator const_iterator; | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 		// iterate over all factors
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 		for (const_iterator factor = graph->begin(); factor < graph->end(); factor++) { | 
					
						
							|  |  |  | 			const shared_ptr<NLC2> constraint = boost::shared_dynamic_cast<NLC2>(*factor); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 			if (constraint == NULL) { | 
					
						
							|  |  |  | 				// if a regular factor, linearize using the default linearization
 | 
					
						
							|  |  |  | 				GaussianFactor::shared_ptr f = (*factor)->linearize(state); | 
					
						
							|  |  |  | 				fg.push_back(f); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				// if a constraint, linearize using the constraint method (2 configs)
 | 
					
						
							|  |  |  | 				GaussianFactor::shared_ptr f, c; | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 				boost::tie(f,c) = constraint->linearize(state, state_lambda); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 				fg.push_back(f); | 
					
						
							|  |  |  | 				fg.push_back(c); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 22:29:11 +08:00
										 |  |  | 		if (verbose) fg.print("Linearized graph"); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// create an ordering
 | 
					
						
							|  |  |  | 		Ordering ordering; | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 		ordering += "x1", "x2", "l1", "l2", "L1"; | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// optimize linear graph to get full delta config
 | 
					
						
							| 
									
										
										
										
											2009-11-29 05:00:09 +08:00
										 |  |  | 		VectorConfig delta = fg.optimize(ordering); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:29:11 +08:00
										 |  |  | 		if (verbose) delta.print("Delta Config"); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// update both state variables
 | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | 		state = expmap(state, delta); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:29:11 +08:00
										 |  |  | 		if (verbose) state.print("newState"); | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | 		state_lambda = expmap(state_lambda, delta); | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 		if (verbose) state_lambda.print("newStateLam"); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// verify
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	Config2D expected; | 
					
						
							|  |  |  | 	expected.insert(x1, pt_x1); | 
					
						
							|  |  |  | 	expected.insert(l1, Point2(1.0, 6.0)); | 
					
						
							|  |  |  | 	expected.insert(l2, Point2(1.0, 6.0)); | 
					
						
							|  |  |  | 	expected.insert(x2, Point2(5.0, 6.0)); | 
					
						
							| 
									
										
										
										
											2009-11-20 22:05:21 +08:00
										 |  |  | 	CHECK(assert_equal(expected, state, 1e-5)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-11-20 00:50:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | /* ********************************************************************* */ | 
					
						
							|  |  |  | // VSLAM Examples
 | 
					
						
							|  |  |  | /* ********************************************************************* */ | 
					
						
							|  |  |  | // make a realistic calibration matrix
 | 
					
						
							|  |  |  | double fov = 60; // degrees
 | 
					
						
							|  |  |  | size_t w=640,h=480; | 
					
						
							|  |  |  | Cal3_S2 K(fov,w,h); | 
					
						
							|  |  |  | boost::shared_ptr<Cal3_S2> shK(new Cal3_S2(K)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | using namespace gtsam::visualSLAM; | 
					
						
							|  |  |  | using namespace boost; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | // typedefs for visual SLAM example
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | typedef visualSLAM::Config VConfig; | 
					
						
							|  |  |  | typedef visualSLAM::Graph VGraph; | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | typedef boost::shared_ptr<ProjectionFactor> shared_vf; | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | typedef NonlinearOptimizer<VGraph,VConfig> VOptimizer; | 
					
						
							|  |  |  | typedef SQPOptimizer<VGraph, VConfig> VSOptimizer; | 
					
						
							|  |  |  | typedef NonlinearConstraint2< | 
					
						
							|  |  |  | 	VConfig, visualSLAM::PointKey, Pose3, visualSLAM::PointKey, Pose3> VNLC2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  |  * Ground truth for a visual SLAM example with stereo vision | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | TEST (SQP, stereo_truth ) { | 
					
						
							|  |  |  | 	bool verbose = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create initial estimates
 | 
					
						
							|  |  |  | 	Rot3 faceDownY(Matrix_(3,3, | 
					
						
							|  |  |  | 				1.0, 0.0, 0.0, | 
					
						
							|  |  |  | 				0.0, 0.0, 1.0, | 
					
						
							|  |  |  | 				0.0, 1.0, 0.0)); | 
					
						
							|  |  |  | 	Pose3 pose1(faceDownY, Point3()); // origin, left camera
 | 
					
						
							|  |  |  | 	SimpleCamera camera1(K, pose1); | 
					
						
							|  |  |  | 	Pose3 pose2(faceDownY, Point3(2.0, 0.0, 0.0)); // 2 units to the left
 | 
					
						
							|  |  |  | 	SimpleCamera camera2(K, pose2); | 
					
						
							|  |  |  | 	Point3 landmark(1.0, 5.0, 0.0); //centered between the cameras, 5 units away
 | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 	Point3 landmarkNoisy(1.0, 6.0, 0.0); | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 	// create truth config
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	boost::shared_ptr<VConfig> truthConfig(new Config); | 
					
						
							| 
									
										
										
										
											2010-01-14 10:58:29 +08:00
										 |  |  | 	truthConfig->insert(1, camera1.pose()); | 
					
						
							|  |  |  | 	truthConfig->insert(2, camera2.pose()); | 
					
						
							|  |  |  | 	truthConfig->insert(1, landmark); | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create graph
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	shared_ptr<VGraph> graph(new VGraph()); | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create equality constraints for poses
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	graph->push_back(shared_ptr<PoseConstraint>(new PoseConstraint(1, camera1.pose()))); | 
					
						
							|  |  |  | 	graph->push_back(shared_ptr<PoseConstraint>(new PoseConstraint(2, camera2.pose()))); | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create VSLAM factors
 | 
					
						
							|  |  |  | 	Point2 z1 = camera1.project(landmark); | 
					
						
							|  |  |  | 	if (verbose) z1.print("z1"); | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	shared_vf vf1(new ProjectionFactor(z1, 1.0, 1, 1, shK)); | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | 	graph->push_back(vf1); | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | 	Point2 z2 = camera2.project(landmark); | 
					
						
							|  |  |  | 	if (verbose) z2.print("z2"); | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	shared_vf vf2(new ProjectionFactor(z2, 1.0, 2, 1, shK)); | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | 	graph->push_back(vf2); | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | 	if (verbose) graph->print("Graph after construction"); | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create ordering
 | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | 	shared_ptr<Ordering> ord(new Ordering()); | 
					
						
							|  |  |  | 	*ord += "x1", "x2", "l1"; | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create optimizer
 | 
					
						
							| 
									
										
										
										
											2010-01-20 10:28:23 +08:00
										 |  |  | 	VOptimizer::shared_solver solver(new VOptimizer::solver(ord)); | 
					
						
							|  |  |  | 	VOptimizer optimizer(graph, truthConfig, solver); | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// optimize
 | 
					
						
							|  |  |  | 	VOptimizer afterOneIteration = optimizer.iterate(); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// verify
 | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | 	DOUBLES_EQUAL(0.0, optimizer.error(), 1e-9); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// check if correct
 | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 	if (verbose) afterOneIteration.config()->print("After iteration"); | 
					
						
							| 
									
										
										
										
											2009-11-24 00:45:53 +08:00
										 |  |  | 	CHECK(assert_equal(*truthConfig,*(afterOneIteration.config()))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | /* *********************************************************************
 | 
					
						
							|  |  |  |  * Ground truth for a visual SLAM example with stereo vision | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  |  * with some noise injected into the initial config | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | TEST (SQP, stereo_truth_noisy ) { | 
					
						
							|  |  |  | 	bool verbose = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-25 00:03:57 +08:00
										 |  |  | 	// setting to determine how far away the noisy landmark is,
 | 
					
						
							|  |  |  | 	// given that the ground truth is 5m in front of the cameras
 | 
					
						
							| 
									
										
										
										
											2009-12-01 01:36:34 +08:00
										 |  |  | 	double noisyDist = 7.6; | 
					
						
							| 
									
										
										
										
											2009-11-25 00:03:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 	// create initial estimates
 | 
					
						
							|  |  |  | 	Rot3 faceDownY(Matrix_(3,3, | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 			1.0, 0.0, 0.0, | 
					
						
							|  |  |  | 			0.0, 0.0, 1.0, | 
					
						
							|  |  |  | 			0.0, 1.0, 0.0)); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 	Pose3 pose1(faceDownY, Point3()); // origin, left camera
 | 
					
						
							|  |  |  | 	SimpleCamera camera1(K, pose1); | 
					
						
							|  |  |  | 	Pose3 pose2(faceDownY, Point3(2.0, 0.0, 0.0)); // 2 units to the left
 | 
					
						
							|  |  |  | 	SimpleCamera camera2(K, pose2); | 
					
						
							|  |  |  | 	Point3 landmark(1.0, 5.0, 0.0); //centered between the cameras, 5 units away
 | 
					
						
							| 
									
										
										
										
											2009-11-25 00:03:57 +08:00
										 |  |  | 	Point3 landmarkNoisy(1.0, noisyDist, 0.0); // initial point is too far out
 | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create truth config
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	boost::shared_ptr<Config> truthConfig(new Config); | 
					
						
							| 
									
										
										
										
											2010-01-14 10:58:29 +08:00
										 |  |  | 	truthConfig->insert(1, camera1.pose()); | 
					
						
							|  |  |  | 	truthConfig->insert(2, camera2.pose()); | 
					
						
							|  |  |  | 	truthConfig->insert(1, landmark); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create config
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	boost::shared_ptr<Config> noisyConfig(new Config); | 
					
						
							| 
									
										
										
										
											2010-01-14 10:58:29 +08:00
										 |  |  | 	noisyConfig->insert(1, camera1.pose()); | 
					
						
							|  |  |  | 	noisyConfig->insert(2, camera2.pose()); | 
					
						
							|  |  |  | 	noisyConfig->insert(1, landmarkNoisy); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create graph
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	shared_ptr<Graph> graph(new Graph()); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create equality constraints for poses
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	graph->push_back(shared_ptr<PoseConstraint>(new PoseConstraint(1, camera1.pose()))); | 
					
						
							|  |  |  | 	graph->push_back(shared_ptr<PoseConstraint>(new PoseConstraint(2, camera2.pose()))); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create VSLAM factors
 | 
					
						
							|  |  |  | 	Point2 z1 = camera1.project(landmark); | 
					
						
							|  |  |  | 	if (verbose) z1.print("z1"); | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	shared_vf vf1(new ProjectionFactor(z1, 1.0, 1, 1, shK)); | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | 	graph->push_back(vf1); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 	Point2 z2 = camera2.project(landmark); | 
					
						
							|  |  |  | 	if (verbose) z2.print("z2"); | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	shared_vf vf2(new ProjectionFactor(z2, 1.0, 2, 1, shK)); | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | 	graph->push_back(vf2); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (verbose)  { | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | 		graph->print("Graph after construction"); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 		noisyConfig->print("Initial config"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create ordering
 | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | 	shared_ptr<Ordering> ord(new Ordering()); | 
					
						
							|  |  |  | 	*ord += "x1", "x2", "l1"; | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create optimizer
 | 
					
						
							| 
									
										
										
										
											2010-01-20 10:28:23 +08:00
										 |  |  | 	VOptimizer::shared_solver solver(new VOptimizer::solver(ord)); | 
					
						
							|  |  |  | 	VOptimizer optimizer0(graph, noisyConfig, solver); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-25 00:03:57 +08:00
										 |  |  | 	if (verbose) | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | 		cout << "Initial Error: " << optimizer0.error() << endl; | 
					
						
							| 
									
										
										
										
											2009-11-25 00:03:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-01 01:36:34 +08:00
										 |  |  | 	// use Levenberg-Marquardt optimization
 | 
					
						
							| 
									
										
										
										
											2009-11-25 00:03:57 +08:00
										 |  |  | 	double relThresh = 1e-5, absThresh = 1e-5; | 
					
						
							| 
									
										
										
										
											2010-01-12 04:17:28 +08:00
										 |  |  | 	VOptimizer optimizer(optimizer0.levenbergMarquardt(relThresh, absThresh, VOptimizer::SILENT)); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-25 00:03:57 +08:00
										 |  |  | 	// verify
 | 
					
						
							|  |  |  | 	DOUBLES_EQUAL(0.0, optimizer.error(), 1e-9); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// check if correct
 | 
					
						
							|  |  |  | 	if (verbose) { | 
					
						
							|  |  |  | 		optimizer.config()->print("After iteration"); | 
					
						
							|  |  |  | 		cout << "Final error: " << optimizer.error() << endl; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	CHECK(assert_equal(*truthConfig,*(optimizer.config()))); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:14:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | /* ********************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | namespace sqp_stereo { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 	// binary constraint between landmarks
 | 
					
						
							|  |  |  | 	/** g(x) = x-y = 0 */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	Vector g(const Config& config, const list<Symbol>& keys) { | 
					
						
							|  |  |  | 		return config[PointKey(keys.front().index())].vector() | 
					
						
							|  |  |  | 				- config[PointKey(keys.back().index())].vector(); | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 11:05:47 +08:00
										 |  |  | 	/** jacobian at l1 */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	Matrix G1(const Config& config, const list<Symbol>& keys) { | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 		return eye(3); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 11:05:47 +08:00
										 |  |  | 	/** jacobian at l2 */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	Matrix G2(const Config& config, const list<Symbol>& keys) { | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | 		return -1.0 * eye(3); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // \namespace sqp_stereo
 | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | /* ********************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | VGraph stereoExampleGraph() { | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 	// create initial estimates
 | 
					
						
							|  |  |  | 	Rot3 faceDownY(Matrix_(3,3, | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 			1.0, 0.0, 0.0, | 
					
						
							|  |  |  | 			0.0, 0.0, 1.0, | 
					
						
							|  |  |  | 			0.0, 1.0, 0.0)); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 	Pose3 pose1(faceDownY, Point3()); // origin, left camera
 | 
					
						
							|  |  |  | 	SimpleCamera camera1(K, pose1); | 
					
						
							|  |  |  | 	Pose3 pose2(faceDownY, Point3(2.0, 0.0, 0.0)); // 2 units to the left
 | 
					
						
							|  |  |  | 	SimpleCamera camera2(K, pose2); | 
					
						
							|  |  |  | 	Point3 landmark1(1.0, 5.0, 0.0); //centered between the cameras, 5 units away
 | 
					
						
							|  |  |  | 	Point3 landmark2(1.0, 5.0, 0.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create graph
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	VGraph graph; | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create equality constraints for poses
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	graph.push_back(shared_ptr<PoseConstraint>(new PoseConstraint(1, camera1.pose()))); | 
					
						
							|  |  |  | 	graph.push_back(shared_ptr<PoseConstraint>(new PoseConstraint(2, camera2.pose()))); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	// create  factors
 | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 	Point2 z1 = camera1.project(landmark1); | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	shared_vf vf1(new ProjectionFactor(z1, 1.0, 1, 1, shK)); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 	graph.push_back(vf1); | 
					
						
							|  |  |  | 	Point2 z2 = camera2.project(landmark2); | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	shared_vf vf2(new ProjectionFactor(z2, 1.0, 2, 2, shK)); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 	graph.push_back(vf2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create the binary equality constraint between the landmarks
 | 
					
						
							|  |  |  | 	// NOTE: this is really just a linear constraint that is exactly the same
 | 
					
						
							|  |  |  | 	// as the previous examples
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	list<Symbol> keys; keys += "l1", "l2"; | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	visualSLAM::PointKey l1(1), l2(2); | 
					
						
							|  |  |  | 	shared_ptr<VNLC2> c2( | 
					
						
							|  |  |  | 			new VNLC2(boost::bind(sqp_stereo::g, _1, keys), | 
					
						
							|  |  |  | 					 l1, boost::bind(sqp_stereo::G1, _1, keys), | 
					
						
							|  |  |  | 					 l2, boost::bind(sqp_stereo::G2, _1, keys), | 
					
						
							|  |  |  | 					 3, "L12")); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 	graph.push_back(c2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return graph; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | /* ********************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | boost::shared_ptr<VConfig> stereoExampleTruthConfig() { | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 	// create initial estimates
 | 
					
						
							|  |  |  | 	Rot3 faceDownY(Matrix_(3,3, | 
					
						
							|  |  |  | 				1.0, 0.0, 0.0, | 
					
						
							|  |  |  | 				0.0, 0.0, 1.0, | 
					
						
							|  |  |  | 				0.0, 1.0, 0.0)); | 
					
						
							|  |  |  | 	Pose3 pose1(faceDownY, Point3()); // origin, left camera
 | 
					
						
							|  |  |  | 	SimpleCamera camera1(K, pose1); | 
					
						
							|  |  |  | 	Pose3 pose2(faceDownY, Point3(2.0, 0.0, 0.0)); // 2 units to the left
 | 
					
						
							|  |  |  | 	SimpleCamera camera2(K, pose2); | 
					
						
							|  |  |  | 	Point3 landmark1(1.0, 5.0, 0.0); //centered between the cameras, 5 units away
 | 
					
						
							|  |  |  | 	Point3 landmark2(1.0, 5.0, 0.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create config
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	boost::shared_ptr<Config> truthConfig(new Config); | 
					
						
							| 
									
										
										
										
											2010-01-14 10:58:29 +08:00
										 |  |  | 	truthConfig->insert(1, camera1.pose()); | 
					
						
							|  |  |  | 	truthConfig->insert(2, camera2.pose()); | 
					
						
							|  |  |  | 	truthConfig->insert(1, landmark1); | 
					
						
							|  |  |  | 	truthConfig->insert(2, landmark2); // create two landmarks in same place
 | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return truthConfig; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | /* *********************************************************************
 | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  |  * SQP version of the above stereo example, | 
					
						
							|  |  |  |  * with the initial case as the ground truth | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | TEST (SQP, stereo_sqp ) { | 
					
						
							|  |  |  | 	bool verbose = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// get a graph
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	VGraph graph = stereoExampleGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 	if (verbose) graph.print("Graph after construction"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// get the truth config
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	boost::shared_ptr<VConfig> truthConfig = stereoExampleTruthConfig(); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create ordering
 | 
					
						
							|  |  |  | 	Ordering ord; | 
					
						
							|  |  |  | 	ord += "x1", "x2", "l1", "l2"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create optimizer
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	VSOptimizer optimizer(graph, ord, truthConfig); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// optimize
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	VSOptimizer afterOneIteration = optimizer.iterate(); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// check if correct
 | 
					
						
							|  |  |  | 	CHECK(assert_equal(*truthConfig,*(afterOneIteration.config()))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | /* *********************************************************************
 | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  |  * SQP version of the above stereo example, | 
					
						
							|  |  |  |  * with noise in the initial estimate | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | TEST (SQP, stereo_sqp_noisy ) { | 
					
						
							|  |  |  | 	bool verbose = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// get a graph
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	Graph graph = stereoExampleGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create initial data
 | 
					
						
							|  |  |  | 	Rot3 faceDownY(Matrix_(3,3, | 
					
						
							|  |  |  | 			1.0, 0.0, 0.0, | 
					
						
							|  |  |  | 			0.0, 0.0, 1.0, | 
					
						
							|  |  |  | 			0.0, 1.0, 0.0)); | 
					
						
							|  |  |  | 	Pose3 pose1(faceDownY, Point3()); // origin, left camera
 | 
					
						
							|  |  |  | 	Pose3 pose2(faceDownY, Point3(2.0, 0.0, 0.0)); // 2 units to the left
 | 
					
						
							|  |  |  | 	Point3 landmark1(0.5, 5.0, 0.0); //centered between the cameras, 5 units away
 | 
					
						
							|  |  |  | 	Point3 landmark2(1.5, 5.0, 0.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// noisy config
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	boost::shared_ptr<Config> initConfig(new Config); | 
					
						
							| 
									
										
										
										
											2010-01-14 10:58:29 +08:00
										 |  |  | 	initConfig->insert(1, pose1); | 
					
						
							|  |  |  | 	initConfig->insert(2, pose2); | 
					
						
							|  |  |  | 	initConfig->insert(1, landmark1); | 
					
						
							|  |  |  | 	initConfig->insert(2, landmark2); // create two landmarks in same place
 | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create ordering
 | 
					
						
							|  |  |  | 	Ordering ord; | 
					
						
							|  |  |  | 	ord += "x1", "x2", "l1", "l2"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create optimizer
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	VSOptimizer optimizer(graph, ord, initConfig); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// optimize
 | 
					
						
							|  |  |  | 	double start_error = optimizer.error(); | 
					
						
							|  |  |  | 	int maxIt = 2; | 
					
						
							|  |  |  | 	for (int i=0; i<maxIt; ++i) { | 
					
						
							|  |  |  | 		if (verbose) cout << "\n ************************** \n" | 
					
						
							|  |  |  | 						  << " Iteration: " << i << endl; | 
					
						
							|  |  |  | 		//if (verbose) optimizer.graph()->print();
 | 
					
						
							|  |  |  | 		if (verbose) optimizer.config()->print(); | 
					
						
							|  |  |  | 		if (verbose) | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 			optimizer = optimizer.iterate(VSOptimizer::FULL); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 			optimizer = optimizer.iterate(VSOptimizer::SILENT); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (verbose) cout << "Initial Error: " << start_error << "\n" | 
					
						
							|  |  |  | 					  << "Final Error:   " << optimizer.error() << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// get the truth config
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	boost::shared_ptr<Config> truthConfig = stereoExampleTruthConfig(); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (verbose) { | 
					
						
							|  |  |  | 		initConfig->print("Initial Config"); | 
					
						
							|  |  |  | 		truthConfig->print("Truth Config"); | 
					
						
							|  |  |  | 		optimizer.config()->print("After optimization"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// check if correct
 | 
					
						
							| 
									
										
										
										
											2009-12-01 01:36:34 +08:00
										 |  |  | 	CHECK(assert_equal(*truthConfig,*(optimizer.config()))); | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 13:34:49 +08:00
										 |  |  | /* *********************************************************************
 | 
					
						
							| 
									
										
										
										
											2009-11-24 22:44:46 +08:00
										 |  |  |  * SQP version of the above stereo example, | 
					
						
							|  |  |  |  * with noise in the initial estimate and manually specified | 
					
						
							|  |  |  |  * lagrange multipliers | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | TEST (SQP, stereo_sqp_noisy_manualLagrange ) { | 
					
						
							|  |  |  | 	bool verbose = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// get a graph
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	Graph graph = stereoExampleGraph(); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:44:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create initial data
 | 
					
						
							|  |  |  | 	Rot3 faceDownY(Matrix_(3,3, | 
					
						
							|  |  |  | 			1.0, 0.0, 0.0, | 
					
						
							|  |  |  | 			0.0, 0.0, 1.0, | 
					
						
							|  |  |  | 			0.0, 1.0, 0.0)); | 
					
						
							|  |  |  | 	Pose3 pose1(faceDownY, Point3()); // origin, left camera
 | 
					
						
							|  |  |  | 	Pose3 pose2(faceDownY, Point3(2.0, 0.0, 0.0)); // 2 units to the left
 | 
					
						
							|  |  |  | 	Point3 landmark1(0.5, 5.0, 0.0); //centered between the cameras, 5 units away
 | 
					
						
							|  |  |  | 	Point3 landmark2(1.5, 5.0, 0.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// noisy config
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	boost::shared_ptr<Config> initConfig(new Config); | 
					
						
							| 
									
										
										
										
											2010-01-14 10:58:29 +08:00
										 |  |  | 	initConfig->insert(1, pose1); | 
					
						
							|  |  |  | 	initConfig->insert(2, pose2); | 
					
						
							|  |  |  | 	initConfig->insert(1, landmark1); | 
					
						
							|  |  |  | 	initConfig->insert(2, landmark2); // create two landmarks in same place
 | 
					
						
							| 
									
										
										
										
											2009-11-24 22:44:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create ordering with lagrange multiplier included
 | 
					
						
							|  |  |  | 	Ordering ord; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	ord += "x1", "x2", "l1", "l2", "L12"; | 
					
						
							| 
									
										
										
										
											2009-11-24 22:44:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create lagrange multipliers
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	VSOptimizer::shared_vconfig initLagrangeConfig(new VectorConfig); | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	initLagrangeConfig->insert("L12", Vector_(3, 0.0, 0.0, 0.0)); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:44:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create optimizer
 | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 	VSOptimizer optimizer(graph, ord, initConfig, initLagrangeConfig); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:44:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// optimize
 | 
					
						
							|  |  |  | 	double start_error = optimizer.error(); | 
					
						
							|  |  |  | 	int maxIt = 5; | 
					
						
							| 
									
										
										
										
											2009-12-01 01:36:34 +08:00
										 |  |  | 	for (int i=0; i<maxIt; ++i) { | 
					
						
							|  |  |  | 		if (verbose) { | 
					
						
							|  |  |  | 			cout << "\n ************************** \n" | 
					
						
							|  |  |  | 				 << " Iteration: " << i << endl; | 
					
						
							|  |  |  | 			optimizer.config()->print("Config Before Iteration"); | 
					
						
							|  |  |  | 			optimizer.configLagrange()->print("Lagrange Before Iteration"); | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 			optimizer = optimizer.iterate(VSOptimizer::FULL); | 
					
						
							| 
									
										
										
										
											2009-12-01 01:36:34 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | 			optimizer = optimizer.iterate(VSOptimizer::SILENT); | 
					
						
							| 
									
										
										
										
											2009-12-01 01:36:34 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-11-24 22:44:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (verbose) cout << "Initial Error: " << start_error << "\n" | 
					
						
							|  |  |  | 					  << "Final Error:   " << optimizer.error() << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// get the truth config
 | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 	boost::shared_ptr<Config> truthConfig = stereoExampleTruthConfig(); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:44:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (verbose) { | 
					
						
							|  |  |  | 		initConfig->print("Initial Config"); | 
					
						
							|  |  |  | 		truthConfig->print("Truth Config"); | 
					
						
							|  |  |  | 		optimizer.config()->print("After optimization"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// check if correct
 | 
					
						
							| 
									
										
										
										
											2009-12-01 01:36:34 +08:00
										 |  |  | 	CHECK(assert_equal(*truthConfig,*(optimizer.config()))); | 
					
						
							| 
									
										
										
										
											2009-11-24 22:44:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-17 03:12:56 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr); } | 
					
						
							|  |  |  | /* ************************************************************************* */ |