| 
									
										
										
										
											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-05-15 08:47:19 +08:00
										 |  |  | #include <gtsam_unstable/discrete/BinaryAllDiff.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 07:12:17 +08:00
										 |  |  | #include <gtsam/discrete/DiscreteKey.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * 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 Index and an Index. In this factor, we | 
					
						
							|  |  |  | 	 * keep the Indices locally, and the Indices are stored in IndexFactor. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | 	class AllDiff: public Constraint { | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		std::map<Index,size_t> cardinalities_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		DiscreteKey discreteKey(size_t i) const { | 
					
						
							|  |  |  | 			Index j = keys_[i]; | 
					
						
							|  |  |  | 			return DiscreteKey(j,cardinalities_.at(j)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/// Constructor
 | 
					
						
							|  |  |  | 		AllDiff(const DiscreteKeys& dkeys); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// print
 | 
					
						
							| 
									
										
										
										
											2012-06-30 18:30:06 +08:00
										 |  |  | 		virtual void print(const std::string& s = "", | 
					
						
							|  |  |  | 				const IndexFormatter& formatter = DefaultIndexFormatter) const; | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/// Calculate value = expensive !
 | 
					
						
							|  |  |  | 		virtual double operator()(const Values& values) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/// Convert into a decisiontree, can be *very* expensive !
 | 
					
						
							| 
									
										
										
										
											2012-06-23 05:45:36 +08:00
										 |  |  | 		virtual DecisionTreeFactor toDecisionTreeFactor() const; | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/// Multiply into a decisiontree
 | 
					
						
							|  |  |  | 		virtual DecisionTreeFactor operator*(const DecisionTreeFactor& f) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * 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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/// Partially apply known values
 | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | 		virtual Constraint::shared_ptr partiallyApply(const Values&) const; | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/// Partially apply known values, domain version
 | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | 		virtual Constraint::shared_ptr partiallyApply(const std::vector<Domain>&) const; | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace gtsam
 |