Compare commits

..
3 Commits
Author SHA1 Message Date
Ross 8c28e7155a update for latest proc run 2023-12-27 19:15:00 +00:00
Ross 3d064a75b9 add shift worker names constraint 2023-12-27 19:12:08 +00:00
Ross 740836e31a add limit 2023-12-27 17:55:54 +00:00
5 changed files with 235 additions and 9 deletions
+6 -7
View File
@@ -29,13 +29,13 @@ suspend_on_finish = False
solve = True
# time_to_run = 508500
# time_to_run = 2660000
time_to_run = 60 * 1
time_to_run = 60 * 60 * 85
# allow = 5000
ratio = 0.01
ratio = 0.001
# ratio = 0.9
start_date = datetime.date(2023, 9, 4)
start_date = datetime.date(2024, 3, 4)
# start_date = datetime.date(2023, 5, 1)
# start_date = start_date + datetime.timedelta(weeks=13)
# start_date = datetime.date(2022, 10, 10)
@@ -53,7 +53,7 @@ Rota.constraint_options["hard_constrain_pair_separation"] = True
Rota.add_shifts(
SingleShift(
sites=("exeter", "exeter twilights and weekends"),
sites=("exeter", "exeter twilights and weekends", "exeter no nights"),
name="exeter_twilight",
length=12.5,
days=days[:5],
@@ -90,7 +90,7 @@ Rota.add_shifts(
constraint=[{"name": "max_shifts_per_week", "options": 2}],
),
SingleShift(
sites=("exeter", "exeter twilights and weekends"),
sites=("exeter", "exeter twilights and weekends", "exeter no nights"),
name="weekend_exeter",
length=12.5,
days=days[5:],
@@ -123,7 +123,7 @@ Rota.add_shifts(
# force_as_block_unless_nwd=True
),
SingleShift(
sites=("plymouth", "plymouth twilights and weekends"),
sites=("plymouth", "plymouth twilights and weekends", "plymouth first on weekends only"),
name="weekend_plymouth1",
length=8,
days=days[5:],
@@ -335,7 +335,6 @@ if load_leave:
# Rota.build_model()
solver_options = {"ratio": ratio, "seconds": time_to_run, "threads": 10}
solver_options = {"ratio": ratio, "seconds": time_to_run, }
#start_time = time.time()
Rota.build_and_solve(solver_options, export=True)
+1 -1
View File
@@ -134,7 +134,7 @@ def load_leave(Rota):
if lower_item in shifts:
worker["requests"].append(WorkRequests(date=date, shift=lower_item))
elif lower_item in ("work_request", "happy to work"):
elif lower_item in ("work_request", "happy to work", "offered"):
worker["requests"].append(WorkRequests(date=date, shift="*"))
+31
View File
@@ -252,6 +252,7 @@ class RotaBuilder(object):
"avoid_st2_first_month": False,
"hard_constrain_pair_separation": False,
"avoid_shifts_by_grades": [],
"avoid_shifts_by_worker_names": [],
}
self.results = None
@@ -1175,6 +1176,7 @@ class RotaBuilder(object):
)
# Most of our constraints apply per worker
worker: Worker
for worker in self.workers:
console.rule(
f"Generate worker constraints: [bold blue]{worker.name}[/bold blue]"
@@ -1209,6 +1211,28 @@ class RotaBuilder(object):
)
pass
for constraint_dict in self.constraint_options["avoid_shifts_by_worker_names"]:
if invalid_shifts := set(constraint_dict["shifts"]).difference(
set(self.get_shift_names())
):
if self.ignore_valid_shifts:
continue # Skip if we don't want to raise an error
else:
raise InvalidShift(
f"avoid shifts by grade constraint contains a non existent shift: {invalid_shifts}"
)
if worker.name in constraint_dict["names"]:
self.model.constraints.add(
0
== sum(
self.model.works[worker.id, week, day, shift]
for week, day, shift in self.get_all_shiftname_combinations()
if week in constraint_dict["weeks"]
and shift in constraint_dict["shifts"]
)
)
for constraint_dict in self.constraint_options["avoid_shifts_by_grades"]:
if invalid_shifts := set(constraint_dict["shifts"]).difference(
set(self.get_shift_names())
@@ -2522,6 +2546,13 @@ class RotaBuilder(object):
for p in pairs:
self.worker_pairs.append(tuple(pairs[p]))
def add_worker_name_constraint_by_week(
self, names: Iterable[str], weeks: Iterable[int], shifts
):
self.constraint_options["avoid_shifts_by_worker_names"].append(
{"names": names, "weeks": weeks, "shifts": shifts}
)
def add_grade_constraint_by_week(
self, grades: Iterable[int], weeks: Iterable[int], shifts
):
@@ -0,0 +1,196 @@
import datetime
import pytest
from rota.shifts import InvalidShift, NoWorkers, RotaBuilder, SingleShift, days
from rota.workers import Worker
import itertools
def generate_basic_rota(weeks_to_rota=9):
start_date = datetime.date(2022, 3, 7)
Rota = RotaBuilder(
start_date,
weeks_to_rota=weeks_to_rota,
)
# Add a few workers
Rota.add_workers(
[
Worker(name="worker1", site="group1", grade=1),
Worker(name="worker2", site="group1", grade=2),
Worker(name="worker3", site="group1", grade=3),
]
)
return Rota
def date_generator(from_date, days):
n = 0
while True:
yield from_date
n = n + 1
if n >= days:
break
from_date = from_date + datetime.timedelta(days=1)
class TestWorkerRequests:
def test_avoid_worker_constraint(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1",), name="a", length= 12.5, days=days,
workers_required=2,
force_as_block=False,
),
)
Rota.add_worker_name_constraint_by_week(["worker1"], [1,2,3], ["a"])
Rota.add_worker_name_constraint_by_week(["worker2"], [4,5,6], ["a"])
Rota.add_worker_name_constraint_by_week(["worker3"], [7,8,9], ["a"])
Rota.build_and_solve(options={"ratio": 0.000})
Rota.export_rota_to_html("avoid_grade_constraint")
shift_summary = Rota.get_shift_summary_dict()
for worker_name in shift_summary:
worker_shifts = shift_summary[worker_name]
worker = Rota.get_worker_by_name(worker_name)
shift_string = Rota.get_worker_shift_list_string(worker)
match worker_name:
case "worker1":
assert shift_string.startswith("-"*21)
case "worker2":
assert shift_string.startswith(21*"a"+"-"*21)
case "worker3":
assert shift_string.endswith("-"*21)
def test_avoid_worker_constraint_invalid_shift(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1",), name="a", length= 12.5, days=days,
workers_required=1,
force_as_block=False,
),
)
with pytest.raises(InvalidShift):
Rota.add_worker_name_constraint_by_week(["worker1"], [1,2,3], ["b"])
Rota.build_and_solve(options={"ratio": 0.000})
Rota.add_shifts(
SingleShift(
sites=("group1",), name="b", length= 12.5, days=days,
workers_required=1,
force_as_block=False,
),
)
Rota.add_worker_name_constraint_by_week([1], [1,2,3], ["b"])
Rota.build_and_solve(options={"ratio": 0.000})
assert Rota.results.solver.status == "ok"
# TODO: implement check for invalid workers
## def test_avoid_grade_constraint_invalid_worker(self):
## Rota = generate_basic_rota()
##
## Rota.add_shifts(
## SingleShift(
## sites=("group1",), name="a", length= 12.5, days=days,
## workers_required=2,
## force_as_block=False,
## ),
## )
## with pytest.raises(ValueError):
##
## Rota.add_worker_name_constraint_by_week([4], [1,2,3], ["a"])
## Rota.build_and_solve(options={"ratio": 0.000})
##
## Rota.add_worker(
## Worker(name="worker4", site="group1", grade=4),
## )
##
##
## Rota.add_worker_name_constraint_by_week([4], [1,2,3], ["a"])
## Rota.build_and_solve(options={"ratio": 0.000})
## assert Rota.results.solver.status == "ok"
def test_avoid_worker_constraint_multiple_workers(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1",), name="a", length= 12.5, days=days,
workers_required=1,
force_as_block=False,
),
)
Rota.add_worker_name_constraint_by_week(["worker1", "worker2"], [1,2,3], ["a"])
Rota.build_and_solve(options={"ratio": 0.000})
assert Rota.results.solver.status == "ok"
Rota.add_worker_name_constraint_by_week(["worker3"], [1,2,3], ["a"])
Rota.build_and_solve(options={"ratio": 0.000})
assert Rota.results.solver.status == "warning"
def test_avoid_worker_constraint_multiple_shifts(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1",), name="a", length= 12.5, days=days,
workers_required=1,
force_as_block=False,
),
SingleShift(
sites=("group1",), name="b", length= 12.5, days=days,
workers_required=1,
force_as_block=False,
),
)
Rota.add_worker_name_constraint_by_week(["worker1"], [1,2,3], ["a", "b"])
Rota.add_worker_name_constraint_by_week(["worker2"], [4,5,6], ["a", "b"])
Rota.add_worker_name_constraint_by_week(["worker3"], [7,8,9], ["a", "b"])
Rota.build_and_solve(options={"ratio": 0.000})
Rota.export_rota_to_html("avoid_grade_constraint")
shift_summary = Rota.get_shift_summary_dict()
for worker_name in shift_summary:
worker_shifts = shift_summary[worker_name]
worker = Rota.get_worker_by_name(worker_name)
shift_string = Rota.get_worker_shift_list_string(worker)
match worker_name:
case "worker1":
assert shift_string.startswith("-"*21)
case "worker2":
assert "-"*21 in shift_string # hacky
case "worker3":
assert shift_string.endswith("-"*21)
Rota.add_worker_name_constraint_by_week(["worker2"], [7,8,9], ["a"])
Rota.build_and_solve(options={"ratio": 0.000})
assert Rota.results.solver.status == "ok"
Rota.add_worker_name_constraint_by_week(["worker1"], [7,8,9], ["a"])
Rota.build_and_solve(options={"ratio": 0.000})
assert Rota.results.solver.status == "warning"
+1 -1
View File
@@ -143,7 +143,7 @@ class TestBalancing:
),
)
Rota.build_and_solve(options={"ratio": 0.000})
Rota.build_and_solve(options={"ratio": 0.0001})
Rota.export_rota_to_html("basic_balancing_weighted_shifts2")
shift_summary = Rota.get_shift_summary_dict()