CSP.cpp
Go to the documentation of this file.
1 /*
2  * CSP.cpp
3  * @brief Constraint Satisfaction Problem class
4  * @date Feb 6, 2012
5  * @author Frank Dellaert
6  */
7 
10 #include <gtsam/base/Testable.h>
11 
12 using namespace std;
13 
14 namespace gtsam {
15 
17  CSP::sharedValues CSP::optimalAssignment() const {
18  DiscreteBayesNet::shared_ptr chordal = this->eliminateSequential();
19  sharedValues mpe = chordal->optimize();
20  return mpe;
21  }
22 
24  CSP::sharedValues CSP::optimalAssignment(const Ordering& ordering) const {
25  DiscreteBayesNet::shared_ptr chordal = this->eliminateSequential(ordering);
26  sharedValues mpe = chordal->optimize();
27  return mpe;
28  }
29 
30  void CSP::runArcConsistency(size_t cardinality, size_t nrIterations, bool print) const {
31  // Create VariableIndex
32  VariableIndex index(*this);
33  // index.print();
34 
35  size_t n = index.size();
36 
37  // Initialize domains
38  std::vector < Domain > domains;
39  for (size_t j = 0; j < n; j++)
40  domains.push_back(Domain(DiscreteKey(j,cardinality)));
41 
42  // Create array of flags indicating a domain changed or not
43  std::vector<bool> changed(n);
44 
45  // iterate nrIterations over entire grid
46  for (size_t it = 0; it < nrIterations; it++) {
47  bool anyChange = false;
48  // iterate over all cells
49  for (size_t v = 0; v < n; v++) {
50  // keep track of which domains changed
51  changed[v] = false;
52  // loop over all factors/constraints for variable v
53  const FactorIndices& factors = index[v];
54  for(size_t f: factors) {
55  // if not already a singleton
56  if (!domains[v].isSingleton()) {
57  // get the constraint and call its ensureArcConsistency method
58  Constraint::shared_ptr constraint = boost::dynamic_pointer_cast<Constraint>((*this)[f]);
59  if (!constraint) throw runtime_error("CSP:runArcConsistency: non-constraint factor");
60  changed[v] = constraint->ensureArcConsistency(v,domains) || changed[v];
61  }
62  } // f
63  if (changed[v]) anyChange = true;
64  } // v
65  if (!anyChange) break;
66  // TODO: Sudoku specific hack
67  if (print) {
68  if (cardinality == 9 && n == 81) {
69  for (size_t i = 0, v = 0; i < (size_t)std::sqrt((double)n); i++) {
70  for (size_t j = 0; j < (size_t)std::sqrt((double)n); j++, v++) {
71  if (changed[v]) cout << "*";
72  domains[v].print();
73  cout << "\t";
74  } // i
75  cout << endl;
76  } // j
77  } else {
78  for (size_t v = 0; v < n; v++) {
79  if (changed[v]) cout << "*";
80  domains[v].print();
81  cout << "\t";
82  } // v
83  }
84  cout << endl;
85  } // print
86  } // it
87 
88 #ifndef INPROGRESS
89  // Now create new problem with all singleton variables removed
90  // We do this by adding simplifying all factors using parial application
91  // TODO: create a new ordering as we go, to ensure a connected graph
92  // KeyOrdering ordering;
93  // vector<Index> dkeys;
94  for(const DiscreteFactor::shared_ptr& f: factors_) {
95  Constraint::shared_ptr constraint = boost::dynamic_pointer_cast<Constraint>(f);
96  if (!constraint) throw runtime_error("CSP:runArcConsistency: non-constraint factor");
97  Constraint::shared_ptr reduced = constraint->partiallyApply(domains);
98  if (print) reduced->print();
99  }
100 #endif
101  }
102 } // gtsam
103 
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
Concept check for values that can be used in unit tests.
static enum @843 ordering
ArrayXcf v
Definition: Cwise_arg.cpp:1
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:32
int n
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
Definition: Half.h:150
GaussianFactorGraph factors(list_of(factor1)(factor2)(factor3))
size_t size() const
The number of variable entries. This is equal to the number of unique variable Keys.
Definition: VariableIndex.h:80
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:34
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
boost::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
traits
Definition: chartTesting.h:28
boost::shared_ptr< This > shared_ptr
boost::shared_ptr< Values > sharedValues
Definition: CSP.h:27
std::ptrdiff_t j
boost::shared_ptr< Constraint > shared_ptr
Definition: Constraint.h:36
boost::shared_ptr< Values > sharedValues


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