gtsam 4.2
gtsam
Loading...
Searching...
No Matches
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
19class GTSAM_UNSTABLE_EXPORT Domain : public Constraint {
20 size_t cardinality_;
21 std::set<size_t> values_;
22
23 public:
24 typedef boost::shared_ptr<Domain> shared_ptr;
25
26 // Constructor on Discrete Key initializes an "all-allowed" domain
27 Domain(const DiscreteKey& dkey)
28 : Constraint(dkey.first), cardinality_(dkey.second) {
29 for (size_t v = 0; v < cardinality_; v++) values_.insert(v);
30 }
31
32 // Constructor on Discrete Key with single allowed value
33 // Consider SingleValue constraint
34 Domain(const DiscreteKey& dkey, size_t v)
35 : Constraint(dkey.first), cardinality_(dkey.second) {
36 values_.insert(v);
37 }
38
40 Key key() const { return keys_[0]; }
41
42 // The associated discrete key
43 DiscreteKey discreteKey() const { return DiscreteKey(key(), cardinality_); }
44
46 void insert(size_t value) { values_.insert(value); }
47
49 void erase(size_t value) { values_.erase(value); }
50
51 size_t nrValues() const { return values_.size(); }
52
53 bool isSingleton() const { return nrValues() == 1; }
54
55 size_t firstValue() const { return *values_.begin(); }
56
57 // print
58 void print(const std::string& s = "", const KeyFormatter& formatter =
59 DefaultKeyFormatter) const override;
60
62 bool equals(const DiscreteFactor& other, double tol) const override {
63 if (!dynamic_cast<const Domain*>(&other))
64 return false;
65 else {
66 const Domain& f(static_cast<const Domain&>(other));
67 return (cardinality_ == f.cardinality_) && (values_ == f.values_);
68 }
69 }
70
71 // Return concise string representation, mostly to debug arc consistency.
72 // Converts from base 0 to base1.
73 std::string base1Str() const;
74
75 // Check whether domain cotains a specific value.
76 bool contains(size_t value) const { return values_.count(value) > 0; }
77
79 double operator()(const DiscreteValues& values) const override;
80
82 DecisionTreeFactor toDecisionTreeFactor() const override;
83
85 DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override;
86
87 /*
88 * Ensure Arc-consistency by checking every possible value of domain j.
89 * @param j domain to be checked
90 * @param (in/out) domains all domains, but only domains->at(j) will be
91 * checked.
92 * @return true if domains->at(j) was changed, false otherwise.
93 */
94 bool ensureArcConsistency(Key j, Domains* domains) const override;
95
103 boost::optional<Domain> checkAllDiff(const KeyVector keys,
104 const Domains& domains) const;
105
107 Constraint::shared_ptr partiallyApply(const DiscreteValues& values) const override;
108
110 Constraint::shared_ptr partiallyApply(const Domains& domains) const override;
111};
112
113} // namespace gtsam
specialized key for discrete variables
std::pair< Key, size_t > DiscreteKey
Key type for discrete variables.
Definition DiscreteKey.h:36
Global functions in a separate testing namespace.
Definition chartTesting.h:28
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:156
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition DiscreteFactor.h:38
KeyVector keys_
The keys involved in this factor.
Definition Factor.h:85
Base class for constraint factors Derived classes include SingleValue, BinaryAllDiff,...
Definition Constraint.h:36
Constraint(Key j)
Construct unary constraint factor.
Definition Constraint.h:42
The Domain class represents a constraint that restricts the possible values a particular variable,...
Definition Domain.h:19
void erase(size_t value)
Erase a value, non const :-(.
Definition Domain.h:49
bool equals(const DiscreteFactor &other, double tol) const override
equals
Definition Domain.h:62
boost::shared_ptr< Domain > shared_ptr
allowed values
Definition Domain.h:24
Key key() const
The one key.
Definition Domain.h:40
void insert(size_t value)
Insert a value, non const :-(.
Definition Domain.h:46
The Factor::error simply extracts the.