PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/ComputerAlgebra/Satisfiability/Lisp/Generators/plans/Sudoku.hpp

https://github.com/MLewsey/oklibrary
C++ Header | 249 lines | 0 code | 2 blank | 247 comment | 0 complexity | bfd1490ffbfe2821017865660d68842e MD5 | raw file
  1. // Oliver Kullmann, 5.2.2008 (Swansea)
  2. /* Copyright 2008 Oliver Kullmann
  3. This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
  4. it and/or modify it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation and included in this library; either version 3 of the
  6. License, or any later version. */
  7. /*!
  8. \file ComputerAlgebra/Satisfiability/Lisp/Generators/plans/Sudoku.hpp
  9. \brief Plans for Maxima-generators related to Sudoku-like problems
  10. \todo Relations to other modules
  11. <ul>
  12. <li> Compare with Applications/LatinSquares/plans/Sudoku.hpp. </li>
  13. <li> See the investigations in
  14. Experimentation/Investigations/LatinSquares/plans/Sudoku.hpp. </li>
  15. </ul>
  16. \todo Formulating "Sudoku problem instances"
  17. <ul>
  18. <li> We need an easy method for specifying partial assignments to
  19. fields. </li>
  20. <li> Likely a "Sudoku problem instance" is just given by a partial
  21. assignments to the variables. </li>
  22. <li> However this would be most natural for variables with domain
  23. {1,...,N}. Using the boolean variables corresponds to the use of
  24. signed literals (where we can enable/disable specific values for
  25. for specific fields). </li>
  26. <li> So, there are different forms of Sudoku-problems for different types
  27. of literals used by the partial assignment. </li>
  28. <li> Improved output:
  29. <ol>
  30. <li> We need the possibility to add a list of clauses (in the original
  31. variables).
  32. <ul>
  33. <li> Specifying that field (i,j) carries number k then simply happens by
  34. the unit-clause {sdk(i,j,k)}. </li>
  35. </ul>
  36. </li>
  37. <li> The Dimacs-output should contain those clauses at the beginning.
  38. </li>
  39. <li> In order to facilitate adding clauses to the already standardised
  40. Dimacs-file, the index of variables should be well-defined.
  41. <ul>
  42. <li> Perhaps innermost is the field-value, then the column index, then
  43. the row index (thus just following the coordinate-ordering already
  44. in place for the sdk-variables). </li>
  45. <li> I.e., for p = 3 we had the order sdk(1,1,1), ..., sdk(1,1,9),
  46. sdk(1,2,1), ..., sdk(9,9,9). </li>
  47. </ul>
  48. </li>
  49. <li> So some helper functions are needed to compute the standardised
  50. indices from the original names. </li>
  51. <li> Compare "Extensions" in
  52. ComputerAlgebra/Satisfiability/Lisp/Generators/plans/LatinSquares.hpp.
  53. </li>
  54. <li> For creating the Sudoku Dimacs-files then we do not need the
  55. standardisation anymore, but we could directly use the variable indices.
  56. </li>
  57. </ol>
  58. </li>
  59. </ul>
  60. \todo Improve implementation
  61. <ul>
  62. <li> Compare "Systematisation" in
  63. ComputerAlgebra/Satisfiability/Lisp/Generators/plans/LatinSquares.hpp. </li>
  64. <li> One should try whether using "create_list" in sdk_different_boxes,
  65. without storing the intermediate value, really slows down the computation
  66. (possibly the optimisation is detected?). </li>
  67. <li> DONE Can something be done about a stack-overflow for
  68. output_strong_sdk(6,"~/sdk_s_6.cnf") on csltok? Works on cs-wsok, and so
  69. it just seems to be a question of main memory (though csltok has 2GB). </li>
  70. <li> Surjectivity constraints: DONE
  71. <ol>
  72. <li> For rows, columns, and boxes translate the surjectivity
  73. constraints. </li>
  74. <li> Create a "dual weak sdk". </li>
  75. <li> The union of weak sdk and dual weak sdk then deserves the
  76. name "strong sdk". </li>
  77. </ol>
  78. </li>
  79. <li> Add the conditions, that a field doesn't get several numbers.
  80. DONE. </li>
  81. <li> DONE (except of sdk_different_boxes, where it doesn't seem possible
  82. to store an intermediate value)
  83. Replace, if possible, all loops by the use of "create_list". </li>
  84. </ul>
  85. \todo Random Sudoku problems
  86. <ul>
  87. <li> For the creation of random Sudoku-problems the easiest approach
  88. seems to fix the number f of fixed fields, and then choose f random
  89. fields from the N^2 fields, and for each chosen field choose a random
  90. value from {1,...,N}. In other words, using the natural notion of
  91. (non-boolean) literals a random problem is just given by a random
  92. partial assignment with a fixed number of f variables.
  93. <ol>
  94. <li> Such random Sudoku-problems should also undergo a phase transition
  95. (for fixed p and appropriate f) ?! </li>
  96. <li> Creation of the partial assignment (with non-boolean variables)
  97. simply as a random clause by the OKgenerator. </li>
  98. <li> For the analysis one can filter out partial assignments which
  99. are not r_k-consistent (so r_0-consistency just means that the
  100. partial assignment does not (directly) falsify a clause); no
  101. consistency check then would be "-1-consistency". </li>
  102. <li> To speed up the creation, filtering out of r_k-inconsistent
  103. partial assignments can be done already during the creation (without
  104. disturbing the probability distribution). </li>
  105. <li> However, even r_1-reduction for box-dimension 2 takes a long
  106. time in Maxima (90 s). Thus this filtering cannot be done in Maxima,
  107. but we need C++ components. </li>
  108. </ol>
  109. </li>
  110. </ul>
  111. \todo Sampling of all solutions
  112. <ul>
  113. <li> A solution is just a total assignment (fulfilling the
  114. Sudoku-constraints. </li>
  115. <li> See Satisfiability/Enumeration/plans/general.hpp. </li>
  116. <li> A natural approach seems to be to create random instances,
  117. remove the unsatisfiable ones, and pick a solution.
  118. <ol>
  119. <li> Just picking the first solution might lead to a bias? </li>
  120. <li> So we could try to pick the second, third, ... solution. </li>
  121. <li> Perfectly, we would enumerate all solutions and pick one at
  122. random. But this is likely infeasible in most cases. </li>
  123. <li> But sampling the solution space of a problem instance
  124. might be more feasible. </li>
  125. <li> Of course, one could apply this approach just to the
  126. problem given by the empty partial assignment, and obtain in this
  127. way all solutions. </li>
  128. <li> However it might be worthwile to first restrict the solution
  129. space. </li>
  130. </ol>
  131. </li>
  132. </ul>
  133. \todo Sampling of minimally uniquely satisfiable problems
  134. <ul>
  135. <li> Here the task is to sample (count, enumerate etc.) all Sudoku problems
  136. which are minimally uniquely satisfiable (they are uniquely satisfiable,
  137. and removing any assignment creates further solutions). </li>
  138. <li> So "minimal" here is related to the partial assignment (and not
  139. to the problem instance). </li>
  140. <li> The natural approach is to create a random solution (see "Sampling of
  141. all solutions" above), which is trivially uniquely satisfiable and then
  142. randomly remove assignments (i.e., literals) which can be removed without
  143. creating further solutions, until we obtain a minimally uniquely satisfiable
  144. problem.
  145. <ol>
  146. <li> DONE (see minUNISAT in
  147. Satisfiability/Lisp/Generators/plans/PlantedSolutions.hpp)
  148. We should create a general procedure, which starts with some unique
  149. solution phi (could be a partial assignment in general), and then picks
  150. variables by some heuristics and removes them from phi. </li>
  151. <li> Using the boolean literals, in general we get a "fractured Sudoku
  152. instance", where fields have forbidden values. </li>
  153. <li> But if we use just a positive partial assignment, which only states
  154. the occupied fields (not the other forbidden values, which follow by
  155. UCP), then actually we get standard Sudoku problems, and don't need
  156. to use negatively monosigned literals. </li>
  157. <li> For Sudoku it should be most natural to start with some given
  158. total solution (represented by a partial assignment containing
  159. only positive literals). </li>
  160. <li> Interesting questions here are what heuristics to use for removing
  161. a variable (if there are several choices), and whether the solution we
  162. start with makes a difference. </li>
  163. <li> The first objective could be to use the OKsolver-2002, and to first
  164. maximise the number of nodes in the tree, second the number of
  165. 2-reductions. </li>
  166. <li> There should be instances which are not completely decided by
  167. 2-reduction; but we can also try maximise the number of 2-reductions
  168. for those instances decided by 2-reduction (where the OKsolver-2002
  169. only needs one node). </li>
  170. <li> For specification purposes the Maxima functions are fine (of course),
  171. but they will be too slow, so that later a little C++ program is needed.
  172. </li>
  173. </ol>
  174. </li>
  175. <li> Is the discussion and implementation of a Sudoku generator at
  176. http://skas-blog.blogspot.com/2007/07/end-of-sudoku-road.html
  177. relevant for us?
  178. <ol>
  179. <li> To me (OK) this looks rather amateurish; and, of
  180. course, completely ignored is the main perspective for us, that
  181. of generalised Sudoku as an NP-complete problem. </li>
  182. <li> In those discussions the "hardness" of a problem always plays
  183. a role; I think a good way to measure it is the least k such that
  184. r_k yields the solution (this only works for unique solutions;
  185. otherwise generalised reductions have to be used). </li>
  186. </ol>
  187. </li>
  188. </ul>
  189. \todo Sudoku generator for the Minion constraint solver.
  190. <ul>
  191. <li> This should be placed into a module for constraint satisfaction. </li>
  192. <li> What is the meaning of "generator" here? Is this just a file-format, or,
  193. more meaningfully, do we first create a constraint-satisfaction-problem,
  194. using a suitable Maxima-representation of "constraints", and then output
  195. this in Minion-format? </li>
  196. <li> Parameters: n (box size), N = n^2, M = N^2 </li>
  197. <li> Cells variables : {x_1,...,x_M}. dom(x_i) = {1,...,N} for
  198. i = 1...M. x_i = j means j is assigned to cell i. </li>
  199. <li> Boxes are represented by vectors v_i (i = 1..N) of cell
  200. variables. </li>
  201. <li> The Sudoku then consists of a single N x N matrix of cell variables.
  202. </li>
  203. <li> %Implementation:
  204. <ul>
  205. <li> Row constraints:
  206. \verbatim
  207. alldifferent(row(m_0),i)
  208. \endverbatim
  209. for i = 1..N
  210. </li>
  211. <li> Column constraints:
  212. \verbatim
  213. alldifferent(col(m_0,i))
  214. \endverbatim
  215. for i = 1..N
  216. </li>
  217. <li> Box constraints:
  218. \verbatim
  219. alldifferent(v_i)
  220. \endverbatim
  221. for i = 1..N
  222. </li>
  223. </ul>
  224. </li>
  225. <li> Syntax:
  226. (what syntax?? the file-content?)
  227. \verbatim
  228. sudoku_minion_format(boxsize, filename)
  229. \endverbatim
  230. </li>
  231. </ul>
  232. */