File: queens5.zpl

package info (click to toggle)
zimpl 2.07.ds1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,416 kB
  • ctags: 2,560
  • sloc: ansic: 18,311; yacc: 882; lex: 326; makefile: 232; sh: 219
file content (42 lines) | stat: -rw-r--r-- 981 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# $Id: queens5.zpl,v 1.3 2006/09/18 13:30:35 bzfkocht Exp $
#
# This is a formulation of the n queens problem using binary variables.
# variables. Since the number of queens is maximized, the size of the
# board can be set arbitrarily.
#
# set packing formulation
#
param columns := 8;

set I   := { 1 .. columns };
set IxI := I * I;

var x[IxI] binary;

maximize queens: sum <i,j> in IxI : x[i,j];

# one in a row 
subto row: forall <i> in I do
   sum <i,j> in IxI : x[i,j] <= 1;

# one in a column
subto col: forall <j> in I do
   sum <i,j> in IxI : x[i,j] <= 1;

# rowwise down
subto drd: forall <i> in I do
   sum <m,n> in IxI with m - i == n - 1: x[m,n] <= 1;
      
# rowwise up
subto dru: forall <i> in I do
   sum <m,n> in IxI with m - i == 1 - n: x[m,n] <= 1;
      
# colwise down
subto dcd: forall <j> in I do
   sum <m,n> in IxI with m - 1 == n - j: x[m,n] <= 1;

# colwise up
subto dcu: forall <j> in I do
   sum <m,n> in IxI with columns - m == n - j: x[m,n] <= 1;