33
Modelling Christina Burt, Stephen J. Maher, Jakob Witzig Zuse Institute Berlin Berlin, Germany 29th September 2015

Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Modelling

Christina Burt, Stephen J. Maher, Jakob Witzig

Zuse Institute BerlinBerlin, Germany

29th September 2015

Page 2: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Modelling Languages

Jakob Witzig

Burt, Maher, Witzig – Modelling 1 / 22

Page 3: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Modelling Languages: What are they?

Algebraic Modelling Languages are high-level computer programminglanguages. They

▶ provide a concise, readable way to express/formulate a mathematicalproblem,

▶ do not solve the problems directly, but rather, call external solvers,

▶ generally contain a mix of declarative and procedural elements.

Examples of modelling languages include Xpress-Mosel, ZIMPL, AIMMS,AMPL, GAMS and OPL.

Burt, Maher, Witzig – Modelling 2 / 22

Page 4: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Modelling Languages: Pros and Cons

The benefits of using a modelling language to implement a model include:

▶ can be a fast way to implement a model,

▶ easy to learn due to few basic language elements; short learning curveto achieve high expressivity,

▶ syntax is usually very similar to mathematical notation, making itintuitive,

▶ simplified expression of some elements of math programming models,such as sets and logical expressions.

Burt, Maher, Witzig – Modelling 3 / 22

Page 5: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Modelling Languages: Pros and Cons

ZIMPL:set Warehouses := {1,2,3,4};set Plants := {1,2};var transport[Warehouses * Plants] integer;

Gurobi:Warehouses = [1,2,3,4]

Plants = [1,2]

transport = []for w in warehouses:

transport.append([])

for p in plants:

transport[w].append(m.addVar(obj=transCosts[w][p],name="Trans%d.%d" % (p, w) ))

Burt, Maher, Witzig – Modelling 4 / 22

Page 6: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Modelling Languages: Pros and Cons

The drawbacks of using a modelling language include:

▶ access to solver features, such as callbacks, is often difficult (or notpossible)

▶ the solving is decoupled from the modelling process to some extent –the user still must understand how to model well for the targetalgorithm,

▶ the language interpreter may perform inefficient conversionscompared to what a reformulation may achieve.

Burt, Maher, Witzig – Modelling 5 / 22

Page 7: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Modelling Languages: Pros and Cons

Today we will do a modelling exercise that will illustrate:

▶ how quickly you can learn a modelling language,

▶ how quickly you can implement a model and obtain a solution froman installed solver.

Burt, Maher, Witzig – Modelling 6 / 22

Page 8: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Some Modelling Languages

▶ ZIMPL: for writing down a model quickly and output a format for LP,MIP and MINLP that can be read into any LP/MIP solver (and SCIPfor MINLP);

▶ GAMS: contains procedural elements for modifying the data andmodel during the procedure, can switch between solvers, has an IDEwith tree-view and model debugging;

▶ AIMMS: contains procedural elements and tools for deployment, e.g.it is possible to write a GUI for a model-application for partners whodo not want to see the model itself

▶ AMPL: integrates a modelling language, command language (fordebugging and analysing), and a scripting language for modifying thedata and model during the procedure.

Burt, Maher, Witzig – Modelling 7 / 22

Page 9: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Getting Started with ZIMPL

Jakob Witzig

Burt, Maher, Witzig – Modelling 8 / 22

Page 10: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

What is ZIMPL?

▶ Zuse Institute Mathematical Programming Language

▶ Free software, first release 2001

▶ Language to translate mathematical models into (mixed) integerprograms.

▶ ZIMPL is part of the SCIP Optimization Suite(http://scip.zib.de/)

▶ User Guide: http://zimpl.zib.de/download/zimpl.pdf

Burt, Maher, Witzig – Modelling 9 / 22

Page 11: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

What is ZIMPL?

▶ Zuse Institute Mathematical Programming Language

▶ Free software, first release 2001

▶ Language to translate mathematical models into (mixed) integerprograms.

▶ ZIMPL is part of the SCIP Optimization Suite(http://scip.zib.de/)

▶ User Guide: http://zimpl.zib.de/download/zimpl.pdf

Burt, Maher, Witzig – Modelling 9 / 22

Page 12: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

What is ZIMPL?

▶ Zuse Institute Mathematical Programming Language

▶ Free software, first release 2001

▶ Language to translate mathematical models into (mixed) integerprograms.

▶ ZIMPL is part of the SCIP Optimization Suite(http://scip.zib.de/)

▶ User Guide: http://zimpl.zib.de/download/zimpl.pdf

Burt, Maher, Witzig – Modelling 9 / 22

Page 13: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

ZIMPL Format & Functions

▶ Each line ends with a semicolon

▶ 6 different statements:▶ Sets,▶ Parameters,▶ Variables,▶ Objective,▶ Constraints,▶ Function definition, and print commands.

▶ Rational arithmetic functions (e.g., modulo, factorial, min/max of aset, rounding, . . . )

▶ Double precision functions: square root, logarithm, exponentialfunction

▶ Set related functions (e.g., cross product, union, argmin, . . . )

▶ Input stream to read in data files

Burt, Maher, Witzig – Modelling 10 / 22

Page 14: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

ZIMPL Format & Functions

▶ Each line ends with a semicolon

▶ 6 different statements:▶ Sets,▶ Parameters,▶ Variables,▶ Objective,▶ Constraints,▶ Function definition, and print commands.

▶ Rational arithmetic functions (e.g., modulo, factorial, min/max of aset, rounding, . . . )

▶ Double precision functions: square root, logarithm, exponentialfunction

▶ Set related functions (e.g., cross product, union, argmin, . . . )

▶ Input stream to read in data files

Burt, Maher, Witzig – Modelling 10 / 22

Page 15: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

ZIMPL Format & Functions

▶ Each line ends with a semicolon

▶ 6 different statements:▶ Sets,▶ Parameters,▶ Variables,▶ Objective,▶ Constraints,▶ Function definition, and print commands.

▶ Rational arithmetic functions (e.g., modulo, factorial, min/max of aset, rounding, . . . )

▶ Double precision functions: square root, logarithm, exponentialfunction

▶ Set related functions (e.g., cross product, union, argmin, . . . )

▶ Input stream to read in data files

Burt, Maher, Witzig – Modelling 10 / 22

Page 16: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

ZIMPL Format & Functions

▶ Each line ends with a semicolon

▶ 6 different statements:▶ Sets,▶ Parameters,▶ Variables,▶ Objective,▶ Constraints,▶ Function definition, and print commands.

▶ Rational arithmetic functions (e.g., modulo, factorial, min/max of aset, rounding, . . . )

▶ Double precision functions: square root, logarithm, exponentialfunction

▶ Set related functions (e.g., cross product, union, argmin, . . . )

▶ Input stream to read in data files

Burt, Maher, Witzig – Modelling 10 / 22

Page 17: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

ZIMPL Format & Functions

▶ Each line ends with a semicolon

▶ 6 different statements:▶ Sets,▶ Parameters,▶ Variables,▶ Objective,▶ Constraints,▶ Function definition, and print commands.

▶ Rational arithmetic functions (e.g., modulo, factorial, min/max of aset, rounding, . . . )

▶ Double precision functions: square root, logarithm, exponentialfunction

▶ Set related functions (e.g., cross product, union, argmin, . . . )

▶ Input stream to read in data files

Burt, Maher, Witzig – Modelling 10 / 22

Page 18: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

ZIMPL Format & Functions

▶ Each line ends with a semicolon

▶ 6 different statements:▶ Sets,▶ Parameters,▶ Variables,▶ Objective,▶ Constraints,▶ Function definition, and print commands.

▶ Rational arithmetic functions (e.g., modulo, factorial, min/max of aset, rounding, . . . )

▶ Double precision functions: square root, logarithm, exponentialfunction

▶ Set related functions (e.g., cross product, union, argmin, . . . )

▶ Input stream to read in data files

Burt, Maher, Witzig – Modelling 10 / 22

Page 19: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Executing ZIMPL

To get an LP file of example.zpl

zimpl example.zpl

Use SCIP to read in directly and solve

scip -f example.zpl

Burt, Maher, Witzig – Modelling 11 / 22

Page 20: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Sets

▶ Set Definitions:s e t A := {1 . . 4} ;s e t B := {1 to 3} ;s e t D[B] := <1>{”a ” , ”b”} , <2>{”c ”} , <3>{”c ” , ”e ” , ” f ”} ;s e t E := {”monkey ” , ” g i r a f f e ” , ” e l e phan t ”} ;

▶ Set Operations:s e t C := A c r o s s C ;s e t P [ ] := power se t (D) ;s e t I := i n d e x s e t (P ) ;s e t S [ ] := s u b s e t s ( I , 2 ) ;s e t U := un ion <i> i n I : D[ i ] ;

Burt, Maher, Witzig – Modelling 12 / 22

Page 21: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Parameters

▶ Indexsets:s e t I := {1 . . 10} ;s e t J := {”a ” , ”b ” , ”c ” , ”x ” , ”y ” , ” z ” ,} ;

▶ Parameters:param h [ I ∗J ] := | ”a ” , ”c ” , ”x ” , ” z” |

| 1 | 12 , 17 , 99 , 23 || 3 | 4 , 3 ,−17 , 66∗5 .5 || 5 | 2/3 , −.4 , 3 , abs (−4) || 9 | 1 , 2 , 0 , 3 | d e f a u l t −99;

param g [ I ∗ I ∗ I ] := | 1 , 2 , 3 || 1 , 3 | 0 , 0 , 1 || 2 , 1 | 1 , 0 , 1 | ;

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 ;

Burt, Maher, Witzig – Modelling 13 / 22

Page 22: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Reading Sets and Parameters

▶ Input file data.txt:1 2 ab con12 3 bc con24 5 de con3

▶ Read Sets:

s e t Q := { r ead ” data . t x t ” as ”<4s>” } ;

▶ Read Parameter:

param co s t [Q] := read ” data . t x t ” as ”2n 1n ” ;

Burt, Maher, Witzig – Modelling 14 / 22

Page 23: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Variables

▶ Types: binary, integer, and real

▶ Implicit bounds are [0,∞]

▶ Declare binary/integer variables as implicit

▶ Example:va r x1 ;va r x2 b i n a r y ;va r x3 i n t e g e r >= − i n f i n i t y ;va r y [A ] r e a l >= 2 <= 18 ;va r z [<a , b> i n C ] i n t e g e r

>= a∗10 <= i f b <= 3 then p [ b ] e l s e i n f i n i t y end ;va r w i m p l i c i t b i n a r y ;

Burt, Maher, Witzig – Modelling 15 / 22

Page 24: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Objective Functions

▶ Syntax: sense name : term;

▶ Can be minimize or maximize

▶ Must be at most one objective function

▶ Example:min imize ob j : 3∗ x1 − 1∗ x2 ;min im ize c o s t : sum <i , j> i n E wi th i < j : c [ i , j ] ∗ x [ i , j ] ;maximize p r o f i t : sum <i> i n I : p [ i ] ∗ x [ i ] ;

Burt, Maher, Witzig – Modelling 16 / 22

Page 25: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Constraints

▶ Syntax: subto name : term sense term;

▶ Sense can be ≤, ==, and ≥subto c1 : sum <i , j> i n E wi th i < j and j != 1 : x [ i , j ] == 1 ;

▶ Can be combined with if-conditionssubto c2 : f o r a l l <i> i n I do

i f i <= 5 then 3∗y [ i ] >= 1e l s e −2∗y [ i ] <= 0 end ;

▶ Combining with andsubto c3 : f o r a l l <i , j> i n E do

i f i < j then sum <k> i n K : y [ k , i , j ] == 1 and x [ i , j ] == 2e l s e sum <k> i n K : y [ k , i , j ] == 0 end ;

Burt, Maher, Witzig – Modelling 17 / 22

Page 26: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Constraints

▶ Syntax: subto name : term sense term;

▶ Sense can be ≤, ==, and ≥subto c1 : sum <i , j> i n E wi th i < j and j != 1 : x [ i , j ] == 1 ;

▶ Can be combined with if-conditionssubto c2 : f o r a l l <i> i n I do

i f i <= 5 then 3∗y [ i ] >= 1e l s e −2∗y [ i ] <= 0 end ;

▶ Combining with andsubto c3 : f o r a l l <i , j> i n E do

i f i < j then sum <k> i n K : y [ k , i , j ] == 1 and x [ i , j ] == 2e l s e sum <k> i n K : y [ k , i , j ] == 0 end ;

Burt, Maher, Witzig – Modelling 17 / 22

Page 27: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Constraints

▶ Syntax: subto name : term sense term;

▶ Sense can be ≤, ==, and ≥subto c1 : sum <i , j> i n E wi th i < j and j != 1 : x [ i , j ] == 1 ;

▶ Can be combined with if-conditionssubto c2 : f o r a l l <i> i n I do

i f i <= 5 then 3∗y [ i ] >= 1e l s e −2∗y [ i ] <= 0 end ;

▶ Combining with andsubto c3 : f o r a l l <i , j> i n E do

i f i < j then sum <k> i n K : y [ k , i , j ] == 1 and x [ i , j ] == 2e l s e sum <k> i n K : y [ k , i , j ] == 0 end ;

Burt, Maher, Witzig – Modelling 17 / 22

Page 28: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Function Definition and Print Commands

▶ Functions Definition:▶ Syntax: def* name(input) := term;▶ * = numb, strg, bool, and set▶ Example:

defnumb d i s t ( a , b ) := s q r t ( a∗a + b∗b ) ;d e f b o o l smal ( a , b ) := a < b ;d e f s e t b i gg ( i ) := { <j> i n K wi th j > i } ;

▶ Print Commands:▶ Print Sets:

s e t I := {1 . . 10} ;do p r i n t I ;do p r i n t ” C a r d i n a l i t y o f I : ” , ca rd ( I ) ;

▶ Print Supersets:

do f o r a l l <i> i n I w i th i >= 4 do p r i n t s q r t ( i ) ;

Burt, Maher, Witzig – Modelling 18 / 22

Page 29: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Function Definition and Print Commands

▶ Functions Definition:▶ Syntax: def* name(input) := term;▶ * = numb, strg, bool, and set▶ Example:

defnumb d i s t ( a , b ) := s q r t ( a∗a + b∗b ) ;d e f b o o l smal ( a , b ) := a < b ;d e f s e t b i gg ( i ) := { <j> i n K wi th j > i } ;

▶ Print Commands:▶ Print Sets:

s e t I := {1 . . 10} ;do p r i n t I ;do p r i n t ” C a r d i n a l i t y o f I : ” , ca rd ( I ) ;

▶ Print Supersets:

do f o r a l l <i> i n I w i th i >= 4 do p r i n t s q r t ( i ) ;

Burt, Maher, Witzig – Modelling 18 / 22

Page 30: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Exercise: ZIMPL

The SCIP Team

Burt, Maher, Witzig – Modelling 19 / 22

Page 31: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Exercise: ZIMPL

Implement the model of the equitable coach problem you havecreated before in ZIMPL.

Use the code skeleton: /home/exercise/modelling/Exercise ECP.zpl

The ZIMPL user guide is located at: /home/exercise/modelling/

Burt, Maher, Witzig – Modelling 20 / 22

Page 32: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

The Code Skeleton

▶ You can execute the *.zpl file with:

zimpl Exercise ECP.zpl -D FILE=⟨test data⟩

▶ You have to . . .▶ . . . complete the reading of parameters and sets▶ . . . define a function▶ . . . define your own variables▶ . . . define the model itself (objective function, constraints, etc.)

Burt, Maher, Witzig – Modelling 21 / 22

Page 33: Modelling - co-at-work.zib.deco-at-work.zib.de/berlin2015/files/modelling_languages.pdf · Modelling Languages: What are they? Algebraic Modelling Languages are high-level computer

Exercise: ZIMPL

Implement the model of the equitable coach problem you havecreated before in ZIMPL.

Use the code skeleton: /home/exercise/modelling/Exercise ECP.zpl

The ZIMPL user guide is located at: /home/exercise/modelling/

Burt, Maher, Witzig – Modelling 22 / 22