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 
10 #include <gtsam/base/Testable.h>
11 #include <boost/make_shared.hpp>
12 
13 namespace gtsam {
14 
15  using namespace std;
16 
17  /* ************************************************************************* */
18  void Domain::print(const string& s,
19  const KeyFormatter& formatter) const {
20 // cout << s << ": Domain on " << formatter(keys_[0]) << " (j=" <<
21 // formatter(keys_[0]) << ") with values";
22 // for (size_t v: values_) cout << " " << v;
23 // cout << endl;
24  for (size_t v: values_) cout << v;
25  }
26 
27  /* ************************************************************************* */
28  double Domain::operator()(const Values& values) const {
29  return contains(values.at(keys_[0]));
30  }
31 
32  /* ************************************************************************* */
35  keys += DiscreteKey(keys_[0],cardinality_);
36  vector<double> table;
37  for (size_t i1 = 0; i1 < cardinality_; ++i1)
38  table.push_back(contains(i1));
39  DecisionTreeFactor converted(keys, table);
40  return converted;
41  }
42 
43  /* ************************************************************************* */
45  // TODO: can we do this more efficiently?
46  return toDecisionTreeFactor() * f;
47  }
48 
49  /* ************************************************************************* */
50  bool Domain::ensureArcConsistency(size_t j, vector<Domain>& domains) const {
51  if (j != keys_[0]) throw invalid_argument("Domain check on wrong domain");
52  Domain& D = domains[j];
53  for(size_t value: values_)
54  if (!D.contains(value)) throw runtime_error("Unsatisfiable");
55  D = *this;
56  return true;
57  }
58 
59  /* ************************************************************************* */
60  bool Domain::checkAllDiff(const KeyVector keys, vector<Domain>& domains) {
61  Key j = keys_[0];
62  // for all values in this domain
63  for(size_t value: values_) {
64  // for all connected domains
65  for(Key k: keys)
66  // if any domain contains the value we cannot make this domain singleton
67  if (k!=j && domains[k].contains(value))
68  goto found;
69  values_.clear();
70  values_.insert(value);
71  return true; // we changed it
72  found:;
73  }
74  return false; // we did not change it
75  }
76 
77  /* ************************************************************************* */
79  const Values& values) const {
80  Values::const_iterator it = values.find(keys_[0]);
81  if (it != values.end() && !contains(it->second)) throw runtime_error(
82  "Domain::partiallyApply: unsatisfiable");
83  return boost::make_shared < Domain > (*this);
84  }
85 
86  /* ************************************************************************* */
88  const vector<Domain>& domains) const {
89  const Domain& Dk = domains[keys_[0]];
90  if (Dk.isSingleton() && !contains(*Dk.begin())) throw runtime_error(
91  "Domain::partiallyApply: unsatisfiable");
92  return boost::make_shared < Domain > (Dk);
93  }
94 
95 /* ************************************************************************* */
96 } // namespace gtsam
bool isSingleton() const
Definition: Domain.h:60
Concept check for values that can be used in unit tests.
ArrayXcf v
Definition: Cwise_arg.cpp:1
leaf::MyValues values
Definition: Half.h:150
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:44
const KeyFormatter & formatter
double operator()(const Values &values) const override
Calculate value.
Definition: Domain.cpp:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
bool checkAllDiff(const KeyVector keys, std::vector< Domain > &domains)
Definition: Domain.cpp:60
const_iterator begin() const
Definition: Factor.h:127
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:34
Constraint::shared_ptr partiallyApply(const Values &values) const override
Partially apply known values.
Definition: Domain.cpp:78
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
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
boost::transform_iterator< boost::function1< ConstKeyValuePair, const ConstKeyValuePtrPair & >, KeyValueMap::const_iterator > const_iterator
Const forward iterator, with value type ConstKeyValuePair.
Definition: Values.h:124
RealScalar s
traits
Definition: chartTesting.h:28
ArrayXXf table(10, 4)
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decisiontree.
Definition: Domain.cpp:33
bool ensureArcConsistency(size_t j, std::vector< Domain > &domains) const override
Definition: Domain.cpp:50
bool contains(size_t value) const
Definition: Domain.h:82
const KeyVector keys
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
std::ptrdiff_t j
boost::shared_ptr< Constraint > shared_ptr
Definition: Constraint.h:36
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:37


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:59