gtsam  4.1.0
gtsam
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 
13 namespace gtsam {
14 
18  class GTSAM_UNSTABLE_EXPORT Domain: public Constraint {
19 
20  size_t cardinality_;
21  std::set<size_t> values_;
22 
23  public:
24 
25  typedef boost::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++)
31  values_.insert(v);
32  }
33 
34  // Constructor on Discrete Key with single allowed value
35  // Consider SingleValue constraint
36  Domain(const DiscreteKey& dkey, size_t v) :
37  Constraint(dkey.first), cardinality_(dkey.second) {
38  values_.insert(v);
39  }
40 
42  Domain(const Domain& other) :
43  Constraint(other.keys_[0]), values_(other.values_) {
44  }
45 
47  void insert(size_t value) {
48  values_.insert(value);
49  }
50 
52  void erase(size_t value) {
53  values_.erase(value);
54  }
55 
56  size_t nrValues() const {
57  return values_.size();
58  }
59 
60  bool isSingleton() const {
61  return nrValues() == 1;
62  }
63 
64  size_t firstValue() const {
65  return *values_.begin();
66  }
67 
68  // print
69  void print(const std::string& s = "",
70  const KeyFormatter& formatter = DefaultKeyFormatter) const override;
71 
73  bool equals(const DiscreteFactor& other, double tol) const override {
74  if(!dynamic_cast<const Domain*>(&other))
75  return false;
76  else {
77  const Domain& f(static_cast<const Domain&>(other));
78  return (cardinality_==f.cardinality_) && (values_==f.values_);
79  }
80  }
81 
82  bool contains(size_t value) const {
83  return values_.count(value)>0;
84  }
85 
87  double operator()(const Values& values) const override;
88 
90  DecisionTreeFactor toDecisionTreeFactor() const override;
91 
93  DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override;
94 
95  /*
96  * Ensure Arc-consistency
97  * @param j domain to be checked
98  * @param domains all other domains
99  */
100  bool ensureArcConsistency(size_t j, std::vector<Domain>& domains) const override;
101 
107  bool checkAllDiff(const KeyVector keys, std::vector<Domain>& domains);
108 
110  Constraint::shared_ptr partiallyApply(const Values& values) const override;
111 
113  Constraint::shared_ptr partiallyApply(
114  const std::vector<Domain>& domains) const override;
115  };
116 
117 } // namespace gtsam
gtsam::Constraint
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition: Constraint.h:32
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Key type for discrete conditionals Includes name and cardinality.
Definition: DiscreteKey.h:34
gtsam::Domain::erase
void erase(size_t value)
erase a value, non const :-(
Definition: Domain.h:52
gtsam::Domain::insert
void insert(size_t value)
insert a value, non const :-(
Definition: Domain.h:47
gtsam::DiscreteFactor
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition: DiscreteFactor.h:34
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
DiscreteKey.h
specialized key for discrete variables
gtsam::Domain::equals
bool equals(const DiscreteFactor &other, double tol) const override
equals
Definition: Domain.h:73
gtsam::Domain::shared_ptr
boost::shared_ptr< Domain > shared_ptr
allowed values
Definition: Domain.h:25
gtsam::KeyFormatter
boost::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
gtsam::Domain
Domain restriction constraint.
Definition: Domain.h:18
Constraint.h
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
gtsam::Domain::Domain
Domain(const Domain &other)
Constructor.
Definition: Domain.h:42
gtsam::operator*
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:45