add limit grades rule
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import datetime
|
||||
import itertools
|
||||
from typing import Iterable, List, Sequence, Tuple, Set
|
||||
from typing import Dict, Iterable, List, Sequence, Tuple, Set
|
||||
|
||||
import datetime
|
||||
|
||||
@@ -82,7 +82,16 @@ class SingleShift(object):
|
||||
self.assign_as_block = assign_as_block
|
||||
self.force_as_block = force_as_block
|
||||
self.force_as_block_unless_nwd = force_as_block_unless_nwd
|
||||
self.constraints = constraints
|
||||
|
||||
self.constraint_options = {}
|
||||
self.constraints = []
|
||||
for c in constraints:
|
||||
match c:
|
||||
case (constraint, options):
|
||||
self.constraints.append(constraint)
|
||||
self.constraint_options[constraint] = options
|
||||
case constraint:
|
||||
self.constraints.append(constraint)
|
||||
|
||||
self.bank_holidays_only = bank_holidays_only
|
||||
|
||||
@@ -170,7 +179,8 @@ class RotaBuilder(object):
|
||||
"balance_nights_across_sites": True,
|
||||
"balance_bank_holidays": True,
|
||||
"balance_blocks": True,
|
||||
"balance_shifts": True,
|
||||
"balance_shifts": True, # Does not use a quadratic function
|
||||
"balance_shifts_quadratic": False,
|
||||
"balance_shifts_over_workers": True,
|
||||
"minimise_shift_diffs": False, # less sophisticated version of balance_shifts_over_workers
|
||||
"balance_weekends": True,
|
||||
@@ -436,7 +446,7 @@ class RotaBuilder(object):
|
||||
initialize=0,
|
||||
)
|
||||
|
||||
if self.constraint_options["balance_shifts"]:
|
||||
if self.constraint_options["balance_shifts"] or self.constraint_options["balance_shifts_quadratic"]:
|
||||
self.model.shift_count_t1 = Var(
|
||||
(
|
||||
(worker.id, shift.name)
|
||||
@@ -457,6 +467,17 @@ class RotaBuilder(object):
|
||||
initialize=0,
|
||||
)
|
||||
|
||||
if self.constraint_options["balance_shifts_quadratic"]:
|
||||
self.model.shift_count_w = Var(
|
||||
(
|
||||
(worker.id, shift.name)
|
||||
for worker in self.workers
|
||||
for shift in self.get_shifts()
|
||||
),
|
||||
domain=NonNegativeReals,
|
||||
initialize=0,
|
||||
)
|
||||
|
||||
if self.constraint_options["balance_nights"]:
|
||||
# We also try to even out the night shifts seperately
|
||||
self.model.night_shift_count = Var(
|
||||
@@ -722,6 +743,35 @@ class RotaBuilder(object):
|
||||
rule=nightShiftMaxSTRule,
|
||||
)
|
||||
|
||||
# Limit grades
|
||||
def limitGradesRule(model, week, shift, grade, limit):
|
||||
workers = [w for w in self.workers if w.grade == grade]
|
||||
if not workers:
|
||||
return Constraint.Skip
|
||||
return (
|
||||
sum(
|
||||
model.shift_week_worker_assigned[shift.name, week, w.id]
|
||||
for w in workers
|
||||
)
|
||||
<= limit
|
||||
)
|
||||
|
||||
for shift in self.get_shifts_with_constraint("limit_grade_number"):
|
||||
if not shift.constraint_options["limit_grade_number"]:
|
||||
raise ValueError("Constraint option must be defined for 'limit_grade_number'")
|
||||
for grade in shift.constraint_options["limit_grade_number"]:
|
||||
#self.model.limit_grades_constraint = Constraint(
|
||||
setattr(self.model, f"limit_grades_constraint_{shift.name}_{grade}", Constraint(
|
||||
[week for week in self.weeks],
|
||||
#[shift for shift in self.get_shifts_with_constraint("limit_grade_number")],
|
||||
[shift],
|
||||
[grade],
|
||||
[shift.constraint_options["limit_grade_number"][grade]],
|
||||
rule=limitGradesRule,
|
||||
#name=f"limit_grades_constraint_{shift.name}_{grade}"
|
||||
))
|
||||
|
||||
|
||||
def nightShiftMinST4Rule(model, week, shift):
|
||||
single_workers = [w for w in self.workers if w.grade >= 4]
|
||||
if not single_workers:
|
||||
@@ -785,13 +835,34 @@ class RotaBuilder(object):
|
||||
|
||||
# # Count the number of workers from each site on each night shift
|
||||
# # As 1 or 0 is optimum we can simply subtract 1 from the number
|
||||
if self.constraint_options["balance_nights_across_sites"]:
|
||||
for site in self.sites:
|
||||
for shift in self.get_shifts_with_constraint("night"):
|
||||
#if self.constraint_options["balance_nights_across_sites"]:
|
||||
# for site in self.sites:
|
||||
# for shift in self.get_shifts_with_constraint("night"):
|
||||
# block = shift.name
|
||||
# for week in self.weeks:
|
||||
# self.model.constraints.add(
|
||||
# self.model.night_per_site[week, block, site]
|
||||
# == sum(
|
||||
# self.model.shift_week_worker_assigned[
|
||||
# block, week, worker.id
|
||||
# ]
|
||||
# for worker in self.workers
|
||||
# if worker.site == site
|
||||
# )
|
||||
# # 1 >= sum(self.model.shift_week_worker_assigned[shift.name, week, worker.id] for worker in self.workers if worker.site == site)
|
||||
# )
|
||||
# self.model.constraints.add(
|
||||
# self.model.night_per_site_t1[week, block, site]
|
||||
# - self.model.night_per_site_t2[week, block, site]
|
||||
# == self.model.night_per_site[week, block, site] - 1
|
||||
# )
|
||||
for site in self.sites:
|
||||
for shift in self.get_shifts_with_constraint("balance_across_groups"):
|
||||
if site in shift.site:
|
||||
block = shift.name
|
||||
for week in self.weeks:
|
||||
self.model.constraints.add(
|
||||
self.model.night_per_site[week, block, site]
|
||||
self.model.shift_per_site[week, block, site]
|
||||
== sum(
|
||||
self.model.shift_week_worker_assigned[
|
||||
block, week, worker.id
|
||||
@@ -802,30 +873,10 @@ class RotaBuilder(object):
|
||||
# 1 >= sum(self.model.shift_week_worker_assigned[shift.name, week, worker.id] for worker in self.workers if worker.site == site)
|
||||
)
|
||||
self.model.constraints.add(
|
||||
self.model.night_per_site_t1[week, block, site]
|
||||
- self.model.night_per_site_t2[week, block, site]
|
||||
== self.model.night_per_site[week, block, site] - 1
|
||||
self.model.shift_per_site_t1[week, block, site]
|
||||
- self.model.shift_per_site_t2[week, block, site]
|
||||
== self.model.shift_per_site[week, block, site] - 1
|
||||
)
|
||||
for site in self.sites:
|
||||
for shift in self.get_shifts_with_constraint("balance_across_groups"):
|
||||
block = shift.name
|
||||
for week in self.weeks:
|
||||
self.model.constraints.add(
|
||||
self.model.shift_per_site[week, block, site]
|
||||
== sum(
|
||||
self.model.shift_week_worker_assigned[
|
||||
block, week, worker.id
|
||||
]
|
||||
for worker in self.workers
|
||||
if worker.site == site
|
||||
)
|
||||
# 1 >= sum(self.model.shift_week_worker_assigned[shift.name, week, worker.id] for worker in self.workers if worker.site == site)
|
||||
)
|
||||
self.model.constraints.add(
|
||||
self.model.shift_per_site_t1[week, block, site]
|
||||
- self.model.shift_per_site_t2[week, block, site]
|
||||
== self.model.shift_per_site[week, block, site] - 1
|
||||
)
|
||||
|
||||
weeks_days = self.get_week_day_combinations()
|
||||
|
||||
@@ -1082,6 +1133,46 @@ class RotaBuilder(object):
|
||||
== self.model.shift_count_diff[worker.id, shift.name]
|
||||
)
|
||||
|
||||
|
||||
if self.constraint_options["balance_shifts_quadratic"]:
|
||||
# This may need to be updated
|
||||
xU = 25
|
||||
xL = 1
|
||||
self.model.constraints.add(
|
||||
inequality(
|
||||
xL,
|
||||
self.model.shift_count_t1[worker.id, shift.name]
|
||||
+ self.model.shift_count_t2[worker.id, shift.name]
|
||||
+ 1,
|
||||
xU,
|
||||
)
|
||||
)
|
||||
|
||||
self.model.constraints.add(
|
||||
self.model.shift_count_w[worker.id, shift.name]
|
||||
>= xL
|
||||
* (
|
||||
self.model.shift_count_t1[worker.id, shift.name]
|
||||
+ self.model.shift_count_t2[worker.id, shift.name]
|
||||
+ 1
|
||||
)
|
||||
* 2
|
||||
- xL * xL
|
||||
)
|
||||
|
||||
self.model.constraints.add(
|
||||
self.model.shift_count_w[worker.id, shift.name]
|
||||
>= xU
|
||||
* (
|
||||
self.model.shift_count_t1[worker.id, shift.name]
|
||||
+ self.model.shift_count_t2[worker.id, shift.name]
|
||||
+ 1
|
||||
)
|
||||
* 2
|
||||
- xU * xU
|
||||
)
|
||||
|
||||
|
||||
# Define worker_shift_count_t1 and worker_shift_count_t2 constraints for the object
|
||||
# Thus bypassing the need for a quadratic solver
|
||||
# t1-t2 is the target
|
||||
@@ -1459,22 +1550,24 @@ 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("night"):
|
||||
# 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)
|
||||
# )
|
||||
#for shift in self.get_shifts_with_constraint("night"):
|
||||
for shift in self.get_shifts():
|
||||
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)
|
||||
# )
|
||||
|
||||
# Force night shifts to be assigned in blocks
|
||||
self.model.constraints.add(
|
||||
self.model.shift_week_worker_assigned[
|
||||
shift.name, week, worker.id
|
||||
]
|
||||
* len(shift.shift_days)
|
||||
== sum(
|
||||
self.model.works[worker.id, week, day, shift.name]
|
||||
for day in shift.shift_days
|
||||
# Force night shifts to be assigned in blocks
|
||||
self.model.constraints.add(
|
||||
self.model.shift_week_worker_assigned[
|
||||
shift.name, week, worker.id
|
||||
]
|
||||
* len(shift.shift_days)
|
||||
== sum(
|
||||
self.model.works[worker.id, week, day, shift.name]
|
||||
for day in shift.shift_days
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
#
|
||||
|
||||
@@ -1728,6 +1821,19 @@ class RotaBuilder(object):
|
||||
else:
|
||||
shift_balancing = 0
|
||||
|
||||
if self.constraint_options["balance_shifts_quadratic"]:
|
||||
shift_balancing = sum(
|
||||
balance_modifier_constant
|
||||
* (
|
||||
self.model.shift_count_t1[worker.id, shift.name]
|
||||
+ self.model.shift_count_t2[worker.id, shift.name]
|
||||
)
|
||||
for worker in self.workers
|
||||
for shift in self.get_shifts()
|
||||
)
|
||||
else:
|
||||
shift_balancing = 0
|
||||
|
||||
if self.constraint_options["minimise_shift_diffs"]:
|
||||
shift_diff_modifier_constant = 10
|
||||
shift_diff_balancing = sum(
|
||||
@@ -1808,6 +1914,22 @@ class RotaBuilder(object):
|
||||
else:
|
||||
nights_site_balancing = 0
|
||||
|
||||
|
||||
if self.get_shifts_with_constraint("balance_across_groups"):
|
||||
block_site_balancing = sum(
|
||||
(
|
||||
self.model.shift_per_site_t1[week, shift.name, site]
|
||||
+ self.model.shift_per_site_t2[week, shift.name, site]
|
||||
)
|
||||
* 2000
|
||||
# self.model.night_per_site2[week, block, site]
|
||||
for week in self.weeks
|
||||
for shift in self.get_shifts_with_constraint("balance_across_groups")
|
||||
for site in self.sites
|
||||
)
|
||||
else:
|
||||
block_site_balancing = 0
|
||||
|
||||
if self.constraint_options["balance_blocks"]:
|
||||
blocks_balancing = sum(
|
||||
1 * self.model.blocks_assigned[week, shift]
|
||||
@@ -1833,6 +1955,7 @@ class RotaBuilder(object):
|
||||
+ shift_diff_balancing
|
||||
+ preferences
|
||||
+ nights_site_balancing
|
||||
+ block_site_balancing
|
||||
+ bank_holiday_balancing
|
||||
+ blocks_balancing
|
||||
- work_requests
|
||||
@@ -2117,6 +2240,20 @@ class RotaBuilder(object):
|
||||
s.extend(self.shifts_to_force_as_blocks())
|
||||
return s
|
||||
|
||||
def get_workers_by_group(self) -> Dict[str, Worker]:
|
||||
group_workers = defaultdict(set)
|
||||
for worker in self.workers:
|
||||
group_workers[worker.site].add(worker)
|
||||
|
||||
return group_workers
|
||||
|
||||
def get_workers_by_grade(self) -> Dict[str, Worker]:
|
||||
group_workers = defaultdict(set)
|
||||
for worker in self.workers:
|
||||
group_workers[worker.grade].add(worker)
|
||||
|
||||
return group_workers
|
||||
|
||||
def get_workers_for_shift(self, shift: SingleShift) -> List[Worker]:
|
||||
return [worker for worker in self.workers if worker.site in shift.site]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user