Lesson 2
Sums, forall constraints, multiperiod model
I. Mosel model: Indices, Sums, Forall constrainst
Download the Lesson2_Bar.mos Mosel model from hereThe 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 hereIn this model, we are running an oil refinery. We are pre-planning our production for the upcoming 5 months.
- We can sell the following products: 95% motor fuel, 98% motor fuel, JET fuel, fuel oil, propane-butane gas, toluene-xylene mixture, hydrogen, asphalt, polyethylene, polypropylene.
- We use the following resources to produce them: crude oil, natural gas, electricity, water, sulphur, molybdenum, zeolites, rhenium, platinum, resin. (The blending chart in specified in the Mosel model.)
- Each resource and product has a pre-defined buy price and sell cost for each period.
- We can also store each of our products in our warehouse. We have a limited capacity for each product, and storing them costs money.
- We have 10 million USD to work with and would like to produce the most profit possible.
- In reality, oil companies only use the first period of the plan, and in the following periods they adjust and rerun the model with more accurate data for that period. The reason for running a multi-period model is to make sure we utilize our storage capability. (Otherwise an LP model would get rid of all products in the first period in order to maximize profit.)
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