| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Domain.h | 
					
						
							|  |  |  |  * @brief Domain restriction constraint | 
					
						
							|  |  |  |  * @date Feb 13, 2012 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | #include <gtsam_unstable/discrete/Constraint.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 07:12:17 +08:00
										 |  |  | #include <gtsam/discrete/DiscreteKey.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Domain restriction constraint | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2013-03-14 02:56:21 +08:00
										 |  |  |   class GTSAM_UNSTABLE_EXPORT Domain: public Constraint { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     size_t cardinality_; /// Cardinality
 | 
					
						
							|  |  |  |     std::set<size_t> values_; /// allowed values
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     typedef boost::shared_ptr<Domain> shared_ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Constructor on Discrete Key initializes an "all-allowed" domain
 | 
					
						
							|  |  |  |     Domain(const DiscreteKey& dkey) : | 
					
						
							|  |  |  |       Constraint(dkey.first), cardinality_(dkey.second) { | 
					
						
							|  |  |  |       for (size_t v = 0; v < cardinality_; v++) | 
					
						
							|  |  |  |         values_.insert(v); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Constructor on Discrete Key with single allowed value
 | 
					
						
							|  |  |  |     // Consider SingleValue constraint
 | 
					
						
							|  |  |  |     Domain(const DiscreteKey& dkey, size_t v) : | 
					
						
							|  |  |  |       Constraint(dkey.first), cardinality_(dkey.second) { | 
					
						
							|  |  |  |       values_.insert(v); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /// Constructor
 | 
					
						
							|  |  |  |     Domain(const Domain& other) : | 
					
						
							|  |  |  |       Constraint(other.keys_[0]), values_(other.values_) { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /// insert a value, non const :-(
 | 
					
						
							|  |  |  |     void insert(size_t value) { | 
					
						
							|  |  |  |       values_.insert(value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /// erase a value, non const :-(
 | 
					
						
							|  |  |  |     void erase(size_t value) { | 
					
						
							|  |  |  |        values_.erase(value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t nrValues() const { | 
					
						
							|  |  |  |       return values_.size(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool isSingleton() const { | 
					
						
							|  |  |  |       return nrValues() == 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t firstValue() const { | 
					
						
							|  |  |  |       return *values_.begin(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // print
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |     void print(const std::string& s = "", | 
					
						
							|  |  |  |         const KeyFormatter& formatter = DefaultKeyFormatter) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-12 01:42:30 +08:00
										 |  |  |     /// equals
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |     bool equals(const DiscreteFactor& other, double tol) const override { | 
					
						
							| 
									
										
										
										
											2013-10-12 01:42:30 +08:00
										 |  |  |       if(!dynamic_cast<const Domain*>(&other)) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       else { | 
					
						
							|  |  |  |         const Domain& f(static_cast<const Domain&>(other)); | 
					
						
							|  |  |  |         return (cardinality_==f.cardinality_) && (values_==f.values_); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     bool contains(size_t value) const { | 
					
						
							|  |  |  |       return values_.count(value)>0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /// Calculate value
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |     double operator()(const Values& values) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /// Convert into a decisiontree
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |     DecisionTreeFactor toDecisionTreeFactor() const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /// Multiply into a decisiontree
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |     DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * Ensure Arc-consistency | 
					
						
							|  |  |  |      * @param j domain to be checked | 
					
						
							|  |  |  |      * @param domains all other domains | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |     bool ensureArcConsistency(size_t j, std::vector<Domain>& domains) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      *  Check for a value in domain that does not occur in any other connected domain. | 
					
						
							|  |  |  |      *  If found, we make this a singleton... Called in AllDiff::ensureArcConsistency | 
					
						
							|  |  |  |      *  @param keys connected domains through alldiff | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |     bool checkAllDiff(const KeyVector keys, std::vector<Domain>& domains); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /// Partially apply known values
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |     Constraint::shared_ptr partiallyApply(const Values& values) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /// Partially apply known values, domain version
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |     Constraint::shared_ptr partiallyApply( | 
					
						
							|  |  |  |         const std::vector<Domain>& domains) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // namespace gtsam
 |