many improvements
This commit is contained in:
+323
-238
@@ -1,4 +1,5 @@
|
||||
from copy import deepcopy
|
||||
from re import S
|
||||
from black import main
|
||||
import pytest
|
||||
from rota.shifts import NoWorkers, RotaBuilder, SingleShift, days
|
||||
@@ -17,7 +18,6 @@ class TestDemoRota:
|
||||
start_date,
|
||||
weeks_to_rota=weeks_to_rota,
|
||||
balance_offset_modifier=1,
|
||||
max_weekend_frequency=1,
|
||||
)
|
||||
|
||||
Rota.constraint_options["max_shifts_per_week"] = 5
|
||||
@@ -32,22 +32,22 @@ class TestDemoRota:
|
||||
Rota.constraint_options["balance_bank_holidays"] = False
|
||||
|
||||
# Add a few workers
|
||||
worker1 = Worker(name= "worker1", site= "group1", grade= 1)
|
||||
worker2 = Worker(name= "worker2", site= "group1", grade= 1)
|
||||
worker3 = Worker(name= "worker3", site= "group1", grade= 1)
|
||||
worker4 = Worker(name= "worker4", site= "group1", grade= 1)
|
||||
worker5 = Worker(name= "worker5", site= "group2", grade= 1, fte=60)
|
||||
worker6 = Worker(name= "worker6", site= "group2", grade= 1, fte=40)
|
||||
worker1 = Worker(name="worker1", site="group1", grade=1)
|
||||
worker2 = Worker(name="worker2", site="group1", grade=1)
|
||||
worker3 = Worker(name="worker3", site="group1", grade=1)
|
||||
worker4 = Worker(name="worker4", site="group1", grade=1)
|
||||
worker5 = Worker(name="worker5", site="group2", grade=1, fte=60)
|
||||
worker6 = Worker(name="worker6", site="group2", grade=1, fte=40)
|
||||
|
||||
Rota.add_workers((worker1, worker2, worker3, worker4, worker5, worker6))
|
||||
|
||||
# Add a weekday and weekend shift
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2"),
|
||||
"weekday",
|
||||
12.5,
|
||||
days[:5],
|
||||
sites=("group1", "group2"),
|
||||
name="weekday",
|
||||
length=12.5,
|
||||
days=days[:5],
|
||||
balance_offset=1,
|
||||
workers_required=1,
|
||||
# We use a different balance weighting for each shift as otherwise
|
||||
@@ -55,10 +55,10 @@ class TestDemoRota:
|
||||
# balance_weighting=0.6,
|
||||
),
|
||||
SingleShift(
|
||||
("group1",),
|
||||
"weekend",
|
||||
12.5,
|
||||
days[5:],
|
||||
sites=("group1",),
|
||||
name="weekend",
|
||||
length=12.5,
|
||||
days=days[5:],
|
||||
balance_offset=4,
|
||||
# balance_weighting=0.5
|
||||
),
|
||||
@@ -191,7 +191,6 @@ class TestDemoRotaNights:
|
||||
start_date,
|
||||
weeks_to_rota=weeks_to_rota,
|
||||
balance_offset_modifier=1,
|
||||
max_weekend_frequency=1,
|
||||
)
|
||||
|
||||
Rota.constraint_options["max_shifts_per_week"] = 5
|
||||
@@ -223,12 +222,12 @@ class TestDemoRotaNights:
|
||||
# Add a weekday and weekend shift
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2"),
|
||||
"night_weekday",
|
||||
12.5,
|
||||
days[:4],
|
||||
sites=("group1", "group2"),
|
||||
name="night_weekday",
|
||||
length=12.5,
|
||||
days=days[:4],
|
||||
workers_required=1,
|
||||
constraint=["preclear2", "postclear2"],
|
||||
constraint=[{"name": "preclear2"}, {"name": "postclear2"}],
|
||||
# constraint=["night"],
|
||||
force_as_block=True
|
||||
# We use a different balance weighting for each shift as otherwise
|
||||
@@ -236,20 +235,20 @@ class TestDemoRotaNights:
|
||||
# balance_weighting=0.6,
|
||||
),
|
||||
SingleShift(
|
||||
("group1", "group2"),
|
||||
"night_weekend",
|
||||
12.5,
|
||||
days[4:],
|
||||
sites=("group1", "group2"),
|
||||
name="night_weekend",
|
||||
length=12.5,
|
||||
days=days[4:],
|
||||
# balance_weighting=0.5
|
||||
constraint=["preclear2", "postclear2"],
|
||||
constraint=[{"name": "preclear2"}, {"name": "postclear2"}],
|
||||
# constraint=["night"],
|
||||
force_as_block=True,
|
||||
),
|
||||
SingleShift(
|
||||
("group1", "group2"),
|
||||
"twilight",
|
||||
12.5,
|
||||
days[:5],
|
||||
sites=("group1", "group2"),
|
||||
name="twilight",
|
||||
length=12.5,
|
||||
days=days[:5],
|
||||
# balance_weighting=0.5
|
||||
workers_required=2,
|
||||
# constraint=["preclear2", "postclear2"],
|
||||
@@ -271,11 +270,9 @@ class TestDemoRotaNights:
|
||||
|
||||
Rota.export_rota_to_html("test3")
|
||||
|
||||
|
||||
def test_start_date(self):
|
||||
assert self.Rota.start_date == self.start_date
|
||||
|
||||
|
||||
def test_night_assignment(self):
|
||||
shift_summary = self.Rota.get_shift_summary_dict()
|
||||
|
||||
@@ -285,14 +282,14 @@ class TestDemoRotaNights:
|
||||
worker = self.Rota.get_worker_by_name(worker_name)
|
||||
|
||||
if worker.fte == 40:
|
||||
assert worker_shifts["night_weekday"] in (0,4)
|
||||
assert worker_shifts["night_weekend"] in (0,3)
|
||||
assert worker_shifts["night_weekday"] in (0, 4)
|
||||
assert worker_shifts["night_weekend"] in (0, 3)
|
||||
elif worker.fte == 60:
|
||||
assert worker_shifts["night_weekday"] in (0,4,8)
|
||||
assert worker_shifts["night_weekend"] in (0,3,6)
|
||||
assert worker_shifts["night_weekday"] in (0, 4, 8)
|
||||
assert worker_shifts["night_weekend"] in (0, 3, 6)
|
||||
else:
|
||||
assert worker_shifts["night_weekday"] in (4,8)
|
||||
assert worker_shifts["night_weekend"] in (3,6)
|
||||
assert worker_shifts["night_weekday"] in (4, 8)
|
||||
assert worker_shifts["night_weekend"] in (3, 6)
|
||||
|
||||
|
||||
class TestDemoRotaClear:
|
||||
@@ -303,7 +300,6 @@ class TestDemoRotaClear:
|
||||
start_date,
|
||||
weeks_to_rota=weeks_to_rota,
|
||||
balance_offset_modifier=1,
|
||||
max_weekend_frequency=1,
|
||||
)
|
||||
Rota.constraint_options["max_shifts_per_month"] = 20
|
||||
|
||||
@@ -323,30 +319,30 @@ class TestDemoRotaClear:
|
||||
def test_preclear(self):
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2"),
|
||||
"a",
|
||||
12.5,
|
||||
days[:5],
|
||||
sites=("group1", "group2"),
|
||||
name="a",
|
||||
length=12.5,
|
||||
days=days[:5],
|
||||
balance_offset=40,
|
||||
workers_required=2,
|
||||
constraint=["preclear"],
|
||||
constraint=[{"name": "preclear"}],
|
||||
),
|
||||
SingleShift(
|
||||
("group1", "group2"),
|
||||
"b",
|
||||
12.5,
|
||||
days[:5],
|
||||
sites=("group1", "group2"),
|
||||
name="b",
|
||||
length=12.5,
|
||||
days=days[:5],
|
||||
balance_offset=40,
|
||||
workers_required=2,
|
||||
),
|
||||
SingleShift(
|
||||
("group1", "group2"),
|
||||
"c",
|
||||
12.5,
|
||||
days[3],
|
||||
sites=("group1", "group2"),
|
||||
name="c",
|
||||
length=12.5,
|
||||
days=days[3],
|
||||
balance_offset=40,
|
||||
workers_required=1,
|
||||
constraint=["preclear2", "postclear2"],
|
||||
constraint=[{"name": "preclear2"}, {"name": "postclear2"}],
|
||||
),
|
||||
)
|
||||
|
||||
@@ -389,7 +385,6 @@ class TestDemoRotaShiftConstraints:
|
||||
start_date,
|
||||
weeks_to_rota=weeks_to_rota,
|
||||
balance_offset_modifier=1,
|
||||
max_weekend_frequency=1,
|
||||
)
|
||||
# Rota.constraint_options["max_shifts_per_week"] = 5
|
||||
# Rota.constraint_options["max_shifts_per_month"] = 20
|
||||
@@ -399,10 +394,10 @@ class TestDemoRotaShiftConstraints:
|
||||
Rota.add_workers((worker1, worker2))
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1",),
|
||||
"a",
|
||||
12.5,
|
||||
days,
|
||||
sites=("group1",),
|
||||
name="a",
|
||||
length=12.5,
|
||||
days=days,
|
||||
balance_offset=40,
|
||||
workers_required=1,
|
||||
),
|
||||
@@ -479,14 +474,14 @@ class TestDemoRotaBalanceShiftSites:
|
||||
def test_balance_blocks_across_2_groups(self):
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2"),
|
||||
"weekday_night",
|
||||
12.5,
|
||||
days[:4],
|
||||
sites=("group1", "group2"),
|
||||
name="weekday_night",
|
||||
length=12.5,
|
||||
days=days[:4],
|
||||
balance_offset=40,
|
||||
workers_required=2,
|
||||
force_as_block=True,
|
||||
constraint=["balance_across_groups"],
|
||||
constraint=[{"name": "balance_across_groups"}],
|
||||
),
|
||||
)
|
||||
self.Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
@@ -515,14 +510,14 @@ class TestDemoRotaBalanceShiftSites:
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2"),
|
||||
"weekday_night",
|
||||
12.5,
|
||||
days[:4],
|
||||
sites=("group1", "group2"),
|
||||
name="weekday_night",
|
||||
length=12.5,
|
||||
days=days[:4],
|
||||
balance_offset=40,
|
||||
workers_required=3,
|
||||
force_as_block=True,
|
||||
constraint=["balance_across_groups"],
|
||||
constraint=[{"name": "balance_across_groups"}],
|
||||
),
|
||||
)
|
||||
self.Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
@@ -549,14 +544,14 @@ class TestDemoRotaBalanceShiftSites:
|
||||
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2", "group3"),
|
||||
"weekday_night",
|
||||
12.5,
|
||||
days[:4],
|
||||
sites=("group1", "group2", "group3"),
|
||||
name="weekday_night",
|
||||
length=12.5,
|
||||
days=days[:4],
|
||||
balance_offset=40,
|
||||
workers_required=3,
|
||||
force_as_block=True,
|
||||
constraint=["balance_across_groups"],
|
||||
constraint=[{"name": "balance_across_groups"}],
|
||||
),
|
||||
)
|
||||
self.Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
@@ -580,8 +575,7 @@ class TestDemoRotaBalanceShiftSites:
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
|
||||
class TestLimitConstraints:
|
||||
def generate_limit_constraint_rota():
|
||||
weeks_to_rota = 8
|
||||
start_date = datetime.date(2022, 3, 7)
|
||||
|
||||
@@ -603,30 +597,41 @@ class TestLimitConstraints:
|
||||
|
||||
Rota.shifts = []
|
||||
|
||||
return Rota
|
||||
|
||||
class TestLimitConstraints:
|
||||
|
||||
def test_constraint_limit_grades(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
Rota = generate_limit_constraint_rota()
|
||||
Rota.shifts = []
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2", "group3"),
|
||||
"weekday_night",
|
||||
12.5,
|
||||
days,
|
||||
balance_offset=40,
|
||||
sites=("group1", "group2", "group3"),
|
||||
name="weekday_night",
|
||||
length=12.5,
|
||||
days=days,
|
||||
balance_offset=60,
|
||||
workers_required=4,
|
||||
force_as_block=True,
|
||||
constraint=[("limit_grade_number", {2: 1})],
|
||||
constraint=[{"name": "limit_grade_number", "options": {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})
|
||||
# Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
# Rota.constraint_options["balance_shifts_over_workers"] = False
|
||||
#Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
Rota.build_and_solve(options={"ratio": 0.1})
|
||||
|
||||
Rota.export_rota_to_html("constraint_limit_grades")
|
||||
|
||||
assert Rota.results.solver.status == "ok"
|
||||
assert Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
grade_workers = Rota.get_workers_by_grade()
|
||||
|
||||
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))
|
||||
shift_patterns.append(Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
@@ -636,35 +641,33 @@ class TestLimitConstraints:
|
||||
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(
|
||||
Rota = generate_limit_constraint_rota()
|
||||
Rota.shifts = []
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2", "group3"),
|
||||
"weekday_night",
|
||||
12.5,
|
||||
days,
|
||||
sites=("group1", "group2", "group3"),
|
||||
name="weekday_night",
|
||||
length=12.5,
|
||||
days=days,
|
||||
balance_offset=40,
|
||||
workers_required=4,
|
||||
force_as_block=True,
|
||||
constraint=[("limit_grade_number", {3: 2})],
|
||||
constraint=[{"name": "limit_grade_number", "options": {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})
|
||||
# Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
# Rota.constraint_options["balance_shifts_over_workers"] = False
|
||||
Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
Rota.build_and_solve(options={"ratio": 0.1})
|
||||
|
||||
self.Rota.export_rota_to_html("test9")
|
||||
Rota.export_rota_to_html("test9")
|
||||
|
||||
grade_workers = self.Rota.get_workers_by_grade()
|
||||
grade_workers = 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))
|
||||
shift_patterns.append(Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
@@ -674,35 +677,51 @@ class TestLimitConstraints:
|
||||
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"
|
||||
assert Rota.results.solver.status == "ok"
|
||||
assert Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
def test_constraint_limit_grades3(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
Rota = generate_limit_constraint_rota()
|
||||
|
||||
Rota.workers = []
|
||||
|
||||
worker1 = Worker(name="worker1", site="group1", grade=2, remote_site="group1")
|
||||
worker2 = Worker(name="worker2", site="group1", grade=2, remote_site="group1")
|
||||
worker3 = Worker(name="worker3", site="group1", grade=2, remote_site="group1")
|
||||
worker4 = Worker(name="worker4", site="group3", grade=3, remote_site="group2")
|
||||
worker5 = Worker(name="worker5", site="group3", grade=3, remote_site="group2")
|
||||
worker6 = Worker(name="worker6", site="group3", grade=3, remote_site="group2")
|
||||
worker7 = Worker(name="worker7", site="group3", grade=2, remote_site="group2")
|
||||
|
||||
Rota.add_workers((worker1, worker2, worker3, worker4))
|
||||
Rota.add_workers((worker5, worker6))
|
||||
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2", "group3"),
|
||||
"weekday_night",
|
||||
12.5,
|
||||
days,
|
||||
balance_offset=40,
|
||||
sites=("group1", "group2", "group3"),
|
||||
name="weekday_night",
|
||||
length=12.5,
|
||||
days=days,
|
||||
balance_offset=60,
|
||||
workers_required=4,
|
||||
force_as_block=True,
|
||||
constraint=[("limit_grade_number", {2: 3, 3: 1})],
|
||||
constraint=[{"name": "limit_grade_number", "options": {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})
|
||||
# Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
# Rota.constraint_options["balance_shifts_over_workers"] = False
|
||||
Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
Rota.build_and_solve(options={"ratio": 0.01})
|
||||
|
||||
self.Rota.export_rota_to_html("test9")
|
||||
Rota.export_rota_to_html("test9")
|
||||
assert Rota.results.solver.status == "ok"
|
||||
assert Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
grade_workers = self.Rota.get_workers_by_grade()
|
||||
grade_workers = 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))
|
||||
shift_patterns.append(Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
@@ -712,55 +731,57 @@ class TestLimitConstraints:
|
||||
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(
|
||||
Rota = generate_limit_constraint_rota()
|
||||
Rota.shifts = []
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2", "group3"),
|
||||
"weekday_night",
|
||||
12.5,
|
||||
days,
|
||||
sites=("group1", "group2", "group3"),
|
||||
name="weekday_night",
|
||||
length=12.5,
|
||||
days=days,
|
||||
balance_offset=40,
|
||||
workers_required=4,
|
||||
force_as_block=True,
|
||||
constraint=[("limit_grade_number", {2: 4, 3: 0})],
|
||||
constraint=[{"name": "limit_grade_number", "options": {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})
|
||||
# Rota.constraint_options["balance_nights_across_sites"] = False
|
||||
# Rota.constraint_options["balance_shifts_over_workers"] = False
|
||||
Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
Rota.build_and_solve(options={"ratio": 0.1})
|
||||
|
||||
assert self.Rota.results.solver.termination_condition == "infeasible"
|
||||
assert Rota.results.solver.termination_condition == "infeasible"
|
||||
|
||||
def test_constraint_minimum_grades(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
Rota = generate_limit_constraint_rota()
|
||||
Rota.shifts = []
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2", "group3"),
|
||||
"weekday_night",
|
||||
12.5,
|
||||
days,
|
||||
sites=("group1", "group2", "group3"),
|
||||
name="weekday_night",
|
||||
length=12.5,
|
||||
days=days,
|
||||
balance_offset=40,
|
||||
workers_required=1,
|
||||
force_as_block=True,
|
||||
constraint=[("minimum_grade_number", (3, 1))],
|
||||
constraint=[{"name": "minimum_grade_number", "options": (3, 1)}],
|
||||
),
|
||||
)
|
||||
|
||||
self.Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
self.Rota.build_and_solve(options={"ratio": 0.1})
|
||||
Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
Rota.build_and_solve(options={"ratio": 0.1})
|
||||
|
||||
self.Rota.export_rota_to_html("test9")
|
||||
Rota.export_rota_to_html("test9")
|
||||
|
||||
grade_workers = self.Rota.get_workers_by_grade()
|
||||
assert Rota.results.solver.status == "ok"
|
||||
|
||||
grade_workers = 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))
|
||||
shift_patterns.append(Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
@@ -770,33 +791,33 @@ class TestLimitConstraints:
|
||||
for day_shifts in zipped_lists:
|
||||
assert day_shifts.count("weekday_night") == limit
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
|
||||
def test_constraint_minimum_grades2(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
Rota = generate_limit_constraint_rota()
|
||||
Rota.shifts = []
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2", "group3"),
|
||||
"weekday_night",
|
||||
12.5,
|
||||
days,
|
||||
sites=("group1", "group2", "group3"),
|
||||
name="weekday_night",
|
||||
length=12.5,
|
||||
days=days,
|
||||
balance_offset=40,
|
||||
workers_required=4,
|
||||
force_as_block=True,
|
||||
constraint=[("minimum_grade_number", (3, 3))],
|
||||
constraint=[{"name": "minimum_grade_number", "options": (3, 3)}],
|
||||
),
|
||||
)
|
||||
|
||||
self.Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
self.Rota.build_and_solve(options={"ratio": 0.1})
|
||||
Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
Rota.build_and_solve(options={"ratio": 0.1})
|
||||
|
||||
self.Rota.export_rota_to_html("test9")
|
||||
Rota.export_rota_to_html("test9")
|
||||
|
||||
grade_workers = self.Rota.get_workers_by_grade()
|
||||
grade_workers = 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))
|
||||
shift_patterns.append(Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
@@ -806,71 +827,79 @@ class TestLimitConstraints:
|
||||
for day_shifts in zipped_lists:
|
||||
assert day_shifts.count("weekday_night") == limit
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert Rota.results.solver.status == "ok"
|
||||
|
||||
def test_constraint_minimum_grades3(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
Rota = generate_limit_constraint_rota()
|
||||
Rota.shifts = []
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2", "group3"),
|
||||
"weekday_night",
|
||||
12.5,
|
||||
days,
|
||||
sites=("group1", "group2", "group3"),
|
||||
name="weekday_night",
|
||||
length=12.5,
|
||||
days=days,
|
||||
balance_offset=40,
|
||||
workers_required=4,
|
||||
force_as_block=True,
|
||||
constraint=[("minimum_grade_number", (3, 0))],
|
||||
constraint=[{"name": "minimum_grade_number", "options": (3, 0)}],
|
||||
),
|
||||
)
|
||||
|
||||
self.Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
self.Rota.build_and_solve(options={"ratio": 0.1})
|
||||
Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
Rota.build_and_solve(options={"ratio": 0.1})
|
||||
|
||||
self.Rota.export_rota_to_html("test9")
|
||||
Rota.export_rota_to_html("test9")
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert Rota.results.solver.status == "ok"
|
||||
|
||||
def test_constraint_minimum_grades_no_valid_worker(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
Rota = generate_limit_constraint_rota()
|
||||
Rota.shifts = []
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2", "group3"),
|
||||
"weekday_night",
|
||||
12.5,
|
||||
days,
|
||||
sites=("group1", "group2", "group3"),
|
||||
name="weekday_night",
|
||||
length=12.5,
|
||||
days=days,
|
||||
balance_offset=40,
|
||||
workers_required=4,
|
||||
force_as_block=True,
|
||||
constraint=[("minimum_grade_number", (4, 1))],
|
||||
constraint=[{"name": "minimum_grade_number", "options": (4, 1)}],
|
||||
),
|
||||
)
|
||||
|
||||
self.Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
Rota.constraint_options["balance_shifts_quadratic"] = True
|
||||
with pytest.raises(ValueError):
|
||||
self.Rota.build_and_solve(options={"ratio": 0.1})
|
||||
Rota.build_and_solve(options={"ratio": 0.1})
|
||||
|
||||
def test_constraint_require_remote_site_presence_week(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
Rota = generate_limit_constraint_rota()
|
||||
Rota.shifts = []
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2", "group3"),
|
||||
"night",
|
||||
12.5,
|
||||
days,
|
||||
sites=("group1", "group2", "group3"),
|
||||
name="night",
|
||||
length=12.5,
|
||||
days=days,
|
||||
balance_offset=40,
|
||||
workers_required=2,
|
||||
force_as_block=True,
|
||||
constraint=[("require_remote_site_presence_week", ("group1", 2))],
|
||||
constraint=[
|
||||
{
|
||||
"name": "require_remote_site_presence_week",
|
||||
"options": ("group1", 2),
|
||||
}
|
||||
],
|
||||
),
|
||||
)
|
||||
self.Rota.build_and_solve(options={"ratio": 0.00})
|
||||
self.Rota.export_rota_to_html("remote1")
|
||||
Rota.build_and_solve(options={"ratio": 0.00})
|
||||
Rota.export_rota_to_html("remote1")
|
||||
|
||||
group_workers = self.Rota.get_workers_by_group()
|
||||
group_workers = 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))
|
||||
shift_patterns.append(Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
@@ -880,67 +909,79 @@ class TestLimitConstraints:
|
||||
for day_shifts in zipped_lists:
|
||||
assert day_shifts.count("night") == limit
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota.results.solver.termination_condition == "optimal"
|
||||
assert Rota.results.solver.status == "ok"
|
||||
assert Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
def test_constraint_require_remote_site_presence_week2(self):
|
||||
self.Rota.constraint_options["minimise_shift_diffs"] = False
|
||||
self.Rota.constraint_options["balance_shifts_quadratic"] = False
|
||||
self.Rota.constraint_options["balance_shift"] = False
|
||||
self.Rota.constraint_options["balance_nights_across_sites"] = True
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
Rota = generate_limit_constraint_rota()
|
||||
Rota.constraint_options["minimise_shift_diffs"] = False
|
||||
Rota.constraint_options["balance_shifts_quadratic"] = False
|
||||
Rota.constraint_options["balance_shift"] = False
|
||||
Rota.constraint_options["balance_nights_across_sites"] = True
|
||||
Rota.shifts = []
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2", "group3"),
|
||||
"night",
|
||||
12.5,
|
||||
days,
|
||||
sites=("group1", "group2", "group3"),
|
||||
name="night",
|
||||
length=12.5,
|
||||
days=days,
|
||||
balance_offset=20,
|
||||
workers_required=3,
|
||||
force_as_block=True,
|
||||
constraint=[("require_remote_site_presence_week", ("group2", 2))],
|
||||
constraint=[
|
||||
{
|
||||
"name": "require_remote_site_presence_week",
|
||||
"options": ("group1", 2),
|
||||
}
|
||||
],
|
||||
),
|
||||
)
|
||||
self.Rota.build_and_solve(options={"ratio": 0.00})
|
||||
self.Rota.export_rota_to_html("remote2")
|
||||
Rota.build_and_solve(options={"ratio": 0.00})
|
||||
Rota.export_rota_to_html("remote2")
|
||||
|
||||
group_workers = self.Rota.get_workers_by_remote_group()
|
||||
group_workers = Rota.get_workers_by_remote_group()
|
||||
for group in group_workers:
|
||||
shift_patterns = []
|
||||
for w in group_workers[group]:
|
||||
shift_patterns.append(self.Rota.get_worker_shift_list(w))
|
||||
shift_patterns.append(Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
if group == "group2":
|
||||
if group == "group1":
|
||||
for day_shifts in zipped_lists:
|
||||
assert day_shifts.count("night") >= 2
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota.results.solver.termination_condition == "optimal"
|
||||
assert Rota.results.solver.status == "ok"
|
||||
assert Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
def test_constraint_require_remote_site_presence_week3(self):
|
||||
self.Rota.shifts = []
|
||||
self.Rota.add_shifts(
|
||||
Rota = generate_limit_constraint_rota()
|
||||
Rota.shifts = []
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1", "group2", "group3"),
|
||||
"night",
|
||||
12.5,
|
||||
days,
|
||||
sites=("group1", "group2", "group3"),
|
||||
name="night",
|
||||
length=12.5,
|
||||
days=days,
|
||||
balance_offset=20,
|
||||
workers_required=3,
|
||||
force_as_block=True,
|
||||
constraint=[("require_remote_site_presence_week", ("group2", 1))],
|
||||
constraint=[
|
||||
{
|
||||
"name": "require_remote_site_presence_week",
|
||||
"options": ("group2", 1),
|
||||
}
|
||||
],
|
||||
),
|
||||
)
|
||||
self.Rota.build_and_solve(options={"ratio": 0.00})
|
||||
self.Rota.export_rota_to_html("remote3")
|
||||
Rota.build_and_solve(options={"ratio": 0.00})
|
||||
Rota.export_rota_to_html("remote3")
|
||||
|
||||
group_workers = self.Rota.get_workers_by_remote_group()
|
||||
group_workers = Rota.get_workers_by_remote_group()
|
||||
for group in group_workers:
|
||||
shift_patterns = []
|
||||
for w in group_workers[group]:
|
||||
shift_patterns.append(self.Rota.get_worker_shift_list(w))
|
||||
shift_patterns.append(Rota.get_worker_shift_list(w))
|
||||
|
||||
zipped_lists = list(zip(*shift_patterns))
|
||||
|
||||
@@ -948,8 +989,9 @@ class TestLimitConstraints:
|
||||
for day_shifts in zipped_lists:
|
||||
assert day_shifts.count("night") >= 1
|
||||
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota.results.solver.termination_condition == "optimal"
|
||||
assert Rota.results.solver.status == "ok"
|
||||
assert Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
|
||||
# class TestNoWorkerRota:
|
||||
# weeks_to_rota = 10
|
||||
@@ -979,23 +1021,41 @@ class TestNightUnavailable:
|
||||
|
||||
# Add a few workers
|
||||
worker1 = Worker(
|
||||
name="worker1", site="group1", grade=1, not_available_to_work=[{ "date": datetime.datetime.strptime("15/03/22", "%d/%m/%y").date(), "reason" : "****" },]
|
||||
name="worker1",
|
||||
site="group1",
|
||||
grade=1,
|
||||
not_available_to_work=[
|
||||
{
|
||||
"date": datetime.datetime.strptime("15/03/22", "%d/%m/%y").date(),
|
||||
"reason": "****",
|
||||
},
|
||||
],
|
||||
)
|
||||
worker2 = Worker(
|
||||
name="worker2", site="group1", grade=1, not_available_to_work=[ { "date": datetime.datetime.strptime("14/03/22", "%d/%m/%y").date(), "reason": "****" } ]
|
||||
name="worker2",
|
||||
site="group1",
|
||||
grade=1,
|
||||
not_available_to_work=[
|
||||
{
|
||||
"date": datetime.datetime.strptime("14/03/22", "%d/%m/%y").date(),
|
||||
"reason": "****",
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
Rota.add_workers((worker1, worker2))
|
||||
|
||||
|
||||
|
||||
def test_basic_assignment(self, capsys):
|
||||
self.Rota.shifts = []
|
||||
|
||||
# Add a weekday and weekend shift
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1",), "night_weekend", 12.5, days[5:], constraint=["night"]
|
||||
sites =("group1",),
|
||||
name = "night_weekend",
|
||||
length = 12.5,
|
||||
days = days[5:],
|
||||
constraint=[{"name": "night"}],
|
||||
),
|
||||
)
|
||||
|
||||
@@ -1005,7 +1065,6 @@ class TestNightUnavailable:
|
||||
assert self.Rota.results.solver.status == "ok"
|
||||
assert self.Rota.results.solver.termination_condition == "optimal"
|
||||
|
||||
|
||||
self.Rota.export_rota_to_html("testnight")
|
||||
summary = self.Rota.get_shift_summary_dict()
|
||||
|
||||
@@ -1018,7 +1077,12 @@ class TestNightUnavailable:
|
||||
# Add a weekday and weekend shift
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1",), "night_weekend", 12.5, days[5:], workers_required=2, constraint=["night"]
|
||||
sites =("group1",),
|
||||
name = "night_weekend",
|
||||
length = 12.5,
|
||||
days = days[5:],
|
||||
workers_required=2,
|
||||
constraint=[{"name": "night"}],
|
||||
),
|
||||
)
|
||||
|
||||
@@ -1032,7 +1096,12 @@ class TestNightUnavailable:
|
||||
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1",), "night_weekend", 12.5, days[5:], workers_required=2, constraint=[]
|
||||
sites = ("group1",),
|
||||
name = "night_weekend",
|
||||
length = 12.5,
|
||||
days = days[5:],
|
||||
workers_required=2,
|
||||
constraint=[],
|
||||
),
|
||||
)
|
||||
|
||||
@@ -1044,16 +1113,31 @@ class TestNightUnavailable:
|
||||
def test_assign_split(self):
|
||||
self.Rota.shifts = []
|
||||
worker3 = Worker(
|
||||
name="worker3", site="group1", grade=1, not_available_to_work=({"date":datetime.datetime.strptime("13/03/22", "%d/%m/%y").date()},)
|
||||
name="worker3",
|
||||
site="group1",
|
||||
grade=1,
|
||||
not_available_to_work=(
|
||||
{"date": datetime.datetime.strptime("13/03/22", "%d/%m/%y").date()},
|
||||
),
|
||||
)
|
||||
worker4 = Worker(
|
||||
name="worker4", site="group1", grade=1, not_available_to_work=({"date":datetime.datetime.strptime("12/03/22", "%d/%m/%y").date()},)
|
||||
name="worker4",
|
||||
site="group1",
|
||||
grade=1,
|
||||
not_available_to_work=(
|
||||
{"date": datetime.datetime.strptime("12/03/22", "%d/%m/%y").date()},
|
||||
),
|
||||
)
|
||||
self.Rota.add_workers((worker3, worker4))
|
||||
|
||||
self.Rota.add_shifts(
|
||||
SingleShift(
|
||||
("group1",), "night_weekend", 12.5, days[5:], workers_required=3, constraint=[]
|
||||
sites=("group1",),
|
||||
name="night_weekend",
|
||||
length=12.5,
|
||||
days=days[5:],
|
||||
workers_required=3,
|
||||
constraint=[],
|
||||
),
|
||||
)
|
||||
|
||||
@@ -1067,6 +1151,7 @@ class TestNightUnavailable:
|
||||
for worker in summary:
|
||||
assert summary[worker]["night_weekend"] == 15
|
||||
|
||||
|
||||
# def test_assign_split_night_constraint(self):
|
||||
# self.Rota.shifts = []
|
||||
# worker3 = Worker(
|
||||
|
||||
Reference in New Issue
Block a user