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 
74  throw std::runtime_error("Domain::error not implemented");
75  }
76 
77  // Return concise string representation, mostly to debug arc consistency.
78  // Converts from base 0 to base1.
79  std::string base1Str() const;
80 
81  // Check whether domain cotains a specific value.
82  bool contains(size_t value) const { return values_.count(value) > 0; }
83 
85  double operator()(const DiscreteValues& values) const override;
86 
88  DecisionTreeFactor toDecisionTreeFactor() const override;
89 
91  DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override;
92 
93  /*
94  * Ensure Arc-consistency by checking every possible value of domain j.
95  * @param j domain to be checked
96  * @param (in/out) domains all domains, but only domains->at(j) will be
97  * checked.
98  * @return true if domains->at(j) was changed, false otherwise.
99  */
100  bool ensureArcConsistency(Key j, Domains* domains) const override;
101 
109  std::optional<Domain> checkAllDiff(const KeyVector keys,
110  const Domains& domains) const;
111 
113  Constraint::shared_ptr partiallyApply(const DiscreteValues& values) const override;
114 
116  Constraint::shared_ptr partiallyApply(const Domains& domains) const override;
117 };
118 
119 } // namespace gtsam
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:44
gtsam::Domain::isSingleton
bool isSingleton() const
Definition: Domain.h:54
gtsam::Domain::contains
bool contains(size_t value) const
Definition: Domain.h:82
s
RealScalar s
Definition: level1_cplx_impl.h:126
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
gtsam::Domain::nrValues
size_t nrValues() const
Definition: Domain.h:52
Constraint.h
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
gtsam::Domain::Domain
Domain(const DiscreteKey &dkey, size_t v)
Definition: Domain.h:35
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
gtsam::Domain::values_
std::set< size_t > values_
Cardinality.
Definition: Domain.h:22
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:92
gtsam::Domain::erase
void erase(size_t value)
Erase a value, non const :-(.
Definition: Domain.h:50
gtsam::Domain::insert
void insert(size_t value)
Insert a value, non const :-(.
Definition: Domain.h:47
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::AlgebraicDecisionTree< Key >
gtsam::operator*
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
operator()
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
Definition: IndexedViewMethods.h:73
gtsam::Domain::equals
bool equals(const DiscreteFactor &other, double tol) const override
equals
Definition: Domain.h:63
gtsam::KeyFormatter
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
gtsam::Domain
Definition: Domain.h:20
gtsam::Domains
std::map< Key, Domain > Domains
Definition: Constraint.h:29
gtsam::Domain::key
Key key() const
The one key.
Definition: Domain.h:41
DiscreteKey.h
specialized key for discrete variables
key
const gtsam::Symbol key('X', 0)
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
gtsam
traits
Definition: SFMdata.h:40
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::Domain::Domain
Domain(const DiscreteKey &dkey)
Definition: Domain.h:28
gtsam::Domain::errorTree
AlgebraicDecisionTree< Key > errorTree() const override
Compute error for each assignment and return as a tree.
Definition: Domain.h:73
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
gtsam::Domain::cardinality_
size_t cardinality_
Definition: Domain.h:21
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::Domain::firstValue
size_t firstValue() const
Definition: Domain.h:56
gtsam::DiscreteFactor
Definition: DiscreteFactor.h:39
gtsam::Constraint::shared_ptr
std::shared_ptr< Constraint > shared_ptr
Definition: Constraint.h:37
gtsam::Domain::shared_ptr
std::shared_ptr< Domain > shared_ptr
allowed values
Definition: Domain.h:25
gtsam::Constraint
Definition: Constraint.h:35
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
test_callbacks.value
value
Definition: test_callbacks.py:160
gtsam::Domain::discreteKey
DiscreteKey discreteKey() const
Definition: Domain.h:44
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:32:26