Domain.cpp
Go to the documentation of this file.
1 /*
2  * Domain.cpp
3  * @brief Domain restriction constraint
4  * @date Feb 13, 2012
5  * @author Frank Dellaert
6  */
7 
8 #include <gtsam/base/Testable.h>
11 
12 #include <sstream>
13 namespace gtsam {
14 
15 using namespace std;
16 
17 /* ************************************************************************* */
18 void Domain::print(const string& s, const KeyFormatter& formatter) const {
19  cout << s << ": Domain on " << formatter(key()) << " (j=" << formatter(key())
20  << ") with values";
21  for (size_t v : values_) cout << " " << v;
22  cout << endl;
23 }
24 
25 /* ************************************************************************* */
26 string Domain::base1Str() const {
27  stringstream ss;
28  for (size_t v : values_) ss << v + 1;
29  return ss.str();
30 }
31 
32 /* ************************************************************************* */
34  return contains(values.at(key()));
35 }
36 
37 /* ************************************************************************* */
39  const DiscreteKeys keys{DiscreteKey(key(), cardinality_)};
40  vector<double> table;
41  for (size_t i1 = 0; i1 < cardinality_; ++i1) table.push_back(contains(i1));
42  DecisionTreeFactor converted(keys, table);
43  return converted;
44 }
45 
46 /* ************************************************************************* */
48  // TODO: can we do this more efficiently?
49  return toDecisionTreeFactor() * f;
50 }
51 
52 /* ************************************************************************* */
53 bool Domain::ensureArcConsistency(Key j, Domains* domains) const {
54  if (j != key()) throw invalid_argument("Domain check on wrong domain");
55  Domain& D = domains->at(j);
56  for (size_t value : values_)
57  if (!D.contains(value)) throw runtime_error("Unsatisfiable");
58  D = *this;
59  return true;
60 }
61 
62 /* ************************************************************************* */
63 std::optional<Domain> Domain::checkAllDiff(const KeyVector keys,
64  const Domains& domains) const {
65  Key j = key();
66  // for all values in this domain
67  for (const size_t value : values_) {
68  // for all connected domains
69  for (const Key k : keys)
70  // if any domain contains the value we cannot make this domain singleton
71  if (k != j && domains.at(k).contains(value)) goto found;
72  // Otherwise: return a singleton:
73  return Domain(this->discreteKey(), value);
74  found:;
75  }
76  return {}; // we did not change it
77 }
78 
79 /* ************************************************************************* */
81  DiscreteValues::const_iterator it = values.find(key());
82  if (it != values.end() && !contains(it->second))
83  throw runtime_error("Domain::partiallyApply: unsatisfiable");
84  return std::make_shared<Domain>(*this);
85 }
86 
87 /* ************************************************************************* */
89  const Domain& Dk = domains.at(key());
90  if (Dk.isSingleton() && !contains(*Dk.begin()))
91  throw runtime_error("Domain::partiallyApply: unsatisfiable");
92  return std::make_shared<Domain>(Dk);
93 }
94 
95 /* ************************************************************************* */
96 } // namespace gtsam
const gtsam::Symbol key('X', 0)
std::string base1Str() const
Definition: Domain.cpp:26
Concept check for values that can be used in unit tests.
std::optional< Domain > checkAllDiff(const KeyVector keys, const Domains &domains) const
Definition: Domain.cpp:63
std::shared_ptr< Constraint > shared_ptr
Definition: Constraint.h:37
leaf::MyValues values
Definition: BFloat16.h:88
double operator()(const DiscreteValues &values) const override
Calculate value.
Definition: Domain.cpp:33
bool isSingleton() const
Definition: Domain.h:54
void print(const std::string &s="", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: Domain.cpp:18
DecisionTreeFactor operator*(const DecisionTreeFactor &f) const override
Multiply into a decisiontree.
Definition: Domain.cpp:47
const KeyFormatter & formatter
bool ensureArcConsistency(Key j, Domains *domains) const override
Definition: Domain.cpp:53
std::map< Key, Domain > Domains
Definition: Constraint.h:29
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
static std::stringstream ss
Definition: testBTree.cpp:31
traits
Definition: chartTesting.h:28
bool contains(size_t value) const
Definition: Domain.h:77
ArrayXXf table(10, 4)
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decisiontree.
Definition: Domain.cpp:38
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
Constraint::shared_ptr partiallyApply(const DiscreteValues &values) const override
Partially apply known values.
Definition: Domain.cpp:80
const KeyVector keys
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
const_iterator begin() const
Definition: Factor.h:145
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
std::ptrdiff_t j
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41


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