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