Вы находитесь на странице: 1из 46

Foundations of Logistics Systems

Engineering
Dr. Evrim Ursavas

Getting Started
with Xpress-IVE and Mosel
language

Optimisation.
Problem definition & data
for parameters.
Real World

Model and
parameters.
Mathematical Model

Output (for verification purposes).

Computer Program
NOTE: We will be using
XpressMP

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 0s


and 1s, 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.

Glossary of terms (contd)


Memory

This is where the computer stores your models


information, such as input parameters.
Information stored in memory can be retrieved during the
execution of your program code.

Mathematical operators

Debugging

Operator
Add
Multiply
Divide

Symbol Used
+
*
/

Operator
Subtract
Exponent ("Power of")

Symbol Used
^

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.

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

From our university..


https://uwp.rug.nl/
Mathematics & Statistics->Xpress->Xpress Ive
Practicals in teams..
One student logs in..

Run model
Execution
options

620-362 Applied
Operations
Research
(XpressMP
lecture)

Compile model
Switch
between
models
Run Bar

Coding Window
Project
Bar

Information Bar

The (balanced) transportation problem


Lets 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:
To
From
Plant 1
Plant 2
Plant 3
Demand (million kWh)

City 1
$8
$9
$14
45

City 2
$6
$12
$9
20

City 3
$10
$13
$16
30

City 4
$9
$7
$5
30

Supply (million kWh)


35
50
40

Formulate an LP to minimise the cost of meeting


each citys power demand.

Balanced Transportation
Problem (BTP)
3

c x

Minimise

i =1 j =1

s.t.

ij

j =1

from supply point i to


demand point j

si = supply limit of
supply point i

= si , i

d j = demand amount of
demand point j

x
i =1

ij ij

cij = unit trans portation cost

ij

= d j , j

xij 0, i, j

xij = number of units transport ed


supply point i to demand
point j

first a bit of general information

12

How to write a model


in Mosel Xpress-IVE

Name of the model & options


Parameters
Declarations (decision variables, arrays, etc.)
Data input
Objective function
Constraints
Output & results

13

How to write a model

(1) Name of the model & options


model ModelName
options
uses mmxprs
!other sections
end-model

For comment write ! before the commented word

14

How to write a model

(2) Parameters optional section


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

15

How to write a model

(3) Declarations (variables, arrays, etc.)


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

16

How to write a model


(4) Data input optional section

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

17

How to write a model


(5) Objective function

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

18

How to write a model

x1 + 3 x2 5 x3 8

(6) Constraints

! simple constraint
X1+3*X2-5*X3<=8

zi 1 for i = 1, .. ,10

! multiple constraints using loop


forall(i in 1..10) Z(i) <=1

10

x
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
10

x
j =1

ij

=1

for i = 1, .. ,5

19

How to write a model


(7) Output & results

! 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)))

Lets get back to our


transportation problem

General Mosel Program Structure

WARNING: Mosel is case-sensitive!

22

Modules

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

620-362 Applied
Operations
Research
(XpressMP
lecture)

24

Declarations (contd)
For BTP

620-362 Applied
Operations
Research
(XpressMP
lecture)

25

620-362 Applied
Operations
Research
(XpressMP
lecture)

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]
InputParam1
InputParam2:= [11,12,21,22,31,32]
InputParam2
InputParam3:= [111,112,121,122,211,212 ,221,222]

! initialisation for
! initialisation for
! initialisation for InputParam3

NOTE:
Data input from file is another method of parameter initialisation.

26

Initialisation (contd)
For BTP

620-362 Applied
Operations
Research
(XpressMP
lecture)

27

620-362 Applied
Operations
Research
(XpressMP
lecture)

Objective Function
ObjectiveName:= function
NOTE:
function can be be expressed in
a format very similar to a mathematical function, e.g. the objective for BTP

TotalCost = cij xij

i =1 j =1
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.

28

620-362 Applied
Operations
Research
(XpressMP
lecture)

Constraints
The following constraint

x
i =1

ij

1, j

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 twodimensional decision variable.

Constraints (forall loop description)


The idea behind a forall loop:

Consider the following constraints (specific


version):
x1 1

x2 1

xn 1 1
xn 1
The corresponding generic version is

xi 1, i = 1,2,..., n

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

31

620-362 Applied
Operations
Research
(XpressMP
lecture)

Constraints (contd)
and the following constraint

xij 1, i < j

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 nonnegative real number. If xij is an integer variable, then is_integer
should replace is_binary in the above constraint.

Constraints (contd)
Minimise c x
3

i =1 j =1

s.t.

For BTP

ij

= si , i

ij

= d j , j

j =1
3

x
i =1

ij ij

xij 0, i, j

32

620-362 Applied
Operations
Research
(XpressMP
lecture)

33

620-362 Applied
Operations
Research
(XpressMP
lecture)

Optimisation Statement
This is the easy bit:
If the objective function,
3
4
say TotalCost = cij xij
, is to be
i =1 j =1
minimised, then the following statement is needed:
minimise(TotalCost)

34

620-362 Applied
Operations
Research
(XpressMP
lecture)

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.

Output (contd)
For BTP

35

620-362 Applied
Operations
Research
(XpressMP
lecture)

Another Example
A Bin Packing Problem

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

Packing Problem

IP
formulation

Minimise
1, if item i in bin j
xijBin
= Packing (Contd)
otherwise
0,
1, if bin j is used
yj =
otherwise
0,

qi : size of item i

j =1

s.t. xij y j , i, j
n

x
=
j =1

Q j : capacity of bin j

ij

q x
i =1

i ij

1, i
Q j , j

xij {0,1} , i, j
y j {0,1} , j

Bin Packing (Contd)

xij y j , i, j

y
j =1

x
j =1

ij

= 1, i

q x
i =1

i ij

Q j , j

40

Bin Packing (Contd)

620-362 Applied
Operations
Research
(XpressMP
lecture)

Bin Packing (Contd)


if condition then
Action_if
else
Action_otherwise
end-if

if condition then
Action_if
end-if

Bin Packing (contd)


forall(i in LIST) do
Statement
end-do

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).

43

Bin Packing (Contd)

620-362 Applied
Operations
Research
(XpressMP
lecture)

44

Bin Packing (Contd)

Bin Packing (Contd) - Debugging


If there are errors in your Mosel code, e.g. instead of
forall, you typed Forall (recall that Mosel is casesensitive), 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.

Bin Packing (Contd) - 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.

Вам также может понравиться