lemon-project-template-glpk

annotate deps/glpk/examples/sudoku.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
rev   line source
alpar@9 1 /* SUDOKU, Number Placement Puzzle */
alpar@9 2
alpar@9 3 /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
alpar@9 4
alpar@9 5 /* Sudoku, also known as Number Place, is a logic-based placement
alpar@9 6 puzzle. The aim of the canonical puzzle is to enter a numerical
alpar@9 7 digit from 1 through 9 in each cell of a 9x9 grid made up of 3x3
alpar@9 8 subgrids (called "regions"), starting with various digits given in
alpar@9 9 some cells (the "givens"). Each row, column, and region must contain
alpar@9 10 only one instance of each numeral.
alpar@9 11
alpar@9 12 Example:
alpar@9 13
alpar@9 14 +-------+-------+-------+
alpar@9 15 | 5 3 . | . 7 . | . . . |
alpar@9 16 | 6 . . | 1 9 5 | . . . |
alpar@9 17 | . 9 8 | . . . | . 6 . |
alpar@9 18 +-------+-------+-------+
alpar@9 19 | 8 . . | . 6 . | . . 3 |
alpar@9 20 | 4 . . | 8 . 3 | . . 1 |
alpar@9 21 | 7 . . | . 2 . | . . 6 |
alpar@9 22 +-------+-------+-------+
alpar@9 23 | . 6 . | . . . | 2 8 . |
alpar@9 24 | . . . | 4 1 9 | . . 5 |
alpar@9 25 | . . . | . 8 . | . 7 9 |
alpar@9 26 +-------+-------+-------+
alpar@9 27
alpar@9 28 (From Wikipedia, the free encyclopedia.) */
alpar@9 29
alpar@9 30 param givens{1..9, 1..9}, integer, >= 0, <= 9, default 0;
alpar@9 31 /* the "givens" */
alpar@9 32
alpar@9 33 var x{i in 1..9, j in 1..9, k in 1..9}, binary;
alpar@9 34 /* x[i,j,k] = 1 means cell [i,j] is assigned number k */
alpar@9 35
alpar@9 36 s.t. fa{i in 1..9, j in 1..9, k in 1..9: givens[i,j] != 0}:
alpar@9 37 x[i,j,k] = (if givens[i,j] = k then 1 else 0);
alpar@9 38 /* assign pre-defined numbers using the "givens" */
alpar@9 39
alpar@9 40 s.t. fb{i in 1..9, j in 1..9}: sum{k in 1..9} x[i,j,k] = 1;
alpar@9 41 /* each cell must be assigned exactly one number */
alpar@9 42
alpar@9 43 s.t. fc{i in 1..9, k in 1..9}: sum{j in 1..9} x[i,j,k] = 1;
alpar@9 44 /* cells in the same row must be assigned distinct numbers */
alpar@9 45
alpar@9 46 s.t. fd{j in 1..9, k in 1..9}: sum{i in 1..9} x[i,j,k] = 1;
alpar@9 47 /* cells in the same column must be assigned distinct numbers */
alpar@9 48
alpar@9 49 s.t. fe{I in 1..9 by 3, J in 1..9 by 3, k in 1..9}:
alpar@9 50 sum{i in I..I+2, j in J..J+2} x[i,j,k] = 1;
alpar@9 51 /* cells in the same region must be assigned distinct numbers */
alpar@9 52
alpar@9 53 /* there is no need for an objective function here */
alpar@9 54
alpar@9 55 solve;
alpar@9 56
alpar@9 57 for {i in 1..9}
alpar@9 58 { for {0..0: i = 1 or i = 4 or i = 7}
alpar@9 59 printf " +-------+-------+-------+\n";
alpar@9 60 for {j in 1..9}
alpar@9 61 { for {0..0: j = 1 or j = 4 or j = 7} printf(" |");
alpar@9 62 printf " %d", sum{k in 1..9} x[i,j,k] * k;
alpar@9 63 for {0..0: j = 9} printf(" |\n");
alpar@9 64 }
alpar@9 65 for {0..0: i = 9}
alpar@9 66 printf " +-------+-------+-------+\n";
alpar@9 67 }
alpar@9 68
alpar@9 69 data;
alpar@9 70
alpar@9 71 /* These data correspond to the example above. */
alpar@9 72
alpar@9 73 param givens : 1 2 3 4 5 6 7 8 9 :=
alpar@9 74 1 5 3 . . 7 . . . .
alpar@9 75 2 6 . . 1 9 5 . . .
alpar@9 76 3 . 9 8 . . . . 6 .
alpar@9 77 4 8 . . . 6 . . . 3
alpar@9 78 5 4 . . 8 . 3 . . 1
alpar@9 79 6 7 . . . 2 . . . 6
alpar@9 80 7 . 6 . . . . 2 8 .
alpar@9 81 8 . . . 4 1 9 . . 5
alpar@9 82 9 . . . . 8 . . 7 9 ;
alpar@9 83
alpar@9 84 end;