You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
9.7 KiB

2 months ago
  1. %* pbn.tex *%
  2. \documentclass[11pt,draft]{article}
  3. \usepackage{amssymb}
  4. \begin{document}
  5. \title{Solving Paint-By-Numbers Puzzles with GLPK}
  6. \author{Andrew Makhorin {\tt<mao@gnu.org>}}
  7. \date{August 2011}
  8. \maketitle
  9. \section{Introduction$^1$}
  10. \footnotetext[1]{This section is based on the material from [1].}
  11. A {\it paint-by-numbers} puzzle consists of an $m\times n$ grid of
  12. pixels (the {\it canvas}) together with $m+n$ {\it cluster-size
  13. sequences}, one for each row and column. The goal is to paint the canvas
  14. with a picture that satisfies the following constraints:
  15. 1. Each pixel must be blank or white.
  16. 2. If a row or column has cluster-size sequence $s_1$, $s_2$, \dots,
  17. $s_k$, then it must contain $k$ clusters of black pixels---the first
  18. with $s_1$ black pixels, the second with $s_2$ black pixels, and so on.
  19. It should be noted that ``first'' means ``leftmost'' for rows and
  20. ``topmost'' for columns, and that rows and columns need not begin or end
  21. with black pixels.
  22. \subsubsection*{Example}
  23. \def\arraystretch{.8}
  24. \begin{center}
  25. \begin{tabular}{*{3}{@{$\;\;$}c}c*{10}{@{\ }c}@{}}
  26. & & && & &1& &1& & & & & \\
  27. & & && & &1& &1& & & & & \\
  28. & & &&2&1&1&1&1&1&2&3& & \\
  29. & & &&3&2&1&2&1&2&3&4&8&9\\
  30. \\
  31. &3&6&&$\blacksquare$&$\blacksquare$&$\blacksquare$&$\square$&
  32. $\blacksquare$&$\blacksquare$&$\blacksquare$&$\blacksquare$&
  33. $\blacksquare$&$\blacksquare$\\
  34. &1&4&&$\blacksquare$&$\square$&$\square$&$\square$&$\square$&
  35. $\square$&$\blacksquare$&$\blacksquare$&$\blacksquare$&$\blacksquare$\\
  36. 1&1&3&&$\square$&$\square$&$\blacksquare$&$\square$&$\blacksquare$&
  37. $\square$&$\square$&$\blacksquare$&$\blacksquare$&$\blacksquare$\\
  38. & &2&&$\square$&$\square$&$\square$&$\square$&$\square$&$\square$&
  39. $\square$&$\square$&$\blacksquare$&$\blacksquare$\\
  40. &3&3&&$\square$&$\square$&$\blacksquare$&$\blacksquare$&$\blacksquare$&
  41. $\square$&$\square$&$\blacksquare$&$\blacksquare$&$\blacksquare$\\
  42. &1&4&&$\blacksquare$&$\square$&$\square$&$\square$&$\square$&$\square$&
  43. $\blacksquare$&$\blacksquare$&$\blacksquare$&$\blacksquare$\\
  44. &2&5&&$\blacksquare$&$\blacksquare$&$\square$&$\square$&$\square$&
  45. $\blacksquare$&$\blacksquare$&$\blacksquare$&$\blacksquare$&
  46. $\blacksquare$\\
  47. &2&5&&$\blacksquare$&$\blacksquare$&$\square$&$\square$&$\square$&
  48. $\blacksquare$&$\blacksquare$&$\blacksquare$&$\blacksquare$&
  49. $\blacksquare$\\
  50. &1&1&&$\square$&$\square$&$\square$&$\blacksquare$&$\square$&$\square$&
  51. $\square$&$\square$&$\square$&$\blacksquare$\\
  52. & &3&&$\square$&$\square$&$\blacksquare$&$\blacksquare$&$\blacksquare$&
  53. $\square$&$\square$&$\square$&$\square$&$\square$\\
  54. \end{tabular}
  55. \end{center}
  56. \def\arraystretch{1}
  57. \section{Solving a puzzle}
  58. The Paint-By-Numbers puzzle can be formulated as a 0-1 integer
  59. feasibility problem. The formulation used in GLPK was proposed in [1].
  60. For solving puzzles there are used two components, which both are
  61. coded in the GNU MathProg modeling language [2]: the model section and
  62. the data section. The model section is common for all puzzles and
  63. placed in file \verb|pbn.mod|. This file is included in the GLPK
  64. distribution and can be found in subdirectory \verb|examples/pbn|.
  65. To solve a particular puzzle the user only needs to prepare the data
  66. section, which defines input data to the puzzle. The data section for
  67. the example puzzle from the previous section may look like follows
  68. (here \verb|m| is the number of rows, and \verb|n| is the number of
  69. columns):
  70. \begin{footnotesize}
  71. \begin{verbatim}
  72. data;
  73. param m := 10;
  74. param n := 10;
  75. param row : 1 2 3 :=
  76. 1 3 6 .
  77. 2 1 4 .
  78. 3 1 1 3
  79. 4 2 . .
  80. 5 3 3 .
  81. 6 1 4 .
  82. 7 2 5 .
  83. 8 2 5 .
  84. 9 1 1 .
  85. 10 3 . .
  86. ;
  87. param col : 1 2 3 4 :=
  88. 1 2 3 . .
  89. 2 1 2 . .
  90. 3 1 1 1 1
  91. 4 1 2 . .
  92. 5 1 1 1 1
  93. 6 1 2 . .
  94. 7 2 3 . .
  95. 8 3 4 . .
  96. 9 8 . . .
  97. 10 9 . . .
  98. ;
  99. end;
  100. \end{verbatim}
  101. \end{footnotesize}
  102. \newpage
  103. Let the data section for a puzzle be placed in file \verb|foo.dat|.
  104. Then to solve the puzzle the user should enter the following command:
  105. \begin{verbatim}
  106. glpsol --minisat -m pbn.mod -d foo.dat
  107. \end{verbatim}
  108. \noindent
  109. This command invokes \verb|glpsol|, the GLPK LP/MIP stand-alone solver,
  110. which reads the model section from file \verb|pbn.mod|, the data section
  111. from file \verb|foo.dat|, translates them to an internal representation,
  112. and solves the resulting 0-1 integer feasibility problem. The option
  113. \verb|--minisat| tells \verb|glpsol| to translate the feasibility
  114. problem to a CNF satisfiability problem and then use the MiniSat solver
  115. [3] to solve it.
  116. If a solution to the puzzle has been found, that is indicated by the
  117. message \verb|SATISFIABLE|, \verb|glpsol| prints the solution to the
  118. standard output (terminal), writes it to file \verb|solution.ps| in the
  119. PostScript format, and also writes it to file \verb|solution.dat| in the
  120. form of MathProg data section, which can be used later to check for
  121. multiple solutions, if necessary (for details see the next section).
  122. The message \verb|UNSATISFIABLE| means that the puzzle has no solution.
  123. Usually the time taken to solve a puzzle of moderate size (up to 50 rows
  124. and columns) varies from several seconds to several minutes. However,
  125. hard or large puzzles may require much more time.
  126. Data sections for some example puzzles included in the GLPK distribution
  127. can be found in subdirectory \verb|examples/pbn|.
  128. \section{Checking for multiple solutions}
  129. Sometimes the user may be interested to know if the puzzle has exactly
  130. one (unique) solution or it has multiple solutions. To check that the
  131. user should solve the puzzle as explained above in the previous section
  132. and then enter the following command:
  133. \begin{verbatim}
  134. glpsol --minisat -m pbn.mod -d foo.dat -d solution.dat
  135. \end{verbatim}
  136. \noindent
  137. In this case \verb|glpsol| reads an additional data section from file
  138. \verb|solution.dat|, which contains the previously found solution,
  139. activates an additional constraint in the model section to forbid
  140. finding the solution specified in the additional data section, and
  141. attempts to find another solution. The message \verb|UNSATISFIABLE|
  142. reported by \verb|glpsol| will mean that the puzzle has a unique
  143. solution, while the message \verb|SATISFIABLE| will mean that the puzzle
  144. has at least two different solutions.
  145. \newpage
  146. \section{Solution times}
  147. The table on the next page shows solution times on a sample set of
  148. the paint-by-numbers puzzles from the \verb|<webpbn.com>| website.
  149. This sample set was used in the survey [4] to compare efficiency of
  150. existing PBN solvers.
  151. The authors of some puzzles from the sample set have given permission
  152. for their puzzles to be freely redistributed as long as the original
  153. attribution and copyright statement are retained. In the table these
  154. puzzles are marked by an asterisk (*). The files containing the
  155. MathProg data sections for these puzzles are included in the GLPK
  156. distribution and can be found in subdirectory \verb|examples/pbn|.
  157. All runs were performed on Intel Pentium 4 (CPU 3GHz, 2GB of RAM).
  158. The C compiler used was GCC 3.4.4 with default optimization options.
  159. The column `Sol.Time' shows the time, in seconds, taken by the
  160. \verb|glpsol| solver to find a solution to corresponding puzzle. The
  161. column `Chk.Time' shows the time, in seconds, taken by \verb|glpsol| to
  162. check for multiple solutions, i.e. either to prove that the puzzle has
  163. a unique solution or find another solution to the puzzle. Both these
  164. times do not include the time used to translate the MathProg model and
  165. data sections into an internal MIP representation, but include the time
  166. used to translate the 0-1 feasibility problem to a CNF satisfiability
  167. problem.
  168. \begin{thebibliography}{10}
  169. \bibitem{1}
  170. Robert A. Bosch, ``Painting by Numbers'', 2000.\\
  171. \verb|<http://www.oberlin.edu/~math/faculty/bosch/pbn-page.html>|.
  172. \bibitem{2}
  173. GLPK: Modeling Language GNU MathProg. Language Reference. (This
  174. document is included in the GLPK distribution and can be found in
  175. subdirectory \verb|doc|.)
  176. \bibitem{3}
  177. Niklas E\'en, Niklas S\"orensson, ``An Extensible SAT-solver'',
  178. Chalmers University of Technology, Sweden. \verb|<http://minisat.se/>|.
  179. \bibitem{4}
  180. Jan Wolter, ``Survey of Paint-by-Number Puzzle Solvers''.\\
  181. \verb|<http://webpbn.com/survey/>|.
  182. \end{thebibliography}
  183. \newpage
  184. \begin{table}
  185. \caption{Solution times on the sample set of puzzles from [4]}
  186. \begin{center}
  187. \begin{tabular}{@{}lllcrr@{}}
  188. \hline
  189. \multicolumn{2}{c}{Puzzle}&Size&Notes&Sol.Time, s&Chk.Time, s\\
  190. \hline
  191. \#1&Dancer* &$10\times 5$&L&$<1$&$<1$\\
  192. \#6&Cat* &$20\times 20$&L&$<1$&$<1$\\
  193. \#21&Skid* &$25\times 14$&L, B&$<1$&$<1$\\
  194. \#27&Bucks* &$23\times 27$&B&$<1$&$<1$\\
  195. \#23&Edge* &$11\times 10$&&$<1$&$<1$\\
  196. \#2413&Smoke &$20\times 20$&&$<1$&$<1$\\
  197. \#16&Knot* &$34\times 34$&L&1&1\\
  198. \#529&Swing* &$45\times 45$&L&1&1\\
  199. \#65&Mum* &$40\times 34$&&1&1\\
  200. \#7604&DiCap &$55\times 55$&&10&10\\
  201. \#1694&Tragic &$50\times 45$&&3&3\\
  202. \#1611&Merka &$60\times 55$&B&4&4\\
  203. \#436&Petro* &$35\times 40$&&1&1\\
  204. \#4645&M\&M &$70\times 50$&B&5&6\\
  205. \#3541&Signed &$50\times 60$&&7&7\\
  206. \#803&Light* &$45\times 50$&B&1&1\\
  207. \#6574&Forever*&$25\times 25$&&1&1\\
  208. \#2040&Hot &$60\times 55$&&6&6\\
  209. \#6739&Karate &$40\times 40$&M&2&2\\
  210. \#8098&9-Dom* &$19\times 19$&&1&2\\
  211. \#2556&Flag &$45\times 65$&M, B&2&2\\
  212. \#2712&Lion &$47\times 47$&M&11&12\\
  213. \#10088&Marley &$63\times 52$&M&135&226\\
  214. \#9892&Nature &$40\times 50$&M&850&1053\\
  215. \hline
  216. \end{tabular}
  217. \begin{tabular}{@{}lp{102mm}@{}}
  218. *&Puzzle designer has given permission to redistribute the puzzle.\\
  219. L&Puzzle is line solvable. That is, it can be solved one line at a
  220. time.\\
  221. B&Puzzle contains blank rows or columns.\\
  222. M&Puzzle has multiple solutions.\\
  223. \end{tabular}
  224. \end{center}
  225. \end{table}
  226. \end{document}