1 | /* SUDOKU, Number Placement Puzzle */ |
---|
2 | |
---|
3 | /* Written in GNU MathProg by Andrew Makhorin <mao@mai2.rcnet.ru> */ |
---|
4 | |
---|
5 | /* This example shows how to use the table statement. |
---|
6 | The sudoku to be solves is read from file sudoku_in.csv. |
---|
7 | The solution is written to sudoku_out.csv. |
---|
8 | The file format is CSV as defined in |
---|
9 | RFC 4180 - Common Format and MIME Type for |
---|
10 | Comma-Separated Values (CSV) Files */ |
---|
11 | |
---|
12 | /* Sudoku, also known as Number Place, is a logic-based placement |
---|
13 | puzzle. The aim of the canonical puzzle is to enter a numerical |
---|
14 | digit from 1 through 9 in each cell of a 9x9 grid made up of 3x3 |
---|
15 | subgrids (called "regions"), starting with various digits given in |
---|
16 | some cells (the "givens"). Each row, column, and region must contain |
---|
17 | only one instance of each numeral. |
---|
18 | |
---|
19 | Example: |
---|
20 | |
---|
21 | +-------+-------+-------+ |
---|
22 | | 5 3 . | . 7 . | . . . | |
---|
23 | | 6 . . | 1 9 5 | . . . | |
---|
24 | | . 9 8 | . . . | . 6 . | |
---|
25 | +-------+-------+-------+ |
---|
26 | | 8 . . | . 6 . | . . 3 | |
---|
27 | | 4 . . | 8 . 3 | . . 1 | |
---|
28 | | 7 . . | . 2 . | . . 6 | |
---|
29 | +-------+-------+-------+ |
---|
30 | | . 6 . | . . . | 2 8 . | |
---|
31 | | . . . | 4 1 9 | . . 5 | |
---|
32 | | . . . | . 8 . | . 7 9 | |
---|
33 | +-------+-------+-------+ |
---|
34 | |
---|
35 | (From Wikipedia, the free encyclopedia.) */ |
---|
36 | set fields dimen 2; |
---|
37 | |
---|
38 | param id; |
---|
39 | |
---|
40 | param givens{1..9, 1..9}, integer, >= 0, <= 9, default 0; |
---|
41 | /* the "givens" */ |
---|
42 | |
---|
43 | /* |
---|
44 | table ti IN 'MySQL' 'Database=glpk;UID=glpk;PWD=gnu' |
---|
45 | 'sudoku' : |
---|
46 | fields <- [COL, LIN], givens ~ VAL; |
---|
47 | */ |
---|
48 | table ti IN 'MySQL' 'Database=glpk;UID=glpk;PWD=gnu' |
---|
49 | 'SELECT * FROM sudoku WHERE ID = ' & id : |
---|
50 | fields <- [COL, LIN], givens ~ VAL; |
---|
51 | |
---|
52 | var x{i in 1..9, j in 1..9, k in 1..9}, binary; |
---|
53 | /* x[i,j,k] = 1 means cell [i,j] is assigned number k */ |
---|
54 | |
---|
55 | s.t. fa{i in 1..9, j in 1..9, k in 1..9: givens[i,j] != 0}: |
---|
56 | x[i,j,k] = (if givens[i,j] = k then 1 else 0); |
---|
57 | /* assign pre-defined numbers using the "givens" */ |
---|
58 | |
---|
59 | s.t. fb{i in 1..9, j in 1..9}: sum{k in 1..9} x[i,j,k] = 1; |
---|
60 | /* each cell must be assigned exactly one number */ |
---|
61 | |
---|
62 | s.t. fc{i in 1..9, k in 1..9}: sum{j in 1..9} x[i,j,k] = 1; |
---|
63 | /* cells in the same row must be assigned distinct numbers */ |
---|
64 | |
---|
65 | s.t. fd{j in 1..9, k in 1..9}: sum{i in 1..9} x[i,j,k] = 1; |
---|
66 | /* cells in the same column must be assigned distinct numbers */ |
---|
67 | |
---|
68 | s.t. fe{I in 1..9 by 3, J in 1..9 by 3, k in 1..9}: |
---|
69 | sum{i in I..I+2, j in J..J+2} x[i,j,k] = 1; |
---|
70 | /* cells in the same region must be assigned distinct numbers */ |
---|
71 | |
---|
72 | /* there is no need for an objective function here */ |
---|
73 | |
---|
74 | solve; |
---|
75 | |
---|
76 | table ta{(i,j) in fields} OUT |
---|
77 | 'MySQL' 'Database=glpk;UID=glpk;PWD=gnu' |
---|
78 | 'DELETE FROM sudoku_solution' |
---|
79 | 'WHERE ID = ' & id & ';' |
---|
80 | 'INSERT INTO sudoku_solution' |
---|
81 | '(ID, COL, LIN, VAL)' |
---|
82 | 'VALUES(?, ?, ?, ?);' : |
---|
83 | id ~ ID, i ~ COL, j ~ LIN, (sum{k in 1..9} x[i,j,k] * k) ~ VAL; |
---|
84 | |
---|
85 | printf "\nSudoku to be solved\n"; |
---|
86 | for {i in 1..9} |
---|
87 | { for {0..0: i = 1 or i = 4 or i = 7} |
---|
88 | printf " +-------+-------+-------+\n"; |
---|
89 | for {j in 1..9} |
---|
90 | { for {0..0: j = 1 or j = 4 or j = 7} printf(" |"); |
---|
91 | printf " %d", givens[i,j]; |
---|
92 | for {0..0: j = 9} printf(" |\n"); |
---|
93 | } |
---|
94 | for {0..0: i = 9} |
---|
95 | printf " +-------+-------+-------+\n"; |
---|
96 | } |
---|
97 | printf "\nSolution\n"; |
---|
98 | for {i in 1..9} |
---|
99 | { for {0..0: i = 1 or i = 4 or i = 7} |
---|
100 | printf " +-------+-------+-------+\n"; |
---|
101 | for {j in 1..9} |
---|
102 | { for {0..0: j = 1 or j = 4 or j = 7} printf(" |"); |
---|
103 | printf " %d", sum{k in 1..9} x[i,j,k] * k; |
---|
104 | for {0..0: j = 9} printf(" |\n"); |
---|
105 | } |
---|
106 | for {0..0: i = 9} |
---|
107 | printf " +-------+-------+-------+\n"; |
---|
108 | } |
---|
109 | |
---|
110 | data; |
---|
111 | |
---|
112 | param id := 1; |
---|
113 | end; |
---|