lemon-project-template-glpk

diff deps/glpk/examples/sql/sudoku_mysql.mod @ 9:33de93886c88

Import GLPK 4.47
author Alpar Juttner <alpar@cs.elte.hu>
date Sun, 06 Nov 2011 20:59:10 +0100
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/deps/glpk/examples/sql/sudoku_mysql.mod	Sun Nov 06 20:59:10 2011 +0100
     1.3 @@ -0,0 +1,113 @@
     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 +/*
    1.47 +table ti IN 'MySQL' 'Database=glpk;UID=glpk;PWD=gnu'
    1.48 +  'sudoku' :
    1.49 +  fields <- [COL, LIN], givens ~ VAL;
    1.50 +*/
    1.51 +table ti IN 'MySQL' 'Database=glpk;UID=glpk;PWD=gnu'
    1.52 +  'SELECT * FROM sudoku WHERE ID = ' & id :
    1.53 +  fields <- [COL, LIN], givens ~ VAL;
    1.54 +
    1.55 +var x{i in 1..9, j in 1..9, k in 1..9}, binary;
    1.56 +/* x[i,j,k] = 1 means cell [i,j] is assigned number k */
    1.57 +
    1.58 +s.t. fa{i in 1..9, j in 1..9, k in 1..9: givens[i,j] != 0}:
    1.59 +     x[i,j,k] = (if givens[i,j] = k then 1 else 0);
    1.60 +/* assign pre-defined numbers using the "givens" */
    1.61 +
    1.62 +s.t. fb{i in 1..9, j in 1..9}: sum{k in 1..9} x[i,j,k] = 1;
    1.63 +/* each cell must be assigned exactly one number */
    1.64 +
    1.65 +s.t. fc{i in 1..9, k in 1..9}: sum{j in 1..9} x[i,j,k] = 1;
    1.66 +/* cells in the same row must be assigned distinct numbers */
    1.67 +
    1.68 +s.t. fd{j in 1..9, k in 1..9}: sum{i in 1..9} x[i,j,k] = 1;
    1.69 +/* cells in the same column must be assigned distinct numbers */
    1.70 +
    1.71 +s.t. fe{I in 1..9 by 3, J in 1..9 by 3, k in 1..9}:
    1.72 +     sum{i in I..I+2, j in J..J+2} x[i,j,k] = 1;
    1.73 +/* cells in the same region must be assigned distinct numbers */
    1.74 +
    1.75 +/* there is no need for an objective function here */
    1.76 +
    1.77 +solve;
    1.78 +
    1.79 +table ta{(i,j) in fields} OUT
    1.80 +  'MySQL' 'Database=glpk;UID=glpk;PWD=gnu'
    1.81 +  'DELETE FROM sudoku_solution'
    1.82 +  'WHERE ID = ' & id & ';'
    1.83 +  'INSERT INTO sudoku_solution'
    1.84 +  '(ID, COL, LIN, VAL)'
    1.85 +  'VALUES(?, ?, ?, ?);' :
    1.86 +  id ~ ID, i ~ COL, j ~ LIN, (sum{k in 1..9} x[i,j,k] * k) ~ VAL;
    1.87 +
    1.88 +printf "\nSudoku to be solved\n";
    1.89 +for {i in 1..9}
    1.90 +{  for {0..0: i = 1 or i = 4 or i = 7}
    1.91 +     printf " +-------+-------+-------+\n";
    1.92 +   for {j in 1..9}
    1.93 +   {  for {0..0: j = 1 or j = 4 or j = 7} printf(" |");
    1.94 +      printf " %d", givens[i,j];
    1.95 +      for {0..0: j = 9} printf(" |\n");
    1.96 +   }
    1.97 +   for {0..0: i = 9}
    1.98 +   printf " +-------+-------+-------+\n";
    1.99 +   }
   1.100 +printf "\nSolution\n";
   1.101 +for {i in 1..9}
   1.102 +{  for {0..0: i = 1 or i = 4 or i = 7}
   1.103 +      printf " +-------+-------+-------+\n";
   1.104 +   for {j in 1..9}
   1.105 +   {  for {0..0: j = 1 or j = 4 or j = 7} printf(" |");
   1.106 +      printf " %d", sum{k in 1..9} x[i,j,k] * k;
   1.107 +      for {0..0: j = 9} printf(" |\n");
   1.108 +   }
   1.109 +   for {0..0: i = 9}
   1.110 +      printf " +-------+-------+-------+\n";
   1.111 +}
   1.112 +
   1.113 +data;
   1.114 +
   1.115 +param id := 1;
   1.116 +end;