This is a report that I have worked on during my 2nd semester of my master degree. It talks about solving Nurse Rostering Problem using Constraint Programming between open source software (CHOCO) and commercial software (IBM ILOG OPL).
Nurse Rostering Problem - A comparison between Constraint Programming on CHOCO and IBM ILOG
1. Nurse Rostering Problem
(with Constraint Programming on CHOCO and IBM
ILOG OPL)
Chhaily Moun
(55-090238-6006-0)
A report submitted in partial fulfillment of the subject for
Optimization Method (970532)
Department of Software System Engineering
Thai-German Graduate School of Engineering (TGGS)
King Mongkut’s University of Technology North Bangkok
March 01 2013
2. 1
Table of content
Table of content………………………………………………. 1
Table of figure…………………………………………………2
I. Introduction……………………………………………………3
a. Structure of report………………………………………….3
b. Motivation………………………………………………….3
c. Literature review……………………………………………4
d. Scope of study………………………………………………5
II. Solving approach………………………………………………6
a. Constraint Programming Model……………………………6
b. CHOCO NRP Implementation……………………………..7
c. IBM ILOG OPL NRP Implementation…………………….9
III. Experimental result and comparison...………………………..12
Experiment I…………………………………………………..12
Experiment II…………………………………………………13
Experiment III………………………………………………..15
Experiment IV………………………………………………..16
Discussion…………………………………………………….17
IV. Conclusion and future work….……………………………….19
References ……………………………………………………20
Appendix A…………………………………………………...21
Appendix B…………………………………………………...24
3. 2
Table of figure
Figure 1 – Variables and domain variables in CHOCO ………………...7
Figure 2 – Constraint on number of nurses per shift in CHOCO. ………8
Figure 3 – Constraint on one day off every 5 working days in CHOCO..8
Figure 4 – Constraint on different working shift in CHOCO…………...8
Figure 5 – Constraint on number of holiday for each nurse in CHOCO..9
Figure 6 – Optimize the objective in CHOCO………………………….9
Figure 7 – Variables and domain variables in IBM ILOG………………9
Figure 8 – Constraint on number of nurses per shift in IBM ILOG…...10
Figure 9 – Constraint on one day off every 5 working days in IBM
ILOG.. …………………………………………………….10
Figure 10 – Constraint on different working shift in IBM ILOG……..10
Figure 11 - Constraint on number of holiday for each nurse in IBM
ILOG………………………………………………………11
Figure 12 – Optimize the objective in IBM ILOG…………………….11
Figure 13 – CHOCO result for experiment I………………………….12
Figure 14 – IBM ILOG result for experiment I……………………….13
Figure 15 – Experiment I………………………………………………13
Figure 16 – CHOCO result for experiment II…………………………14
Figure 17 – IBM ILOG result for experiment II………………………14
Figure 18 – Experiment II …………………………………………….14
Figure 19 - CHOCO result for experiment III…………………………15
Figure 20 – IBM ILOG result for experiment III………………………15
Figure 21 – Experiment III…………………………………………….16
Figure 22 – CHOCO result for experiment IV…………………………16
Figure 23 – IBM ILOG result for experiment IV……………………...17
Figure 24 – Experiment IV…………………………………………….17
Figure 25 – Comparison of all results…………………………………18
4. 3
I. Introduction
a. Structure of report
This report is organized into four main parts as follows:
• First part is introduction where I introduce the structure of
the whole report followed by motivation of the problem. In
the motivation section, I introduce the important and
difficulty of the problem (Nurse Rostering Problem). Then I
raise some papers related to Nurse Rostering Problem
methodology to present in literature review section. I cover a
few techniques applied in different solvers. The last section
of the first part is scope of study. There I just limit the
problem that I will use for this investigation.
• In the second part, I start with mathematical model for the
problem with Constraint Programming. Then I have two
solving approaches for the mentioned model. First approach,
I use it on an open source solver. I choose Choco, developed
and distributed by BSD [1, 2]. And I choose another
commercial solver for the second approach. I choose IBM
ILOG CPLEX Optimization Studio, developed and
distributed by IBM [3], for the commercial one.
• In the third part of the report, I present some experiments on
both open source solver and commercial solver with their
corresponding results. And I will make comparison from the
results.
• The last part is about conclusion from the investigation and
the part I could not study on. I also introduce what to be
studied on Nurse Rostering Problem in the future.
b. Motivation
Scheduling nurse in small to medium size hospital is one of the
most important administration activities to run the hospital
efficiently and have all nurses worked equally. Besides its
5. 4
importance, scheduling nurse is a very challenging task to do in
many hospitals since the number of nurses is quite large. Because
of its importance and complexity, this problem has been studied
extensively in both Operational Research and Artificial
Intelligence for more than 40 years (as cited in [4]). And this
problem is well known as Nurse Rostering Problem (NRP). The
problem is NP hard problem in most real world cases [4]. It is
really challenge as there are many different rules to be satisfied to
ensure high quality roster. And it is possible that the problem is
over-constrained when some rules come in conflict.
NRP is problem of generating rosters for all nurses in the hospital
where shifts, holiday, regulations, work practices and specific
nurse preferences are considered in a defined period. The problem
is usually categorized into two groups of constraint: hard and soft
constraint [4].
• Hard constraints refer to those to be satisfied in order
to obtain feasible rosters for use in practice.
• Soft constraints refer to those to be satisfied as much
as possible in order to have a more efficient roster.
To contribute to this problem, I will model the problem based on
Constraint Programming, apply on two different solvers and make
comparison.
c. Literature review
As mentioned in the previous section, NRP has been studied more
than 40 years ago in both OR and AI areas; so there are already a
wide range of techniques used.
Some techniques are based on meta-heuristics (as cited in [4])
which are effective for large scale and complicated problems. In
the meanwhile, meta-heuristics have their own drawback for they
neither produce optimal solutions nor reduce search space.
The others are based on Integer Programming [5]. In [5], the author
used Integer Programming Based Local Search technique applying
on a roster of 30 days with 25 staffs. The result is good somehow
despite the fact that it is not optimal.
6. 5
AI techniques such as CP have also been used largely for NRP. As
cited in [4], it is quite good for solving small scale NRP only. But
it is interesting in [6] where the author applied column generation
scheme with CP and the result is quite good comparing to other
techniques. Anyway, it is expensive to solve too many constraints
using CP.
The others techniques which have been studied recently are
hybrids such as IP-hybrid, CP-hybrid. Once again they are
effective for many constraints with expensive cost.
d. Scope of study
To investigate on NRP for an effective solution requires much
effort since the problem is quite complex. And many studies have
been conducted and the results tend to be better from time to time
regardless of cost. On the one hand, small and medium size
hospitals are unlikely to pay for those high cost techniques and the
problems have fewer constraints. For this reason plus I have limit
time for this study; I choose to study this problem using Constraint
Programming. As mentioned in the previous section, CP can be
used to effectively schedule small scale problems. From the CP
model, I conduct some experiments using CHOCO and IBM ILOG
OPL solver. I also make comparison of the experiment results from
both solvers.
Here, I choose CHOCO solver because it is open source and
suitable for small and medium size hospital. Then I choose IBM
ILOG OPL solver, a commercial product, with expectation that
commercial product will perform better and to some distance we
may need to use commercial product in case it is better.
7. 6
II. Solving approach
In this section, I introduce CP mathematical model for NRP. I then
implement it on CHOCO and IBM ILOG CPLEX optimization studio.
The NRP I have here is to assign a set of nurses to four shifts (i.e. Off,
Morning, Afternoon, Night) on a period of 30 days. All nurses need to
have equivalent working shifts and holiday. The schedule need 4 nurses,
2 nurses and 2 nurses for morning, afternoon and evening shifts
correspondingly. In the problem, we also take previous period holiday
into account in the current period so as to have balance working and
holiday for all nurses. Also each nurse cannot work on the same shift for
more than 2 days consecutively. Then the holiday for each nurse must
spread approximately equally during the period (i.e. around 6 holidays
per 30 days period with 10 nurses).
a. Constraint Programming Model
Different instances of the problem are defined as follows:
• N: set of nurses
• n: number of nurses
• S: set of shift type S = {0(off), 1(morning), 2(afternoon),
3(evening)}
• d: number of days during the period
• P: set of shift pattern per day P = {2(Off), 4(Morning),
2(Afternoon), 2(Night)} for 10 nurses rostering
• rij ∈ S: roster for day i and nurse j; i ∈ p; j ∈ n
• Offseti: the number of offset which is more or less than
average holiday for nurse I; i ∈ n
Variables:
R = {rij | i ∈ d, j ∈ n }
Domains of variable:
S(R) = {S(r11), …, S(rpn)}; rij ∈ S; i ∈ d; j ∈ n
Constraints:
§ Set number of nurses per shift
count(Ri, sj) = pj; ∀ i ∈ d, sj∈ S, pj ∈ P, j ∈ 3
8. 7
§ Set approximately 1 day off in every 5 days
5 <= 𝒓𝒊𝒋
𝟓
𝒊!𝟏
𝒏
𝒋!𝟏 <=8
5 <= 𝒓𝒊𝒋
𝟏𝟎
𝒊!𝟔
𝒏
𝒋!𝟏 <=8
5 <= 𝒓𝒊𝒋
𝟏𝟓
𝒊!𝟏𝟏
𝒏
𝒋!𝟏 <=8
5 <= 𝒓𝒊𝒋
𝟐𝟎
𝒊!𝟏𝟔
𝒏
𝒋!𝟏 <=8
5 <= 𝒓𝒊𝒋
𝟐𝟓
𝒊!𝟐𝟏
𝒏
𝒋!𝟏 <=8
5 <= 𝒓𝒊𝒋
𝟑𝟎
𝒊!𝟐𝟔
𝒏
𝒋!𝟏 <=8
§ Each nurse cannot work on the same shift for more than 2
days consecutively
rij != r(i+1)j ,∀ i ∈ d%2; j ∈
n
§ Balance number of holiday for each nurse per period by
taking previous month holiday into account
0 + Offseti <= count(𝑹𝒊
𝑻
, s0) <= 10 + Offseti
§ Minimize the difference between offset of each nurse
Minimize Max(Offset) – Min(Offset)
b. CHOCO NRP Implementation
Variables & domains of variable:
Figure
1
-‐
Variables
and
domain
variables
in
CHOCO
9. 8
Constraints:
§ Set number of nurses per shift
Figure
2
-‐
Constraint
on
number
of
nurses
per
shift
in
CHOCO
§ Set approximately 1 day off in every 5 days
Figure
3
-‐
Constraint
on
one
day
off
every
5
working
days
in
CHOCO
§ Each nurse cannot work on the same shift for more than 2
days consecutively
Figure
4
-‐
Constraint
on
different
working
shift
in
CHOCO
10. 9
§ Balance number of holiday for each nurse per period by
taking previous month holiday into account
Figure
5
-‐
Constraint
on
number
of
holiday
for
each
nurse
in
CHOCO
§ Minimize the difference between offset of each nurse
Figure
6
-‐
Optimize
the
objective
in
CHOCO
For the whole runnable code, please go to appendix A.
c. IBM ILOG OPL NRP Implementation
Variables & domains of variable:
Figure
7
-‐
Variables
and
domain
of
variables
in
IBM
ILOG
11. 10
Constraints:
§ Set number of nurses per shift
Figure
8
-‐
Constraint
on
number
of
nurses
per
shift
in
IBM
ILOG
§ Set approximately 1 day off in every 5 days
Figure
9
-‐
Constraint
on
one
day
off
every
5
working
days
in
IBM
ILOG
§ Each nurse cannot work on the same shift for more than 2
days consecutively
Figure
10
-‐
Constraint
on
different
working
shift
in
IBM
ILOG
12. 11
§ Balance number of holiday for each nurse per period by
taking previous month holiday into account
Figure
11
-‐
Constraint
on
number
of
holiday
for
each
nurse
in
IBM
ILOG
§ Minimize the difference between offset of each nurse
Figure
12
-‐
Optimize
the
objective
in
IBM
ILOG
For the whole runnable code in IBM ILOG OPL, please go to
appendix B.
13. 12
III. Experimental result and comparison
To evaluate the models above and make comparison between
commercial and free solver, I conduct four different experiments on
both CHOCO and IBM ILOG OPL implementations.
The results from each solver are displayed in grid; each row represents
roster for each nurse and each column represents roster for each day.
The intersection between row and column is the shift on the schedule
where ‘M’ denotes morning shift, ‘A’ denotes afternoon shift, ‘N’
denotes night shift and ‘O’ denotes off. The column next to the last
one shows the number of holiday offset for each nurse from the
previous month and the last column shows the holiday offset for the
current schedule.
Experiment I
In the first experiment, I choose the problem with 10 nurses to be
assigned on 30 days period and each nurse has 0 holiday offset from
previous month.
Following figures show the result from both CHOCO and IBM ILOG.
Figure
13
-‐
CHOCO
results
for
experiment
I
14. 13
Figure
14
-‐
IBM
ILOG
result
for
experiment
I
Figure
15
-‐
Experiment
I
Experiment II
In the second experiment, I choose the problem with 10 nurses to be
assigned on 30 days period with the holiday offset vary from -4 to 4
for each nurse.
Following figures show the result from both CHOCO and IBM ILOG.
15. 14
Figure
16
-‐
CHOCO
result
for
experiment
II
Figure
17
-‐
IBM
ILOG
result
for
experiment
II
Figure
18
-‐
Experiment
II
16. 15
Experiment III
In the third experiment, I increase the problem size to 12 nurses to be
assigned on 30 days period and each nurse has 0 holiday offset from
previous month.
Following figures show the result from both CHOCO and IBM ILOG.
Figure
19
-‐
CHOCO
result
for
experiment
III
Figure
20
-‐
IBM
ILOG
result
for
experiment
III
17. 16
Figure
21
-‐
Experiment
III
Experiment IV
In the fourth experiment, I choose the problem with 12 nurses to be
assigned on 30 days period and once again the holiday offset vary
from -4 to 4.
Following figures show the result from both CHOCO and IBM ILOG.
Figure
22
-‐
CHOCO
result
for
experiment
IV
18. 17
Figure
23
-‐
IBM
ILOG
result
for
experiment
IV
Figure
24
-‐
Experiment
IV
Discussion
From the four experiments, we can see that IBM ILOG performs
much better than CHOCO. IBM ILOG always find better solution and
spend shorter time to computer. We cannot get perfect results from
CHOCO because it always take long time to get the best results and I
will just set the time limit for a while until the result is better and
enough for comparison.
19. 18
Figure
25
-‐
Comparison
of
all
experiment
Comparing between experiment I and III, we can find out that smaller
problem could compute faster than a larger one with either free or
commercial solver.
Then if we take a look at experiment II and IV, we are surprised to see
that the larger problem computer faster than the smaller one since the
large problem allows more relaxation.
Another comparson should be made either between experiment I and
II or between experiment III and IV, the result shows that the problem
with offset equal to zero compute faster than the one with vary offset.
20. 19
IV. Conclusion and future work
To conclude, the result is acceptable for small and medium size
hospital. It takes time for large data set, especially with CHOCO. I
believe that the model is not so efficient comparing to some recent
publications with newly found algorithms but I am satisfied with the
results produced since I lack both time and experience for this study.
By the end of this study, I could have some preliminary idea for more
works to be conducted. To continue working on this problem, I
might need to optimize the model to cope with large practical data.
The first idea that comes to my mind for optimizing the model is to
have it on Integer Programming and possibly hybrid CP-IP model.
21. 20
References
[1] CHOCO, http://www.emn.fr/z-info/choco-solver/
[2] Berkeley Software Distribution [BSD], “CHOCO Documentation”,
Apr. 2010.
[3] International Business Machines Corporation [IBM], “IBM ILOG
OPL Language User’s Manual”, 2009.
[4] Rong Qu and Fang He, “A Hybrid Constraint Programming Approach
for Nurse Rostering Problems”, School of Computer Science,
University of Nottingham, UK.
[5] Seiya Hasegawa and Yukio Kosugi, “Solving Nurse Scheduling
Problem by Integer-Programming-Based Local Search”, IEEE
International Conference on Systesms, Man, and Cybernetics, pp.
1474-1480, October 2006. (In Taipei, Taiwan).
[6] Fang He and Rong Qu, “A Constraint Programming based Column
Generation Approach to Nurse Rostering Problems”, School of
Computer Science, University of Nottingham, UK.
22. 21
Appendix A
The following is code that I implement in CHOCO for Nurse Rostering
Problem I modeled in Constraint Programming.
/******************************************************************
* CHOCO
* Constraint Programming Model for NRP
* Author: Chhaily Moun, MSc candidate, Software Systems Engineering
* Creation Date: Feb 15, 2013
*******************************************************************/
import choco.Choco;
import choco.Options;
import choco.cp.model.CPModel;
import choco.cp.solver.CPSolver;
import choco.kernel.model.Model;
import choco.kernel.model.variables.integer.IntegerExpressionVariable;
import choco.kernel.model.variables.integer.IntegerVariable;
import choco.kernel.solver.Solver;
public class NRPSolution {
public NRPSolution(){
int period = 30;
int numNurse = 10;
final int MShift = 1;
final int AShift = 2;
final int NShift = 3;
int []pattern = {4,4,2,2};//Off Morning Afternoon Evening
int off[]={4,-4,4,4,4,-4,4,-4,-4,-4};//{0,0,0,0,0,0,0,0,0,0};
//Variable declaration
String shift[] = {"O","M","A","N"};
int numShifts = shift.length-1;
//Decision variable
IntegerVariable[][] roster = Choco.makeIntVarArray("roster",
period,numNurse, 0, numShifts);
IntegerExpressionVariable [][]T =
Choco.makeIntVarArray("transpose", numNurse, period);
Model m = new CPModel();
23. 22
// Set number of nurses per shift
for(int i = 0; i < period; i++){
IntegerVariable Mocc = Choco.makeIntVar("Morning" + i,
pattern[1],pattern[1]);
m.addConstraint(Choco.occurrence(Mocc,(IntegerVariable[])roster[i],MSh
ift));
IntegerVariable Aocc = Choco.makeIntVar("Afternoon" + i, pattern[2],
pattern[2]);
m.addConstraint(Choco.occurrence(Aocc,(IntegerVariable[])roster[i],NSh
ift));
IntegerVariable Nocc = Choco.makeIntVar("Night" + i,
pattern[3],pattern[3]);
m.addConstraint(Choco.occurrence(Nocc,(IntegerVariable[])roster[i],ASh
ift));
}
//Constraint for changing pattern
for(int i = 0; i < numNurse; i++){
IntegerVariable total = Choco.makeIntVar("shift_" +
(i+1), 5, 8);
m.addConstraint(Choco.eq(total,Choco.sum(roster[1][i],roster[2][i],ros
ter[3][i],roster[4][i],roster[0][i])));
m.addConstraint(Choco.eq(total,Choco.sum(roster[6][i],roster[7][i],ros
ter[8][i],roster[9][i],roster[5][i])));
m.addConstraint(Choco.eq(total,Choco.sum(roster[11][i],roster[12][i],r
oster[13][i],roster[14][i],roster[10][i])));
m.addConstraint(Choco.eq(total,Choco.sum(roster[15][i],roster[16][i],r
oster[17][i],roster[18][i],roster[19][i])));
m.addConstraint(Choco.eq(total,Choco.sum(roster[20][i],roster[21][i],r
oster[22][i],roster[23][i],roster[24][i])));
m.addConstraint(Choco.eq(total,Choco.sum(roster[25][i],roster[26][i],r
oster[27][i],roster[28][i],roster[29][i])));
//Make each nurse has shift changes
for(int j = 0; j < period-1; j+=2){
m.addConstraint(Choco.allDifferent(roster[j][i],roster[j+1][i]));
}
24. 23
}
for(int i = 0; i < period; i++)
for(int j = 0; j < numNurse; j++){
T[j][i] = roster[i][j];
}
IntegerVariable Offset[] = new IntegerVariable[numNurse];
for(int i = 0; i < numNurse; i++){
//Constraint on off days per months
Offset[i] = Choco.makeIntVar("OFF",5 + off[i], 10 + off[i] );
m.addConstraint(Choco.occurrence(Offset[i],(IntegerVariable[])T[i],0);
}
IntegerVariable obj =
Choco.makeIntVar("Obj",Options.V_OBJECTIVE);
m.addVariable(obj);
m.addConstraint(Choco.eq(obj,
Choco.minus(Choco.max(Offset),Choco.min(Offset))));
//Create solver s
Solver s = new CPSolver();
s.setTimeLimit(5040000);
s.read(m);
s.minimize(s.getVar(obj),true);
int []cHoliday = new int[numNurse];
System.out.println("Time taken: " + s.getTimeCount());
System.out.println("Number of nurses: " + numNurse);
System.out.println("Length of schedule: " + period);
System.out.println("Nurse roster:");
for(int j = 0; j < numNurse; j++){
System.out.print("Nurse " + (j+1) + ": ");
for(int i = 0; i < period; i++){
if(s.getVar(roster[i][j]).getVal() == 0){
cHoliday[j]+=1;
}
System.out.print(shift[s.getVar(roster[i][j]).getVal()]+" ");
}
System.out.println("t" + off[j] + "t" + (cHoliday[j] - 6 +
off[j]));
}
}
public static void main(String[]arg){
new NRPSolution();
}
25. 24
}
Appendix B
The following is code that I implement in IBM ILOG OPL for Nurse
Rostering Problem I modeled in Constraint Programming.
/*********************************************
* OPL 12.4 Model
* Constraint Programming Model for NRP
* Author: CHHAILY
* Creation Date: Feb 22, 2013 at 9:46:18 AM
*********************************************/
using CP;
range sRange = 0..3;
string shift [sRange] = ["O", "M", "A", "N"];
int OShift = 0;
int MShift = 1;
int AShift = 2;
int NShift = 3;
int cHoliday = 0;
int period = 30;
int numNurse = 10;
int pattern [sRange]=[4,4,2,2];
range days = 1..period;
range nurses = 1..numNurse;
int off[nurses]=[4,-4,4,4,4,-4,4,-4,-4,-4];//[0,0,0,0,0,0,0,0,0,0];
dvar int roster[days][nurses] in sRange;
dvar int offset[nurses] in 0..30;
dvar int obj;
execute{
var p = cp.param;
p.timeLimit = 1000;
}
minimize obj;
subject to{
//Assign number of nurses per shift
forall(i in days){
count(all(n in nurses) roster[i][n],MShift) == pattern[1];
count(all(n in nurses) roster[i][n],AShift) == pattern[2];
count(all(n in nurses) roster[i][n],NShift) == pattern[3];
}
forall(n in nurses){
4 <= sum(d in 1..5) roster[d][n] <= 8;
4 <= sum(d in 6..10) roster[d][n] <= 8;
4 <= sum(d in 11..15) roster[d][n] <= 8;
4 <= sum(d in 16..20) roster[d][n] <= 8;
4 <= sum(d in 21..25) roster[d][n] <= 8;