testSudoku.cpp
Go to the documentation of this file.
1 /*
2  * testSudoku.cpp
3  * @brief develop code for Sudoku CSP solver
4  * @date Jan 29, 2012
5  * @author Frank Dellaert
6  */
7 
10 #include <boost/assign/std/map.hpp>
12 #include <iostream>
13 #include <sstream>
14 #include <stdarg.h>
15 
16 using namespace std;
17 using namespace gtsam;
18 
19 #define PRINT false
20 
21 class Sudoku: public CSP {
22 
24  size_t n_;
25 
27  typedef std::pair<size_t, size_t> IJ;
28  std::map<IJ, DiscreteKey> dkeys_;
29 
30 public:
31 
33  const DiscreteKey& dkey(size_t i, size_t j) const {
34  return dkeys_.at(IJ(i, j));
35  }
36 
38  Key key(size_t i, size_t j) const {
39  return dkey(i, j).first;
40  }
41 
43  Sudoku(size_t n, ...) :
44  n_(n) {
45  // Create variables, ordering, and unary constraints
46  va_list ap;
47  va_start(ap, n);
48  Key k=0;
49  for (size_t i = 0; i < n; ++i) {
50  for (size_t j = 0; j < n; ++j, ++k) {
51  // create the key
52  IJ ij(i, j);
53  dkeys_[ij] = DiscreteKey(k, n);
54  // get the unary constraint, if any
55  int value = va_arg(ap, int);
56  // cout << value << " ";
57  if (value != 0) addSingleValue(dkeys_[ij], value - 1);
58  }
59  //cout << endl;
60  }
61  va_end(ap);
62 
63  // add row constraints
64  for (size_t i = 0; i < n; i++) {
65  DiscreteKeys dkeys;
66  for (size_t j = 0; j < n; j++)
67  dkeys += dkey(i, j);
68  addAllDiff(dkeys);
69  }
70 
71  // add col constraints
72  for (size_t j = 0; j < n; j++) {
73  DiscreteKeys dkeys;
74  for (size_t i = 0; i < n; i++)
75  dkeys += dkey(i, j);
76  addAllDiff(dkeys);
77  }
78 
79  // add box constraints
80  size_t N = (size_t)sqrt(double(n)), i0 = 0;
81  for (size_t I = 0; I < N; I++) {
82  size_t j0 = 0;
83  for (size_t J = 0; J < N; J++) {
84  // Box I,J
85  DiscreteKeys dkeys;
86  for (size_t i = i0; i < i0 + N; i++)
87  for (size_t j = j0; j < j0 + N; j++)
88  dkeys += dkey(i, j);
89  addAllDiff(dkeys);
90  j0 += N;
91  }
92  i0 += N;
93  }
94  }
95 
98  for (size_t i = 0; i < n_; i++) {
99  for (size_t j = 0; j < n_; j++) {
100  Key k = key(i, j);
101  cout << 1 + assignment->at(k) << " ";
102  }
103  cout << endl;
104  }
105  }
106 
108  void printSolution() {
109  DiscreteFactor::sharedValues MPE = optimalAssignment();
110  printAssignment(MPE);
111  }
112 
113 };
114 
115 /* ************************************************************************* */
117 {
118  Sudoku csp(4,
119  1,0, 0,4,
120  0,0, 0,0,
121 
122  4,0, 2,0,
123  0,1, 0,0);
124 
125  // Do BP
126  csp.runArcConsistency(4,10,PRINT);
127 
128  // optimize and check
129  CSP::sharedValues solution = csp.optimalAssignment();
131  insert(expected)
132  (csp.key(0,0), 0)(csp.key(0,1), 1)(csp.key(0,2), 2)(csp.key(0,3), 3)
133  (csp.key(1,0), 2)(csp.key(1,1), 3)(csp.key(1,2), 0)(csp.key(1,3), 1)
134  (csp.key(2,0), 3)(csp.key(2,1), 2)(csp.key(2,2), 1)(csp.key(2,3), 0)
135  (csp.key(3,0), 1)(csp.key(3,1), 0)(csp.key(3,2), 3)(csp.key(3,3), 2);
136  EXPECT(assert_equal(expected,*solution));
137  //csp.printAssignment(solution);
138 }
139 
140 /* ************************************************************************* */
142 {
143  Sudoku sudoku(9,
144  0,0,5, 0,9,0, 0,0,1,
145  0,0,0, 0,0,2, 0,7,3,
146  7,6,0, 0,0,8, 2,0,0,
147 
148  0,1,2, 0,0,9, 0,0,4,
149  0,0,0, 2,0,3, 0,0,0,
150  3,0,0, 1,0,0, 9,6,0,
151 
152  0,0,1, 9,0,0, 0,5,8,
153  9,7,0, 5,0,0, 0,0,0,
154  5,0,0, 0,3,0, 7,0,0);
155 
156  // Do BP
157  sudoku.runArcConsistency(4,10,PRINT);
158 
159  // sudoku.printSolution(); // don't do it
160 }
161 
162 /* ************************************************************************* */
163 TEST_UNSAFE( Sudoku, extreme)
164 {
165  Sudoku sudoku(9,
166  0,0,9, 7,4,8, 0,0,0,
167  7,0,0, 0,0,0, 0,0,0,
168  0,2,0, 1,0,9, 0,0,0,
169 
170  0,0,7, 0,0,0, 2,4,0,
171  0,6,4, 0,1,0, 5,9,0,
172  0,9,8, 0,0,0, 3,0,0,
173 
174  0,0,0, 8,0,3, 0,2,0,
175  0,0,0, 0,0,0, 0,0,6,
176  0,0,0, 2,7,5, 9,0,0);
177 
178  // Do BP
179  sudoku.runArcConsistency(9,10,PRINT);
180 
181 #ifdef METIS
182  VariableIndexOrdered index(sudoku);
183  index.print("index");
184  ofstream os("/Users/dellaert/src/hmetis-1.5-osx-i686/extreme-dual.txt");
185  index.outputMetisFormat(os);
186 #endif
187 
188  //sudoku.printSolution(); // don't do it
189 }
190 
191 /* ************************************************************************* */
192 TEST_UNSAFE( Sudoku, AJC_3star_Feb8_2012)
193 {
194  Sudoku sudoku(9,
195  9,5,0, 0,0,6, 0,0,0,
196  0,8,4, 0,7,0, 0,0,0,
197  6,2,0, 5,0,0, 4,0,0,
198 
199  0,0,0, 2,9,0, 6,0,0,
200  0,9,0, 0,0,0, 0,2,0,
201  0,0,2, 0,6,3, 0,0,0,
202 
203  0,0,9, 0,0,7, 0,6,8,
204  0,0,0, 0,3,0, 2,9,0,
205  0,0,0, 1,0,0, 0,3,7);
206 
207  // Do BP
208  sudoku.runArcConsistency(9,10,PRINT);
209 
210  //sudoku.printSolution(); // don't do it
211 }
212 
213 /* ************************************************************************* */
214 int main() {
215  TestResult tr;
216  return TestRegistry::runAllTests(tr);
217 }
218 /* ************************************************************************* */
219 
A insert(1, 2)=0
static int runAllTests(TestResult &result)
Matrix expected
Definition: testMatrix.cpp:974
int n
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
Sudoku(size_t n,...)
Constructor.
Definition: testSudoku.cpp:43
Definition: Half.h:150
#define N
Definition: gksort.c:12
void printAssignment(DiscreteFactor::sharedValues assignment) const
Print readable form of assignment.
Definition: testSudoku.cpp:97
#define PRINT
Definition: testSudoku.cpp:19
void printSolution()
solve and print solution
Definition: testSudoku.cpp:108
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:34
std::pair< size_t, size_t > IJ
discrete keys
Definition: testSudoku.cpp:27
#define EXPECT(condition)
Definition: Test.h:151
void runArcConsistency(size_t cardinality, size_t nrIterations=10, bool print=false) const
Definition: CSP.cpp:30
Key key(size_t i, size_t j) const
return Key for cell(i,j)
Definition: testSudoku.cpp:38
static const Matrix I
Definition: lago.cpp:35
JacobiRotation< float > J
traits
Definition: chartTesting.h:28
std::map< IJ, DiscreteKey > dkeys_
Definition: testSudoku.cpp:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
size_t n_
sudoku size
Definition: testSudoku.cpp:24
ofstream os("timeSchurFactors.csv")
boost::shared_ptr< Values > sharedValues
TEST_UNSAFE(Sudoku, small)
Definition: testSudoku.cpp:116
const DiscreteKey & dkey(size_t i, size_t j) const
return DiscreteKey for cell(i,j)
Definition: testSudoku.cpp:33
Definition: CSP.h:21
boost::shared_ptr< Values > sharedValues
Definition: CSP.h:27
int main()
Definition: testSudoku.cpp:214
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
std::ptrdiff_t j
sharedValues optimalAssignment() const
Find the best total assignment - can be expensive.
Definition: CSP.cpp:17
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:37


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:49:57