| 1 | /* |
| 2 | * BinaryAllDiff.h |
| 3 | * @brief Binary "all-different" constraint |
| 4 | * @date Feb 6, 2012 |
| 5 | * @author Frank Dellaert |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <gtsam/discrete/DecisionTreeFactor.h> |
| 11 | #include <gtsam_unstable/discrete/Constraint.h> |
| 12 | #include <gtsam_unstable/discrete/Domain.h> |
| 13 | |
| 14 | namespace gtsam { |
| 15 | |
| 16 | /** |
| 17 | * Binary AllDiff constraint |
| 18 | * Returns 1 if values for two keys are different, 0 otherwise. |
| 19 | */ |
| 20 | class BinaryAllDiff : public Constraint { |
| 21 | size_t cardinality0_, cardinality1_; /// cardinality |
| 22 | |
| 23 | public: |
| 24 | /// Constructor |
| 25 | BinaryAllDiff(const DiscreteKey& key1, const DiscreteKey& key2) |
| 26 | : Constraint(key1.first, key2.first), |
| 27 | cardinality0_(key1.second), |
| 28 | cardinality1_(key2.second) {} |
| 29 | |
| 30 | // print |
| 31 | void print( |
| 32 | const std::string& s = "" , |
| 33 | const KeyFormatter& formatter = DefaultKeyFormatter) const override { |
| 34 | std::cout << s << "BinaryAllDiff on " << formatter(keys_[0]) << " and " |
| 35 | << formatter(keys_[1]) << std::endl; |
| 36 | } |
| 37 | |
| 38 | /// equals |
| 39 | bool equals(const DiscreteFactor& other, double tol) const override { |
| 40 | if (!dynamic_cast<const BinaryAllDiff*>(&other)) |
| 41 | return false; |
| 42 | else { |
| 43 | const BinaryAllDiff& f(static_cast<const BinaryAllDiff&>(other)); |
| 44 | return (cardinality0_ == f.cardinality0_) && |
| 45 | (cardinality1_ == f.cardinality1_); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Calculate value |
| 50 | double evaluate(const Assignment<Key>& values) const override { |
| 51 | return (double)(values.at(k: keys_[0]) != values.at(k: keys_[1])); |
| 52 | } |
| 53 | |
| 54 | /// Convert into a decisiontree |
| 55 | DecisionTreeFactor toDecisionTreeFactor() const override { |
| 56 | DiscreteKeys keys; |
| 57 | keys.push_back(x: DiscreteKey(keys_[0], cardinality0_)); |
| 58 | keys.push_back(x: DiscreteKey(keys_[1], cardinality1_)); |
| 59 | std::vector<double> table; |
| 60 | for (size_t i1 = 0; i1 < cardinality0_; i1++) |
| 61 | for (size_t i2 = 0; i2 < cardinality1_; i2++) table.push_back(x: i1 != i2); |
| 62 | DecisionTreeFactor converted(keys, table); |
| 63 | return converted; |
| 64 | } |
| 65 | |
| 66 | /// Multiply into a decisiontree |
| 67 | DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override { |
| 68 | // TODO: can we do this more efficiently? |
| 69 | return toDecisionTreeFactor() * f; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Ensure Arc-consistency by checking every possible value of domain j. |
| 74 | * @param j domain to be checked |
| 75 | * @param (in/out) domains all domains, but only domains->at(j) will be checked. |
| 76 | * @return true if domains->at(j) was changed, false otherwise. |
| 77 | */ |
| 78 | bool ensureArcConsistency(Key j, Domains* domains) const override { |
| 79 | throw std::runtime_error( |
| 80 | "BinaryAllDiff::ensureArcConsistency not implemented" ); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | /// Partially apply known values |
| 85 | Constraint::shared_ptr partiallyApply(const DiscreteValues&) const override { |
| 86 | throw std::runtime_error("BinaryAllDiff::partiallyApply not implemented" ); |
| 87 | } |
| 88 | |
| 89 | /// Partially apply known values, domain version |
| 90 | Constraint::shared_ptr partiallyApply( |
| 91 | const Domains&) const override { |
| 92 | throw std::runtime_error("BinaryAllDiff::partiallyApply not implemented" ); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | } // namespace gtsam |
| 97 | |