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.

268 lines
9.2 KiB

  1. /* PBN, Paint-By-Numbers Puzzle */
  2. /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
  3. /* NOTE: See also the document "Solving Paint-By-Numbers Puzzles with
  4. GLPK", which is included in the GLPK distribution. */
  5. /* A paint-by-numbers puzzle consists of an m*n grid of pixels (the
  6. canvas) together with m+n cluster-size sequences, one for each row
  7. and column. The goal is to paint the canvas with a picture that
  8. satisfies the following constraints:
  9. 1. Each pixel must be blank or white.
  10. 2. If a row or column has cluster-size sequence s1, s2, ..., sk,
  11. then it must contain k clusters of black pixels - the first with
  12. s1 black pixels, the second with s2 black pixels, and so on.
  13. It should be noted that "first" means "leftmost" for rows and
  14. "topmost" for columns, and that rows and columns need not begin or
  15. end with black pixels.
  16. Example:
  17. 1 1
  18. 1 1
  19. 2 1 1 1 1 1 2 3
  20. 3 2 1 2 1 2 3 4 8 9
  21. 3 6 # # # . # # # # # #
  22. 1 4 # . . . . . # # # #
  23. 1 1 3 . . # . # . . # # #
  24. 2 . . . . . . . . # #
  25. 3 3 . . # # # . . # # #
  26. 1 4 # . . . . . # # # #
  27. 2 5 # # . . . # # # # #
  28. 2 5 # # . . . # # # # #
  29. 1 1 . . . # . . . . . #
  30. 3 . . # # # . . . . .
  31. (In Russia such puzzles are known as "Japanese crosswords".)
  32. References:
  33. Robert A. Bosch, "Painting by Numbers", 2000.
  34. <http://www.oberlin.edu/~math/faculty/bosch/pbn-page.html> */
  35. /*--------------------------------------------------------------------*/
  36. /* Main part based on the formulation proposed by Robert Bosch. */
  37. param m, integer, >= 1;
  38. /* the number of rows */
  39. param n, integer, >= 1;
  40. /* the number of columns */
  41. param row{i in 1..m, 1..n div 2}, integer, >= 0, default 0;
  42. /* the cluster-size sequence for row i (raw data) */
  43. param col{j in 1..n, 1..m div 2}, integer, >= 0, default 0;
  44. /* the cluster-size sequence for column j (raw data) */
  45. param kr{i in 1..m} := sum{t in 1..n div 2: row[i,t] > 0} 1;
  46. /* the number of clusters in row i */
  47. param kc{j in 1..n} := sum{t in 1..m div 2: col[j,t] > 0} 1;
  48. /* the number of clusters in column j */
  49. param sr{i in 1..m, t in 1..kr[i]} := row[i,t], integer, >= 1;
  50. /* the cluster-size sequence for row i */
  51. param sc{j in 1..n, t in 1..kc[j]} := col[j,t], integer, >= 1;
  52. /* the cluster-size sequence for column j */
  53. check{i in 1..m}: sum{t in 1..kr[i]} sr[i,t] <= n - (kr[i] - 1);
  54. /* check that the sum of the cluster sizes in each row is valid */
  55. check{j in 1..n}: sum{t in 1..kc[j]} sc[j,t] <= m - (kc[j] - 1);
  56. /* check that the sum of the cluster sizes in each column is valid */
  57. check: sum{i in 1..m, t in 1..kr[i]} sr[i,t] =
  58. sum{j in 1..n, t in 1..kc[j]} sc[j,t];
  59. /* check that the sum of the cluster sizes in all rows is equal to the
  60. sum of the cluster sizes in all columns */
  61. param er{i in 1..m, t in 1..kr[i]} :=
  62. if t = 1 then 1 else er[i,t-1] + sr[i,t-1] + 1;
  63. /* the smallest value of j such that row i's t-th cluster can be
  64. placed in row i with its leftmost pixel occupying pixel j */
  65. param lr{i in 1..m, t in 1..kr[i]} :=
  66. if t = kr[i] then n + 1 - sr[i,t] else lr[i,t+1] - sr[i,t] - 1;
  67. /* the largest value of j such that row i's t-th cluster can be
  68. placed in row i with its leftmost pixel occupying pixel j */
  69. param ec{j in 1..n, t in 1..kc[j]} :=
  70. if t = 1 then 1 else ec[j,t-1] + sc[j,t-1] + 1;
  71. /* the smallest value of i such that column j's t-th cluster can be
  72. placed in column j with its topmost pixel occupying pixel i */
  73. param lc{j in 1..n, t in 1..kc[j]} :=
  74. if t = kc[j] then m + 1 - sc[j,t] else lc[j,t+1] - sc[j,t] - 1;
  75. /* the largest value of i such that column j's t-th cluster can be
  76. placed in column j with its topmost pixel occupying pixel i */
  77. var z{i in 1..m, j in 1..n}, binary;
  78. /* z[i,j] = 1, if row i's j-th pixel is painted black
  79. z[i,j] = 0, if row i's j-th pixel is painted white */
  80. var y{i in 1..m, t in 1..kr[i], j in er[i,t]..lr[i,t]}, binary;
  81. /* y[i,t,j] = 1, if row i's t-th cluster is placed in row i with its
  82. leftmost pixel occupying pixel j
  83. y[i,t,j] = 0, if not */
  84. var x{j in 1..n, t in 1..kc[j], i in ec[j,t]..lc[j,t]}, binary;
  85. /* x[j,t,i] = 1, if column j's t-th cluster is placed in column j with
  86. its topmost pixel occupying pixel i
  87. x[j,t,i] = 0, if not */
  88. s.t. fa{i in 1..m, t in 1..kr[i]}:
  89. sum{j in er[i,t]..lr[i,t]} y[i,t,j] = 1;
  90. /* row i's t-th cluster must appear in row i exactly once */
  91. s.t. fb{i in 1..m, t in 1..kr[i]-1, j in er[i,t]..lr[i,t]}:
  92. y[i,t,j] <= sum{jp in j+sr[i,t]+1..lr[i,t+1]} y[i,t+1,jp];
  93. /* row i's (t+1)-th cluster must be placed to the right of its t-th
  94. cluster */
  95. s.t. fc{j in 1..n, t in 1..kc[j]}:
  96. sum{i in ec[j,t]..lc[j,t]} x[j,t,i] = 1;
  97. /* column j's t-th cluster must appear in column j exactly once */
  98. s.t. fd{j in 1..n, t in 1..kc[j]-1, i in ec[j,t]..lc[j,t]}:
  99. x[j,t,i] <= sum{ip in i+sc[j,t]+1..lc[j,t+1]} x[j,t+1,ip];
  100. /* column j's (t+1)-th cluster must be placed below its t-th cluster */
  101. s.t. fe{i in 1..m, j in 1..n}:
  102. z[i,j] <= sum{t in 1..kr[i], jp in er[i,t]..lr[i,t]:
  103. j-sr[i,t]+1 <= jp and jp <= j} y[i,t,jp];
  104. /* the double coverage constraint stating that if row i's j-th pixel
  105. is painted black, then at least one of row i's clusters must be
  106. placed in such a way that it covers row i's j-th pixel */
  107. s.t. ff{i in 1..m, j in 1..n}:
  108. z[i,j] <= sum{t in 1..kc[j], ip in ec[j,t]..lc[j,t]:
  109. i-sc[j,t]+1 <= ip and ip <= i} x[j,t,ip];
  110. /* the double coverage constraint making sure that if row i's j-th
  111. pixel is painted black, then at least one of column j's clusters
  112. covers it */
  113. s.t. fg{i in 1..m, j in 1..n, t in 1..kr[i], jp in er[i,t]..lr[i,t]:
  114. j-sr[i,t]+1 <= jp and jp <= j}: z[i,j] >= y[i,t,jp];
  115. /* the constraint to prevent white pixels from being covered by the
  116. row clusters */
  117. s.t. fh{i in 1..m, j in 1..n, t in 1..kc[j], ip in ec[j,t]..lc[j,t]:
  118. i-sc[j,t]+1 <= ip and ip <= i}: z[i,j] >= x[j,t,ip];
  119. /* the constraint to prevent white pixels from being covered by the
  120. column clusters */
  121. /* this is a feasibility problem, so no objective is needed */
  122. /*--------------------------------------------------------------------*/
  123. /* The following part is used only to check for multiple solutions. */
  124. param zz{i in 1..m, j in 1..n}, binary, default 0;
  125. /* zz[i,j] is z[i,j] for a previously found solution */
  126. s.t. fz{1..1 : sum{i in 1..m, j in 1..n} zz[i,j] > 0}:
  127. sum{i in 1..m, j in 1..n}
  128. (if zz[i,j] then (1 - z[i,j]) else z[i,j]) >= 1;
  129. /* the constraint to forbid finding a solution, which is identical to
  130. the previously found one; this constraint is included in the model
  131. only if the previously found solution specified by the parameter zz
  132. is provided in the data section */
  133. solve;
  134. /*--------------------------------------------------------------------*/
  135. /* Print solution to the standard output. */
  136. for {i in 1..m}
  137. { printf{j in 1..n} " %s", if z[i,j] then "#" else ".";
  138. printf "\n";
  139. }
  140. /*--------------------------------------------------------------------*/
  141. /* Write solution to a text file in PostScript format. */
  142. param ps, symbolic, default "solution.ps";
  143. printf "%%!PS-Adobe-3.0\n" > ps;
  144. printf "%%%%Creator: GLPK (pbn.mod)\n" >> ps;
  145. printf "%%%%BoundingBox: 0 0 %d %d\n",
  146. 6 * (n + 2), 6 * (m + 2) >> ps;
  147. printf "%%%%EndComments\n" >> ps;
  148. printf "<</PageSize [%d %d]>> setpagedevice\n",
  149. 6 * (n + 2), 6 * (m + 2) >> ps;
  150. printf "0.1 setlinewidth\n" >> ps;
  151. printf "/A { 2 copy 2 copy 2 copy newpath moveto exch 6 add exch line" &
  152. "to\n" >> ps;
  153. printf "exch 6 add exch 6 add lineto 6 add lineto closepath } bind de" &
  154. "f\n" >> ps;
  155. printf "/W { A stroke } def\n" >> ps;
  156. printf "/B { A fill } def\n" >> ps;
  157. printf {i in 1..m, j in 1..n} "%d %d %s\n",
  158. (j - 1) * 6 + 6, (m - i) * 6 + 6,
  159. if z[i,j] then "B" else "W" >> ps;
  160. printf "%%%%EOF\n" >> ps;
  161. printf "Solution has been written to file %s\n", ps;
  162. /*--------------------------------------------------------------------*/
  163. /* Write solution to a text file in the form of MathProg data section,
  164. which can be used later to check for multiple solutions. */
  165. param dat, symbolic, default "solution.dat";
  166. printf "data;\n" > dat;
  167. printf "\n" >> dat;
  168. printf "param zz :" >> dat;
  169. printf {j in 1..n} " %d", j >> dat;
  170. printf " :=\n" >> dat;
  171. for {i in 1..m}
  172. { printf " %2d", i >> dat;
  173. printf {j in 1..n} " %s", if z[i,j] then "1" else "." >> dat;
  174. printf "\n" >> dat;
  175. }
  176. printf ";\n" >> dat;
  177. printf "\n" >> dat;
  178. printf "end;\n" >> dat;
  179. printf "Solution has also been written to file %s\n", dat;
  180. /*--------------------------------------------------------------------*/
  181. /* The following data correspond to the example above. */
  182. data;
  183. param m := 10;
  184. param n := 10;
  185. param row : 1 2 3 :=
  186. 1 3 6 .
  187. 2 1 4 .
  188. 3 1 1 3
  189. 4 2 . .
  190. 5 3 3 .
  191. 6 1 4 .
  192. 7 2 5 .
  193. 8 2 5 .
  194. 9 1 1 .
  195. 10 3 . .
  196. ;
  197. param col : 1 2 3 4 :=
  198. 1 2 3 . .
  199. 2 1 2 . .
  200. 3 1 1 1 1
  201. 4 1 2 . .
  202. 5 1 1 1 1
  203. 6 1 2 . .
  204. 7 2 3 . .
  205. 8 3 4 . .
  206. 9 8 . . .
  207. 10 9 . . .
  208. ;
  209. end;