| 
									
										
										
										
											2014-12-09 19:13:57 +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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * LinearInequality.h | 
					
						
							|  |  |  |  * @brief: LinearInequality derived from Base with constrained noise model | 
					
						
							|  |  |  |  * @date: Nov 27, 2014 | 
					
						
							|  |  |  |  * @author: thduynguyen | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/linear/JacobianFactor.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 01:51:19 +08:00
										 |  |  | typedef Eigen::RowVectorXd RowVector; | 
					
						
							| 
									
										
										
										
											2014-12-13 01:43:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * This class defines Linear constraints by inherit Base | 
					
						
							|  |  |  |  * with the special Constrained noise model | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class LinearInequality: public JacobianFactor { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |   typedef LinearInequality This; ///< Typedef to this class
 | 
					
						
							|  |  |  |   typedef JacobianFactor Base; ///< Typedef to base class
 | 
					
						
							|  |  |  |   typedef boost::shared_ptr<This> shared_ptr; ///< shared_ptr to this class
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |   Key dualKey_; | 
					
						
							| 
									
										
										
										
											2014-12-13 01:01:16 +08:00
										 |  |  |   bool active_; | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |   /** default constructor for I/O */ | 
					
						
							|  |  |  |   LinearInequality() : | 
					
						
							| 
									
										
										
										
											2014-12-16 00:47:04 +08:00
										 |  |  |       Base(), active_(true) { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Conversion from HessianFactor (does Cholesky to obtain Jacobian matrix) */ | 
					
						
							|  |  |  |   explicit LinearInequality(const HessianFactor& hf) { | 
					
						
							|  |  |  |     throw std::runtime_error( | 
					
						
							|  |  |  |         "Cannot convert HessianFactor to LinearInequality"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Construct unary factor */ | 
					
						
							| 
									
										
										
										
											2014-12-13 01:43:07 +08:00
										 |  |  |   LinearInequality(Key i1, const RowVector& A1, double b, Key dualKey) : | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |       Base(i1, A1, (Vector(1) << b).finished(), noiseModel::Constrained::All(1)), dualKey_( | 
					
						
							| 
									
										
										
										
											2014-12-16 00:47:04 +08:00
										 |  |  |           dualKey), active_(true) { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Construct binary factor */ | 
					
						
							| 
									
										
										
										
											2014-12-13 01:43:07 +08:00
										 |  |  |   LinearInequality(Key i1, const RowVector& A1, Key i2, const RowVector& A2, double b, | 
					
						
							| 
									
										
										
										
											2014-12-13 01:01:16 +08:00
										 |  |  |       Key dualKey) : | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |       Base(i1, A1, i2, A2, (Vector(1) << b).finished(), noiseModel::Constrained::All(1)), dualKey_( | 
					
						
							| 
									
										
										
										
											2014-12-16 00:47:04 +08:00
										 |  |  |           dualKey), active_(true) { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Construct ternary factor */ | 
					
						
							| 
									
										
										
										
											2014-12-13 01:43:07 +08:00
										 |  |  |   LinearInequality(Key i1, const RowVector& A1, Key i2, const RowVector& A2, Key i3, | 
					
						
							|  |  |  |       const RowVector& A3, double b, Key dualKey) : | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |       Base(i1, A1, i2, A2, i3, A3, (Vector(1) << b).finished(), | 
					
						
							| 
									
										
										
										
											2014-12-16 00:47:04 +08:00
										 |  |  |           noiseModel::Constrained::All(1)), dualKey_(dualKey), active_(true) { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Construct an n-ary factor
 | 
					
						
							|  |  |  |    * @tparam TERMS A container whose value type is std::pair<Key, Matrix>, specifying the | 
					
						
							|  |  |  |    *         collection of keys and matrices making up the factor. */ | 
					
						
							|  |  |  |   template<typename TERMS> | 
					
						
							| 
									
										
										
										
											2014-12-13 01:01:16 +08:00
										 |  |  |   LinearInequality(const TERMS& terms, double b, Key dualKey) : | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |       Base(terms, (Vector(1) << b).finished(), noiseModel::Constrained::All(1)), dualKey_( | 
					
						
							| 
									
										
										
										
											2014-12-16 00:47:04 +08:00
										 |  |  |           dualKey), active_(true) { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Virtual destructor */ | 
					
						
							|  |  |  |   virtual ~LinearInequality() { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** equals */ | 
					
						
							|  |  |  |   virtual bool equals(const GaussianFactor& lf, double tol = 1e-9) const { | 
					
						
							|  |  |  |     return Base::equals(lf, tol); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** print */ | 
					
						
							|  |  |  |   virtual void print(const std::string& s = "", const KeyFormatter& formatter = | 
					
						
							|  |  |  |       DefaultKeyFormatter) const { | 
					
						
							| 
									
										
										
										
											2014-12-13 14:04:46 +08:00
										 |  |  |     if (active()) | 
					
						
							|  |  |  |       Base::print(s + "  Active", formatter); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       Base::print(s + "  Inactive", formatter); | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Clone this LinearInequality */ | 
					
						
							|  |  |  |   virtual GaussianFactor::shared_ptr clone() const { | 
					
						
							|  |  |  |     return boost::static_pointer_cast<GaussianFactor>( | 
					
						
							|  |  |  |         boost::make_shared<LinearInequality>(*this)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// dual key
 | 
					
						
							|  |  |  |   Key dualKey() const { return dualKey_; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 01:01:16 +08:00
										 |  |  |   /// return true if this constraint is active
 | 
					
						
							|  |  |  |   bool active() const { return active_; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Make this inequality constraint active
 | 
					
						
							|  |  |  |   void activate() { active_ = true; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Make this inequality constraint inactive
 | 
					
						
							|  |  |  |   void inactivate() { active_ = false; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   /** Special error_vector for constraints (A*x-b) */ | 
					
						
							|  |  |  |   Vector error_vector(const VectorValues& c) const { | 
					
						
							|  |  |  |     return unweighted_error(c); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 01:01:16 +08:00
										 |  |  |   /** Special error for single-valued inequality constraints. */ | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   virtual double error(const VectorValues& c) const { | 
					
						
							| 
									
										
										
										
											2014-12-13 01:01:16 +08:00
										 |  |  |     return error_vector(c)[0]; | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** dot product of row s with the corresponding vector in p */ | 
					
						
							| 
									
										
										
										
											2014-12-13 01:01:16 +08:00
										 |  |  |   double dotProductRow(const VectorValues& p) const { | 
					
						
							|  |  |  |     double aTp = 0.0; | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |     for (const_iterator xj = begin(); xj != end(); ++xj) { | 
					
						
							|  |  |  |       Vector pj = p.at(*xj); | 
					
						
							| 
									
										
										
										
											2014-12-13 01:01:16 +08:00
										 |  |  |       Vector aj = getA(xj).transpose(); | 
					
						
							|  |  |  |       aTp += aj.dot(pj); | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-12-13 01:01:16 +08:00
										 |  |  |     return aTp; | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-22 05:02:06 +08:00
										 |  |  | }; // \ LinearInequality
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-22 05:02:06 +08:00
										 |  |  | /// traits
 | 
					
						
							| 
									
										
										
										
											2014-12-26 23:47:51 +08:00
										 |  |  | template<> struct traits<LinearInequality> : public Testable<LinearInequality> {}; | 
					
						
							| 
									
										
										
										
											2014-12-22 05:02:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // \ namespace gtsam
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  | 
 |