1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/sql/sudoku_odbc.mod Mon Dec 06 13:09:21 2010 +0100
1.3 @@ -0,0 +1,111 @@
1.4 +/* SUDOKU, Number Placement Puzzle */
1.5 +
1.6 +/* Written in GNU MathProg by Andrew Makhorin <mao@mai2.rcnet.ru> */
1.7 +
1.8 +/* This example shows how to use the table statement.
1.9 + The sudoku to be solves is read from file sudoku_in.csv.
1.10 + The solution is written to sudoku_out.csv.
1.11 + The file format is CSV as defined in
1.12 + RFC 4180 - Common Format and MIME Type for
1.13 + Comma-Separated Values (CSV) Files */
1.14 +
1.15 +/* Sudoku, also known as Number Place, is a logic-based placement
1.16 + puzzle. The aim of the canonical puzzle is to enter a numerical
1.17 + digit from 1 through 9 in each cell of a 9x9 grid made up of 3x3
1.18 + subgrids (called "regions"), starting with various digits given in
1.19 + some cells (the "givens"). Each row, column, and region must contain
1.20 + only one instance of each numeral.
1.21 +
1.22 + Example:
1.23 +
1.24 + +-------+-------+-------+
1.25 + | 5 3 . | . 7 . | . . . |
1.26 + | 6 . . | 1 9 5 | . . . |
1.27 + | . 9 8 | . . . | . 6 . |
1.28 + +-------+-------+-------+
1.29 + | 8 . . | . 6 . | . . 3 |
1.30 + | 4 . . | 8 . 3 | . . 1 |
1.31 + | 7 . . | . 2 . | . . 6 |
1.32 + +-------+-------+-------+
1.33 + | . 6 . | . . . | 2 8 . |
1.34 + | . . . | 4 1 9 | . . 5 |
1.35 + | . . . | . 8 . | . 7 9 |
1.36 + +-------+-------+-------+
1.37 +
1.38 + (From Wikipedia, the free encyclopedia.) */
1.39 +set fields dimen 2;
1.40 +
1.41 +param id;
1.42 +
1.43 +param givens{1..9, 1..9}, integer, >= 0, <= 9, default 0;
1.44 +/* the "givens" */
1.45 +
1.46 +table ti IN 'iODBC'
1.47 + 'DSN=glpk;UID=glpk;PWD=gnu'
1.48 + 'SELECT * FROM sudoku'
1.49 + 'WHERE ID = ' & id :
1.50 + fields <- [COL, LIN], givens ~ VAL;
1.51 +
1.52 +var x{i in 1..9, j in 1..9, k in 1..9}, binary;
1.53 +/* x[i,j,k] = 1 means cell [i,j] is assigned number k */
1.54 +
1.55 +s.t. fa{i in 1..9, j in 1..9, k in 1..9: givens[i,j] != 0}:
1.56 + x[i,j,k] = (if givens[i,j] = k then 1 else 0);
1.57 +/* assign pre-defined numbers using the "givens" */
1.58 +
1.59 +s.t. fb{i in 1..9, j in 1..9}: sum{k in 1..9} x[i,j,k] = 1;
1.60 +/* each cell must be assigned exactly one number */
1.61 +
1.62 +s.t. fc{i in 1..9, k in 1..9}: sum{j in 1..9} x[i,j,k] = 1;
1.63 +/* cells in the same row must be assigned distinct numbers */
1.64 +
1.65 +s.t. fd{j in 1..9, k in 1..9}: sum{i in 1..9} x[i,j,k] = 1;
1.66 +/* cells in the same column must be assigned distinct numbers */
1.67 +
1.68 +s.t. fe{I in 1..9 by 3, J in 1..9 by 3, k in 1..9}:
1.69 + sum{i in I..I+2, j in J..J+2} x[i,j,k] = 1;
1.70 +/* cells in the same region must be assigned distinct numbers */
1.71 +
1.72 +/* there is no need for an objective function here */
1.73 +
1.74 +
1.75 +solve;
1.76 +
1.77 +table ta {(i, j) in {i1 in 1..9} cross {i2 in 1..9}} OUT
1.78 + 'iODBC' 'DSN=glpk;UID=glpk;PWD=gnu'
1.79 + 'DELETE FROM sudoku_solution'
1.80 + 'WHERE ID = ' & id & ';'
1.81 + 'INSERT INTO sudoku_solution'
1.82 + '(ID, COL, LIN, VAL)'
1.83 + 'VALUES(?, ?, ?, ?);' :
1.84 + id ~ ID, i ~ COL, j ~ LIN, (sum{k in 1..9} x[i,j,k] * k) ~ VAL;
1.85 +
1.86 +printf "\nSudoku to be solved\n";
1.87 +for {i in 1..9}
1.88 +{ for {0..0: i = 1 or i = 4 or i = 7}
1.89 + printf " +-------+-------+-------+\n";
1.90 + for {j in 1..9}
1.91 + { for {0..0: j = 1 or j = 4 or j = 7} printf(" |");
1.92 + printf " %d", givens[i,j];
1.93 + for {0..0: j = 9} printf(" |\n");
1.94 + }
1.95 + for {0..0: i = 9}
1.96 + printf " +-------+-------+-------+\n";
1.97 + }
1.98 +printf "\nSolution\n";
1.99 +for {i in 1..9}
1.100 +{ for {0..0: i = 1 or i = 4 or i = 7}
1.101 + printf " +-------+-------+-------+\n";
1.102 + for {j in 1..9}
1.103 + { for {0..0: j = 1 or j = 4 or j = 7} printf(" |");
1.104 + printf " %d", sum{k in 1..9} x[i,j,k] * k;
1.105 + for {0..0: j = 9} printf(" |\n");
1.106 + }
1.107 + for {0..0: i = 9}
1.108 + printf " +-------+-------+-------+\n";
1.109 +}
1.110 +
1.111 +data;
1.112 +
1.113 +param id := 1;
1.114 +end;