| 
									
										
										
										
											2014-12-23 08:49:32 +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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-23 07:35:22 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |  * @file    NonlinearInequalityFactorGraph.h | 
					
						
							|  |  |  |  * @author  Duy-Nguyen Ta | 
					
						
							|  |  |  |  * @author  Krunal Chande | 
					
						
							|  |  |  |  * @author  Luca Carlone | 
					
						
							|  |  |  |  * @date    Dec 15, 2014 | 
					
						
							| 
									
										
										
										
											2014-12-23 07:35:22 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2014-12-24 03:44:43 +08:00
										 |  |  | #include <gtsam/linear/VectorValues.h>
 | 
					
						
							| 
									
										
										
										
											2014-12-23 07:56:19 +08:00
										 |  |  | #include <gtsam_unstable/linear/LinearInequalityFactorGraph.h>
 | 
					
						
							| 
									
										
										
										
											2014-12-24 03:44:43 +08:00
										 |  |  | #include <gtsam_unstable/nonlinear/NonlinearConstraint.h>
 | 
					
						
							| 
									
										
										
										
											2014-12-23 07:35:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | class NonlinearInequalityFactorGraph : public FactorGraph<NonlinearFactor> { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-12-24 03:44:43 +08:00
										 |  |  |   /// Default constructor
 | 
					
						
							| 
									
										
										
										
											2014-12-23 07:35:22 +08:00
										 |  |  |   NonlinearInequalityFactorGraph() { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-21 11:19:14 +08:00
										 |  |  |   /// Linearize to a LinearInequalityFactorGraph
 | 
					
						
							| 
									
										
										
										
											2014-12-23 07:35:22 +08:00
										 |  |  |   LinearInequalityFactorGraph::shared_ptr linearize( | 
					
						
							|  |  |  |       const Values& linearizationPoint) const { | 
					
						
							|  |  |  |     LinearInequalityFactorGraph::shared_ptr linearGraph( | 
					
						
							|  |  |  |         new LinearInequalityFactorGraph()); | 
					
						
							|  |  |  |     BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, *this){ | 
					
						
							|  |  |  |       JacobianFactor::shared_ptr jacobian = boost::dynamic_pointer_cast<JacobianFactor>( | 
					
						
							|  |  |  |           factor->linearize(linearizationPoint)); | 
					
						
							|  |  |  |       NonlinearConstraint::shared_ptr constraint = boost::dynamic_pointer_cast<NonlinearConstraint>(factor); | 
					
						
							|  |  |  |       linearGraph->add(LinearInequality(*jacobian, constraint->dualKey())); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return linearGraph; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Return true if the all errors are <= 0.0 | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   bool checkFeasibilityAndComplimentary(const Values& values, const VectorValues& duals, double tol) const { | 
					
						
							|  |  |  |     BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, *this){ | 
					
						
							|  |  |  |       NoiseModelFactor::shared_ptr noiseModelFactor = boost::dynamic_pointer_cast<NoiseModelFactor>( | 
					
						
							|  |  |  |           factor); | 
					
						
							|  |  |  |       Vector error = noiseModelFactor->unwhitenedError(values); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Primal feasibility condition: all constraints need to be <= 0.0
 | 
					
						
							|  |  |  |       if (error[0] > tol) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Complimentary condition: errors of active constraints need to be 0.0
 | 
					
						
							|  |  |  |       NonlinearConstraint::shared_ptr constraint = boost::dynamic_pointer_cast<NonlinearConstraint>( | 
					
						
							|  |  |  |           factor); | 
					
						
							|  |  |  |       Key dualKey = constraint->dualKey(); | 
					
						
							|  |  |  |       if (!duals.exists(dualKey)) continue;  // if dualKey doesn't exist, it is an inactive constraint!
 | 
					
						
							|  |  |  |       if (fabs(error[0]) > tol) // for active constraint, the error should be 0.0
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | } |