Domain.h
Go to the documentation of this file.
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
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
const gtsam::Symbol key('X', 0)
size_t cardinality_
Definition: Domain.h:21
std::shared_ptr< Constraint > shared_ptr
Definition: Constraint.h:37
leaf::MyValues values
bool isSingleton() const
Definition: Domain.h:54
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
size_t firstValue() const
Definition: Domain.h:56
Key key() const
The one key.
Definition: Domain.h:41
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
const KeyFormatter & formatter
bool equals(const DiscreteFactor &other, double tol) const override
equals
Definition: Domain.h:63
DiscreteKey discreteKey() const
Definition: Domain.h:44
std::map< Key, Domain > Domains
Definition: Constraint.h:29
std::set< size_t > values_
Cardinality.
Definition: Domain.h:22
Array< int, Dynamic, 1 > v
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
RealScalar s
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
Domain(const DiscreteKey &dkey)
Definition: Domain.h:28
traits
Definition: chartTesting.h:28
specialized key for discrete variables
bool contains(size_t value) const
Definition: Domain.h:77
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std::shared_ptr< Domain > shared_ptr
allowed values
Definition: Domain.h:25
const G double tol
Definition: Group.h:86
const KeyVector keys
size_t nrValues() const
Definition: Domain.h:52
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
internal::enable_if< internal::valid_indexed_view_overload< RowIndices, ColIndices >::value &&internal::traits< typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::ReturnAsIndexedView, typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST
Domain(const DiscreteKey &dkey, size_t v)
Definition: Domain.h:35
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
std::ptrdiff_t j
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


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:10