-
Notifications
You must be signed in to change notification settings - Fork 2
/
shift_schedule.py
184 lines (153 loc) · 5.55 KB
/
shift_schedule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""Constraint programming for employee scheduling."""
from ortools.sat.python import cp_model
model = cp_model.CpModel()
# The employees and the roles they are qualified for
employees = {"Phil": ["Restocker"],
"Emma": ["Cashier", "Restocker"],
"David": ["Cashier", "Restocker"],
"Rebecca": ["Cashier"]}
# List of days for the schedule
days = ["Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"]
# List of shifts in a day
shifts = ["Morning",
"Afternoon",
"Evening"]
# List of possible roles
roles = ["Cashier",
"Restocker"]
# `schedule[e][r][d][s]` indicates if employee `e`
# works role `r` on day `d` during shift `s`
schedule = {e:
{r:
{d:
{s: model.new_bool_var(f"schedule_{e}_{r}_{d}_{s}")
for s in shifts}
for d in days}
for r in roles}
for e in employees}
# A cashier has to be present at all times
for d in days:
for s in shifts:
model.add(sum(schedule[e]["Cashier"][d][s]
for e in employees)
== 1)
# We need a restocker once per day
for d in days:
model.add(sum(schedule[e]["Restocker"][d][s]
for e in employees
for s in shifts)
== 1)
# Two restocking shifts should not be adjacent
for i in range(len(days)-1):
model.add(sum(schedule[e]["Restocker"][days[i]]["Evening"] +
schedule[e]["Restocker"][days[i+1]]["Morning"]
for e in employees)
<= 1)
# An employee can only work one role per shift
for e in employees:
for d in days:
for s in shifts:
model.add(sum(schedule[e][r][d][s]
for r in roles)
<= 1)
# An employee can only be assigned 8 hours of work
# per day (with no idle time in-between shifts)
for e in employees:
for d in days:
model.add(sum(schedule[e][r][d]["Morning"] +
schedule[e][r][d]["Evening"]
for r in roles)
<= 1)
# Some employees are not qualified for certain roles
for e in employees:
for r in roles:
for d in days:
for s in shifts:
if r not in employees[e]:
model.add(schedule[e][r][d][s] == 0)
# Do not assign more than 10 shifts to the same employee in the same week
for e in employees:
model.add(sum(schedule[e][r][d][s]
for r in roles
for d in days
for s in shifts)
<= 10)
# Phil must work 4 shifts per week
model.add(sum(schedule["Phil"][r][d][s]
for r in roles
for d in days
for s in shifts)
== 4)
# Phil cannot work during the day from Monday to Friday
model.add(sum(schedule["Phil"][r][d][s]
for r in roles
for d in days if d not in ["Saturday", "Sunday"]
for s in shifts if s in ["Morning", "Afternoon"])
== 0)
# Don't assign Phil and Emma to the same shifts
for d in days:
for s in shifts:
model.add(sum(schedule[e][r][d][s]
for e in ["Phil", "Emma"]
for r in roles)
<= 1)
# Assign the weekend shifts equally between all employees
for e in employees:
model.add(sum(schedule[e][r][d][s]
for r in roles
for d in ["Saturday", "Sunday"]
for s in shifts)
== 2)
# Emma doesn't work from Monday to Wednesday
model.add(sum(schedule["Emma"][r][d][s]
for r in roles
for d in ["Monday", "Tuesday", "Wednesday"]
for s in shifts)
== 0)
# `total_shifts[e]` indicates the number of shifts assigned to employee `e`
total_shifts = {e: model.new_int_var(0, 10, f"total_shifts_{e}")
for e in employees}
# Link `total_shifts` and `schedule`
for e in employees:
model.add(total_shifts[e] == sum(schedule[e][r][d][s]
for r in roles
for d in days
for s in shifts))
# `min_shifts` and `max_shifts` indicate the minimum and
# maximum number of shifts assigned to any employee
min_shifts = model.new_int_var(0, 10, "min_shifts")
max_shifts = model.new_int_var(0, 10, "max_shifts")
# Link `min_shifts`/`max_shifts` and `total_shifts`
model.add_min_equality(min_shifts, [total_shifts[e] for e in employees if e != "Phil"])
model.add_max_equality(max_shifts, [total_shifts[e] for e in employees if e != "Phil"])
# Objective: Distribute the shifts fairly between employees
model.minimize(max_shifts - min_shifts)
# Solve the model
solver = cp_model.CpSolver()
solver.solve(model)
# Print the solution
print(f"{' '*10} | " +
" | ".join([f"{d:^9}" for d in days]) +
" | Total |")
print(f"{' '*10} | " +
' | '.join([f"M | A | E" for d in range(len(days))]) +
" | |")
for e in employees:
shifts_worked = sum([solver.value(schedule[e][r][d][s])
for r in roles
for s in shifts
for d in days])
print(f"{e:<10} | " +
' | '.join(["C" if solver.value(schedule[e]["Cashier"][d][s]) == 1 else
"R" if solver.value(schedule[e]["Restocker"][d][s]) == 1 else " "
for d in days
for s in shifts]) +
" | " +
f"{shifts_worked:^5}" +
" | ")