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 
97  throw std::runtime_error("BinaryAllDiff::error not implemented");
98  }
99 };
100 
101 } // namespace gtsam
key1
const Symbol key1('v', 1)
gtsam::BinaryAllDiff::cardinality0_
size_t cardinality0_
Definition: BinaryAllDiff.h:21
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:44
gtsam::BinaryAllDiff::cardinality1_
size_t cardinality1_
Definition: BinaryAllDiff.h:21
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::BinaryAllDiff::toDecisionTreeFactor
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decisiontree.
Definition: BinaryAllDiff.h:55
gtsam::BinaryAllDiff::partiallyApply
Constraint::shared_ptr partiallyApply(const DiscreteValues &) const override
Partially apply known values.
Definition: BinaryAllDiff.h:85
Constraint.h
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::BinaryAllDiff::BinaryAllDiff
BinaryAllDiff(const DiscreteKey &key1, const DiscreteKey &key2)
cardinality
Definition: BinaryAllDiff.h:25
gtsam::BinaryAllDiff
Definition: BinaryAllDiff.h:20
gtsam::BinaryAllDiff::equals
bool equals(const DiscreteFactor &other, double tol) const override
equals
Definition: BinaryAllDiff.h:39
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::AlgebraicDecisionTree< Key >
key2
const Symbol key2('v', 2)
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::BinaryAllDiff::errorTree
AlgebraicDecisionTree< Key > errorTree() const override
Compute error for each assignment and return as a tree.
Definition: BinaryAllDiff.h:96
gtsam::Domains
std::map< Key, Domain > Domains
Definition: Constraint.h:29
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
Domain.h
gtsam
traits
Definition: SFMdata.h:40
gtsam::Factor::keys_
KeyVector keys_
The keys involved in this factor.
Definition: Factor.h:88
i1
double i1(double x)
Definition: i1.c:150
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::Factor::keys
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:143
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
gtsam::BinaryAllDiff::operator*
DecisionTreeFactor operator*(const DecisionTreeFactor &f) const override
Multiply into a decisiontree.
Definition: BinaryAllDiff.h:67
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::BinaryAllDiff::print
void print(const std::string &s="", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: BinaryAllDiff.h:31
gtsam::DiscreteFactor
Definition: DiscreteFactor.h:39
gtsam::Constraint::shared_ptr
std::shared_ptr< Constraint > shared_ptr
Definition: Constraint.h:37
gtsam::Constraint
Definition: Constraint.h:35
gtsam::BinaryAllDiff::ensureArcConsistency
bool ensureArcConsistency(Key j, Domains *domains) const override
Definition: BinaryAllDiff.h:78
gtsam::BinaryAllDiff::partiallyApply
Constraint::shared_ptr partiallyApply(const Domains &) const override
Partially apply known values, domain version.
Definition: BinaryAllDiff.h:90
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
DecisionTreeFactor.h
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
gtsam::BinaryAllDiff::operator()
double operator()(const DiscreteValues &values) const override
Calculate value.
Definition: BinaryAllDiff.h:50


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:01:55