Back to Homepage
Operations Research
Programming Languages
Course requirements
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14

Lesson 2
Sums, forall constraints, multiperiod model


I. Mosel model: Indices, Sums, Forall constrainst

Download the Lesson2_Bar.mos Mosel model from here

The mmsystem module provides date and time functions.
Arrays can be initialized with ::.

The following syntax can be used to describe a "forall" condition:
forall(a in A) material(a) <= limit(a)

This is equivalent to:
$$\forall a \in A:\quad \text{material}(a) \le \text{limit}(a)$$

The following syntax can be used to describe a "sum" over an index:
sum(a in A) material(a)

This is equivalent to:
$$\sum_{a \in A} \text{material}(a)$$

Constraints can be named, but it isn't necessary:
REVENUE := sum(d in drinks) sellprice(d)*production(d)
sum(d in drinks) sellprice(d)*production(d)
The above two lines are functionally identical, but the first line can be referred to later.

I./1. Excerise 1

a) Run the model and read the output. Which constraint did we run into?
b) Change the amount of money we start with from 8000 to 10000 and rerun the model. Which constraint(s) did we run into now?
c) Change the amount of money back from 10000 to 8000. Test the speed of the model with and without the forall(d in drinks) production(d) is_integer constraint. Which do you expect to be faster? Which is faster in reality?
d) Add the following constraint to the model: Due to a contract, we have to use at least 10 (kg) of mint and at least 50 (kg) combined orange juice and pineapple juice. Rerun the model with integer constraints. Help: blending(d,11) refers to the element in the d'th row and 11th column of the blending matrix. The 11th column lists orange juice usage (12 for pineapple juice, 13 for mint).
e) Add the following constraint to the model: Due to government regulation, we have to sell at least one fifth as much honey as sugar. (Sugar is index 5, honey is index 6.)

II. Multiperiod model: Working with indices & more complex constraints

Download the Lesson2_Refinery.mos Mosel model from here

In this model, we are running an oil refinery. We are pre-planning our production for the upcoming 5 months.

II./1. Excercise 2

a) Run the model and view the output. (It only shows solutions which are non-zero.)
b) What do the following two constraints mean? How do they differ? Which one did we inlcude in the model and why?
$$ b1)\ \forall p \in \text{ PRODUCTS }:\quad \text{ production}(p) = \sum_{r \in \text{ RESOURCES}} \text{ buy}(r) \text{ blending}(p,r)$$ $$ b2)\ \forall r \in \text{ RESOURCES }:\quad \text{ buy}(r) = \sum_{p \in \text{ PRODUCTS}} \text{ production}(p) \text{ blending}(p,r)$$ c) Model the following constraint: In Period 3, due to a planned shutdown, we aren't able to produce any hydrogen. Moreover, in Periods 2 and 3 we will renovate our pressurized storage facility, so we aren't able to store any propane-butane gas and toluene-xylene mixture.
d) Model the following constraint: We need to build up our petrochemistry supply, so by the end of the 5th Period, we need at least 50000 kg polyethylene and 70000 kg polypropylene in our storage.
e) Model the following capability: We are now able to store each input resource in our warehouse (not just output products). The maximum storage capacities can be found in maxStorageRes and the storage cost of reach resource can be found in storageCostRes. Hint: You will need to make an array of decision variables (mpvar) for the amount of storage of each resource in each period, e.g. storageRes. You will then need to make/modify some constraints.

III. Mosel functions

Functions with return variables:

function getDayOfWeek(day: date, isLeapYear: boolean): string
 ...
 returned:=weekday(d)
end-function

Functions without return variables:

procedure readData(excelLocation: string, saveData: boolean, dataInstances: integer)
 ...
end-procedure

Forward procedures (procedures defined later in the code):

forward procedure readData
...
readData("c:/users/xy/Documents/mosel_input.xlsx",false,5)
...
procedure readData(excelLocation: string, saveData: boolean, dataInstances: integer)
 ...
end-procedure

Function overloading: Functions and procedures can have the same name if they're using different input types:

function getDayOfWeek(day: date, isLeapYear: boolean): string
 ...
end-function

function getDayOfWeek(day: string, isLeapYear: boolean): string
 ...
end-function

function getDayOfWeek(day: date): string
 ...
end-function

function getDayOfWeek(day: string): string
 ...
end-function

But for example adding a function like this would result in an error, because we already have a function that takes a single string input:

function getDayOfWeek(fullDate: string): string
 ...
end-function