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