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 9
Constraint Programming


I. Mosel model: Sudoku

Download the Lesson9_Sudoku.mos Mosel model from here

Download the sudoku.dat Data file from here

We are solving a Sudoku with given numbers in the givenNumbers array. This is a sparse (dynamic) array, so its values are stored as an index + value pair, where each index is a (row, column) pair.

I/1. How to run the program

We are using a different solver now (XPress Kalis), which only allows for 50 decision variables with the free license. But our sudoku model has 9x9=81 cells, and 9 possibilities (binary variables) for each, which is 9x9x9=729 binary decision variables.
  1. Connect to the university server with WinSCP: leibniz.math.bme.hu, use your university account name and password.
  2. Copy the Lesson9_Sudoku.mos and sudoku.dat files to your university account file system. (Don't go into any subfolder, it makes the code execution a bit harder.)
  3. Connect to leibniz.math.bme.hu again with PuTTY. Again you'll need your university account name and password.
  4. Use the following commands to run your mosel file:

        source /usr/local/bin/xpvars.sh
        mosel Lesson9_Sudoku.mos

(You can right click to paste these commands.)

II. Constraint programming capabilities

On the surface this is similar to an IP model:
But there are other capabilities:
And so on. You can read about further capabilities in

<install location>/xpressmp/docs/solver/kalis/kalis_ug/kalisug.pdf
<install location>/xpressmp/docs/solver/kalis/kalis_ref/kalisref.pdf

II/1. Exercise 1

a) Find all the Constraint Programming capabilities in Lesson9_Sudoku.mos!
b) Start a new Mosel model! Set up the following 4 by 4 Sudoku pattern and solve it with kalis:


III. Scheduling Problems

This type of problem is a large topic in Combinatorial Optimization. The question is the following: Without precedence, and with the minimax objective function, this is very closely related to the multi-knapsack problem:


However in reality, a precedence graph is almost always required.

III./1. Simple Scheduling Problem in Mosel

Download the Lesson9_Scheduling.mos Mosel model from here

There are special scheduling parameters defined in XPress Kalis to make modelling easier:

III./2. Exercise 2

a) Find all the scheduling parameters in Lesson9_Scheduling.mos!
b) What would happen if we were to use 4 unary machines instead of 1 with 4 threads? We can accomplish this by changing res to res: array(1..M) of cpresource in declarations, and using 4 unary resources instead:

forall(m in 1..M) set_resource_attributes(res(m), KALIS_UNARY_RESOURCE, 1)

Moreover, we would need to change res to res(m) in the following for-loop, and add m in 1..M to the loop as well! How would this affect the solution?
c) Change back to 1 machine with 4 threads. (You can press undo as many times as you need.) Solve the same problem without using the set_task_attributes function! You will need to replace it with some other functions!
d) Add the following constraint: There is a planned shutdown 20 units of time after we're starting, lasting 3 units of time, affecting 2 of our machines. These machines won't be functioning. Any task started on these machines before the shutdown has to be finished before the shutdown. (Hint: Use 2 dummy jobs with set starting times and ending times!)
e) Add the following constraint: We cannot work on tasks that take at least 20 units of time between 24 and 26 units of time after starting, because of a high risk of an unexpected shutdown. (Hint: Set the domain of these tasks! The setdomain function also works with setdomain(x:cpvar, domain:set of integers).)

III./3. Multi-type machine Scheduling Problem in Mosel

Download the Lesson9_MultitypeScheduling.mos Mosel model from here

Download the multitype_scheduling.dat Data file from here

In this model, we've added the constraint that each job can only be done on a specific type of machine. For example, there are excavation jobs, piling vehicle driver jobs, carpentry jobs, concrete mixing jobs, etc.