gtsam  4.0.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  virtual void print(const std::string& s = "",
70  const KeyFormatter& formatter = DefaultKeyFormatter) const;
71 
73  bool equals(const DiscreteFactor& other, double tol) const {
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  virtual double operator()(const Values& values) const;
88 
90  virtual DecisionTreeFactor toDecisionTreeFactor() const;
91 
93  virtual DecisionTreeFactor operator*(const DecisionTreeFactor& f) const;
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;
101 
107  bool checkAllDiff(const KeyVector keys, std::vector<Domain>& domains);
108 
110  virtual Constraint::shared_ptr partiallyApply(
111  const Values& values) const;
112 
114  virtual Constraint::shared_ptr partiallyApply(
115  const std::vector<Domain>& domains) const;
116  };
117 
118 } // namespace gtsam
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition: Constraint.h:32
specialized key for discrete variables
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:141
Domain restriction constraint.
Definition: Domain.h:18
boost::shared_ptr< Domain > shared_ptr
allowed values
Definition: Domain.h:25
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition: DiscreteFactor.h:34
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:33
void insert(size_t value)
insert a value, non const :-(
Definition: Domain.h:47
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:56
std::pair< Key, size_t > DiscreteKey
Key type for discrete conditionals Includes name and cardinality.
Definition: DiscreteKey.h:34
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
Domain(const Domain &other)
Constructor.
Definition: Domain.h:42
bool equals(const DiscreteFactor &other, double tol) const
equals
Definition: Domain.h:73
void erase(size_t value)
erase a value, non const :-(
Definition: Domain.h:52
Point2 operator *(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:170