add limit grades rule
This commit is contained in:
+242
-43
@@ -1,3 +1,4 @@
|
||||
from copy import deepcopy
|
||||
from black import main
|
||||
import pytest
|
||||
from shifts import NoWorkers, RotaBuilder, SingleShift, days
|
||||
@@ -383,6 +384,11 @@ class TestDemoRotaShiftConstraints:
|
||||
),
|
||||
)
|
||||
|
||||
Rota3Workers = deepcopy(Rota)
|
||||
worker3 = Worker(Rota3Workers, "worker3", "group1", 1)
|
||||
|
||||
Rota3Workers.add_worker(worker3)
|
||||
|
||||
def test_max_shifts(self):
|
||||
self.Rota.constraint_options["max_shifts_per_month"] = 14
|
||||
self.Rota.build_and_solve()
|
||||
@@ -397,23 +403,11 @@ class TestDemoRotaShiftConstraints:
|
||||
assert self.Rota.results.solver.termination_condition == "infeasible"
|
||||
|
||||
def test_max_shifts_extra_worker(self):
|
||||
self.Rota.constraint_options["max_shifts_per_month"] = 12
|
||||
worker3 = Worker(self.Rota, "worker3", "group1", 1)
|
||||
self.Rota3Workers.constraint_options["max_shifts_per_month"] = 12
|
||||
self.Rota3Workers.build_and_solve()
|
||||
|
||||
self.Rota.add_worker(worker3)
|
||||
self.Rota.build_and_solve()
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
def test_max_shifts_extra_worker_wrong_group(self):
|
||||
self.Rota.constraint_options["max_shifts_per_month"] = 12
|
||||
worker3 = Worker(self.Rota, "worker3", "group2", 1)
|
||||
self.Rota.add_worker(worker3)
|
||||
self.Rota.build_and_solve()
|
||||
|
||||
assert self.Rota.results.solver.status == "warning"
|
||||
assert self.Rota.results.solver.termination_condition == "infeasible"
|
||||
assert self.Rota3Workers.results.solver.status == "ok"
|
||||
assert self.Rota3Workers.results.solver.termination_condition == "optimal"
|
||||
|
||||
def test_max_shifts_per_week_fail(self):
|
||||
self.Rota.constraint_options["max_shifts_per_week"] = 3
|
||||
@@ -423,18 +417,19 @@ class TestDemoRotaShiftConstraints:
|
||||
assert self.Rota.results.solver.termination_condition == "infeasible"
|
||||
|
||||
def test_max_shifts_per_week_pass(self):
|
||||
self.Rota.constraint_options["max_shifts_per_month"] = 14
|
||||
self.Rota.constraint_options["max_shifts_per_week"] = 4
|
||||
self.Rota.build_and_solve()
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
|
||||
def test_max_shifts_per_week_extra_worker_pass(self):
|
||||
self.Rota.constraint_options["max_shifts_per_week"] = 3
|
||||
worker3 = Worker(self.Rota, "worker3", "group1", 1)
|
||||
self.Rota.add_worker(worker3)
|
||||
self.Rota.build_and_solve()
|
||||
self.Rota3Workers.constraint_options["max_shifts_per_week"] = 3
|
||||
#worker3 = Worker(self.Rota, "worker3", "group1", 1)
|
||||
#self.Rota.add_worker(worker3)
|
||||
self.Rota3Workers.build_and_solve()
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota3Workers.results.solver.status == "ok"
|
||||
|
||||
class TestDemoRotaBalanceShiftSites:
|
||||
weeks_to_rota = 8
|
||||
@@ -450,34 +445,238 @@ class TestDemoRotaBalanceShiftSites:
|
||||
worker2 = Worker(Rota, "worker2", "group1", 1)
|
||||
worker3 = Worker(Rota, "worker3", "group2", 1)
|
||||
worker4 = Worker(Rota, "worker4", "group2", 1)
|
||||
worker5 = Worker(Rota, "worker3", "group2", 1)
|
||||
worker6 = Worker(Rota, "worker4", "group2", 1)
|
||||
worker5 = Worker(Rota, "worker5", "group3", 1)
|
||||
worker6 = Worker(Rota, "worker6", "group3", 1)
|
||||
|
||||
Rota.add_workers((worker1, worker2, worker3, worker4))
|
||||
#Rota.add_workers((worker5, worker6))
|
||||
Rota.add_shifts(
|
||||
SingleShift(("group1", "group2"), "weekday_night", 12.5, days[:4], balance_offset=40,
|
||||
workers_required=2,
|
||||
force_as_block=True,
|
||||
constraints=["balance_across_groups"]
|
||||
),
|
||||
#SingleShift(("group1", "group2"), "weekend_night", 12.5, days[4:], balance_offset=40,
|
||||
#workers_required=2,
|
||||
#force_as_block=True,
|
||||
#),
|
||||
)
|
||||
Rota.add_workers((worker5, worker6))
|
||||
|
||||
def test_balance_blocks_across_groups(self):
|
||||
self.Rota.constraint_options["balance_nights_across_sites"] = True
|
||||
#self.Rota.constraint_options["balance_bank_holidays"] = False
|
||||
#self.Rota.constraint_options["balance_weekends"] = False
|
||||
#self.Rota.constraint_options["balance_blocks"] = False
|
||||
self.Rota.build_and_solve(options={"ratio": 0.0, "seconds": 1000, "threads": 10})
|
||||
def test_balance_blocks_across_2_groups(self):
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(("group1", "group2"), "weekday_night", 12.5, days[:4], balance_offset=40,
|
||||
workers_required=2,
|
||||
force_as_block=True,
|
||||
constraints=["balance_across_groups"]
|
||||
),
|
||||
)
|
||||
self.Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
self.Rota.constraint_options["balance_nights"] = False
|
||||
self.Rota.constraint_options["constrain_time_off_after_nights"] = False
|
||||
self.Rota.build_and_solve(options={"ratio": 0.0})
|
||||
self.Rota.export_rota_to_html("test5")
|
||||
|
||||
print(self.Rota.get_workers_by_group())
|
||||
|
||||
group_workers = self.Rota.get_workers_by_group()
|
||||
for group in group_workers:
|
||||
shift_patterns = []
|
||||
for w in group_workers[group]:
|
||||
shift_patterns.append(self.Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
for day_shifts in zipped_lists:
|
||||
assert day_shifts.count("weekday_night") <= 1
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
def test_balance_blocks_across_2_groups_unbalanced(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(("group1", "group2"), "weekday_night", 12.5, days[:4], balance_offset=40,
|
||||
workers_required=3,
|
||||
force_as_block=True,
|
||||
constraints=["balance_across_groups"]
|
||||
),
|
||||
)
|
||||
self.Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
self.Rota.constraint_options["balance_nights"] = False
|
||||
self.Rota.constraint_options["constrain_time_off_after_nights"] = False
|
||||
self.Rota.build_and_solve(options={"ratio": 0.1})
|
||||
|
||||
group_workers = self.Rota.get_workers_by_group()
|
||||
for group in group_workers:
|
||||
shift_patterns = []
|
||||
for w in group_workers[group]:
|
||||
shift_patterns.append(self.Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
for day_shifts in zipped_lists:
|
||||
assert day_shifts.count("weekday_night") <= 2
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
|
||||
def test_balance_blocks_across_3_groups(self):
|
||||
self.Rota.shifts = []
|
||||
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(("group1", "group2", "group3"), "weekday_night", 12.5, days[:4], balance_offset=40,
|
||||
workers_required=3,
|
||||
force_as_block=True,
|
||||
constraints=["balance_across_groups"]
|
||||
),
|
||||
)
|
||||
self.Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
self.Rota.constraint_options["balance_nights"] = False
|
||||
self.Rota.constraint_options["constrain_time_off_after_nights"] = False
|
||||
self.Rota.build_and_solve(options={"ratio": 0.1})
|
||||
|
||||
group_workers = self.Rota.get_workers_by_group()
|
||||
for group in group_workers:
|
||||
shift_patterns = []
|
||||
for w in group_workers[group]:
|
||||
shift_patterns.append(self.Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
for day_shifts in zipped_lists:
|
||||
assert day_shifts.count("weekday_night") <= 1
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
class TestLimitConstraints:
|
||||
weeks_to_rota = 8
|
||||
start_date = datetime.date(2022, 3, 7)
|
||||
|
||||
Rota = RotaBuilder(
|
||||
start_date,
|
||||
weeks_to_rota=weeks_to_rota,
|
||||
)
|
||||
#Rota.constraint_options["max_shifts_per_week"] = 5
|
||||
#Rota.constraint_options["max_shifts_per_month"] = 20
|
||||
worker1 = Worker(Rota, "worker1", "group1", 2)
|
||||
worker2 = Worker(Rota, "worker2", "group1", 2)
|
||||
worker3 = Worker(Rota, "worker3", "group1", 2)
|
||||
worker4 = Worker(Rota, "worker4", "group3", 3)
|
||||
worker5 = Worker(Rota, "worker5", "group3", 3)
|
||||
worker6 = Worker(Rota, "worker6", "group3", 3)
|
||||
|
||||
Rota.add_workers((worker1, worker2, worker3, worker4))
|
||||
Rota.add_workers((worker5, worker6))
|
||||
|
||||
Rota.shifts = []
|
||||
|
||||
|
||||
def test_constraint_limit_grades(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(("group1", "group2", "group3"), "weekday_night", 12.5, days, balance_offset=40,
|
||||
workers_required=4,
|
||||
force_as_block=True,
|
||||
constraints=[("limit_grade_number", {2: 1})]
|
||||
),
|
||||
)
|
||||
#self.Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
#self.Rota.constraint_options["balance_shifts_over_workers"] = False
|
||||
self.Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
self.Rota.build_and_solve(options={"ratio": 0.00})
|
||||
|
||||
grade_workers = self.Rota.get_workers_by_grade()
|
||||
for grade in grade_workers:
|
||||
shift_patterns = []
|
||||
for w in grade_workers[grade]:
|
||||
shift_patterns.append(self.Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
limit = 1
|
||||
if grade > 2:
|
||||
limit = 3
|
||||
for day_shifts in zipped_lists:
|
||||
assert day_shifts.count("weekday_night") == limit
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
def test_constraint_limit_grades2(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(("group1", "group2", "group3"), "weekday_night", 12.5, days, balance_offset=40,
|
||||
workers_required=4,
|
||||
force_as_block=True,
|
||||
constraints=[("limit_grade_number", {3:2})]
|
||||
),
|
||||
)
|
||||
#self.Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
#self.Rota.constraint_options["balance_shifts_over_workers"] = False
|
||||
self.Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
self.Rota.build_and_solve(options={"ratio": 0.1})
|
||||
|
||||
self.Rota.export_rota_to_html("test9")
|
||||
|
||||
grade_workers = self.Rota.get_workers_by_grade()
|
||||
for grade in grade_workers:
|
||||
shift_patterns = []
|
||||
for w in grade_workers[grade]:
|
||||
shift_patterns.append(self.Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
limit = 2
|
||||
if grade > 2:
|
||||
limit = 2
|
||||
for day_shifts in zipped_lists:
|
||||
assert day_shifts.count("weekday_night") == limit
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
def test_constraint_limit_grades3(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(("group1", "group2", "group3"), "weekday_night", 12.5, days, balance_offset=40,
|
||||
workers_required=4,
|
||||
force_as_block=True,
|
||||
constraints=[("limit_grade_number", {2: 3, 3:1})]
|
||||
),
|
||||
)
|
||||
#self.Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
#self.Rota.constraint_options["balance_shifts_over_workers"] = False
|
||||
self.Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
self.Rota.build_and_solve(options={"ratio": 0.1})
|
||||
|
||||
self.Rota.export_rota_to_html("test9")
|
||||
|
||||
grade_workers = self.Rota.get_workers_by_grade()
|
||||
for grade in grade_workers:
|
||||
shift_patterns = []
|
||||
for w in grade_workers[grade]:
|
||||
shift_patterns.append(self.Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
limit = 3
|
||||
if grade > 2:
|
||||
limit = 1
|
||||
for day_shifts in zipped_lists:
|
||||
assert day_shifts.count("weekday_night") == limit
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
def test_constraint_limit_grades4(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(("group1", "group2", "group3"), "weekday_night", 12.5, days, balance_offset=40,
|
||||
workers_required=4,
|
||||
force_as_block=True,
|
||||
constraints=[("limit_grade_number", {2: 4, 3:0})]
|
||||
),
|
||||
)
|
||||
#self.Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
#self.Rota.constraint_options["balance_shifts_over_workers"] = False
|
||||
self.Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
self.Rota.build_and_solve(options={"ratio": 0.1})
|
||||
|
||||
self.Rota.export_rota_to_html("test9")
|
||||
|
||||
assert self.Rota.results.solver.termination_condition == "infeasible"
|
||||
|
||||
#class TestNoWorkerRota:
|
||||
# weeks_to_rota = 10
|
||||
# start_date = datetime.date(2022, 3, 7)
|
||||
@@ -493,5 +692,5 @@ class TestDemoRotaBalanceShiftSites:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
t = TestDemoRotaShiftConstraints()
|
||||
t.test_balance_blocks_across_groups()
|
||||
t = TestLimitConstraints()
|
||||
t.test_constraint_limit_grades4()
|
||||
|
||||
Reference in New Issue
Block a user