| 
									
										
										
										
											2010-10-14 12:54:38 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation,  | 
					
						
							|  |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 11:23:14 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file SimpleRotation.cpp | 
					
						
							|  |  |  |  * @brief This is a super-simple example of optimizing a single rotation according to a single prior | 
					
						
							|  |  |  |  * @date Jul 1, 2010 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-21 23:05:40 +08:00
										 |  |  | #include <cmath>
 | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | #include <gtsam/slam/PriorFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Rot2.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/NoiseModel.h>
 | 
					
						
							| 
									
										
										
										
											2012-02-07 12:58:11 +08:00
										 |  |  | #include <gtsam/nonlinear/Symbol.h>
 | 
					
						
							| 
									
										
										
										
											2011-12-21 07:25:43 +08:00
										 |  |  | #include <gtsam/nonlinear/NonlinearFactorGraph.h>
 | 
					
						
							| 
									
										
										
										
											2012-03-25 03:53:17 +08:00
										 |  |  | #include <gtsam/nonlinear/LevenbergMarquardtOptimizer.h>
 | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2011-11-07 03:08:42 +08:00
										 |  |  |  * TODO: make factors independent of RotValues | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  |  * TODO: make toplevel documentation | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  |  * TODO: Clean up nonlinear optimization API | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const double degree = M_PI / 180; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * This example will perform a relatively trivial optimization on | 
					
						
							|  |  |  | 	 * a single variable with a single factor. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	/**
 | 
					
						
							| 
									
										
										
										
											2012-02-07 12:02:20 +08:00
										 |  |  | 	 *    Step 1: create a factor on to express a unary constraint | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	 * The "prior" in this case is the measurement from a sensor, | 
					
						
							|  |  |  | 	 * with a model of the noise on the measurement. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * The "Key" created here is a label used to associate parts of the | 
					
						
							| 
									
										
										
										
											2011-11-07 03:08:42 +08:00
										 |  |  | 	 * state (stored in "RotValues") with particular factors.  They require | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	 * an index to allow for lookup, and should be unique. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * In general, creating a factor requires: | 
					
						
							|  |  |  | 	 *  - A key or set of keys labeling the variables that are acted upon | 
					
						
							|  |  |  | 	 *  - A measurement value | 
					
						
							|  |  |  | 	 *  - A measurement model with the correct dimensionality for the factor | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	Rot2 prior = Rot2::fromAngle(30 * degree); | 
					
						
							|  |  |  | 	prior.print("goal angle"); | 
					
						
							|  |  |  | 	SharedDiagonal model = noiseModel::Isotropic::Sigma(1, 1 * degree); | 
					
						
							| 
									
										
										
										
											2012-02-07 12:02:20 +08:00
										 |  |  | 	Symbol key('x',1); | 
					
						
							|  |  |  | 	PriorFactor<Rot2> factor(key, prior, model); | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	/**
 | 
					
						
							| 
									
										
										
										
											2012-02-07 12:02:20 +08:00
										 |  |  | 	 *    Step 2: create a graph container and add the factor to it | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	 * Before optimizing, all factors need to be added to a Graph container, | 
					
						
							|  |  |  | 	 * which provides the necessary top-level functionality for defining a | 
					
						
							|  |  |  | 	 * system of constraints. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * In this case, there is only one factor, but in a practical scenario, | 
					
						
							|  |  |  | 	 * many more factors would be added. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2012-02-07 12:02:20 +08:00
										 |  |  | 	NonlinearFactorGraph graph; | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	graph.add(factor); | 
					
						
							|  |  |  | 	graph.print("full graph"); | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	/**
 | 
					
						
							| 
									
										
										
										
											2012-02-07 12:02:20 +08:00
										 |  |  | 	 *    Step 3: create an initial estimate | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	 * An initial estimate of the solution for the system is necessary to | 
					
						
							| 
									
										
										
										
											2011-11-07 03:08:42 +08:00
										 |  |  | 	 * start optimization.  This system state is the "RotValues" structure, | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	 * which is similar in structure to a STL map, in that it maps | 
					
						
							|  |  |  | 	 * keys (the label created in step 1) to specific values. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * The initial estimate provided to optimization will be used as | 
					
						
							|  |  |  | 	 * a linearization point for optimization, so it is important that | 
					
						
							|  |  |  | 	 * all of the variables in the graph have a corresponding value in | 
					
						
							|  |  |  | 	 * this structure. | 
					
						
							| 
									
										
										
										
											2011-08-13 03:24:59 +08:00
										 |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2011-11-07 03:08:42 +08:00
										 |  |  | 	 * The interface to all RotValues types is the same, it only depends | 
					
						
							| 
									
										
										
										
											2011-08-13 03:24:59 +08:00
										 |  |  | 	 * on the type of key used to find the appropriate value map if there | 
					
						
							|  |  |  | 	 * are multiple types of variables. | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2012-02-03 00:16:46 +08:00
										 |  |  | 	Values initial; | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	initial.insert(key, Rot2::fromAngle(20 * degree)); | 
					
						
							| 
									
										
										
										
											2010-10-22 05:38:38 +08:00
										 |  |  | 	initial.print("initial estimate"); | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	/**
 | 
					
						
							| 
									
										
										
										
											2012-02-07 12:02:20 +08:00
										 |  |  | 	 *    Step 4: optimize | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	 * After formulating the problem with a graph of constraints | 
					
						
							|  |  |  | 	 * and an initial estimate, executing optimization is as simple | 
					
						
							|  |  |  | 	 * as calling a general optimization function with the graph and | 
					
						
							| 
									
										
										
										
											2011-11-07 03:08:42 +08:00
										 |  |  | 	 * initial estimate.  This will yield a new RotValues structure | 
					
						
							| 
									
										
										
										
											2011-08-12 01:18:40 +08:00
										 |  |  | 	 * with the final state of the optimization. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2012-03-25 03:53:17 +08:00
										 |  |  | 	Values result = *LevenbergMarquardtOptimizer(graph, initial).optimized(); | 
					
						
							| 
									
										
										
										
											2010-10-22 05:38:38 +08:00
										 |  |  | 	result.print("final result"); | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } |