GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
Domain.h
1 /*
2  * Domain.h
3  * @brief Domain restriction constraint
4  * @date Feb 13, 2012
5  * @author Frank Dellaert
6  */
7 
8 #pragma once
9 
12 #include <optional>
13 
14 namespace gtsam {
15 
20 class GTSAM_UNSTABLE_EXPORT Domain : public Constraint {
21  size_t cardinality_;
22  std::set<size_t> values_;
23 
24  public:
25  typedef std::shared_ptr<Domain> shared_ptr;
26 
27  // Constructor on Discrete Key initializes an "all-allowed" domain
28  Domain(const DiscreteKey& dkey)
29  : Constraint(dkey.first), cardinality_(dkey.second) {
30  for (size_t v = 0; v < cardinality_; v++) values_.insert(v);
31  }
32 
33  // Constructor on Discrete Key with single allowed value
34  // Consider SingleValue constraint
35  Domain(const DiscreteKey& dkey, size_t v)
36  : Constraint(dkey.first), cardinality_(dkey.second) {
37  values_.insert(v);
38  }
39 
41  Key key() const { return keys_[0]; }
42 
43  // The associated discrete key
44  DiscreteKey discreteKey() const { return DiscreteKey(key(), cardinality_); }
45 
47  void insert(size_t value) { values_.insert(value); }
48 
50  void erase(size_t value) { values_.erase(value); }
51 
52  size_t nrValues() const { return values_.size(); }
53 
54  bool isSingleton() const { return nrValues() == 1; }
55 
56  size_t firstValue() const { return *values_.begin(); }
57 
58  // print
59  void print(const std::string& s = "", const KeyFormatter& formatter =
60  DefaultKeyFormatter) const override;
61 
63  bool equals(const DiscreteFactor& other, double tol) const override {
64  if (!dynamic_cast<const Domain*>(&other))
65  return false;
66  else {
67  const Domain& f(static_cast<const Domain&>(other));
68  return (cardinality_ == f.cardinality_) && (values_ == f.values_);
69  }
70  }
71 
72  // Return concise string representation, mostly to debug arc consistency.
73  // Converts from base 0 to base1.
74  std::string base1Str() const;
75 
76  // Check whether domain cotains a specific value.
77  bool contains(size_t value) const { return values_.count(value) > 0; }
78 
80  double operator()(const DiscreteValues& values) const override;
81 
83  DecisionTreeFactor toDecisionTreeFactor() const override;
84 
86  DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override;
87 
88  /*
89  * Ensure Arc-consistency by checking every possible value of domain j.
90  * @param j domain to be checked
91  * @param (in/out) domains all domains, but only domains->at(j) will be
92  * checked.
93  * @return true if domains->at(j) was changed, false otherwise.
94  */
95  bool ensureArcConsistency(Key j, Domains* domains) const override;
96 
104  std::optional<Domain> checkAllDiff(const KeyVector keys,
105  const Domains& domains) const;
106 
108  Constraint::shared_ptr partiallyApply(const DiscreteValues& values) const override;
109 
111  Constraint::shared_ptr partiallyApply(const Domains& domains) const override;
112 };
113 
114 } // namespace gtsam
Definition: DecisionTreeFactor.h:44
Definition: Constraint.h:35
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
Key key() const
The one key.
Definition: Domain.h:41
Definition: Domain.h:20
bool equals(const DiscreteFactor &other, double tol) const override
equals
Definition: Domain.h:63
GTSAM_EXPORT void print(const Matrix &A, const std::string &s, std::ostream &stream)
Definition: DiscreteValues.h:34
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
Definition: chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
specialized key for discrete variables
Definition: DiscreteFactor.h:38
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std::shared_ptr< Domain > shared_ptr
allowed values
Definition: Domain.h:25
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
void insert(size_t value)
Insert a value, non const :-(.
Definition: Domain.h:47
void erase(size_t value)
Erase a value, non const :-(.
Definition: Domain.h:50