| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Domain.h | 
					
						
							|  |  |  |  * @brief Domain restriction constraint | 
					
						
							|  |  |  |  * @date Feb 13, 2012 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | #include <gtsam/discrete/DiscreteKey.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | #include <gtsam_unstable/discrete/Constraint.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |  * The Domain class represents a constraint that restricts the possible values a | 
					
						
							|  |  |  |  * particular variable, with given key, can take on. | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | class GTSAM_UNSTABLE_EXPORT Domain : public Constraint { | 
					
						
							|  |  |  |   size_t cardinality_;       /// Cardinality
 | 
					
						
							|  |  |  |   std::set<size_t> values_;  /// allowed values
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |  public: | 
					
						
							|  |  |  |   typedef boost::shared_ptr<Domain> shared_ptr; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   // 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); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   // 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); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   /// The one key
 | 
					
						
							|  |  |  |   Key key() const { return keys_[0]; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // The associated discrete key
 | 
					
						
							|  |  |  |   DiscreteKey discreteKey() const { return DiscreteKey(key(), cardinality_); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   /// Insert a value, non const :-(
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   void insert(size_t value) { values_.insert(value); } | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   /// Erase a value, non const :-(
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   void erase(size_t value) { values_.erase(value); } | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   size_t nrValues() const { return values_.size(); } | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   bool isSingleton() const { return nrValues() == 1; } | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   size_t firstValue() const { return *values_.begin(); } | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   // print
 | 
					
						
							|  |  |  |   void print(const std::string& s = "", const KeyFormatter& formatter = | 
					
						
							|  |  |  |                                             DefaultKeyFormatter) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   /// equals
 | 
					
						
							|  |  |  |   bool equals(const DiscreteFactor& other, double tol) const override { | 
					
						
							|  |  |  |     if (!dynamic_cast<const Domain*>(&other)) | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |       const Domain& f(static_cast<const Domain&>(other)); | 
					
						
							|  |  |  |       return (cardinality_ == f.cardinality_) && (values_ == f.values_); | 
					
						
							| 
									
										
										
										
											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-21 04:52:12 +08:00
										 |  |  |   // Return concise string representation, mostly to debug arc consistency.
 | 
					
						
							|  |  |  |   // Converts from base 0 to base1.
 | 
					
						
							|  |  |  |   std::string base1Str() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check whether domain cotains a specific value.
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   bool contains(size_t value) const { return values_.count(value) > 0; } | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   /// Calculate value
 | 
					
						
							| 
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 |  |  |   double operator()(const DiscreteValues& values) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   /// Convert into a decisiontree
 | 
					
						
							|  |  |  |   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
										 |  |  |   /*
 | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |    * Ensure Arc-consistency by checking every possible value of domain j. | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |    * @param j domain to be checked | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |    * @param (in/out) domains all domains, but only domains->at(j) will be | 
					
						
							|  |  |  |    * checked. | 
					
						
							|  |  |  |    * @return true if domains->at(j) was changed, false otherwise. | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   bool ensureArcConsistency(Key j, Domains* domains) const override; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   /**
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |    * Check for a value in domain that does not occur in any other connected | 
					
						
							|  |  |  |    * domain. If found, return a a new singleton domain... | 
					
						
							|  |  |  |    * Called in AllDiff::ensureArcConsistency | 
					
						
							|  |  |  |    * @param keys connected domains through alldiff | 
					
						
							|  |  |  |    * @param keys other domains | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   boost::optional<Domain> checkAllDiff(const KeyVector keys, | 
					
						
							|  |  |  |                                        const Domains& domains) const; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   /// Partially apply known values
 | 
					
						
							| 
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 |  |  |   Constraint::shared_ptr partiallyApply(const DiscreteValues& 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
 | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   Constraint::shared_ptr partiallyApply(const Domains& domains) const override; | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | }  // namespace gtsam
 |