AllDiff.cpp
Go to the documentation of this file.
1 /*
2  * AllDiff.cpp
3  * @brief General "all-different" constraint
4  * @date Feb 6, 2012
5  * @author Frank Dellaert
6  */
7 
8 #include <gtsam/base/Testable.h>
11 
12 #include <optional>
13 
14 namespace gtsam {
15 
16 /* ************************************************************************* */
17 AllDiff::AllDiff(const DiscreteKeys& dkeys) : Constraint(dkeys.indices()) {
18  for (const DiscreteKey& dkey : dkeys) cardinalities_.insert(dkey);
19 }
20 
21 /* ************************************************************************* */
22 void AllDiff::print(const std::string& s, const KeyFormatter& formatter) const {
23  std::cout << s << "AllDiff on ";
24  for (Key dkey : keys_) std::cout << formatter(dkey) << " ";
25  std::cout << std::endl;
26 }
27 
28 /* ************************************************************************* */
30  std::set<size_t> taken; // record values taken by keys
31  for (Key dkey : keys_) {
32  size_t value = values.at(dkey); // get the value for that key
33  if (taken.count(value)) return 0.0; // check if value alreday taken
34  taken.insert(value); // if not, record it as taken and keep checking
35  }
36  return 1.0;
37 }
38 
39 /* ************************************************************************* */
41  // We will do this by converting the allDif into many BinaryAllDiff
42  // constraints
43  DecisionTreeFactor converted;
44  size_t nrKeys = keys_.size();
45  for (size_t i1 = 0; i1 < nrKeys; i1++)
46  for (size_t i2 = i1 + 1; i2 < nrKeys; i2++) {
47  BinaryAllDiff binary12(discreteKey(i1), discreteKey(i2));
48  converted = converted * binary12.toDecisionTreeFactor();
49  }
50  return converted;
51 }
52 
53 /* ************************************************************************* */
55  // TODO: can we do this more efficiently?
56  return toDecisionTreeFactor() * f;
57 }
58 
59 /* ************************************************************************* */
60 bool AllDiff::ensureArcConsistency(Key j, Domains* domains) const {
61  Domain& Dj = domains->at(j);
62 
63  // Though strictly not part of allDiff, we check for
64  // a value in domains->at(j) that does not occur in any other connected domain.
65  // If found, we make this a singleton...
66  // TODO: make a new constraint where this really is true
67  std::optional<Domain> maybeChanged = Dj.checkAllDiff(keys_, *domains);
68  if (maybeChanged) {
69  Dj = *maybeChanged;
70  return true;
71  }
72 
73  // Check all other domains for singletons and erase corresponding values.
74  // This is the same as arc-consistency on the equivalent binary constraints
75  bool changed = false;
76  for (Key k : keys_)
77  if (k != j) {
78  const Domain& Dk = domains->at(k);
79  if (Dk.isSingleton()) { // check if singleton
80  size_t value = Dk.firstValue();
81  if (Dj.contains(value)) {
82  Dj.erase(value); // erase value if true
83  changed = true;
84  }
85  }
86  }
87  return changed;
88 }
89 
90 /* ************************************************************************* */
92  DiscreteKeys newKeys;
93  // loop over keys and add them only if they do not appear in values
94  for (Key k : keys_)
95  if (values.find(k) == values.end()) {
96  newKeys.push_back(DiscreteKey(k, cardinalities_.at(k)));
97  }
98  return std::make_shared<AllDiff>(newKeys);
99 }
100 
101 /* ************************************************************************* */
103  const Domains& domains) const {
104  DiscreteValues known;
105  for (Key k : keys_) {
106  const Domain& Dk = domains.at(k);
107  if (Dk.isSingleton()) known[k] = Dk.firstValue();
108  }
109  return partiallyApply(known);
110 }
111 
112 /* ************************************************************************* */
113 } // namespace gtsam
DiscreteKey discreteKey(size_t i) const
Definition: AllDiff.h:22
Constraint::shared_ptr partiallyApply(const DiscreteValues &) const override
Partially apply known values.
Definition: AllDiff.cpp:91
Concept check for values that can be used in unit tests.
std::optional< Domain > checkAllDiff(const KeyVector keys, const Domains &domains) const
Definition: Domain.cpp:63
std::shared_ptr< Constraint > shared_ptr
Definition: Constraint.h:37
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decisiontree, can be very expensive !
Definition: AllDiff.cpp:40
leaf::MyValues values
KeyVector keys_
The keys involved in this factor.
Definition: Factor.h:87
bool isSingleton() const
Definition: Domain.h:54
DecisionTreeFactor operator*(const DecisionTreeFactor &f) const override
Multiply into a decisiontree.
Definition: AllDiff.cpp:54
size_t firstValue() const
Definition: Domain.h:56
AllDiff(const DiscreteKeys &dkeys)
Construct from keys.
Definition: AllDiff.cpp:17
const KeyFormatter & formatter
static const size_t nrKeys
std::map< Key, Domain > Domains
Definition: Constraint.h:29
std::map< Key, size_t > cardinalities_
Definition: AllDiff.h:20
double operator()(const DiscreteValues &values) const override
Calculate value = expensive !
Definition: AllDiff.cpp:29
void print(const std::string &s="", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: AllDiff.cpp:22
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
traits
Definition: chartTesting.h:28
bool contains(size_t value) const
Definition: Domain.h:77
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std::pair< iterator, bool > insert(const value_type &value)
bool ensureArcConsistency(Key j, Domains *domains) const override
Definition: AllDiff.cpp:60
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
std::ptrdiff_t j
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
void erase(size_t value)
Erase a value, non const :-(.
Definition: Domain.h:50


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:33:53