Thorsten Ko
h
for Version 3.3.0
July 29, 2012
Contents
1 Prefa
e
2 Introdu tion
3 Invo ation
4 Format
4.1 Expressions . . . . . . . . . . . . . . . . . .
4.2 Tuples and sets . . . . . . . . . . . . . . . .
4.3 Parameters . . . . . . . . . . . . . . . . . .
4.4 Initializing sets and parameters from a le
4.5 sum -expressions . . . . . . . . . . . . . . .
4.6 forall -statements . . . . . . . . . . . . . . .
4.7 Fun
tion denitions . . . . . . . . . . . . .
4.8 The do print and do
he
k
ommands . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5
6
8
10
12
13
14
14
14
5 Models
15
5.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.2 Obje
tive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.3 Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
6 Modeling examples
6.1 The diet problem . . . . . . . . . . . . . .
6.2 The traveling salesman problem . . . . . .
6.3 The
apa
itated fa
ility lo
ation problem .
6.4 The n -queens problem . . . . . . . . . . .
7 Error messages
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
20
20
21
22
24
28
Zimpl
Abstra
t
pressed in
by a
1 Prefa
e
May the sour
e be with you, Luke!
Many of the things in Zimpl (and a lot more)
an be found in the ex
ellent
book about the modeling language ampl from Robert Fourer, David N. Gay
and Brian W. Kernighan [FGK03. Those interested in an overview of the
urrent state-of-the-art in (
ommer
ial) modeling languages might have a look
at [Kal04b. On the other hand, having the sour
e
ode of a program has its
advantages. The possibility to run it regardless of ar
hite
ture and operating
system, the ability to modify it to suite the needs, and not having to hassle with
li
ense managers may make a mu
h less powerful program the better
hoi
e. And
so Zimpl
ame into being.
By now Zimpl has grown up and matured. It has been used in several industry proje
ts and university le
tures, showing that it is able to
ope with large
s
ale models and also with students. This would have not been possible without my early adopters Armin Fgens
huh, Mar
Pfets
h, Sas
ha Luka
, Daniel
Junglas, Jrg Rambau and Tobias A
hterberg. Thanks for their
omments and
bug reports. Spe
ial thanks to Tuomo Takkula for revising this manual.
Zimpl is li
ensed under the GNU Lesser General Publi
Li
ense Version
3. For more information on free software see http://www.gnu.org. The latest
version of Zimpl
an be found at http://zimpl.zib.de. If you nd any bugs,
then please send an email to mailto:ko
hzib.de, and do not forget to in
lude
an example that shows the problem. If somebody extends Zimpl, then I am
interested in getting pat
hes in order to in
lude them into the main distribution.
The best way to refer to Zimpl in a publi
ation is to
ite my PhD thesis [Ko
04
PHDTHESIS{Ko
h2004,
author
= "Thorsten Ko
h",
title
= "Rapid Mathemati
al Programming",
s
hool
= "Te
hnis
he {Universit\"at} Berlin",
year
= "2004",
url
= "http://www.zib.de/Publi
ations/abstra
ts/ZR-04-58/",
note
= "ZIB-Report 04-58"
Zimpl
2 Introdu
tion
Consider the
graph
(V, A)
min
for all
(i, j) A:
cij xij
(i,j)A
(iv) (v)
xvi
for all
(vi)+ (v)
xiv =
v V \ {s, t}
(1)
all i,j in A
instantiation is
for
v V.
s
17
a
47
min
subje
t to
19
53
b
23
t
ibm
mps
While
shortestpath . lp
ROWS
N
Obj
COLUMNS
INTSTART
'MARKER'
x0
Obj
x0
x1
Obj
x1
1
47
1
x2
x2
Obj
x3
Obj
53
x4
Obj
23
RHS
UP Bound
x0
UP Bound
x1
UP Bound
x2
UP Bound
x3
UP Bound
x4
19
RHS
BOUNDS
ENDATA
Zimpl
Another possibility is the
Minimize
Obj: +17 x0 +47
Subje
t to
0: -1 x3 -1 x2
1: -1 x4 +1 x2
2: +1 x1 +1 x0
Bounds
0 <= x0 <= 1
0 <= x1 <= 1
0 <= x2 <= 1
0 <= x3 <= 1
0 <= x4 <= 1
Generals
x0 x1 x2 x3 x4
End
A must
Abstra
t formulation
Now, with
set V
:={"a","b","s","t"};
set A
:={<"s","a">, <"s","b">, <"a","b">, <"a","t">, <"b","t">};
param
[A := <"s","a"> 17, <"s","b"> 47, <"a","b"> 19, <"a","t"> 53,
<"b","t"> 23;
defset dminus(v) := {<i,v> in A};
defset dplus(v) := {<v,j> in A};
var x[A binary;
minimize
ost: sum<i,j> in A:
[i,j * x[i,j;
subto f
:
forall <v> in V - {"s","t"}:
sum<i,v> in dminus(v): x[i,v == sum<v,i> in dplus(v): x[v,i;
subto uf:
sum<s,i> in dplus("s"): x[s,i == 1;
ompare this with (1). Feeding the s
ript into
or
lp les.
Zimpl lies in
on the mathemati
al model in lieu of dealing merely with
oe
ients. Furthermore,
more often than not instantiations (read
some external data. In some sense, these are the result of the model applied to this
external data. In our example above, the external data is the graph together with the
ost
oe
ients and the spe
i
ation of
from les.
and
t,
the shortest
st-path
problem. Of ourse,
1 The lp format has also some idiosyn
rati
restri
tions. For example variables should not
be named e12 or the like. and it is not possible to spe
ify ranged
onstraints.
Zimpl
set
V:= {read "nodes.txt" as "<1s>"};
set
A:= {read "ar
s.txt" as "<1s,2s>"};
param
[A := read "ar
s.txt" as "<1s,2s>3n";
and the
Zimpl
dened by the les nodes.txt and ar
s.txt. The format of those les will be des
ribed
in Subse
tion 4.4.
3 Invo
ation
In order to run
zimpl ex1.zpl
In general terms the
ommand is:
error message and aborts. In
ase everything goes well, the results are written into two
or more les, depending on the spe
ied options.
The rst output le is the problem generated from the model in either
mps,
plexlp,
and
onstraint names used in the model and their
orresponding names in the problem
le. The reason for this name translation is the limitation of the length of names in
the
Also the
but will for some
ommands only show the rst 20
hara
ters in the output.
A
omplete list of all options understood by
typi
al invo
ation of
Zimpl an
be found in Table 1. A
mps-output
mization problem, the obje tive fun tion will be inverted, be ause the
no provision for stating the sense of the obje
tive fun
tion. The default is to assume
maximization.
4 Format
Ea
h
#,
provided it is
not part of a string, to the end of the line is treated as a
omment and is ignored. If
a line starts with the word
then this le is read and pro essed instead of the line.
Zimpl
-t format
-o name
-F lter
-l length
-n
form
-P lter
-s seed
-v 0..5
-D name=val
-b
-f
-h
-m
-O
-r
-V
Sele
ts the output format. Can be either lp, whi
h is default, or mps,
or pip, whi
h is polynomial IP, or hum, whi
h is only human readable,
or rlp, whi
h is the same as lp but with rows and
olumns randomly
permuted. The permutation is depending on the seed. Also possible is
pip m whi
h means Polynomial IP.
Sets the base-name for the output les.
Defaults to the name of the rst input le with its path and extension
stripped o.
The output is piped through a lter. A %s in the string is repla
ed by the
output lename. For example -F "gzip -
>%s.gz" would
ompress
all the output les.
Sets maximal length of variables and
onstraints for lp format les to
length.
Sele
t the format for the generation of
onstraint names. Can be
m
whi
h will number them 1 . . . n with a `
' in front.
n will use the name
supplied in the subto statement and number them 1 . . . n within the
statement.
f will use the name given with the subto, then a 1 . . . n
number like in
m and then append all the lo
al variables from the forall
statements.
The input is piped through a lter. A %s in the string is repla
ed by
the input lename. For example -P "
pp -DWITH_C1 %s" would pass
the input le through the C-prepro
essor.
Positive seed number for the random number generator. For example
-s `date +%N` will produ
e
hanging random numbers.
Set the verbosity level. 0 is quiet, 1 is default, 2 is verbose, 3 and 4 are
hatter, and 5 is debug.
Sets the parameter name to the spe
ied value. This is equivalent with
having this line in the Zimpl program: param name:=val. If there is
a de
laration in the Zimpl le and a -D setting for the same name, the
latter takes pre
edent.
Enables bison debug output.
Enables ex debug output.
Prints a help message.
Writes a
plexmst (Mip STart) le.
Tries to redu
e the generated LP by doing some presolve analysis.
Writes a
plexord bran
hing order le.
Prints the version number.
4.1 Expressions
Zimpl works on its lowest level with two types of data: Strings and numbers.
Wherever
Numeri
expressions
A number in
Zimpl
as 2, -6.5 or 5.234e-12.
Numeri expressions onsist of numbers, numeri valued parameters, and any of the
Zimpl
operators and fun
tions listed in Table 2. Additionally the fun
tions shown in Table 3
an be used. Note that those fun
tions are only
omputed with normal double pre
ision
oating-point arithmeti
and therefore have limited a
ura
y. Examples on how to use
the
a b, a**b
a+b
a-b
a*b
a/b
a mod b
abs(a)
sgn(a)
floor(a)
eil(a)
round(a)
a!
min(S)
min(s in S) e(s)
max(S)
max(s in S) e(s)
min(a,b,
,...,n)
max(a,b,
,...,n)
sum(s in S) e(s)
prod(s in S) e(s)
ard(S)
random(m,n)
ord(A,n,
)
to the power of b
addition
subtra
tion
multipli
ation
division
modulo
absolute value
sign
round down
round up
round towards zero
fa
torial
minimum of a set
minimum over a set
maximum of a set
maximum over a set
minimum of a list
maximum of a list
sum over a set
produ
t over a set
ardinality of a set
pseudo random number
ordinal
length(s)
if a then b
else
end
length of a string
onditional
ab , b must be integer
a+b
ab
ab
a/b
a mod b
|a|
x > 0 1, x < 0 1, else 0
a
a
a
a!, a must be nonnegative integer
minsS
minsS
e(s)
maxsS
maxsS
e(s)
min(a, b, c, . . . , n)
max(a, b, c, . . . , n)
P
e(s)
QsS
sS e(s)
|S|
[m, n], rational
square root
logarithm to base 10
natural logarithm
exponential fun
tion
a
log10 a
ln a
a
String expressions
", e. g."Hallo Keiken". Two strings
+ operator, i. e."Hallo " + "Keiken" gives "Hallo
Keiken". The fun
tion substr(string, begin, length)
an be used to extra
t parts
of a string. begin is the rst
hara
ter to be used, and
ounting starts with zero at rst
hara
ter. If begin is negative, then
ounting starts at the end of the string. length
A string is delimited by double quotation marks
Zimpl
is the number of
ha
aters to extra
t starting at
determined using the
begin.
Boolean expressions
These evaluate either to
erators
with
true
and, or,
set-expression
and
xor2
or to
and
>
false.
not
tuple in
then
or
else
part of an
if
expression.
Variant expressions
The following is either a numeri
, string or boolean expression, depending on whether
evaluates to a spe i element of a set (details about sets are overed below).
The sets in
Zimpl
and
},
n-th
tuples of a set must be the same, i. e. they have to be either all numbers or all strings.
The denition of a tuple is en
losed in angle bra
kets
<
and
>,
e. g.<1,2,"x">. The
:=
{1,2,<3> }
is not.
set,
the
template
are repla
ed by the values of the
omponents of the respe
tive tuple. For example, a
set
<a,b> in S.
If any
of the pla
eholders are a
tual values, only those tuples mat
hing these values will be
extra
ted. For example,
is
1.
<1,b> in S
Please note that if one of the pla eholders is the name of an already dened
parameter, set or variable, it will be substituted. This will result either in an error or
an a
tual value.
Examples
set A := { 1, 2, 3 };
set B := { "hi", "ha", "ho" };
set C := { <1,2,"x">, <6,5,"y">, <787,12.6,"oh"> };
For set expressions the fun
tions and operators given in Table 4 are dened.
An example for the use of the if boolean-expression then set-expression else setexpression end an be found on page 10 together with the examples for indexed sets.
2a
xor b := a b a b
Zimpl
Examples
set D := A
ross B;
set E := { 6 to 9 } union A without { 2, 3 };
set F := { 1 to 9 } * { 10 to 19 } * { "A", "B" };
set G := proj(F, <3,1>);
# will give: { <"A",1>, <"A",2"> ... <"B",9> }
A*B,
A
ross B
ross produ t
{(x, y) | x A y B}
A+B,
A union B
union
{x | x A x B}
union <i>
in I: S
A inter B
interse tion
inter <i>
in I: S
{x | x A x B}
A\B, A-B,
A without B
dieren e
{x | x A x 6 B}
symmetri
dieren
e
generate,
(default s = 1)
generate
proje
tion
{x | (x A x 6 B) (x B x 6 A)}
{x | x = min(n, m) + i|s| 6 max(n, m),
i N0 , x, n, m, s Z}
{x | x = n + is 6 m, i N0 , x, n, m, s Z}
The new set will
onsist of n-tuples, with
the i-th
omponent being the ei -th
omponent of A.
A symdiff B
{n .. m by s },
{n to m by s }
proj(A, t)
t = (e1 , . . . , en )
argmin <i>
in I : e(i)
minimum argument
argmin(n) <i>
in I : e(i)
n minimum
arguments
argmax <i>
in I : e(i)
maximum argument
argmax(n) <i>
in I : e(i)
n maximum
arguments
if a then b
else
end
onditional
iI Si
iI Si
argminiI e(i)
Conditional sets
It is possible to restri
t a set to tuples that satisfy a Boolean expression. The expression
given by the
with lause is evaluated for ea h tuple in the set and only tuples for whi h
true
Zimpl
Examples
set F := { <i,j> in Q with i > j and i < 5 };
set A := { "a", "b", "
" };
set B := { 1, 2, 3 };
set V := { <a,2> in A*B with a == "a" or a == "b" };
# will give: { <"a",2>, <"b",2> }
set W := argmin(3) <i,j> in B*B : i+j;
# will give: { <1,1>, <1,2>, <2,1> }
Indexed sets
It is possible to index one set with another set resulting in a set of sets. Indexed sets
are a
essed by adding the index of the set in bra
kets
Table 5 lists
the available fun tions. There are three possibilities how to assign to an indexed set:
<i> in I,
the assignment is
Examples
set
set
set
set
set
set
set
set
set
set
I
:= { 1..3 };
A[I
:= <1> {"a","b"}, <2> {"
","e"}, <3> {"f"};
B[<i> in I := { 3 * i };
P[
:= powerset(I);
J
:= indexset(P);
S[
:= subsets(I, 2);
T[
:= subsets(I, 1, 2);
K[<i> in I := if i mod 2 == 0 then { i } else { -i } end;
U
:= union <i> in I : A[i;
IN
:= inter <j> in J : P[j; # empty!
{X | X A}
{X | X A |X| = n}
{X | X A n 6 |X| 6 m}
{1 . . . |A|}
4.3 Parameters
Parameters are the way to dene
onstants within
Zimpl.
or without an index set. Without index a parameter is just a single value whi
h is either
a number or a string. For indexed parameters there is one value for ea
h member of
the set. It is possible to de
lare a
default
value.
10
Zimpl
Parameters are de
lared in the following way: The keyword
param
is followed by
the name of the parameter optionally followed by the index set in square bra
kets.
Then, after the assignment operator there is a list of pairs. The rst element of ea
h
pair is a tuple from the index set, while the se
ond element is the value of the parameter
for this index.
Examples
set A
set C
param
param
param
param
param
param
param
:= { 12 .. 30 };
:= { <1,2,"x">, <6,5,"y">, <3,7,"z"> };
q := 5;
u[A := <13> 17, <17> 29, <23> 14 default 99;
amin := min A;
# = 12
umin := min <a> in A : u[a; # = 14
mmax := max <i> in { 1 .. 10 } : i mod 5;
w[C := <1,2,"x"> 1/2, <6,5,"y"> 2/3;
x[<i> in { 1 .. 8 } with i mod 2 == 0 := 3 * i;
<3,7,"z">
of parameter
w.
Parameter tables
It is possible to initialize multi-dimensional indexed parameters from tables. This is
espe
ially useful for two-dimensional parameters. The data is put in a table stru
ture
with
and one index for ea h row of the table is needed. The olumn index has to be onedimensional, but the row index an be multi-dimensional.
the entry is built by appending the
olumn index to the row index. The entries are
separated by
ommas.
As an be seen in the
third example below, it is possible to add a list of entries after the table.
Examples
set I := { 1 .. 10 };
set J := { "a", "b", "
", "x", "y", "z" };
param h[I*J :=
param g[I*I*I :=
| 1, 2, 3 |
|1,3| 0, 0, 1 |
|2,1| 1, 0, 1 |;
param k[I*I := | 7, 8, 9 |
|4| 89, 67, 55 |
|5| 12, 13, 14 |, <1,2> 17, <3,4> 99;
The last example is equivalent to:
param k[I*I := <4,7> 89, <4,8> 67, <4,9> 55, <5,7> 12,
<5,8> 13, <5,9> 14, <1,2> 17, <3,4> 99;
11
Zimpl
lename
template
tuples to generate. Ea
h input line from the le is split into elds. The splitting is done
a
ording to the following rules: Whenever a spa
e, tab,
omma, semi
olon or
olon is
en
ountered a new eld is started. Text that is en
losed in double quotes is not split
and the quotes are always removed. When a eld is split all spa
e and tab
hara
ters
around the splitting point are removed. If the split is due to a
omma, semi
olon or
olon, ea
h o
urren
e of these
hara
ters starts a new eld.
Examples
All these lines have three elds:
Hallo;12;3
Moin 7 2
"Hallo, Peter"; "Ni
e to meet you" 77
,,2
For ea
h
omponent of the tuple, the number of the eld to use for the value is given,
followed by either
for a string.
After the template some optional modiers an be given. The order does not matter.
mat h s
omment s
in the le. Ea
h line is ended when any of the
omment
hara
ters is found.
instru
ts to skip the rst
n.
use n
skip n
When a le is read, empty lines, omment lines, and unmat hed lines are skipped
use
and
skip lauses.
Examples
set P := { read "nodes.txt" as "<1s>" };
nodes.txt:
<"Hamburg">
Hamburg
Mn
hen
<"Mn
hen">
Berlin
<"Berlin">
set Q := { read "blabla.txt" as "<1s,5n,2n>" skip 1 use 2 };
blabla.txt:
Name;Nr;X;Y;No
skip
<"Hamburg",7,12>
Hamburg;12;x;y;7
Bremen;4;x;y;5
<"Bremen",5,4>
Berlin;2;x;y;8
skip
param
ost[P := read "
ost.txt" as "<1s> 2n"
omment "#";
ost.txt:
skip
# Name Pri
e
<"Hamburg"> 1000
Hamburg 1000
Mn
hen 1200
<"Mn
hen"> 1200
12
Zimpl
Berlin 1400
<"Berlin">
1400
As with table format input, it is possible to add a list of tuples or parameter entries
after a read statement.
Examples
set A := { read "test.txt" as "<2n>", <5>, <6> };
param winniepoh[X :=
read "values.txt" as "<1n,2n> 3n", <1,2> 17, <3,4> 29;
It is also possible to read a single value into a parameter. In this
ase, either the
le should
ontain only a single line, or the read statement should be instru
ted by
means of a
use 1
Examples
# Read the fourth value in the fifth line
param n := read "huhu.dat" as "4n" skip 4 use 1
If all values in a le should be read into a set, this is possible by use of the
template for string values, and
"<n+>"
"<s+>"
Examples
# Read all values into a set
set X := { read "stream.txt" as "<n+>" };
\smallskip
stream.txt:
1 2 3 7 9 5 6 23
63 37 88
4
87 27
# X := { 1, 2, 3, 7, 9, 5, 6, 23, 63, 37, 88, 4, 87, 27 };
13
index
is:
Zimpl
It is allowed to write a
olon
instead of
with
part of an
index
tuple
do
is optional. The
set
instead of
set
with.
must mat h.
sum -expressions.
index
equals that
Zimpl.
only be numbers or strings, but within the fun
tion denition it is possible to a
ess
all otherwise de
lared sets, parameters and variables.
The denition of a fun
tion has to start with
defset, depending
or
on the return value. Next is the name of the fun tion and a list of
:=
Examples
defnumb
defstrg
defbool
defset
dist(a,b)
huehott(a)
wirkli
h(a,b)
bigger(i)
:=
:=
:=
:=
sqrt(a*a + b*b);
if a < 0 then "hue" else "hott" end;
a < b and a >= 10 or b < 5;
{ <j> in K with j > i };
will print to the standard output stream whatever numeri
al, string, Boolean or set
expression, or tuple follows it. This
an be used for example to
he
k if a set has the
expe
ted members, or if some
omputation has the anti
ipated result.
pre
edes a Boolean expression. If this expression does not evaluate to
he k
always
is aborted with an appropriate error message. This
an be used to assert that spe
i
onditions are met.
It is possible to use a
forall
lause before a
or
he k
statement. For string and numeri
values it is possible to give a
omma separated list
to the print statement.
Examples
set I := { 1..10 };
do print I;
do print "Cardinality of I:",
ard(I);
do forall <i> in I with i > 5 do print sqrt(i);
do forall <p> in P do
he
k sum <p,i> in PI : 1 >= 1;
14
Zimpl
5 Models
In this se
tion we use the ma
hinery developed up to now in order to formulate mathemati
al programs. Apart from the usual syntax to de
lare variables, obje
tive fun
tions
and
onstraints there is spe
ial syntax available that permits the easy formulation of
spe
ial
onstraints, su
h as spe
ial ordered sets,
onditional
onstraints, and extended
fun
tions.
5.1 Variables
Like parameters, variables
an be indexed.
possible types: Continuous (
alled
Variables may have lower and upper bounds. Defaults are zero as lower and innity
as upper bound.
It is
possible to
ompute the value of the lower or upper bounds depending on the index
of the variable (see the last de
laration in the example).
impli it, i. e.
the variable
an be assumed to have an integral value in any optimal solution to the
integer program, even if it is de
lared
ontinuous. Use of
impli it
together with a solver that take advantage of this information. As of this writing only
the variable is treated as a ontinous variable. It is possible to spe ify initial values for
Examples
var
var
var
var
var
x1;
x2 binary;
x3 integer >= -infinity
# free variable
y[A real >= 2 <= 18;
z[<a,b> in C integer
>= a * 10 <= if b <= 3 then p[b else infinity end;
var w impli
it binary;
var t[k in K integer >= 1 <= 3 * k startval 2 * k priority 50;
minimize or maximize.
ObjOffset set
Zimpl
put into the obje tive fun tion with the appropriate oe ient.
Example
minimize
ost: 12 * x1 -4.4 * x2 + 5
+ sum <a> in A : u[a * y[a
+ sum <a,b,
> in C with a in E and b > 3 : -a/2 * z[a,b,
;
3 The reason for this is that there is no portable way to put an oset into the obje
tive
fun
tion in neither lp nor mps-format.
15
Zimpl
maximize profit: sum <i> in I :
[i * x[i;
5.3 Constraints
The general format for a
onstraint is:
ranged
forall
Examples
subto
subto
subto
subto
subto
subto
time: 3 * x1 + 4 * x2 <= 7;
spa
e: 50 >= sum <a> in A: 2 * u[a * y[a >= 5;
weird: forall <a> in A: sum <a,b,
> in C: z[a,b,
==55;
21: 6*(sum <i> in A: x[i + sum <j> in B : y[j) >= 2;
40: x[1 == a[1 + 2 * sum <i> in A do 2*a[i*x[i*3+4;
frown: forall <i,j> in X
ross { 1 to 5 } without { <2,3> }
with i > 5 and j < 2 do
sum <i,j,k> in X
ross { 1 to 3 }
ross Z do
p[i * q[j * w[j,k >= if i == 2 then 17 else 53 end;
subto nonlin: 3 * x * y * z + 6 <= x^3 + z * y^2 + 3;
Note that in the example
in all invo
ations of
sum.
and
if in terms
Part of a
onstraint
an be put into an
if-then-else-end.
else part is mandatory and both parts need to in lude some variables in the term.
Examples
subto
2: sum <i> in I :
if (i mod 2 == 0) then 3 * x[i else -2 * y[i end <= 3;
if in
onstraints
It is possible to put two variants of a
onstraint into an
statement inside the result part of an
if
if-statement.
else part is
Examples
subto
1: forall <i> in I do
if (i mod 2 == 0) then 3 * x[i >= 4
else -2 * y[i <= 3 end;
16
forall
optional.
Zimpl
Combining
onstraints with and
It is possible to group
onstraints by
on
atenating them with
and.
always be pro essed together. This is parti ular useful in ombination with
if.
forall and
Examples
subto
1: forall <i> in I:
if (i > 3)
then if (i == 4)
then z[1 + z[2 == i
else z[2 + z[3 == i
end and
z[3 + z[4 == i and
z[4 + z[5 == i
else
if (i == 2)
then z[1 + z[2 == i + 10
else z[2 + z[3 == i + 10
end and
z[3 + z[4 == i + 10 and
z[4 + z[5 == i + 10
end;
Constraint attributes
It is possible to spe
ify spe
ial attributes for a
onstraint regarding how it should be
handled later on.
s
ale Before the
onstraint is written to a le, it is s
aled by 1/ max |c| with c being
the
oe
ients of the
onstraint.
separate Do not in lude the onstraint in the initial LP, but separate it later on. The
lp le it
s
ip separate will be set to true.
in
he konly Do not in lude the onstraint in the initial LP, but separate only to he k
lp le
s
ip
he
k will be set to true.
LAZY CUTS
indi
ator If vif is part of the
onstraint then it is modelled as an indi
ator
onstraint
and not by a big-M formulation.
The attributes are given after the
onstraint, before the semi-
olon and are separate
by
omma.
Examples
subto
1: 1000 * x + 0.3 * y <= 5, s
ale;
subto
2: x + y + z == 7, separate,
he
konly;
subto
3: vif x == 1 then y == 7 end, indi
ator;
17
Zimpl
Spe
ial ordered sets
Zimpl
an be used to spe
ify spe
ial ordered sets (sos) for an integer program. If a
sos a sos le will be written together with the lp or mps le. The
as shown above.
Examples
sos s1: type1: 100 * x[1 + 200 * x[2 + 400 * x[3;
sos s2: type2 priority 100 : sum <i> in I: a[i * x[i;
sos s3: forall <i> in I with i > 2:
type1: (100 + i) * x[i + i * x[i-1;
Extended onstraints
Zimpl
else part
is optional):
where
boolean- onstraint
variables have to be bounded integer or binary variables. It is not possible to use any
ontinuous variables or integer variables with innite bounds in a
All
omparison operators (<,
several terms with
are allowed.
boolean- onstraint.
Also ombination of
and, or, and xor and negation with not is possible. The
onditional
then or else) may in
lude bounded
ontinuous
Be aware that using this onstru t will lead to the generation of several
Examples
var x[I integer >= 0 <= 20;
subto
1: vif 3 * x[1 + x[2 != 7
then sum <i> in I : y[i <= 17
else sum <k> in K : z[k >= 5 end;
subto
2: vif x[1 == 1 and x[2 > 5 then x[3 == 7 end;
subto
3: forall <i> in I with i < max(I) :
vif x[i >= 2 then x[i + 1 <= 4 end;
vabs(t) that
is implemented,
but fun
tions like the minimum or the maximum of two terms, or the sign of a term
an be implemented in a similar manner. Again, using this
onstru
t will lead to the
generation of several additional
onstraints and variables.
18
Zimpl
Examples
var x[I integer >= -5 <= 5;
subto
1: vabs(sum <i> in I : x[i) <= 15;
subto
2: vif vabs(x[1 + x[2) > 2 then x[3 == 2 end;
19
Zimpl
6 Modeling examples
In this se
tion we show some examples of well-known problems translated into
Zimpl
format.
diet
problem, see for example [Dan90 about its impli
ations in pra
ti
e.
Given a set of foods
of nutrient
f.
in food
Now
N,
we have a table
fn
of the amount
denotes for ea h food the maximum number of servings a eptable. Given pri es
cf
for ea
h food, we have to nd a sele
tion of foods that obeys the restri
tions and has
minimal
ost. An integer variable
of servings of food
f.
xf
is introdu ed for ea h
f F indi ating
the number
obtained, i. e. half an egg is not an option. The problem may be stated as:
min
cf xf
subje t to
fF
fn xf > n
for all
nN
0 6 xf 6 f
for all
xf N0
for all
fF
fF
Food
Zimpl as follows:
:=
" Milk" ,
" Pork"
set
Nutrients
:=
set
Attr
:=
Nutrients + {
param
needed [ N u t r i e n t s
<" E n e r g y ">
param
Attr
" Servings" ,
};
};
};
:=
2 0 0 0 , <" P r o t e i n ">
d a t a [ Food
fF
5 5 , <" C a l i u m ">
800;
:=
| " S e r v i n g s " , " Energy " , " P r o t e i n " , " Cal
ium " , " P r i
e " |
| " Oatmeal " |
110
205
32
12
24
160
13
54
13
| " Milk"
160
284
420
22
20
| " Pork"
260
14
80
19
|;
#
var
x [< f >
minimize
subto
in
Food
ost :
need :
(g)
i n t e g e r >= 0 <=
data [ f ,
forall
in
( k al )
Food
<n>
:
in
Food
in
Nutrients
data [ f ,
data [ f ,
( mg )
( ents )
" Servings" ;
x[ f ;
do
x [ f >= n e e d e d [ n ;
The
heapest meal satisfying all requirements
osts 97
ents and
onsists of four servings
of oatmeal, ve servings of milk and two servings of pie.
20
Zimpl
G = (V, E)
the set of links between the
ities. Introdu
ing binary variables
indi
ating if edge
(i, j)
min
xij
dij xij
for ea h
E being
(i, j) E
subje t to
(i,j)E
xij = 2
for all
vV
xij 6 |U| 1
for all
U V, =
6 U 6= V
xij {0, 1}
for all
(i, j) E
(i,j)v
(i,j)E(U)
The data is read in from a le that gives the number of the
ity and the x and y
oordinates. Distan
es between
ities are assumed Eu
lidean. For example:
# City
Berlin
Frankfurt
Leipzig
Heidelberg
Karlsruhe
Hamburg
Bayreuth
Trier
Hannover
The formulation in
the
ities.
Stuttgart
Passau
Augsburg
Koblenz
Dortmund
Bo
hum
Duisburg
Wuppertal
Essen
Jena
X
Y
5251 1340
5011 864
5133 1237
4941 867
4901 840
5356 998
4993 1159
4974 668
5237 972
Zimpl
follows below.
4874 909
4856 1344
4833 1089
5033 759
5148 741
5145 728
5142 679
5124 715
5145 701
5093 1158
P[
on orde
website .
set
:=
set
:=
{ <i , j >
read
set
P[
:=
p o w e r s e t (V ) ;
set
:=
i n d e x s e t (P ) ;
as
"<1s>"
with
<
omment
j
"#"
};
};
param
px [ V
:=
read
as
"<1s> 2 n "
omment
"#" ;
param
py [ V
:=
read
as
"<1s> 3 n "
omment
"#" ;
:=
s q r t ( ( px [ a px [ b ) ^ 2
defnumb
var
dist (a , b)
x [E
minimize
subto
binary ;
ost :
subto
in
in
forall
x[v , j )
with
<v>
dist ( i , j )
in
<k>
in
4 http://www.tsp.gate h.edu
21
x[ i ,
j ;
V do
no_subtour :
forall
+ ( py [ a py [ b ) ^ 2 ) ;
in
x[ i ,v )
== 2 ;
Zimpl
a r d (P [ k )
sum <i , j >
<=
> 2
in
a r d (P [ k )
The resulting
lp
and
a r d (P [ k )
w i t h <i >
in
<
P[ k
a r d (V)
a n d <j >
in
do
P[ k
x[ i , j
1;
An optimal tour for the data above is Berlin, Hamburg, Hannover, Dortmund, Bo
hum, Wuppertal, Essen, Duisburg, Trier, Koblenz, Frankfurt, Heidelberg, Karlsruhe,
Stuttgart, Augsburg, Passau, Bayreuth, Jena, Leipzig, Berlin.
utting sto k
that has to be satised, we have to de ide whi h plant should serve whi h store. We
have
osts
store
s.
cp
and
cps
p .
to
by exa
tly one plant. Of
ourse we are looking for the
heapest solution:
min
cp zp +
pP
cps xps
subje t to
pP,sS
xps = 1
for all
sS
(2)
xps 6 zp
for all
s S, p P
(3)
s xps 6 p
for all
pP
(4)
xps , zp {0, 1}
for all
p P, s S
pP
sS
zp
s.
xps
is to be built.
Equation (2) demands that ea h store is assigned to exa tly one plant.
Inequality (3) makes sure that a plant that serves a shop is built. Inequality (4) assures
that the shops are served by a plant whi
h does not ex
eed its
apa
ity. Putting this
into
5 Only
C.
C.
22
Zimpl
set
PLANTS :=
"A" ,
"B" ,
set
STORES :=
};
set
PS
PLANTS
STORES ;
# How
#
:=
mu
h
will
it
does
..
it
then
ost
"C" ,
to
"D"
build
};
plant
and
what
apa ity
have ?
param
b u i l d i n g [ PLANTS : = <"A">
5 0 0 , <"B">
6 0 0 , <"C">
7 0 0 , <"D">
800;
param
a p a i t y [ PLANTS : = <"A">
4 0 , <"B">
5 5 , <"C">
7 3 , <"D">
90;
# The
demand
param
demand
of
ea h
# Transportation
param
store
[ STORES : =
ost
t r a n s p o r t [ PS
<1>
10 ,
<2>
<3>
17 ,
<4>
8,
<5>
9,
<6>
12 ,
<7>
11 ,
<8>
15 ,
<9>
16;
from
ea h
14 ,
plant
to
ea h
1,
2,
3,
4,
5,
6,
7,
8,
| "A" |
55 ,
4,
17 ,
33 ,
47 ,
98 ,
19 ,
10 ,
| "B" |
42 ,
12 ,
4,
23 ,
16 ,
78 ,
47 ,
9,
82
| "C" |
17 ,
34 ,
65 ,
25 ,
7,
67 ,
45 ,
13 ,
54
| "D" |
60 ,
8,
79 ,
24 ,
28 ,
19 ,
62 ,
18 ,
45
|;
var
x [ PS
binary ;
Is
plant
supplying
var
z [ PLANTS
binary ;
Is
plant
built
# We w a n t
minimize
it
subto
sum <p>
in
store
is
supplied
PLANTS
building [p
in
PS
transport [p , s
by
exa tly
one
z[p
x[p, s ;
plant
assign :
forall
# To
store
heap
ost :
# Ea h
store
:=
<s >
in
STORES
do
sum <p>
in
PLANTS
x[p, s
== 1 ;
be
to
supply
store ,
subto
able
plant
must
be
built
build :
forall
<p , s >
in
PS
do
x [ p , s <= z [ p ;
# The
# that
subto
plant
are
must
be
able
assigned
to
to
meet
the
demands
from
all
stores
it
limit :
forall
<p>
in
PLANTS
in
do
demand [ s
x [ p , s <=
23
apa ity [ p ;
Zimpl
queens on a
queens are on
ombinatorial
sear
h problem often used to test the performan
e of algorithms that solve satisability
problems. Note though, that there are algorithms available whi
h need linear time in
pra
ti
e, like, for example, those of [SG91. We will show four dierent models for the
problem and
ompare their performan
e.
vabs
all dierent
onstraint on the
variables (see
onstraint
1). This makes sure that no queen is lo
ated on the same
olumn than any other queen.
diagonals of a queen by demanding that the absolute value of the row distan
e and the
olumn distan
e of ea
h pair of queens are dierent. We model
a 6= b by abs(a b) > 1.
queens
:=
8;
..
queens
4 4.
set
C :=
set
P :=
{ <i , j >
var
x [C
i n t e g e r >= 1 <= q u e e n s ;
in
};
with
subto
1 :
forall
<i , j >
in
do
subto
2 :
forall
<i , j >
in
do
vabs ( vabs ( x [ i
<
};
vabs ( x [ i
x [ j )
x [ j ) >=
abs ( i
j ) ) >=
1;
1;
The following table shows the performan
e of the model. Sin
e the problem is modeled
as a pure satisability problem, the solution time depends only on how long it takes to
nd a feasible solution.
Vars, Cons,
and
NZ
of variables,
onstraints and non-zero entries in the
onstraint matrix of the generated
integer program.
solver, and
time
Nodes
pu se onds.
Queens
Vars
Cons
NZ
Nodes
Time [s
8
12
16
344
804
1,456
392
924
1,680
951
2,243
4,079
1,324
122,394
>1 mill.
<1
120
>1,700
As we an see, between 12 and 16 queens is the maximum instan e size we an expe t to solve with this model. Neither hanging the
6 Whi h
24
Zimpl
The binary models
Another approa
h to model the problem is to have one binary variable for ea
h square
of the board.
vif
the variables of the blo
ked squares to zero if a queen is pla
ed.
param
olumns
:=
set
:=
..
set
CxC
:=
C;
set
var
(m ==
x [ CxC
maximize
subto
olumns
in
CxC
:=
or
n ==
};
{ <m, n>
or
a b s (m
in
CxC
with
(m !=
i ) == a b s ( n
or
j ))
};
!=
j )
binary ;
queens :
1 :
8;
forall
in
in
in
CxC
CxC
do
x[ i , j ;
vif
x[ i , j
TABU [ i , j
== 1
then
x [ m, n <= 0
end ;
Using extended formulations
an make the models more
omprehensible. For example,
repla
ing
onstraint
1 in line 13 with an equivalent one that does not use
vif as shown
2 :
forall
<i , j >
in
CxC
in
do
a r d (TABU [ i , j )
x[ i , j
TABU [ i , j
plex
x [ m, n <=
a r d (TABU [ i , j ) ;
identi al integer programs. The performan e of the model is shown in the following
S indi
ates the
plex settings used: Either (D)efault, (C)uts 7, or (F)easibility 8 .
Root Node indi
ates the obje
tive fun
tion value of the lp relaxation of the root node.
table.
Queens
Vars
Cons
NZ
D
C
D
C
D
C
C
C
384
448
2,352
864
1,008
7,208
1,536
1,792
16,224
3,456
6,144
4,032
7,168
51,856
119,488
12
16
24
32
Root Node
Nodes
Time [s
13.4301
241
8.0000
0
23.4463 20,911
12.0000
0
35.1807 281,030
16.0000
54
24.0000
38
56.4756 >5,500
<1
<1
<1
1,662
8
42
>2,000
This approa
h solves instan
es with more than 24 queens. The use of aggressive
ut
generation improves the upper bound on the obje
tive fun
tion signi
antly, though it
an be observed that for values of
7 Cuts:
larger than 24
8 Feasibility:
25
Zimpl
trivial upper bound of
n.9
this
hanges:
subto
3 :
forall
<i , j >
forall
in
CxC
<m, n>
in
do
TABU [ i , j
do
x[ i , j
+ x [ m, n <=
1;
As shown in the table below, the optimal upper bound on the obje
tive fun
tion is
always found in the root node.
formulation, i. e.
the solution time depends mainly on the time it needs to nd the
optimal solution. While redu
ing the number of bran
h-and-bound nodes evaluated,
aggressive
ut generation in
reases the total solution time.
With this approa
h instan
es up to 96 queens
an be solved.
integer program gets too large to be generated.
routine is able to aggregate the
onstraints again,
generate the
ip.
Pres. NZ
plex presolve
Zimpl needs too mu
h memory to
Even though the
Pres.
NZ
Root
Node
Nodes
12,640
105,152
857,472
25,280 1,594
210,304 6,060
1,714,944 23,970
2,912,320
5,824,640 53,829
16.0
32.0
64.0
64.0
96.0
96.0
96.0
0
58
110
30
70
30
69
Queens
Vars
Cons
16
32
64
64
96
96
96
D
D
D
C
D
C
F
256
1,024
4,096
9,216
NZ
Time
[s
<1
5
60
89
193
410
66
row :
forall
subto
ol :
in
CxC
diag_ ol_do :
subto
CxC
diag_row_up :
subto
in
in
CxC
diag_ ol_up :
in
<j >
CxC
diag_row_do :
subto
in
<i >
CxC
forall
subto
in
CxC
in
do
x [ i , j <=
in
do
x [ i , j <=
forall
<i >
with m
forall
forall
with
in
i
1;
do
== n
in
i
<j >
with m
forall
<i >
with m
1;
x [ m, n <=
1;
n:
x [ m, n <=
1;
j :
x [ m, n <=
1;
do
1 == n
<j >
1:
do
== 1
in
in
do
a r d (C)
m == n
j :
x [ m, n <=
1;
Here again, the upper bound on the obje
tive fun
tion is always optimal.
of the generated
The size
The
results for dierent instan es size are shown in the following table:
9 For
the 32 queens instan
e the optimal solution is found after 800 nodes, but the upper
bound is still 56.1678.
26
Zimpl
Queens
Vars
Cons
NZ
Root Node
Nodes
Time [s
64
96
96
96
128
128
D
D
C
F
D
F
4,096
9,216
384
576
16,512
37,056
16,384
768
65,792
64.0
96.0
96.0
96.0
128.0
128.0
0
1680
1200
121
>7000
309
<1
331
338
15
>3600
90
In
ase of the 128 queens instan
e with default settings, a solution with 127 queens
is found after 90 bran
h-and-bound nodes, but
mal solution within an hour. From the performan
e of the Feasible setting it
an be
presumed that generating
uts is not bene
ial for this model.
27
Zimpl
7 Error messages
Here is a (hopefully)
omplete list of the in
omprehensible error messages
Zimpl an
produ e:
with a dot.
pip.
or
subto statements
Most
15 6 x 6 2.
xxx
It is only allowed to raise a number to the power of integers. Also trying to raise
10
xxx
You
an only
ompute the fa
torial of integers. Also
omputing the fa
torial of
a number bigger then two billion is generally a bad idea. See also Error 115.
10 The
28
Zimpl
115 Timeout!
You tried to
ompute a number bigger than
1000!.
112.
xxx
xxx
119
xxx
To apply Operation
xxx
two sets, both must have the same dimension tuples,i. e. the tuples must have
the same number of
omponents.
xxx
xxx
To generate a set, the from number must be an integer with an absolute value
of less than two billion.
xxx
To generate a set, the upto number must be an integer with an absolute value
of less than two billion.
xxx
To generate a set, the step number must be an integer with an absolute value
of less than two billion.
xxx
xxx
xxx,
yyy
The index value given in a sele
tion tuple is bigger than the dimension of the
tuples in the set.
xxx
for symbol
The index tuple used in the initialization list of a index set, is not member of the
index set of the set. E.g,
"<1n> 2n".
29
"<1n>"
only having a
Zimpl
133 Unknown symbol
xxx
xxx
for symbol
The index tuple given in the initialization is not member of the index set of the
parameter.
xxx
is empty
The attempt was made to de
lare an indexed parameter with the empty set as
index set.
with
elements.
xxx
trun ated to
yyy
(warning)
An integral variable
an only have an integral bound. So the given non integral
bound was adjusted.
xxx
trun ated to
yyy
(warning)
An integral variable
an only have an integral bound. So the given non integral
bound was adjusted.
xxx
The upper bound given for a variable was smaller than the lower bound.
xxx
for symbol
yyy
The index tuple given is not member of the index set of the symbol.
xxx
The
ardinality for the subsets to generate must be given as an integer smaller
than two billion.
xxx,
yyy
The
ardinality for the subsets to generate must be between 1 and the
ardinality
of the base set.
xxx
The use value must be given as an integer smaller than two billion.
xxx
is not positive
Negative or zero values for the use parameter are not allowed.
xxx
The skip value must be given as an integer smaller than two billion.
xxx
is not positive
Negative or zero values for the skip parameter are not allowed.
>
"<1n,2n>".
There have to be a
<
in this order.
<, >,
1n, 3s.
xxx
30
Zimpl
154 Invalid eld type
xxx
and
s.
xxx
got
yyy
The type found was not the expe ted one, e.g.
xxx
yyy
Two elements from dierent tuples were
ompared and found to be of dierent
types.
161 Line
xxx:
Unterminated string
"
hara ters.
ended.
162 Line
xxx:
Trailing
"yyy"
ignored (warning)
Something was found after the last semi olon in the le.
163 Line
xxx:
Syntax Error
xxx
xxx
for symbol
yyy
mailto:ko hzib.de.
xxx
in all of dene
.zpl le to
yyy
A dene was
alled and one of the arguments was a name (of a variable) for
whi
h no value was dened.
yyy)
zzz
A dene was alled with a dierent number of arguments than in its denition.
31
Zimpl
172 Wrong number of entries (xxx) in table line, expe
ted
yyy
entries
Ea
h line of a parameter initialization table must have exa
tly the same number
of entries as the index (rst) line of the table.
xxx
for symbol
xxx
read as
"yyy".
It was tried to read a eld with an 'n' designation in the template, but what was
read is not a valid number.
"xxx"
name=value.
The
name
ignored (warning)
-D
letter and may onsist only out of letters and numbers in luding the unders ore.
abs.
Either everything is
vabs
fun tion.
Due to
vabs.
vabs has
vif
32
Zimpl
189 Tuple number
xxx
The tuple number must be given as an integer smaller than two billion.
xxx
The omponent number must be given as an integer smaller than two billion.
xxx
The tuple number must be between one and the ardinality of the set.
xxx
The omponent number must be between one and the dimension of the set.
xxx
yyy,
expe ted
zzz
The index tuple of an entry in a parameter initialization list must have the same
dimension as the indexing set of the parameter.
error 134.
xxx
yyy,
expe ted
zzz
The index tuple of an entry in a set initialization list must have the same dimension as the indexing set of the set. If you use a
xxx
(warning)
All weights assigned to variables in an spe ial ordered set have to be unique.
205
xxx
yyy ignored
yyy there
(warning)
were
xxx
than indi es for the symbol. The ex ess entries were ignored.
33
Zimpl
206 argmin/argmax over empty set (warning)
The index expression for the
argmin
argmax
or
was empty.
empty set.
xxx
xxx
not >= 1
ontaining numbers.
ontaining numbers.
ontaining numbers.
ontaining numbers.
xxx
yyy (warning)
xxx of le yyy are ignored.
of
Insert
"<n+>" in
"n+"
instead of
the template.
xxx
ignored
A parameter was de
lared a se
ond time with the same name. The typi
al use
would be to de
lare default values for a parameter in the
xxx
xxx
xxx
in substr is negative
xxx,
should be between
yyy
and
zzz
The
ardinality for the subsets to generate must be between the given lower
bound and the
ardinality of the base set.
34
Zimpl
221 The obje
tive fun
tion has to be linear
Only obje
tive fun
tions with linear
onstraints are allowed.
600 File format
an only handle linear and quadrati
onstraints (warning)
The
hosen le format
an
urrently only handle linear and quadrati
onstraints.
Higher degree
onstaints were ignored.
700 log():
Fun
tion
too
701 sqrt():
argument was
Fun tion
702 ln():
Fun
tion
too
xxx
(or
yyy)
Parsing error. What was found was not what was expe
ted. The statement you
entered is not valid.
.zpl le
to
mailto:ko hzib.de.
he k instru tion
Referen
es
[Chv83
Vaek Chvtal.
Linear Programming.
York, 1983.
[Dan90
[FGK03
tion, 2003.
[GNU03 GNU multiple pre
ision arithmeti
library (GMP), version 4.1.2., 2003. Code
and do
umentation available at
[IBM97
http://gmplib.org.
35
Zimpl
[ILO02
ILOG CPLEX Division, 889 Alder Avenue, Suite 200, In
line Village, NV
89451, USA.
able at
[Kal04a
Josef Kallrath.
Information avail-
[Kal04b
Kluwer, 2004.
[Ko
04
Thorsten Ko h.
Alexander S hrijver.
Combinatorial Optimization.
Springer, 2003.
Hermann S hi hl. Models and the history of modeling. In Josef Kallrath, editor,
2004.
[SG91
[Spi04
Kurt Spielberg. The optimization systems MPSX and OSL. In Josef Kallrath,
editor,
pages 267278.
Kluwer, 2004.
[vH99
http://www.dashoptimization. om.
36