| 1 | /* |
| 2 | * SingleValue.h |
| 3 | * @brief domain 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/Constraint.h> |
| 12 | |
| 13 | namespace gtsam { |
| 14 | |
| 15 | /** |
| 16 | * SingleValue constraint: ensures a variable takes on a certain value. |
| 17 | * This could of course also be implemented by changing its `Domain`. |
| 18 | */ |
| 19 | class GTSAM_UNSTABLE_EXPORT SingleValue : public Constraint { |
| 20 | size_t cardinality_; /// < Number of values |
| 21 | size_t value_; ///< allowed value |
| 22 | |
| 23 | DiscreteKey discreteKey() const { |
| 24 | return DiscreteKey(keys_[0], cardinality_); |
| 25 | } |
| 26 | |
| 27 | public: |
| 28 | typedef std::shared_ptr<SingleValue> shared_ptr; |
| 29 | |
| 30 | /// Construct from key, cardinality, and given value. |
| 31 | SingleValue(Key key, size_t n, size_t value) |
| 32 | : Constraint(key), cardinality_(n), value_(value) {} |
| 33 | |
| 34 | /// Construct from DiscreteKey and given value. |
| 35 | SingleValue(const DiscreteKey& dkey, size_t value) |
| 36 | : Constraint(dkey.first), cardinality_(dkey.second), value_(value) {} |
| 37 | |
| 38 | // print |
| 39 | void print(const std::string& s = "" , const KeyFormatter& formatter = |
| 40 | DefaultKeyFormatter) const override; |
| 41 | |
| 42 | /// equals |
| 43 | bool equals(const DiscreteFactor& other, double tol) const override { |
| 44 | if (!dynamic_cast<const SingleValue*>(&other)) |
| 45 | return false; |
| 46 | else { |
| 47 | const SingleValue& f(static_cast<const SingleValue&>(other)); |
| 48 | return (cardinality_ == f.cardinality_) && (value_ == f.value_); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /// Calculate value |
| 53 | double evaluate(const Assignment<Key>& values) const override; |
| 54 | |
| 55 | /// Convert into a decisiontree |
| 56 | DecisionTreeFactor toDecisionTreeFactor() const override; |
| 57 | |
| 58 | /// Multiply into a decisiontree |
| 59 | DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override; |
| 60 | |
| 61 | /* |
| 62 | * Ensure Arc-consistency: just sets domain[j] to {value_}. |
| 63 | * @param j domain to be checked |
| 64 | * @param (in/out) domains all domains, but only domains->at(j) will be checked. |
| 65 | * @return true if domains->at(j) was changed, false otherwise. |
| 66 | */ |
| 67 | bool ensureArcConsistency(Key j, Domains* domains) const override; |
| 68 | |
| 69 | /// Partially apply known values |
| 70 | Constraint::shared_ptr partiallyApply(const DiscreteValues& values) const override; |
| 71 | |
| 72 | /// Partially apply known values, domain version |
| 73 | Constraint::shared_ptr partiallyApply( |
| 74 | const Domains& domains) const override; |
| 75 | }; |
| 76 | |
| 77 | } // namespace gtsam |
| 78 | |