| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * AllDiff.h | 
					
						
							|  |  |  |  * @brief General "all-different" constraint | 
					
						
							|  |  |  |  * @date Feb 6, 2012 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-16 07:12:17 +08:00
										 |  |  | #include <gtsam/discrete/DiscreteKey.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  | #include <gtsam_unstable/discrete/BinaryAllDiff.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * General AllDiff constraint | 
					
						
							|  |  |  |  * Returns 1 if values for all keys are different, 0 otherwise | 
					
						
							|  |  |  |  * DiscreteFactors are all awkward in that they have to store two types of keys: | 
					
						
							|  |  |  |  * for each variable we have a Key and an Key. In this factor, we | 
					
						
							|  |  |  |  * keep the Indices locally, and the Indices are stored in IndexFactor. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class GTSAM_UNSTABLE_EXPORT AllDiff : public Constraint { | 
					
						
							|  |  |  |   std::map<Key, size_t> cardinalities_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   DiscreteKey discreteKey(size_t i) const { | 
					
						
							|  |  |  |     Key j = keys_[i]; | 
					
						
							|  |  |  |     return DiscreteKey(j, cardinalities_.at(j)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  public: | 
					
						
							|  |  |  |   /// Constructor
 | 
					
						
							|  |  |  |   AllDiff(const DiscreteKeys& dkeys); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // print
 | 
					
						
							|  |  |  |   void print(const std::string& s = "", const KeyFormatter& formatter = | 
					
						
							|  |  |  |                                             DefaultKeyFormatter) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// equals
 | 
					
						
							|  |  |  |   bool equals(const DiscreteFactor& other, double tol) const override { | 
					
						
							|  |  |  |     if (!dynamic_cast<const AllDiff*>(&other)) | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |       const AllDiff& f(static_cast<const AllDiff&>(other)); | 
					
						
							|  |  |  |       return cardinalities_.size() == f.cardinalities_.size() && | 
					
						
							|  |  |  |              std::equal(cardinalities_.begin(), cardinalities_.end(), | 
					
						
							|  |  |  |                         f.cardinalities_.begin()); | 
					
						
							| 
									
										
										
										
											2013-10-12 01:42:30 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-10-12 01:42:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   /// Calculate value = expensive !
 | 
					
						
							|  |  |  |   double operator()(const Values& values) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   /// Convert into a decisiontree, can be *very* expensive !
 | 
					
						
							|  |  |  |   DecisionTreeFactor toDecisionTreeFactor() const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   /// Multiply into a decisiontree
 | 
					
						
							|  |  |  |   DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   /*
 | 
					
						
							|  |  |  |    * Ensure Arc-consistency | 
					
						
							|  |  |  |    * Arc-consistency involves creating binaryAllDiff constraints | 
					
						
							|  |  |  |    * In which case the combinatorial hyper-arc explosion disappears. | 
					
						
							|  |  |  |    * @param j domain to be checked | 
					
						
							|  |  |  |    * @param domains all other domains | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   bool ensureArcConsistency(size_t j, | 
					
						
							|  |  |  |                             std::vector<Domain>& domains) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   /// Partially apply known values
 | 
					
						
							|  |  |  |   Constraint::shared_ptr partiallyApply(const Values&) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   /// Partially apply known values, domain version
 | 
					
						
							|  |  |  |   Constraint::shared_ptr partiallyApply( | 
					
						
							|  |  |  |       const std::vector<Domain>&) const override; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  | }  // namespace gtsam
 |