| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * SingleValue.h | 
					
						
							|  |  |  |  * @brief domain constraint | 
					
						
							|  |  |  |  * @date Feb 6, 2012 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | #include <gtsam/discrete/DiscreteKey.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  | #include <gtsam_unstable/discrete/Constraint.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * SingleValue constraint: ensures a variable takes on a certain value. | 
					
						
							|  |  |  |  * This could of course also be implemented by changing its `Domain`. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class GTSAM_UNSTABLE_EXPORT SingleValue : public Constraint { | 
					
						
							|  |  |  |   size_t cardinality_;  /// < Number of values
 | 
					
						
							|  |  |  |   size_t value_;        ///<  allowed value
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   DiscreteKey discreteKey() const { | 
					
						
							|  |  |  |     return DiscreteKey(keys_[0], cardinality_); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  public: | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  |   typedef std::shared_ptr<SingleValue> shared_ptr; | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /// Construct from key, cardinality, and given value.
 | 
					
						
							|  |  |  |   SingleValue(Key key, size_t n, size_t value) | 
					
						
							|  |  |  |       : Constraint(key), cardinality_(n), value_(value) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Construct from DiscreteKey and given value.
 | 
					
						
							|  |  |  |   SingleValue(const DiscreteKey& dkey, size_t value) | 
					
						
							|  |  |  |       : Constraint(dkey.first), cardinality_(dkey.second), value_(value) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 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 SingleValue*>(&other)) | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |       const SingleValue& f(static_cast<const SingleValue&>(other)); | 
					
						
							|  |  |  |       return (cardinality_ == f.cardinality_) && (value_ == f.value_); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-11 23:31:42 +08:00
										 |  |  |   /// Compute error for each assignment and return as a tree
 | 
					
						
							| 
									
										
										
										
											2024-01-05 16:24:50 +08:00
										 |  |  |   AlgebraicDecisionTree<Key> errorTree() const override { | 
					
						
							| 
									
										
										
										
											2023-11-11 23:31:42 +08:00
										 |  |  |     throw std::runtime_error("SingleValue::error not implemented"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// Calculate value
 | 
					
						
							| 
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 |  |  |   double operator()(const DiscreteValues& values) const override; | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// Convert into a decisiontree
 | 
					
						
							|  |  |  |   DecisionTreeFactor toDecisionTreeFactor() const override; | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// Multiply into a decisiontree
 | 
					
						
							|  |  |  |   DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override; | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /*
 | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |    * Ensure Arc-consistency: just sets domain[j] to {value_}. | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +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-19 04:08:01 +08:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   bool ensureArcConsistency(Key j, Domains* domains) const override; | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// Partially apply known values
 | 
					
						
							| 
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 |  |  |   Constraint::shared_ptr partiallyApply(const DiscreteValues& values) const override; | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// Partially apply known values, domain version
 | 
					
						
							|  |  |  |   Constraint::shared_ptr partiallyApply( | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |       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
 |