lemon-project-template-glpk

diff deps/glpk/examples/magic.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/magic.mod	Sun Nov 06 20:59:10 2011 +0100
     1.3 @@ -0,0 +1,54 @@
     1.4 +/* MAGIC, Magic Square */
     1.5 +
     1.6 +/* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
     1.7 +
     1.8 +/* In recreational mathematics, a magic square of order n is an
     1.9 +   arrangement of n^2 numbers, usually distinct integers, in a square,
    1.10 +   such that n numbers in all rows, all columns, and both diagonals sum
    1.11 +   to the same constant. A normal magic square contains the integers
    1.12 +   from 1 to n^2.
    1.13 +
    1.14 +   (From Wikipedia, the free encyclopedia.) */
    1.15 +
    1.16 +param n, integer, > 0, default 4;
    1.17 +/* square order */
    1.18 +
    1.19 +set N := 1..n^2;
    1.20 +/* integers to be placed */
    1.21 +
    1.22 +var x{i in 1..n, j in 1..n, k in N}, binary;
    1.23 +/* x[i,j,k] = 1 means that cell (i,j) contains integer k */
    1.24 +
    1.25 +s.t. a{i in 1..n, j in 1..n}: sum{k in N} x[i,j,k] = 1;
    1.26 +/* each cell must be assigned exactly one integer */
    1.27 +
    1.28 +s.t. b{k in N}: sum{i in 1..n, j in 1..n} x[i,j,k] = 1;
    1.29 +/* each integer must be assigned exactly to one cell */
    1.30 +
    1.31 +var s;
    1.32 +/* the magic sum */
    1.33 +
    1.34 +s.t. r{i in 1..n}: sum{j in 1..n, k in N} k * x[i,j,k] = s;
    1.35 +/* the sum in each row must be the magic sum */
    1.36 +
    1.37 +s.t. c{j in 1..n}: sum{i in 1..n, k in N} k * x[i,j,k] = s;
    1.38 +/* the sum in each column must be the magic sum */
    1.39 +
    1.40 +s.t. d: sum{i in 1..n, k in N} k * x[i,i,k] = s;
    1.41 +/* the sum in the diagonal must be the magic sum */
    1.42 +
    1.43 +s.t. e: sum{i in 1..n, k in N} k * x[i,n-i+1,k] = s;
    1.44 +/* the sum in the co-diagonal must be the magic sum */
    1.45 +
    1.46 +solve;
    1.47 +
    1.48 +printf "\n";
    1.49 +printf "Magic sum is %d\n", s;
    1.50 +printf "\n";
    1.51 +for{i in 1..n}
    1.52 +{  printf{j in 1..n} "%3d", sum{k in N} k * x[i,j,k];
    1.53 +   printf "\n";
    1.54 +}
    1.55 +printf "\n";
    1.56 +
    1.57 +end;