gtsam 4.2
gtsam
Loading...
Searching...
No Matches
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
20class BinaryAllDiff : public Constraint {
21 size_t cardinality0_, cardinality1_;
22
23 public:
25 BinaryAllDiff(const DiscreteKey& key1, const DiscreteKey& key2)
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
85 Constraint::shared_ptr partiallyApply(const DiscreteValues&) const override {
86 throw std::runtime_error("BinaryAllDiff::partiallyApply not implemented");
87 }
88
90 Constraint::shared_ptr partiallyApply(
91 const Domains&) const override {
92 throw std::runtime_error("BinaryAllDiff::partiallyApply not implemented");
93 }
94};
95
96} // namespace gtsam
std::pair< Key, size_t > DiscreteKey
Key type for discrete variables.
Definition DiscreteKey.h:36
Global functions in a separate testing namespace.
Definition chartTesting.h:28
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
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100
A discrete probabilistic factor.
Definition DecisionTreeFactor.h:45
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition DiscreteFactor.h:38
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition DiscreteKey.h:39
A map from keys to values.
Definition DiscreteValues.h:34
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:140
KeyVector keys_
The keys involved in this factor.
Definition Factor.h:85
bool equals(const DiscreteFactor &other, double tol) const override
equals
Definition BinaryAllDiff.h:39
DecisionTreeFactor operator*(const DecisionTreeFactor &f) const override
Multiply into a decisiontree.
Definition BinaryAllDiff.h:67
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
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
Constraint::shared_ptr partiallyApply(const Domains &) const override
Partially apply known values, domain version.
Definition BinaryAllDiff.h:90
BinaryAllDiff(const DiscreteKey &key1, const DiscreteKey &key2)
cardinality
Definition BinaryAllDiff.h:25
Constraint(Key j)
Construct unary constraint factor.
Definition Constraint.h:42