BinaryAllDiff.h
Go to the documentation of this file.
1 /*
2  * BinaryAllDiff.h
3  * @brief Binary "all-different" constraint
4  * @date Feb 6, 2012
5  * @author Frank Dellaert
6  */
7 
8 #pragma once
9 
13 
14 namespace gtsam {
15 
20 class BinaryAllDiff : public Constraint {
22 
23  public:
26  : Constraint(key1.first, key2.first),
27  cardinality0_(key1.second),
28  cardinality1_(key2.second) {}
29 
30  // print
31  void print(
32  const std::string& s = "",
33  const KeyFormatter& formatter = DefaultKeyFormatter) const override {
34  std::cout << s << "BinaryAllDiff on " << formatter(keys_[0]) << " and "
35  << formatter(keys_[1]) << std::endl;
36  }
37 
39  bool equals(const DiscreteFactor& other, double tol) const override {
40  if (!dynamic_cast<const BinaryAllDiff*>(&other))
41  return false;
42  else {
43  const BinaryAllDiff& f(static_cast<const BinaryAllDiff&>(other));
44  return (cardinality0_ == f.cardinality0_) &&
45  (cardinality1_ == f.cardinality1_);
46  }
47  }
48 
50  double operator()(const DiscreteValues& values) const override {
51  return (double)(values.at(keys_[0]) != values.at(keys_[1]));
52  }
53 
57  keys.push_back(DiscreteKey(keys_[0], cardinality0_));
58  keys.push_back(DiscreteKey(keys_[1], cardinality1_));
59  std::vector<double> table;
60  for (size_t i1 = 0; i1 < cardinality0_; i1++)
61  for (size_t i2 = 0; i2 < cardinality1_; i2++) table.push_back(i1 != i2);
62  DecisionTreeFactor converted(keys, table);
63  return converted;
64  }
65 
68  // TODO: can we do this more efficiently?
69  return toDecisionTreeFactor() * f;
70  }
71 
72  /*
73  * Ensure Arc-consistency by checking every possible value of domain j.
74  * @param j domain to be checked
75  * @param (in/out) domains all domains, but only domains->at(j) will be checked.
76  * @return true if domains->at(j) was changed, false otherwise.
77  */
78  bool ensureArcConsistency(Key j, Domains* domains) const override {
79  throw std::runtime_error(
80  "BinaryAllDiff::ensureArcConsistency not implemented");
81  return false;
82  }
83 
86  throw std::runtime_error("BinaryAllDiff::partiallyApply not implemented");
87  }
88 
91  const Domains&) const override {
92  throw std::runtime_error("BinaryAllDiff::partiallyApply not implemented");
93  }
94 };
95 
96 } // namespace gtsam
std::shared_ptr< Constraint > shared_ptr
Definition: Constraint.h:37
leaf::MyValues values
DecisionTreeFactor operator*(const DecisionTreeFactor &f) const override
Multiply into a decisiontree.
Definition: BinaryAllDiff.h:67
KeyVector keys_
The keys involved in this factor.
Definition: Factor.h:87
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
const KeyFormatter & formatter
Constraint::shared_ptr partiallyApply(const Domains &) const override
Partially apply known values, domain version.
Definition: BinaryAllDiff.h:90
void print(const std::string &s="", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: BinaryAllDiff.h:31
double operator()(const DiscreteValues &values) const override
Calculate value.
Definition: BinaryAllDiff.h:50
bool ensureArcConsistency(Key j, Domains *domains) const override
Definition: BinaryAllDiff.h:78
std::map< Key, Domain > Domains
Definition: Constraint.h:29
const Symbol key1('v', 1)
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
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decisiontree.
Definition: BinaryAllDiff.h:55
Constraint::shared_ptr partiallyApply(const DiscreteValues &) const override
Partially apply known values.
Definition: BinaryAllDiff.h:85
traits
Definition: chartTesting.h:28
bool equals(const DiscreteFactor &other, double tol) const override
equals
Definition: BinaryAllDiff.h:39
ArrayXXf table(10, 4)
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
BinaryAllDiff(const DiscreteKey &key1, const DiscreteKey &key2)
cardinality
Definition: BinaryAllDiff.h:25
const KeyVector & keys() const
Access the factor&#39;s involved variable keys.
Definition: Factor.h:142
const G double tol
Definition: Group.h:86
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
std::ptrdiff_t j
const Symbol key2('v', 2)
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:33:58