| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 11:15:41 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2015-02-25 22:09:33 +08:00
										 |  |  |  * @file    LinearEquality.h | 
					
						
							|  |  |  |  * @brief   LinearEquality derived from Base with constrained noise model | 
					
						
							|  |  |  |  * @date    Nov 27, 2014 | 
					
						
							|  |  |  |  * @author  Duy-Nguyen Ta | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/linear/JacobianFactor.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2015-02-25 11:15:41 +08:00
										 |  |  |  * This class defines a linear equality constraints, inheriting JacobianFactor | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |  * with the special Constrained noise model | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class LinearEquality: public JacobianFactor { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |   typedef LinearEquality 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_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |   /** default constructor for I/O */ | 
					
						
							|  |  |  |   LinearEquality() : | 
					
						
							|  |  |  |       Base() { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-19 09:47:14 +08:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Construct from a constrained noisemodel JacobianFactor with a dual key. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  |   explicit LinearEquality(const JacobianFactor& jf, Key dualKey) : | 
					
						
							|  |  |  |       Base(jf), dualKey_(dualKey) { | 
					
						
							| 
									
										
										
										
											2014-12-19 09:47:14 +08:00
										 |  |  |     if (!jf.isConstrained()) { | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  |       throw std::runtime_error( | 
					
						
							|  |  |  |           "Cannot convert an unconstrained JacobianFactor to LinearEquality"); | 
					
						
							| 
									
										
										
										
											2014-12-19 09:47:14 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   /** Conversion from HessianFactor (does Cholesky to obtain Jacobian matrix) */ | 
					
						
							|  |  |  |   explicit LinearEquality(const HessianFactor& hf) { | 
					
						
							|  |  |  |     throw std::runtime_error("Cannot convert HessianFactor to LinearEquality"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Construct unary factor */ | 
					
						
							|  |  |  |   LinearEquality(Key i1, const Matrix& A1, const Vector& b, Key dualKey) : | 
					
						
							|  |  |  |       Base(i1, A1, b, noiseModel::Constrained::All(b.rows())), dualKey_(dualKey) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Construct binary factor */ | 
					
						
							|  |  |  |   LinearEquality(Key i1, const Matrix& A1, Key i2, const Matrix& A2, | 
					
						
							|  |  |  |       const Vector& b, Key dualKey) : | 
					
						
							|  |  |  |       Base(i1, A1, i2, A2, b, noiseModel::Constrained::All(b.rows())), dualKey_( | 
					
						
							|  |  |  |           dualKey) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Construct ternary factor */ | 
					
						
							|  |  |  |   LinearEquality(Key i1, const Matrix& A1, Key i2, const Matrix& A2, Key i3, | 
					
						
							|  |  |  |       const Matrix& A3, const Vector& b, Key dualKey) : | 
					
						
							|  |  |  |       Base(i1, A1, i2, A2, i3, A3, b, noiseModel::Constrained::All(b.rows())), dualKey_( | 
					
						
							|  |  |  |           dualKey) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** 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> | 
					
						
							|  |  |  |   LinearEquality(const TERMS& terms, const Vector& b, Key dualKey) : | 
					
						
							|  |  |  |       Base(terms, b, noiseModel::Constrained::All(b.rows())), dualKey_(dualKey) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Virtual destructor */ | 
					
						
							| 
									
										
										
										
											2021-01-29 12:02:13 +08:00
										 |  |  |   ~LinearEquality() override { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** equals */ | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |   bool equals(const GaussianFactor& lf, double tol = 1e-9) const override { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |     return Base::equals(lf, tol); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** print */ | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |   void print(const std::string& s = "", const KeyFormatter& formatter = | 
					
						
							|  |  |  |       DefaultKeyFormatter) const override { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |     Base::print(s, formatter); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Clone this LinearEquality */ | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |   GaussianFactor::shared_ptr clone() const override { | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  |     return boost::static_pointer_cast < GaussianFactor | 
					
						
							|  |  |  |         > (boost::make_shared < LinearEquality > (*this)); | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// dual key
 | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  |   Key dualKey() const { | 
					
						
							|  |  |  |     return dualKey_; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 01:02:06 +08:00
										 |  |  |   /// for active set method: equality constraints are always active
 | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  |   bool active() const { | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-12-13 01:02:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Special error for constraints.
 | 
					
						
							|  |  |  |    * I think it should be zero, as this function is meant for objective cost. | 
					
						
							|  |  |  |    * But the name "error" can be misleading. | 
					
						
							|  |  |  |    * TODO: confirm with Frank!! */ | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |   double error(const VectorValues& c) const override { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |     return 0.0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | // \ LinearEquality
 | 
					
						
							| 
									
										
										
										
											2014-12-22 05:02:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /// traits
 | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  | template<> struct traits<LinearEquality> : public Testable<LinearEquality> { | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-12-22 05:02:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // \ namespace gtsam
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  | 
 |