Week 2 - Intro Mosel

46
Foundations of Logistics Systems Engineering Dr. Evrim Ursavas

description

Slides linear programming

Transcript of Week 2 - Intro Mosel

Page 1: Week 2 - Intro Mosel

Foundations of Logistics Systems Engineering Dr. Evrim Ursavas

Page 2: Week 2 - Intro Mosel

Getting Started with Xpress-IVE and Mosel language

Page 3: Week 2 - Intro Mosel

Optimisation….

Real World

Mathematical Model

Computer Program

Problem definition & data for parameters.

Model and parameters.

Output (for verification purposes).

NOTE: We will be using XpressMP

Page 4: Week 2 - Intro Mosel

Glossary of terms • Program Code ▫ This is a type-written code to translate your mathematical

model to a format readable by XpressMP. • Binary Code ▫ This is a generic format of your program code, coded in 0’s

and 1’s, readable by any computer applications. • Compile ▫ This is when XpressMP converts the program code to a

binary code. • Array ▫ This is how you would express and store a list of items in a

program code.

Page 5: Week 2 - Intro Mosel

Glossary of terms (cont’d) • Memory ▫ This is where the computer stores your model’s

information, such as input parameters. ▫ Information stored in memory can be retrieved during the

execution of your program code. • Mathematical operators

• Debugging ▫ This is a term used to describe the procedure of finding

errors in your program code. ▫ The program code errors are referred to as “bugs”.

• Looping ▫ This term is used when a repetitive task is performed.

Operator Symbol Used Operator Symbol UsedAdd + Subtract -

Multiply * Exponent ("Power of") ^Divide /

Page 6: Week 2 - Intro Mosel

XpressMP, Xpress-IVE and Mosel • XpressMP: ▫ Large-scale optimisation software developed by Dash

(http://www.dashoptimization.com) • Xpress-IVE: ▫ IVE stands for Interactive Visual Environment ▫ Provides a friendly user interface for model coding, code

debugging and result display. (Windows environment) • Xpress-Mosel: ▫ a programming language that translates your mathematical

programs/algorithms to a format readable by Xpress Optimiser.

▫ File extension: *.mos

Page 7: Week 2 - Intro Mosel

From our university..

• https://uwp.rug.nl/ • Mathematics & Statistics->Xpress->Xpress Ive

• Practicals in teams.. • One student logs in..

Page 8: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

8

Coding Window

Project Bar

Run Bar

Information Bar

Run model

Compile model

Switch between models

Execution options

Page 9: Week 2 - Intro Mosel

The (balanced) transportation problem • Let’s look at the following problem: Powerco has 3 electric power plants that supply the needs of 4 cities. The supply limit, demand amount and the unit transportation costs are given as follows: Formulate an LP to minimise the cost of meeting each city’s power demand.

From City 1 City 2 City 3 City 4 Supply (million kWh)Plant 1 $8 $6 $10 $9 35Plant 2 $9 $12 $13 $7 50Plant 3 $14 $9 $16 $5 40Demand (million kWh) 45 20 30 30

To

Page 10: Week 2 - Intro Mosel

Balanced Transportation Problem (BTP)

jix

jdx

isxts

xcMinimise

ij

ji

ij

ij

ij

i jijij

,,0

,

,..

3

1

4

1

3

1

4

1

∀≥

∀=

∀=

∑∑

=

=

= =

ji

cij

point demand topoint supply from

cost portation unit trans=

isi

point supply oflimit supply =

jd j

point demand

ofamount demand=

ji

xij

point demand topoint supply

ed transportunits ofnumber =

Page 11: Week 2 - Intro Mosel

first a bit of general information

Page 12: Week 2 - Intro Mosel

• Name of the model & options • Parameters • Declarations (decision variables, arrays, etc.) • Data input • Objective function • Constraints • Output & results

How to write a model in Mosel Xpress-IVE

12

Page 13: Week 2 - Intro Mosel

model ModelName options … uses “mmxprs” … !other sections end-model For comment write “!” before the commented word

How to write a model (1) Name of the model & options

13

Page 14: Week 2 - Intro Mosel

model ModelName !parameters section first parameters MAXTIME=300 USE_LOG=false !... end-parameters !Rest of the model (declarations, statements, etc.) end-model

How to write a model (2) Parameters – optional section

14

Page 15: Week 2 - Intro Mosel

declarations Variable : mpvar VariableArray : array() of mpvar IntegerVariable : mpvar BinaryVariable : mpvar end-declarations IntegerVariable is_integer !when integer variable BinaryVariable is_binary !when binary variable

15

How to write a model (3) Declarations (variables, arrays, etc.)

Page 16: Week 2 - Intro Mosel

declarations UnitCost : array(1..10) of integer end-declarations initializations from “Filename” UnitCost; end-initializations

16

How to write a model (4) Data input – optional section

Page 17: Week 2 - Intro Mosel

Cost:=2*x1+3*x2 !...constraints minimize(Cost) !you don’t need to declare Cost or Profit:=2*x1+3*x2 !...constraints maximize(Profit) !you don’t need to declare Profit

17

How to write a model (5) Objective function

Page 18: Week 2 - Intro Mosel

! simple constraint X1+3*X2-5*X3<=8 ! multiple constraints using loop forall(i in 1..10) Z(i) <=1 ! sum constraint sum(i in 1..10) X(i)<=B ! multiple sum constraints using loop forall(i in 1..5) sum (j in 1..10) X(i,j)=1

18

How to write a model (6) Constraints 853 321 ≤−+ xxx

10,..,11 =≤ iforzi

Bxi

i ≤∑=

10

1

5,..,1110

1==∑

=

iforxj

ij

Page 19: Week 2 - Intro Mosel

! Value of objective function - getobjval writeln(“Objective value: “, getobjval) ! Value of decision variable writeln(“X1 = “,getsol(X1)) ! Values of decision variables in array using loop forall(i in 1..M) writeln(" Y(“, i,") = “, getsol(Y(i)))

19

How to write a model (7) Output & results

Page 20: Week 2 - Intro Mosel

Let’s get back to our transportation problem…

Page 21: Week 2 - Intro Mosel

General Mosel Program Structure

WARNING: Mosel is case-sensitive!

Page 22: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

22

Modules

Page 23: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

23

Declarations declarations List1 = 1..10 List2 = 1..5 List3 = {“Item1”,“Item2”,“Item3”} Var1: mpvar Var2: array(List1) of mpvar Var3: array(List1,List2) of mpvar InputParam1: real InputParam2: array(List1) of integer InputParam3: array(List1,List3) of real end-declarations

Page 24: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

24

Declarations (cont’d)

• For BTP…

Page 25: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

25

Initialisation declarations List1 = 1..3 List2 = 1..2 InputParam1: array(List1) of integer InputParam2: array(List1,List2) of real InputParam3: array(List2,List2,List2) of real end-declarations ! Initialise input parameters InputParam1:= [1,2,3] ! initialisation for

InputParam1 InputParam2:= [11,12,21,22,31,32] ! initialisation for

InputParam2 InputParam3:= [111,112,121,122,211,212 ,221,222] ! initialisation for InputParam3 NOTE: Data input from file is another method of parameter initialisation.

Page 26: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

26

Initialisation (cont’d)

• For BTP…

Page 27: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

27

Objective Function ObjectiveName:= function NOTE: “function” can be be expressed in a format very similar to a mathe- matical function, e.g. the objective for BTP can be expressed in Mosel as

TotalCost:= sum(i in SUPPLY, j in DEMAND) c(i,j)*x(i,j)

NOTE: Declaration is not needed for TotalCost.

∑∑= =

=3

1

4

1i jijij xcTotalCost

Page 28: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

28

Constraints

The following constraint can be expressed in Mosel as

forall(j in LIST1) sum(i in LIST2) x(i,j) <= 1 where LIST1 and LIST2 are range sets and x(i,j) is a two-dimensional decision variable.

jxn

iij ∀≤∑

=

,11

Page 29: Week 2 - Intro Mosel

Constraints (“forall” loop description)

• The idea behind a “forall” loop: ▫ Consider the following constraints (specific

version):

▫ The corresponding generic version is nixi ,...,2,1,1 =≤

11

11

1

2

1

≤≤

≤≤

n

n

xx

xx

Page 30: Week 2 - Intro Mosel

Constraints (“forall” loop description) ▫ If “forall” does not exist, you will have to type in every

single constraint (n times) expressed in the specific version of the constraint, i.e.

x(1) <= 1 x(2) <= 1

.

. x(n) <= 1

▫ Fortunately, “forall” loop can simplify this task. We only need the following line of code

forall(i in LIST) x(i) <= 1 where LIST= 1..n

Page 31: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

31

Constraints (cont’d) …and the following constraint can be expressed in Mosel as

forall(i in LIST1, j in LIST2 | i < j) x(i,j) <= 1 …and if x(i,j) is a binary variable (0-1 variable), then the following constraint must be added:

forall(i in LIST1, j in LIST2) x(i,j) is_binary When the constraint above is not stated, xij is by default a non-negative real number. If xij is an integer variable, then is_integer should replace is_binary in the above constraint.

jixij <∀≤ ,1

Page 32: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

32 Constraints (cont’d)

• For BTP…

jix

jdx

isxts

xcMinimise

ij

ji

ij

ij

ij

i jijij

,,0

,

,..

3

1

4

1

3

1

4

1

∀≥

∀=

∀=

∑∑

=

=

= =

Page 33: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

33

This is the easy bit: If the objective function, say , is to be minimised, then the following statement is needed:

minimise(TotalCost)

Optimisation Statement

∑∑= =

=3

1

4

1i jijij xcTotalCost

Page 34: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

34

Output To display the objective value: writeln(“Objective value is ”, getobjval, “.”) The output is “Objective value is ##.”, displayed at the “Output/Input” pane of the Run Bar. To display the value taken by a decision variable: writeln(“The value of x is ”, getsol(x), “.”) The output is “The value of x is ##.”. NOTE: “write” can also be used, instead of “writeln”. “writeln” inserts a “new line” at the end of the output, “write” does not.

Page 35: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

35 Output (cont’d)

• For BTP…

Page 36: Week 2 - Intro Mosel

Another Example A Bin Packing Problem

Page 37: Week 2 - Intro Mosel

Example1: A Bin Packing Problem

…………

PROBLEM DEFINITION: What is the minimum number of bins needed to contain all items? All items are of different sizes and every bin has a limit on its capacity.

Page 38: Week 2 - Intro Mosel

Bin Packing (Cont’d)

IP formulation

{ }{ }

1

1

1

. . , ,

1,

,

0,1 , ,

0,1 ,

n

jj

ij j

n

ijj

n

i ij ji

ij

j

Minimise y

s t x y i j

x i

q x Q j

x i j

y j

=

=

=

≤ ∀

= ∀

≤ ∀

∈ ∀

∈ ∀

=otherwise,0

bin in item if,1 jixij

=otherwise,0

used is bin if,1 jy j

jQj bin ofcapacity :

iqi item of size :

Page 39: Week 2 - Intro Mosel

Bin Packing (Cont’d)

jiyx jij ,, ∀≤

ixn

jij ∀=∑

=

,11

1,

n

i ij ji

q x Q j=

≤ ∀∑

∑=

n

jjy

1

Page 40: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

40

Bin Packing (Cont’d)

Page 41: Week 2 - Intro Mosel

if condition then Action_if else Action_otherwise end-if

Bin Packing (Cont’d)

if condition then Action_if end-if

Page 42: Week 2 - Intro Mosel

▫ This form of “forall” loop must be used when several statements are included within the loop.

▫ However, for simplicity, this form of “forall” can be used regardlessly (even for constraints).

Bin Packing (cont’d)

forall(i in LIST) do Statement end-do

Page 43: Week 2 - Intro Mosel

620-362 Applied Operations Research (XpressMP lecture)

43

Bin Packing (Cont’d)

Page 44: Week 2 - Intro Mosel

44

Bin Packing (Cont’d)

Page 45: Week 2 - Intro Mosel

Bin Packing (Cont’d) - Debugging • If there are errors in your Mosel code, e.g. instead of

“forall”, you typed “Forall” (recall that Mosel is case-sensitive), the following message will appear at the Information Bar during compilation:

• Click on one of the error messages, Xpress-IVE will navigate to the line with error and the line highlighted.

• USEFUL TIP: Always check the error message on top of the list first. The proceeding errors may be due to the earlier error.

Page 46: Week 2 - Intro Mosel

Bin Packing (Cont’d) - Debugging

• To check if input parameters are assigned the correct values, you can refer to the Project Bar ▫ Double click on your

selection • NOTE: contents in the

Project Bar will only appear after you run the model.