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
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:44
gtsam::Domain::isSingleton
bool isSingleton() const
Definition: Domain.h:54
D
MatrixXcd D
Definition: EigenSolver_EigenSolver_MatrixType.cpp:14
s
RealScalar s
Definition: level1_cplx_impl.h:126
Testable.h
Concept check for values that can be used in unit tests.
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
gtsam::Domain::base1Str
std::string base1Str() const
Definition: Domain.cpp:26
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
gtsam::Factor::begin
const_iterator begin() const
Definition: Factor.h:146
gtsam::Domain::partiallyApply
Constraint::shared_ptr partiallyApply(const DiscreteValues &values) const override
Partially apply known values.
Definition: Domain.cpp:80
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:92
ss
static std::stringstream ss
Definition: testBTree.cpp:31
gtsam::Domain::operator()
double operator()(const DiscreteValues &values) const override
Calculate value.
Definition: Domain.cpp:33
table
ArrayXXf table(10, 4)
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
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::Domain::operator*
DecisionTreeFactor operator*(const DecisionTreeFactor &f) const override
Multiply into a decisiontree.
Definition: Domain.cpp:47
gtsam::Domain::ensureArcConsistency
bool ensureArcConsistency(Key j, Domains *domains) const override
Definition: Domain.cpp:53
gtsam::Domains
std::map< Key, Domain > Domains
Definition: Constraint.h:29
gtsam::Domain::checkAllDiff
std::optional< Domain > checkAllDiff(const KeyVector keys, const Domains &domains) const
Definition: Domain.cpp:63
key
const gtsam::Symbol key('X', 0)
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
Domain.h
gtsam
traits
Definition: SFMdata.h:40
i1
double i1(double x)
Definition: i1.c:150
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
gtsam::Domain::print
void print(const std::string &s="", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: Domain.cpp:18
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
gtsam::Domain::toDecisionTreeFactor
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decisiontree.
Definition: Domain.cpp:38
gtsam::Constraint::shared_ptr
std::shared_ptr< Constraint > shared_ptr
Definition: Constraint.h:37
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
DecisionTreeFactor.h
test_callbacks.value
value
Definition: test_callbacks.py:160


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