gtsam 4.1.1
gtsam
BinaryAllDiff.h
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
12#include <gtsam_unstable/discrete/Domain.h>
13
14namespace gtsam {
15
23class BinaryAllDiff : public Constraint {
24 size_t cardinality0_, cardinality1_;
25
26 public:
28 BinaryAllDiff(const DiscreteKey& key1, const DiscreteKey& key2)
29 : Constraint(key1.first, key2.first),
30 cardinality0_(key1.second),
31 cardinality1_(key2.second) {}
32
33 // print
34 void print(
35 const std::string& s = "",
36 const KeyFormatter& formatter = DefaultKeyFormatter) const override {
37 std::cout << s << "BinaryAllDiff on " << formatter(keys_[0]) << " and "
38 << formatter(keys_[1]) << std::endl;
39 }
40
42 bool equals(const DiscreteFactor& other, double tol) const override {
43 if (!dynamic_cast<const BinaryAllDiff*>(&other))
44 return false;
45 else {
46 const BinaryAllDiff& f(static_cast<const BinaryAllDiff&>(other));
47 return (cardinality0_ == f.cardinality0_) &&
48 (cardinality1_ == f.cardinality1_);
49 }
50 }
51
53 double operator()(const Values& values) const override {
54 return (double)(values.at(keys_[0]) != values.at(keys_[1]));
55 }
56
60 keys.push_back(DiscreteKey(keys_[0], cardinality0_));
61 keys.push_back(DiscreteKey(keys_[1], cardinality1_));
62 std::vector<double> table;
63 for (size_t i1 = 0; i1 < cardinality0_; i1++)
64 for (size_t i2 = 0; i2 < cardinality1_; i2++) table.push_back(i1 != i2);
65 DecisionTreeFactor converted(keys, table);
66 return converted;
67 }
68
71 // TODO: can we do this more efficiently?
72 return toDecisionTreeFactor() * f;
73 }
74
75 /*
76 * Ensure Arc-consistency
77 * @param j domain to be checked
78 * @param domains all other domains
79 */
80 bool ensureArcConsistency(size_t j,
81 std::vector<Domain>& domains) const override {
82 // throw std::runtime_error(
83 // "BinaryAllDiff::ensureArcConsistency not implemented");
84 return false;
85 }
86
88 Constraint::shared_ptr partiallyApply(const Values&) const override {
89 throw std::runtime_error("BinaryAllDiff::partiallyApply not implemented");
90 }
91
93 Constraint::shared_ptr partiallyApply(
94 const std::vector<Domain>&) const override {
95 throw std::runtime_error("BinaryAllDiff::partiallyApply not implemented");
96 }
97};
98
99} // namespace gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
std::pair< Key, size_t > DiscreteKey
Key type for discrete conditionals Includes name and cardinality.
Definition: DiscreteKey.h:34
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
An assignment from labels to value index (size_t).
Definition: Assignment.h:34
A discrete probabilistic factor.
Definition: DecisionTreeFactor.h:38
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition: DiscreteFactor.h:34
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:37
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:125
KeyVector keys_
The keys involved in this factor.
Definition: Factor.h:73
Binary AllDiff constraint Returns 1 if values for two keys are different, 0 otherwise DiscreteFactors...
Definition: BinaryAllDiff.h:23
bool equals(const DiscreteFactor &other, double tol) const override
equals
Definition: BinaryAllDiff.h:42
DecisionTreeFactor operator*(const DecisionTreeFactor &f) const override
Multiply into a decisiontree.
Definition: BinaryAllDiff.h:70
void print(const std::string &s="", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: BinaryAllDiff.h:34
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decisiontree.
Definition: BinaryAllDiff.h:58
Constraint::shared_ptr partiallyApply(const Values &) const override
Partially apply known values.
Definition: BinaryAllDiff.h:88
double operator()(const Values &values) const override
Calculate value.
Definition: BinaryAllDiff.h:53
Constraint::shared_ptr partiallyApply(const std::vector< Domain > &) const override
Partially apply known values, domain version.
Definition: BinaryAllDiff.h:93
BinaryAllDiff(const DiscreteKey &key1, const DiscreteKey &key2)
cardinality
Definition: BinaryAllDiff.h:28
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition: Constraint.h:33