further tests and fixes

This commit is contained in:
Ross
2022-05-18 17:20:28 +01:00
parent f887257a55
commit a8cf3e3460
7 changed files with 953 additions and 102 deletions
+32 -14
View File
@@ -38,7 +38,7 @@ for bank_holiday in bank_holidays.get_holidays(division="england-and-wales"):
bank_holiday_map[bank_holiday["date"]] = bank_holiday["title"]
SHIFT_BOUNDS = {
"bank_holiday": (0, 4),
"bank_holiday": (0, 9),
"shift_count": (0, 400),
"night_shift_count": (0, 19),
"weekend_count": (0, 60),
@@ -76,7 +76,7 @@ class SingleShift(object):
length: float,
shift_days: Sequence[str],
# balance_by_site=True,
balance_offset: float = 2, # this could be generated dynamically
balance_offset: float | None = None,
balance_weighting: float = 1,
workers_required: float = 1,
rota_on_nwds: bool = False,
@@ -95,7 +95,11 @@ class SingleShift(object):
# balance_offset defines the max difference in allocated shifts
# versus target shifts (hard constraint)
# if 0 exactly equal shifts must be assigend (unlikely to be possible)
self.balance_offset = balance_offset
if balance_offset is None:
self.balance_offset = len(shift_days)
else:
self.balance_offset = balance_offset
# weight the shift for balancing, default is 1
self.balance_weighting = balance_weighting
@@ -615,6 +619,12 @@ class RotaBuilder(object):
initialize=availability_init,
)
# Validate shifts are valid
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(f"Invalid worker shift request [shift(s): ${invalid_shifts}]")
def work_request_init(model, wid, week, day, shift):
if (wid, week, day, shift) in self.work_requests:
self.work_requests_map[(wid, week, day)] = shift
@@ -2016,6 +2026,7 @@ class RotaBuilder(object):
"""
Process the added shifts
"""
self.shifts_by_name = {}
self.shift_names = [] # type: List[ShiftName]
@@ -2026,6 +2037,19 @@ class RotaBuilder(object):
self.week_day_shift_product = []
self.week_day_shiftclass_product = []
for s in self.shifts:
if s.name in self.shift_names:
raise InvalidShift(f"Duplicate shift: {s.name}")
self.shifts_by_name[s.name] = s
self.shift_names.append(s.name)
# for day in s.shift_days:
# self.week_day_shifts_dict[(week, day)].add(s.name)
for site in s.site:
self.sites.add(site)
self.shift_counts = defaultdict(int)
for week, day in self.weeks_days_product:
self.week_day_shifts_dict[(week, day)] = set()
@@ -2043,15 +2067,6 @@ class RotaBuilder(object):
self.week_day_shifts_dict[(week, day)].add(s.name)
self.shift_counts[s] = self.shift_counts[s] + 1
# print(self.week_day_shift_product)
for s in self.shifts:
self.shifts_by_name[s.name] = s
self.shift_names.append(s.name)
# for day in s.shift_days:
# self.week_day_shifts_dict[(week, day)].add(s.name)
for site in s.site:
self.sites.add(site)
# # Todo replace with week_day.....
# self.day_shift_product = []
@@ -2762,11 +2777,14 @@ class RotaBuilder(object):
class NoActiveSites(Exception):
"""Raised when there are no active sites"""
pass
class NoWorkers(Exception):
"""Raised when there are no active sites"""
pass
class InvalidShift(Exception):
"""Raised when there are no active sites"""
pass