| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * BinaryAllDiff.h | 
					
						
							|  |  |  |  * @brief Binary "all-different" constraint | 
					
						
							|  |  |  |  * @date Feb 6, 2012 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | #include <gtsam/discrete/DecisionTreeFactor.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | #include <gtsam_unstable/discrete/Constraint.h>
 | 
					
						
							|  |  |  | #include <gtsam_unstable/discrete/Domain.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Binary AllDiff constraint | 
					
						
							|  |  |  |  * Returns 1 if values for two keys are different, 0 otherwise. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class BinaryAllDiff : public Constraint { | 
					
						
							|  |  |  |   size_t cardinality0_, cardinality1_;  /// cardinality
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  public: | 
					
						
							|  |  |  |   /// Constructor
 | 
					
						
							|  |  |  |   BinaryAllDiff(const DiscreteKey& key1, const DiscreteKey& key2) | 
					
						
							|  |  |  |       : Constraint(key1.first, key2.first), | 
					
						
							|  |  |  |         cardinality0_(key1.second), | 
					
						
							|  |  |  |         cardinality1_(key2.second) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // print
 | 
					
						
							|  |  |  |   void print( | 
					
						
							|  |  |  |       const std::string& s = "", | 
					
						
							|  |  |  |       const KeyFormatter& formatter = DefaultKeyFormatter) const override { | 
					
						
							|  |  |  |     std::cout << s << "BinaryAllDiff on " << formatter(keys_[0]) << " and " | 
					
						
							|  |  |  |               << formatter(keys_[1]) << std::endl; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// equals
 | 
					
						
							|  |  |  |   bool equals(const DiscreteFactor& other, double tol) const override { | 
					
						
							|  |  |  |     if (!dynamic_cast<const BinaryAllDiff*>(&other)) | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |       return false; | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |     else { | 
					
						
							|  |  |  |       const BinaryAllDiff& f(static_cast<const BinaryAllDiff&>(other)); | 
					
						
							|  |  |  |       return (cardinality0_ == f.cardinality0_) && | 
					
						
							|  |  |  |              (cardinality1_ == f.cardinality1_); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Calculate value
 | 
					
						
							| 
									
										
										
										
											2024-12-10 10:09:00 +08:00
										 |  |  |   double evaluate(const Assignment<Key>& values) const override { | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |     return (double)(values.at(keys_[0]) != values.at(keys_[1])); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Convert into a decisiontree
 | 
					
						
							|  |  |  |   DecisionTreeFactor toDecisionTreeFactor() const override { | 
					
						
							|  |  |  |     DiscreteKeys keys; | 
					
						
							|  |  |  |     keys.push_back(DiscreteKey(keys_[0], cardinality0_)); | 
					
						
							|  |  |  |     keys.push_back(DiscreteKey(keys_[1], cardinality1_)); | 
					
						
							|  |  |  |     std::vector<double> table; | 
					
						
							|  |  |  |     for (size_t i1 = 0; i1 < cardinality0_; i1++) | 
					
						
							|  |  |  |       for (size_t i2 = 0; i2 < cardinality1_; i2++) table.push_back(i1 != i2); | 
					
						
							|  |  |  |     DecisionTreeFactor converted(keys, table); | 
					
						
							|  |  |  |     return converted; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Multiply into a decisiontree
 | 
					
						
							|  |  |  |   DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override { | 
					
						
							|  |  |  |     // TODO: can we do this more efficiently?
 | 
					
						
							|  |  |  |     return toDecisionTreeFactor() * f; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /*
 | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |    * Ensure Arc-consistency by checking every possible value of domain j. | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |     throw std::runtime_error( | 
					
						
							|  |  |  |         "BinaryAllDiff::ensureArcConsistency not implemented"); | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Partially apply known values
 | 
					
						
							| 
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 |  |  |   Constraint::shared_ptr partiallyApply(const DiscreteValues&) const override { | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |     throw std::runtime_error("BinaryAllDiff::partiallyApply not implemented"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Partially apply known values, domain version
 | 
					
						
							|  |  |  |   Constraint::shared_ptr partiallyApply( | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |       const Domains&) const override { | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |     throw std::runtime_error("BinaryAllDiff::partiallyApply not implemented"); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											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("BinaryAllDiff::error not implemented"); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace gtsam
 |