.
This commit is contained in:
+344
-139
@@ -162,7 +162,6 @@ class RotaBuilder(object):
|
||||
):
|
||||
|
||||
print(f"Start time: {datetime.datetime.now()}")
|
||||
print(f"Weeks to rota: {weeks_to_rota}")
|
||||
print(f"Use previous shifts {use_previous_shifts}")
|
||||
print(f"Use previous shifts (balance_extra) {use_shift_balance_extra}")
|
||||
|
||||
@@ -188,6 +187,10 @@ class RotaBuilder(object):
|
||||
self.rota_days_length = len(self.weeks) * 7
|
||||
self.rota_end_date = self.start_date + datetime.timedelta(self.rota_days_length)
|
||||
|
||||
print(
|
||||
f"Weeks to rota: {weeks_to_rota} ({self.start_date} - {self.rota_end_date})"
|
||||
)
|
||||
|
||||
self.unavailable_to_work = set()
|
||||
self.unavailable_to_work_reason = {}
|
||||
self.pref_not_to_work = {}
|
||||
@@ -205,7 +208,6 @@ class RotaBuilder(object):
|
||||
self.balance_offset_modifier = balance_offset_modifier
|
||||
self.ltft_balance_offset = ltft_balance_offset
|
||||
|
||||
|
||||
self.constraint_options = {
|
||||
"balance_nights": True,
|
||||
"constrain_time_off_after_nights": False,
|
||||
@@ -221,7 +223,7 @@ class RotaBuilder(object):
|
||||
# Don't assign multiple shifts every (n) weeks
|
||||
"max_weekend_frequency": 1, # Requires balance weekends
|
||||
# Don't assign multiple shifts every (n) weeks
|
||||
"max_night_frequency": 4,
|
||||
"max_night_frequency": 1,
|
||||
"max_shifts_per_week": 7,
|
||||
"max_shifts_per_month": 40,
|
||||
# The following will cause an unsolvable problem if a shift
|
||||
@@ -635,9 +637,8 @@ class RotaBuilder(object):
|
||||
else:
|
||||
work_request_sets.add(work_request[3])
|
||||
|
||||
|
||||
# Validate shifts are valid
|
||||
#work_request_sets = set([i[3] for i in self.work_requests])
|
||||
# work_request_sets = set([i[3] for i in self.work_requests])
|
||||
invalid_shifts = work_request_sets - set(self.get_shift_names())
|
||||
if invalid_shifts:
|
||||
raise InvalidShift(
|
||||
@@ -1433,32 +1434,36 @@ class RotaBuilder(object):
|
||||
)
|
||||
|
||||
if self.constraint_options["balance_blocks"]:
|
||||
for week_blocks in self.get_week_block_iterator(
|
||||
self.constraint_options["max_night_frequency"]
|
||||
):
|
||||
if self.get_shifts_with_constraint("night"):
|
||||
# Prevent nights more than once every n weeks
|
||||
self.model.constraints.add(
|
||||
1
|
||||
>= sum(
|
||||
self.model.shift_week_worker_assigned[
|
||||
shift.name, week, worker.id
|
||||
]
|
||||
for week in week_blocks
|
||||
for shift in self.get_shifts_with_constraint("night")
|
||||
if self.constraint_options["max_night_frequency"]:
|
||||
for week_blocks in self.get_week_block_iterator(
|
||||
self.constraint_options["max_night_frequency"]
|
||||
):
|
||||
if self.get_shifts_with_constraint("night"):
|
||||
# Prevent nights more than once every n weeks
|
||||
self.model.constraints.add(
|
||||
1
|
||||
>= sum(
|
||||
self.model.shift_week_worker_assigned[
|
||||
shift.name, week, worker.id
|
||||
]
|
||||
for week in week_blocks
|
||||
for shift in self.get_shifts_with_constraint("night")
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
# if self.constraint_options["balance_weekends"]:
|
||||
for week_blocks in self.get_week_block_iterator(self.constraint_options["max_weekend_frequency"]):
|
||||
# Prevent weekend shifts more than once every n weeks
|
||||
self.model.constraints.add(
|
||||
1
|
||||
>= sum(
|
||||
self.model.works_weekend[worker.id, week]
|
||||
for week in week_blocks
|
||||
if self.constraint_options["max_weekend_frequency"]:
|
||||
for week_blocks in self.get_week_block_iterator(
|
||||
self.constraint_options["max_weekend_frequency"]
|
||||
):
|
||||
# Prevent weekend shifts more than once every n weeks
|
||||
self.model.constraints.add(
|
||||
1
|
||||
>= sum(
|
||||
self.model.works_weekend[worker.id, week]
|
||||
for week in week_blocks
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
for constraint_shift in self.get_shifts_with_constraints(
|
||||
"max_shifts_per_week_block"
|
||||
@@ -1504,7 +1509,6 @@ class RotaBuilder(object):
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
self.model.constraints.add(
|
||||
self.constraint_options["max_shifts_per_week"]
|
||||
>= sum(
|
||||
@@ -1667,7 +1671,7 @@ class RotaBuilder(object):
|
||||
# self.model.shift_week_worker_assigned[s.name, week, worker.id]
|
||||
# for s in self.get_shifts_with_constraint("night")))
|
||||
|
||||
#for shift in self.get_shifts_with_constraint("single_block_per_week"):
|
||||
# for shift in self.get_shifts_with_constraint("single_block_per_week"):
|
||||
# if shift.force_as_block:
|
||||
# # Force nights to be assigned in blocks
|
||||
# # self.model.constraints.add(8* self.model.shift_week_worker_assigned[shift.name, week, worker.id] >= sum(self.model.works[worker.id, week, day, shift.name] for day in self.days)
|
||||
@@ -1709,64 +1713,171 @@ class RotaBuilder(object):
|
||||
|
||||
#
|
||||
|
||||
#def get_pre_may(week_days, max_pre)
|
||||
|
||||
for n in range(len(weeks_days)):
|
||||
|
||||
week, day = weeks_days[n]
|
||||
|
||||
p1 = 1
|
||||
p2 = 1
|
||||
p3 = 1
|
||||
p4 = 1
|
||||
p5 = 1
|
||||
if n > 0:
|
||||
pweek, pday = weeks_days[n - 1]
|
||||
else:
|
||||
p1 = 0
|
||||
pweek, pday = weeks_days[n]
|
||||
pre_map = []
|
||||
for pre_n in range(1, self.max_pre + 1):
|
||||
try:
|
||||
pweek, pday = weeks_days[n - pre_n]
|
||||
p = 1
|
||||
except:
|
||||
pweek, pday = weeks_days[n]
|
||||
p = 0
|
||||
|
||||
try:
|
||||
p2week, p2day = weeks_days[n - 2]
|
||||
except IndexError:
|
||||
p2week, p2day = weeks_days[n]
|
||||
p2 = 0
|
||||
pre_map.append((p, pweek, pday))
|
||||
|
||||
try:
|
||||
p3week, p3day = weeks_days[n - 3]
|
||||
except IndexError:
|
||||
p3week, p3day = weeks_days[n]
|
||||
p3 = 0
|
||||
post_map = []
|
||||
for post_n in range(1, self.max_post + 1):
|
||||
try:
|
||||
pweek, pday = weeks_days[n + post_n]
|
||||
p = 1
|
||||
except:
|
||||
pweek, pday = weeks_days[n]
|
||||
p = 0
|
||||
|
||||
try:
|
||||
p4week, p4day = weeks_days[n - 4]
|
||||
except IndexError:
|
||||
p4week, p4day = weeks_days[n]
|
||||
p4 = 0
|
||||
post_map.append((p, pweek, pday))
|
||||
|
||||
try:
|
||||
p5week, p5day = weeks_days[n - 5]
|
||||
except IndexError:
|
||||
p5week, p5day = weeks_days[n]
|
||||
p5 = 0
|
||||
# p1 = 1
|
||||
# p2 = 1
|
||||
# p3 = 1
|
||||
# p4 = 1
|
||||
# p5 = 1
|
||||
# p6 = 1
|
||||
# p7 = 1
|
||||
# if n > 0:
|
||||
# pweek, pday = weeks_days[n - 1]
|
||||
# else:
|
||||
# p1 = 0
|
||||
# pweek, pday = weeks_days[n]
|
||||
|
||||
n1 = 1
|
||||
n2 = 1
|
||||
try:
|
||||
nweek, nday = weeks_days[n + 1]
|
||||
except IndexError:
|
||||
nweek, nday = weeks_days[n]
|
||||
n1 = 0
|
||||
# try:
|
||||
# p2week, p2day = weeks_days[n - 2]
|
||||
# except IndexError:
|
||||
# p2week, p2day = weeks_days[n]
|
||||
# p2 = 0
|
||||
|
||||
try:
|
||||
n2week, n2day = weeks_days[n + 2]
|
||||
except IndexError:
|
||||
n2week, n2day = weeks_days[n]
|
||||
n2 = 0
|
||||
# try:
|
||||
# p3week, p3day = weeks_days[n - 3]
|
||||
# except IndexError:
|
||||
# p3week, p3day = weeks_days[n]
|
||||
# p3 = 0
|
||||
|
||||
try:
|
||||
n3week, n3day = weeks_days[n + 3]
|
||||
except IndexError:
|
||||
n3week, n3day = weeks_days[n]
|
||||
n3 = 0
|
||||
# try:
|
||||
# p4week, p4day = weeks_days[n - 4]
|
||||
# except IndexError:
|
||||
# p4week, p4day = weeks_days[n]
|
||||
# p4 = 0
|
||||
|
||||
# try:
|
||||
# p5week, p5day = weeks_days[n - 5]
|
||||
# except IndexError:
|
||||
# p5week, p5day = weeks_days[n]
|
||||
# p5 = 0
|
||||
|
||||
# try:
|
||||
# p6week, p6day = weeks_days[n - 6]
|
||||
# except IndexError:
|
||||
# p6week, p6day = weeks_days[n]
|
||||
# p6 = 0
|
||||
|
||||
# try:
|
||||
# p7week, p7day = weeks_days[n - 7]
|
||||
# except IndexError:
|
||||
# p7week, p7day = weeks_days[n]
|
||||
# p7 = 0
|
||||
|
||||
# pre_map = [
|
||||
# (p1, pweek, pday),
|
||||
# (p2, p2week, p2day),
|
||||
# (p3, p3week, p3day),
|
||||
# (p4, p4week, p4day),
|
||||
# (p5, p5week, p5day),
|
||||
# (p6, p6week, p6day),
|
||||
# (p7, p7week, p7day),
|
||||
# ]
|
||||
|
||||
# n1 = 1
|
||||
# n2 = 1
|
||||
# n3 = 1
|
||||
# n4 = 1
|
||||
# n5 = 1
|
||||
# n6 = 1
|
||||
# n7 = 1
|
||||
# if n > 0:
|
||||
# nweek, nday = weeks_days[n - 1]
|
||||
# else:
|
||||
# n1 = 0
|
||||
# nweek, nday = weeks_days[n]
|
||||
|
||||
# try:
|
||||
# n2week, n2day = weeks_days[n - 2]
|
||||
# except IndexError:
|
||||
# n2week, n2day = weeks_days[n]
|
||||
# n2 = 0
|
||||
|
||||
# try:
|
||||
# n3week, n3day = weeks_days[n - 3]
|
||||
# except IndexError:
|
||||
# n3week, n3day = weeks_days[n]
|
||||
# n3 = 0
|
||||
|
||||
# try:
|
||||
# n4week, n4day = weeks_days[n - 4]
|
||||
# except IndexError:
|
||||
# n4week, n4day = weeks_days[n]
|
||||
# n4 = 0
|
||||
|
||||
# try:
|
||||
# n5week, n5day = weeks_days[n - 5]
|
||||
# except IndexError:
|
||||
# n5week, n5day = weeks_days[n]
|
||||
# n5 = 0
|
||||
|
||||
# try:
|
||||
# n6week, n6day = weeks_days[n - 6]
|
||||
# except IndexError:
|
||||
# n6week, n6day = weeks_days[n]
|
||||
# n6 = 0
|
||||
|
||||
# try:
|
||||
# n7week, n7day = weeks_days[n - 7]
|
||||
# except IndexError:
|
||||
# n7week, n7day = weeks_days[n]
|
||||
# n7 = 0
|
||||
|
||||
# post_map = [
|
||||
# (n1, nweek, nday),
|
||||
# (n2, n2week, n2day),
|
||||
# (n3, n3week, n3day),
|
||||
# (n4, n4week, n4day),
|
||||
# (n5, n5week, n5day),
|
||||
# (n6, n6week, n6day),
|
||||
# (n7, n7week, n7day),
|
||||
# ]
|
||||
|
||||
# n1 = 1
|
||||
# n2 = 1
|
||||
# try:
|
||||
# nweek, nday = weeks_days[n + 1]
|
||||
# except IndexError:
|
||||
# nweek, nday = weeks_days[n]
|
||||
# n1 = 0
|
||||
|
||||
# try:
|
||||
# n2week, n2day = weeks_days[n + 2]
|
||||
# except IndexError:
|
||||
# n2week, n2day = weeks_days[n]
|
||||
# n2 = 0
|
||||
|
||||
# try:
|
||||
# n3week, n3day = weeks_days[n + 3]
|
||||
# except IndexError:
|
||||
# n3week, n3day = weeks_days[n]
|
||||
# n3 = 0
|
||||
|
||||
# IF paired we check the following against both workers
|
||||
workers = [worker]
|
||||
@@ -1810,96 +1921,173 @@ class RotaBuilder(object):
|
||||
)
|
||||
|
||||
for constraint_shift in self.get_shifts_with_constraints(
|
||||
"preclear", "preclear2"
|
||||
"pre",
|
||||
):
|
||||
if day in constraint_shift.days:
|
||||
self.model.constraints.add(
|
||||
1
|
||||
>= self.model.works[
|
||||
worker.id, week, day, constraint_shift.name
|
||||
]
|
||||
+ sum(
|
||||
p1 * self.model.works[w.id, pweek, pday, shiftname]
|
||||
for shiftname in self.get_shift_names_by_week_day(
|
||||
pweek, pday
|
||||
for n in range(0, constraint_shift.constraint_options["pre"]):
|
||||
if day in constraint_shift.days:
|
||||
self.model.constraints.add(
|
||||
1
|
||||
>= self.model.works[
|
||||
worker.id, week, day, constraint_shift.name
|
||||
]
|
||||
+ sum(
|
||||
pre_map[n][0] * self.model.works[w.id, pre_map[n][1], pre_map[n][2], shiftname]
|
||||
for shiftname in self.get_shift_names_by_week_day(
|
||||
pre_map[n][1], pre_map[n][2]
|
||||
)
|
||||
if shiftname != constraint_shift.name
|
||||
for w in workers
|
||||
)
|
||||
if shiftname != constraint_shift.name
|
||||
for w in workers
|
||||
)
|
||||
)
|
||||
#for constraint_shift in self.get_shifts_with_constraint( "pre"):
|
||||
# to_sum = []
|
||||
# for n in range(
|
||||
# 0, constraint_shift.constraint_options["pre"]
|
||||
# ):
|
||||
# for w in workers:
|
||||
# for wk, dy, shiftname in self.get_week_day_shift_names_by_week_day(
|
||||
# pre_map[n][1], pre_map[n][2]
|
||||
# ):
|
||||
# if shiftname != constraint_shift.name:
|
||||
# to_sum.append(pre_map[n][0]
|
||||
# * self.model.works[
|
||||
# w.id, wk, dy, shiftname
|
||||
# ])
|
||||
|
||||
for constraint_shift in self.get_shifts_with_constraint("preclear2"):
|
||||
if day in constraint_shift.days:
|
||||
self.model.constraints.add(
|
||||
1
|
||||
>= self.model.works[
|
||||
worker.id, week, day, constraint_shift.name
|
||||
]
|
||||
+ sum(
|
||||
p2 * self.model.works[w.id, p2week, p2day, shiftname]
|
||||
for shiftname in self.get_shift_names_by_week_day(
|
||||
p2week, p2day
|
||||
)
|
||||
if shiftname != constraint_shift.name
|
||||
for w in workers
|
||||
)
|
||||
)
|
||||
|
||||
# if day in constraint_shift.days:
|
||||
# self.model.constraints.add(
|
||||
# 1
|
||||
# >= self.model.works[
|
||||
# worker.id, week, day, constraint_shift.name
|
||||
# ]
|
||||
# + sum(
|
||||
# to_sum
|
||||
# )
|
||||
# )
|
||||
|
||||
for constraint_shift in self.get_shifts_with_constraints(
|
||||
"postclear", "postclear2"
|
||||
"post",
|
||||
):
|
||||
if day in constraint_shift.days:
|
||||
self.model.constraints.add(
|
||||
1
|
||||
>= self.model.works[
|
||||
worker.id, week, day, constraint_shift.name
|
||||
]
|
||||
+ sum(
|
||||
n1 * self.model.works[w.id, nweek, nday, shiftname]
|
||||
for shiftname in self.get_shift_names_by_week_day(
|
||||
nweek, nday
|
||||
for n in range(0, constraint_shift.constraint_options["post"]):
|
||||
if day in constraint_shift.days:
|
||||
self.model.constraints.add(
|
||||
1
|
||||
>= self.model.works[
|
||||
worker.id, week, day, constraint_shift.name
|
||||
]
|
||||
+ sum(
|
||||
post_map[n][0]
|
||||
* self.model.works[
|
||||
w.id, post_map[n][1], post_map[n][2], shiftname
|
||||
]
|
||||
for shiftname in self.get_shift_names_by_week_day(
|
||||
post_map[n][1], post_map[n][2]
|
||||
)
|
||||
if shiftname != constraint_shift.name
|
||||
for w in workers
|
||||
)
|
||||
if shiftname != constraint_shift.name
|
||||
for w in workers
|
||||
)
|
||||
)
|
||||
|
||||
for constraint_shift in self.get_shifts_with_constraints("postclear2"):
|
||||
if day in constraint_shift.days:
|
||||
self.model.constraints.add(
|
||||
1
|
||||
>= self.model.works[
|
||||
worker.id, week, day, constraint_shift.name
|
||||
]
|
||||
+ sum(
|
||||
n2 * self.model.works[w.id, n2week, n2day, shiftname]
|
||||
for shiftname in self.get_shift_names_by_week_day(
|
||||
n2week, n2day
|
||||
)
|
||||
if shiftname != constraint_shift.name
|
||||
for w in workers
|
||||
)
|
||||
)
|
||||
# for constraint_shift in self.get_shifts_with_constraints(
|
||||
# "preclear", "preclear2"
|
||||
# ):
|
||||
# if day in constraint_shift.days:
|
||||
# self.model.constraints.add(
|
||||
# 1
|
||||
# >= self.model.works[
|
||||
# worker.id, week, day, constraint_shift.name
|
||||
# ]
|
||||
# + sum(
|
||||
# p1 * self.model.works[w.id, pweek, pday, shiftname]
|
||||
# for shiftname in self.get_shift_names_by_week_day(
|
||||
# pweek, pday
|
||||
# )
|
||||
# if shiftname != constraint_shift.name
|
||||
# for w in workers
|
||||
# )
|
||||
# )
|
||||
#
|
||||
# for constraint_shift in self.get_shifts_with_constraint("preclear2"):
|
||||
# if day in constraint_shift.days:
|
||||
# self.model.constraints.add(
|
||||
# 1
|
||||
# >= self.model.works[
|
||||
# worker.id, week, day, constraint_shift.name
|
||||
# ]
|
||||
# + sum(
|
||||
# p2 * self.model.works[w.id, p2week, p2day, shiftname]
|
||||
# for shiftname in self.get_shift_names_by_week_day(
|
||||
# p2week, p2day
|
||||
# )
|
||||
# if shiftname != constraint_shift.name
|
||||
# for w in workers
|
||||
# )
|
||||
# )
|
||||
|
||||
# for constraint_shift in self.get_shifts_with_constraints(
|
||||
# "postclear", "postclear2"
|
||||
# ):
|
||||
# if day in constraint_shift.days:
|
||||
# self.model.constraints.add(
|
||||
# 1
|
||||
# >= self.model.works[
|
||||
# worker.id, week, day, constraint_shift.name
|
||||
# ]
|
||||
# + sum(
|
||||
# n1 * self.model.works[w.id, nweek, nday, shiftname]
|
||||
# for shiftname in self.get_shift_names_by_week_day(
|
||||
# nweek, nday
|
||||
# )
|
||||
# if shiftname != constraint_shift.name
|
||||
# for w in workers
|
||||
# )
|
||||
# )
|
||||
|
||||
# for constraint_shift in self.get_shifts_with_constraints("postclear2"):
|
||||
# if day in constraint_shift.days:
|
||||
# self.model.constraints.add(
|
||||
# 1
|
||||
# >= self.model.works[
|
||||
# worker.id, week, day, constraint_shift.name
|
||||
# ]
|
||||
# + sum(
|
||||
# n2 * self.model.works[w.id, n2week, n2day, shiftname]
|
||||
# for shiftname in self.get_shift_names_by_week_day(
|
||||
# n2week, n2day
|
||||
# )
|
||||
# if shiftname != constraint_shift.name
|
||||
# for w in workers
|
||||
# )
|
||||
# )
|
||||
|
||||
# Night constraint means we won't assign a shift the day before
|
||||
# an unavailability
|
||||
for constraint_shift in self.get_shifts_with_constraint("night"):
|
||||
if (
|
||||
worker.id,
|
||||
pweek,
|
||||
pday,
|
||||
# pweek,
|
||||
pre_map[0][1],
|
||||
# pday,
|
||||
pre_map[0][2],
|
||||
constraint_shift.name,
|
||||
) in self.model.works:
|
||||
# Ensure night prior to unavalibity is not assigned
|
||||
self.model.constraints.add(
|
||||
self.model.available[worker.id, week, day]
|
||||
>= self.model.works[
|
||||
worker.id, pweek, pday, constraint_shift.name
|
||||
# worker.id, pweek, pday, constraint_shift.name
|
||||
worker.id,
|
||||
pre_map[0][1],
|
||||
pre_map[0][2],
|
||||
constraint_shift.name,
|
||||
]
|
||||
)
|
||||
|
||||
self.define_objectives()
|
||||
|
||||
print("Building model completed")
|
||||
|
||||
def define_objectives(self):
|
||||
# Define an objective function with model as input, to pass later
|
||||
def obj_rule(m):
|
||||
@@ -2171,6 +2359,15 @@ class RotaBuilder(object):
|
||||
"""
|
||||
return self.week_day_shifts_dict[(week, day)]
|
||||
|
||||
def get_week_day_shift_names_by_week_day(self, week, day: DayStr) -> set:
|
||||
"""Returns the shifts required for a specific day
|
||||
|
||||
Returns:
|
||||
set: Set of ShiftName
|
||||
|
||||
"""
|
||||
return set((week, day, shift_name) for shift_name in self.week_day_shifts_dict[(week, day)])
|
||||
|
||||
def get_shift_by_name(self, name: str) -> SingleShift:
|
||||
"""
|
||||
|
||||
@@ -2240,6 +2437,14 @@ class RotaBuilder(object):
|
||||
self.shift_counts[s.name] = self.shift_counts[s.name] + 1
|
||||
# print(self.week_day_shift_product)
|
||||
|
||||
self.max_pre = 1
|
||||
self.max_post = 1
|
||||
for shift in self.get_shifts_with_constraint("pre"):
|
||||
self.max_pre = max(shift.constraint_options["pre"], self.max_pre)
|
||||
|
||||
for shift in self.get_shifts_with_constraint("post"):
|
||||
self.max_post = max(shift.constraint_options["post"], self.max_post)
|
||||
|
||||
# # Todo replace with week_day.....
|
||||
# self.day_shift_product = []
|
||||
# self.day_shiftclass_product = []
|
||||
|
||||
Reference in New Issue
Block a user