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


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:36