lemon-project-template-glpk

annotate deps/glpk/examples/queens.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 /* QUEENS, a classic combinatorial optimization problem */
alpar@9 2
alpar@9 3 /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
alpar@9 4
alpar@9 5 /* The Queens Problem is to place as many queens as possible on the 8x8
alpar@9 6 (or more generally, nxn) chess board in a way that they do not fight
alpar@9 7 each other. This problem is probably as old as the chess game itself,
alpar@9 8 and thus its origin is not known, but it is known that Gauss studied
alpar@9 9 this problem. */
alpar@9 10
alpar@9 11 param n, integer, > 0, default 8;
alpar@9 12 /* size of the chess board */
alpar@9 13
alpar@9 14 var x{1..n, 1..n}, binary;
alpar@9 15 /* x[i,j] = 1 means that a queen is placed in square [i,j] */
alpar@9 16
alpar@9 17 s.t. a{i in 1..n}: sum{j in 1..n} x[i,j] <= 1;
alpar@9 18 /* at most one queen can be placed in each row */
alpar@9 19
alpar@9 20 s.t. b{j in 1..n}: sum{i in 1..n} x[i,j] <= 1;
alpar@9 21 /* at most one queen can be placed in each column */
alpar@9 22
alpar@9 23 s.t. c{k in 2-n..n-2}: sum{i in 1..n, j in 1..n: i-j == k} x[i,j] <= 1;
alpar@9 24 /* at most one queen can be placed in each "\"-diagonal */
alpar@9 25
alpar@9 26 s.t. d{k in 3..n+n-1}: sum{i in 1..n, j in 1..n: i+j == k} x[i,j] <= 1;
alpar@9 27 /* at most one queen can be placed in each "/"-diagonal */
alpar@9 28
alpar@9 29 maximize obj: sum{i in 1..n, j in 1..n} x[i,j];
alpar@9 30 /* objective is to place as many queens as possible */
alpar@9 31
alpar@9 32 /* solve the problem */
alpar@9 33 solve;
alpar@9 34
alpar@9 35 /* and print its optimal solution */
alpar@9 36 for {i in 1..n}
alpar@9 37 { for {j in 1..n} printf " %s", if x[i,j] then "Q" else ".";
alpar@9 38 printf("\n");
alpar@9 39 }
alpar@9 40
alpar@9 41 end;