| 
									
										
										
										
											2009-11-24 03:57:35 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file SQPOptimizer.h | 
					
						
							|  |  |  |  * @brief Interface for a generic SQP-based nonlinear optimization engine | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | #include "Ordering.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-24 03:57:35 +08:00
										 |  |  | #include "VectorConfig.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * This class is an engine for performing SQP-based optimization | 
					
						
							|  |  |  |  * It stores a graph, a config, and needs a specific ordering, and | 
					
						
							|  |  |  |  * then will perform optimization iterations in a functional way. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | template<class FactorGraph, class Config> | 
					
						
							|  |  |  | class SQPOptimizer { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2009-11-24 05:25:31 +08:00
										 |  |  | 	// verbosity level
 | 
					
						
							|  |  |  | 	typedef enum { | 
					
						
							|  |  |  | 		SILENT, | 
					
						
							|  |  |  | 		FULL | 
					
						
							|  |  |  | 	} Verbosity; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-24 03:57:35 +08:00
										 |  |  | 	// useful for storing configurations
 | 
					
						
							|  |  |  | 	typedef boost::shared_ptr<const Config> shared_config; | 
					
						
							| 
									
										
										
										
											2009-11-24 06:10:52 +08:00
										 |  |  | 	typedef boost::shared_ptr<VectorConfig> shared_vconfig; | 
					
						
							| 
									
										
										
										
											2009-11-24 03:57:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2009-11-24 06:10:52 +08:00
										 |  |  | 	// keep const references to the graph and initial ordering
 | 
					
						
							| 
									
										
										
										
											2009-11-24 03:57:35 +08:00
										 |  |  | 	const FactorGraph* graph_; | 
					
						
							|  |  |  | 	const Ordering* ordering_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// keep configurations
 | 
					
						
							|  |  |  | 	shared_config config_; | 
					
						
							|  |  |  | 	shared_vconfig lagrange_config_; | 
					
						
							| 
									
										
										
										
											2009-11-24 06:10:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// keep a configuration that has been updated to include the lagrange multipliers
 | 
					
						
							|  |  |  | 	Ordering full_ordering_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// keep a set of errors for the overall system and just the constraints
 | 
					
						
							| 
									
										
										
										
											2009-11-24 03:57:35 +08:00
										 |  |  | 	double error_; | 
					
						
							| 
									
										
										
										
											2009-11-24 05:25:31 +08:00
										 |  |  | 	double constraint_error_; | 
					
						
							| 
									
										
										
										
											2009-11-24 03:57:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Standard external constructor | 
					
						
							|  |  |  | 	 * @param graph is the nonlinear graph to optimize | 
					
						
							|  |  |  | 	 * @param ordering is the elimination ordering to use | 
					
						
							|  |  |  | 	 * @param config is the initial configuration for the real variables | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	SQPOptimizer(const FactorGraph& graph, const Ordering& ordering, | 
					
						
							|  |  |  | 			shared_config config); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Constructor that includes a lagrange initialization.  Primarily | 
					
						
							|  |  |  | 	 * for internal iterations, but if the user has an idea of what a good | 
					
						
							|  |  |  | 	 * set of lagrange multipliers is, they can specify them, assuming that | 
					
						
							|  |  |  | 	 * the naming convention is the same as the internal system. | 
					
						
							|  |  |  | 	 * @param graph is the nonlinear graph to optimize | 
					
						
							|  |  |  | 	 * @param ordering is the elimination ordering to use | 
					
						
							|  |  |  | 	 * @param config is the initial configuration for the real variables | 
					
						
							|  |  |  | 	 * @param lagrange is the configuration of lagrange multipliers | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	SQPOptimizer(const FactorGraph& graph, const Ordering& ordering, | 
					
						
							|  |  |  | 			shared_config config, shared_vconfig lagrange); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/// Access functions
 | 
					
						
							|  |  |  | 	const FactorGraph* graph() const { return graph_; } | 
					
						
							|  |  |  | 	const Ordering* ordering() const { return ordering_; } | 
					
						
							|  |  |  | 	shared_config config() const { return config_; } | 
					
						
							| 
									
										
										
										
											2009-11-24 22:44:46 +08:00
										 |  |  | 	shared_vconfig configLagrange() const { return lagrange_config_; } | 
					
						
							| 
									
										
										
										
											2009-11-24 14:39:27 +08:00
										 |  |  | 	double error() const { return error_; } | 
					
						
							| 
									
										
										
										
											2009-11-24 03:57:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-24 05:25:31 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Primary optimization iteration, updates the configs | 
					
						
							|  |  |  | 	 * @return a new optimization object with updated values | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	SQPOptimizer<FactorGraph, Config> iterate(Verbosity verbosity=SILENT) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-29 06:49:14 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Iterates recursively until converence occurs | 
					
						
							|  |  |  | 	 * @param relThresh minimum change in error between iterations | 
					
						
							|  |  |  | 	 * @param absThresh  minimum error necessary to converge | 
					
						
							|  |  |  | 	 * @param constraintThresh  minimum constraint error to be feasible | 
					
						
							|  |  |  | 	 * @param maxIterations is the maximum number of iterations | 
					
						
							|  |  |  | 	 * @param verbosity controls output print statements | 
					
						
							|  |  |  | 	 * @return a new optimization object with final values | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	SQPOptimizer<FactorGraph, Config> | 
					
						
							|  |  |  | 	iterateSolve(double relThresh, double absThresh, double constraintThresh, | 
					
						
							|  |  |  | 			size_t maxIterations = 10, Verbosity verbosity=SILENT) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Checks whether convergence has occurred, and returns true if | 
					
						
							|  |  |  | 	 * the solution will not get better, based on the previous error conditions. | 
					
						
							|  |  |  | 	 * @param full_error is the error all the factors and constraints | 
					
						
							|  |  |  | 	 * @param constraint_error is the error of just the constraints | 
					
						
							|  |  |  | 	 * @param relThresh is the relative threshold between | 
					
						
							|  |  |  | 	 * @return true if the problem has converged | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	bool checkConvergence(double relThresh, double absThresh, | 
					
						
							|  |  |  | 			double constraintThresh, double full_error, double constraint_error) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-29 02:35:36 +08:00
										 |  |  | 	/** Standard print function with optional name */ | 
					
						
							|  |  |  | 	void print(const std::string& s); | 
					
						
							| 
									
										
										
										
											2009-11-24 03:57:35 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |