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