| 1 | /* |
| 2 | * AllDiff.h |
| 3 | * @brief General "all-different" constraint |
| 4 | * @date Feb 6, 2012 |
| 5 | * @author Frank Dellaert |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <gtsam/discrete/DiscreteKey.h> |
| 11 | #include <gtsam_unstable/discrete/BinaryAllDiff.h> |
| 12 | |
| 13 | namespace gtsam { |
| 14 | |
| 15 | /** |
| 16 | * General AllDiff constraint. |
| 17 | * Returns 1 if values for all keys are different, 0 otherwise. |
| 18 | */ |
| 19 | class GTSAM_UNSTABLE_EXPORT AllDiff : public Constraint { |
| 20 | std::map<Key, size_t> cardinalities_; |
| 21 | |
| 22 | DiscreteKey discreteKey(size_t i) const { |
| 23 | Key j = keys_[i]; |
| 24 | return DiscreteKey(j, cardinalities_.at(k: j)); |
| 25 | } |
| 26 | |
| 27 | public: |
| 28 | /// Construct from keys. |
| 29 | AllDiff(const DiscreteKeys& dkeys); |
| 30 | |
| 31 | // print |
| 32 | void print(const std::string& s = "" , const KeyFormatter& formatter = |
| 33 | DefaultKeyFormatter) const override; |
| 34 | |
| 35 | /// equals |
| 36 | bool equals(const DiscreteFactor& other, double tol) const override { |
| 37 | if (!dynamic_cast<const AllDiff*>(&other)) |
| 38 | return false; |
| 39 | else { |
| 40 | const AllDiff& f(static_cast<const AllDiff&>(other)); |
| 41 | return cardinalities_.size() == f.cardinalities_.size() && |
| 42 | std::equal(first1: cardinalities_.begin(), last1: cardinalities_.end(), |
| 43 | first2: f.cardinalities_.begin()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// Calculate value = expensive ! |
| 48 | double evaluate(const Assignment<Key>& values) const override; |
| 49 | |
| 50 | /// Convert into a decisiontree, can be *very* expensive ! |
| 51 | DecisionTreeFactor toDecisionTreeFactor() const override; |
| 52 | |
| 53 | /// Multiply into a decisiontree |
| 54 | DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override; |
| 55 | |
| 56 | /* |
| 57 | * Ensure Arc-consistency by checking every possible value of domain j. |
| 58 | * @param j domain to be checked |
| 59 | * @param (in/out) domains all domains, but only domains->at(j) will be checked. |
| 60 | * @return true if domains->at(j) was changed, false otherwise. |
| 61 | */ |
| 62 | bool ensureArcConsistency(Key j, Domains* domains) const override; |
| 63 | |
| 64 | /// Partially apply known values |
| 65 | Constraint::shared_ptr partiallyApply(const DiscreteValues&) const override; |
| 66 | |
| 67 | /// Partially apply known values, domain version |
| 68 | Constraint::shared_ptr partiallyApply( |
| 69 | const Domains&) const override; |
| 70 | }; |
| 71 | |
| 72 | } // namespace gtsam |
| 73 | |