rename home to remote
This commit is contained in:
-118
@@ -1,118 +0,0 @@
|
||||
import csv
|
||||
import re
|
||||
|
||||
date_re = r"[\d]{1,2}\/[\d]{1,2}\/[\d]{2}"
|
||||
|
||||
|
||||
def load_leave(Rota, use_live_rota):
|
||||
|
||||
file_name = "leave.csv"
|
||||
|
||||
with open(file_name, newline="") as f:
|
||||
shifts = Rota.get_shift_names()
|
||||
|
||||
workers = {}
|
||||
|
||||
reader = csv.reader(f, delimiter=",", quotechar='"')
|
||||
|
||||
leave = {}
|
||||
requests = {}
|
||||
site_prefs = {}
|
||||
|
||||
order = []
|
||||
|
||||
n = 0
|
||||
for row in reader:
|
||||
row_title = row[0]
|
||||
r = row[2:]
|
||||
# header, extract names
|
||||
if n == 1:
|
||||
names = r
|
||||
order = names
|
||||
for name in names:
|
||||
|
||||
workers[name] = {}
|
||||
workers[name]["leave"] = []
|
||||
workers[name]["requests"] = []
|
||||
workers[name]["site_pref"] = []
|
||||
workers[name]["shift_balance_extra"] = {}
|
||||
workers[name]["bank_holiday_extra"] = 0
|
||||
|
||||
for i in range(len(r)):
|
||||
try:
|
||||
a = order[i]
|
||||
except IndexError:
|
||||
continue
|
||||
|
||||
worker = workers[order[i]]
|
||||
|
||||
lower_item = r[i].lower()
|
||||
if lower_item == "derriford":
|
||||
lower_item = "plymouth"
|
||||
|
||||
if lower_item == "derriford twilights":
|
||||
lower_item = "plymouth_twilights"
|
||||
|
||||
if row_title == "PROC site":
|
||||
worker["site_pref"] = lower_item
|
||||
|
||||
if row_title == "Rotation":
|
||||
worker["site"] = lower_item
|
||||
|
||||
elif row_title == "Grade":
|
||||
worker["grade"] = lower_item
|
||||
|
||||
elif row_title == "%FTE":
|
||||
worker["fte"] = lower_item
|
||||
|
||||
elif row_title == "NWD":
|
||||
worker["nwd"] = lower_item
|
||||
|
||||
elif row_title == "Flexible NWD":
|
||||
worker["flexible_nwd"] = lower_item
|
||||
|
||||
elif row_title == "End Date":
|
||||
worker["end_date"] = lower_item
|
||||
|
||||
elif row_title == "OOP":
|
||||
worker["oop"] = lower_item
|
||||
|
||||
elif row_title == "Group":
|
||||
worker["group"] = lower_item
|
||||
|
||||
elif row_title == "Pair":
|
||||
if lower_item == "":
|
||||
worker["pair"] = 0
|
||||
else:
|
||||
worker["pair"] = int(lower_item)
|
||||
|
||||
elif row_title.startswith("Extra_"):
|
||||
if lower_item:
|
||||
shift = row_title[len("Extra_") :]
|
||||
if shift == "twilight":
|
||||
shift = f"{worker['site']}_{shift}"
|
||||
elif shift == "weekend":
|
||||
shift = f"weekend_{worker['site']}"
|
||||
worker["shift_balance_extra"][shift] = float(lower_item)
|
||||
|
||||
elif row_title == "bank_holidays_worked":
|
||||
if lower_item:
|
||||
print("BH", lower_item)
|
||||
worker["bank_holiday_extra"] = int(lower_item)
|
||||
|
||||
elif re.match(date_re, row_title) is not None:
|
||||
|
||||
if lower_item != "":
|
||||
# This may be easier to do as a dict
|
||||
if lower_item in shifts:
|
||||
worker["requests"].append((row[0], lower_item))
|
||||
|
||||
else:
|
||||
worker["leave"].append((row[0], lower_item))
|
||||
|
||||
n = n + 1
|
||||
|
||||
return workers
|
||||
|
||||
|
||||
# load_leave()
|
||||
-377
@@ -1,377 +0,0 @@
|
||||
from pyomo.environ import *
|
||||
from pyomo.opt import SolverFactory
|
||||
from pyomo.opt.results import SolverStatus
|
||||
|
||||
|
||||
from shifts import SingleShift, RotaBuilder, days
|
||||
from workers import Worker
|
||||
|
||||
import datetime
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
use_neos = False
|
||||
use_cplex = False
|
||||
solve = True
|
||||
|
||||
weeks_to_rota = 13
|
||||
|
||||
time_to_run = 258500
|
||||
# allow = 5000
|
||||
ratio = 0.001
|
||||
|
||||
use_live_rota = False
|
||||
suspend_on_finish = False
|
||||
|
||||
|
||||
sites = ("truro", "exeter", "torbay", "barnstaple", "plymouth", "proc", "truro no proc")
|
||||
|
||||
Rota = RotaBuilder(
|
||||
# datetime.date(2022, 6, 6),
|
||||
datetime.date(2022, 3, 7),
|
||||
# Rota = RotaBuilder(datetime.date(2021, 12, 6),
|
||||
# Rota = RotaBuilder(datetime.date(2021, 11, 8),
|
||||
weeks_to_rota=weeks_to_rota,
|
||||
balance_offset_modifier=1,
|
||||
max_weekend_frequency=2,
|
||||
max_night_frequency=2,
|
||||
use_shift_balance_extra=True,
|
||||
use_bank_holiday_extra=True,
|
||||
)
|
||||
|
||||
Rota.constraint_options["limit_to_1_st2_on_nights"] = True
|
||||
Rota.constraint_options["ensure_1_st4_plus_on_nights"] = True
|
||||
Rota.constraint_options["balance_nights"] = True
|
||||
#Rota.constraint_options["constrain_time_off_after_nights"] = True
|
||||
Rota.constraint_options["balance_nights_across_sites"] = True
|
||||
Rota.constraint_options["balance_blocks"] = True
|
||||
Rota.constraint_options["balance_weekends"] = True
|
||||
Rota.constraint_options["balance_shifts_over_workers"] = True
|
||||
Rota.constraint_options["balance_bank_holidays"] = True
|
||||
Rota.constraint_options["avoid_st2_first_month"] = False
|
||||
Rota.constraint_options["hard_constrain_pair_separation"] = True
|
||||
Rota.constraint_options["max_weekends"] = 7
|
||||
#Rota.constraint_options["prevent_monday_and_tuesday_after_full_weekends"] = [
|
||||
# "truro",
|
||||
# "torbay",
|
||||
# "exeter",
|
||||
# "plymouth",
|
||||
# "plymouth no proc",
|
||||
# "truro no proc",
|
||||
#]
|
||||
#Rota.constraint_options["prevent_monday_after_full_weekends"] = [
|
||||
# "plymouth",
|
||||
# "plymouth no proc",
|
||||
#]
|
||||
#Rota.constraint_options["prevent_fridays_before_full_weekends"] = [
|
||||
# "plymouth",
|
||||
# "plymouth no proc",
|
||||
#]
|
||||
#Rota.constraint_options["prevent_thursdays_before_full_weekends"] = [
|
||||
# "truro",
|
||||
# "torbay",
|
||||
# "exeter",
|
||||
# "truro no proc",
|
||||
#]
|
||||
|
||||
Rota.add_shifts(
|
||||
SingleShift(
|
||||
("exeter",),
|
||||
"exeter_twilight",
|
||||
12.5,
|
||||
days[:4],
|
||||
balance_offset=3,
|
||||
constraints=["max_2_shifts_per_week"],
|
||||
),
|
||||
SingleShift(
|
||||
("truro", "truro no proc"), "truro_twilight", 12.5, days[:4], balance_offset=3
|
||||
),
|
||||
SingleShift(
|
||||
("torbay", "torbay twilights"),
|
||||
"torbay_twilight",
|
||||
12.5,
|
||||
days[:4],
|
||||
balance_offset=5,
|
||||
assign_as_block=True,
|
||||
),
|
||||
SingleShift(
|
||||
("plymouth", "plymouth_twilights"),
|
||||
"plymouth_twilight",
|
||||
12.5,
|
||||
days[:5],
|
||||
balance_offset=3,
|
||||
workers_required=1,
|
||||
constraints=["max_2_shifts_per_week"],
|
||||
),
|
||||
SingleShift(
|
||||
("exeter",),
|
||||
"weekend_exeter",
|
||||
12.5,
|
||||
days[4:],
|
||||
balance_offset=4,
|
||||
rota_on_nwds=True,
|
||||
force_as_block=True,
|
||||
constraints=["preclear2", "postclear2"],
|
||||
),
|
||||
SingleShift(
|
||||
("truro", "truro no proc"),
|
||||
"weekend_truro",
|
||||
12.5,
|
||||
days[4:],
|
||||
balance_offset=4,
|
||||
rota_on_nwds=True,
|
||||
force_as_block=True,
|
||||
constraints=["preclear2", "postclear2"],
|
||||
# force_as_block_unless_nwd=True
|
||||
),
|
||||
SingleShift(
|
||||
("torbay",),
|
||||
"weekend_torbay",
|
||||
12.5,
|
||||
days[4:],
|
||||
balance_offset=4,
|
||||
rota_on_nwds=True,
|
||||
force_as_block=True,
|
||||
constraints=["preclear2", "postclear2"],
|
||||
# force_as_block_unless_nwd=True
|
||||
),
|
||||
SingleShift(
|
||||
("plymouth",),
|
||||
"weekend_plymouth1",
|
||||
8,
|
||||
days[5:],
|
||||
balance_offset=4,
|
||||
workers_required=1,
|
||||
rota_on_nwds=True,
|
||||
force_as_block=True,
|
||||
constraints=["preclear2", "postclear2"],
|
||||
),
|
||||
SingleShift(
|
||||
("plymouth",),
|
||||
"weekend_plymouth2",
|
||||
8,
|
||||
days[5:],
|
||||
balance_offset=4,
|
||||
workers_required=1,
|
||||
rota_on_nwds=True,
|
||||
force_as_block=True,
|
||||
constraints=["preclear2", "postclear2"],
|
||||
),
|
||||
SingleShift(
|
||||
("plymouth", "plymouth_twilights"),
|
||||
"plymouth_bank_holidays",
|
||||
8,
|
||||
days[5:],
|
||||
balance_offset=2,
|
||||
workers_required=1,
|
||||
rota_on_nwds=True,
|
||||
force_as_block=False,
|
||||
bank_holidays_only=True,
|
||||
),
|
||||
SingleShift(
|
||||
(sites[:-1]),
|
||||
"night_weekday",
|
||||
12.25,
|
||||
days[:4],
|
||||
balance_offset=4.9,
|
||||
balance_weighting=1,
|
||||
# hard_constrain_shift=False,
|
||||
workers_required=3,
|
||||
force_as_block=True,
|
||||
rota_on_nwds=True,
|
||||
constraints=["night", "preclear2", "postclear2"],
|
||||
),
|
||||
SingleShift(
|
||||
(sites[:-1]),
|
||||
"night_weekend",
|
||||
12.25,
|
||||
days[4:],
|
||||
balance_offset=3.9,
|
||||
balance_weighting=1,
|
||||
# hard_constrain_shift=False,
|
||||
workers_required=3,
|
||||
force_as_block=True,
|
||||
rota_on_nwds=True,
|
||||
constraints=["night", "preclear2", "postclear2"],
|
||||
),
|
||||
)
|
||||
|
||||
use_test_workers = False
|
||||
|
||||
load_leave = True
|
||||
Rota.build_shifts()
|
||||
|
||||
if load_leave:
|
||||
import leave
|
||||
|
||||
workers = leave.load_leave(Rota, use_live_rota)
|
||||
|
||||
|
||||
# Import trainee data
|
||||
if use_test_workers:
|
||||
# Rota.add_workers([
|
||||
# Worker(Rota,
|
||||
# i,
|
||||
# "Truro {}".format(i),
|
||||
# "truro",
|
||||
# 3,
|
||||
# nwd=['Fri', "Sat", "Sun"]) for i in range(1, 2)
|
||||
# ])
|
||||
Rota.add_workers(
|
||||
[
|
||||
Worker(
|
||||
Rota,
|
||||
"Truro {}".format(i),
|
||||
"truro",
|
||||
4,
|
||||
id=i,
|
||||
)
|
||||
for i in range(1, 9)
|
||||
]
|
||||
)
|
||||
Rota.add_workers(
|
||||
[Worker(Rota, i, "Plym {}".format(i), "plymouth", 4, 50) for i in range(9, 17)]
|
||||
)
|
||||
Rota.add_workers(
|
||||
[
|
||||
Worker(
|
||||
Rota, i, "Plym {}".format(i), "plymouth", 4, 80, end_date="2020/10/05"
|
||||
)
|
||||
for i in range(17, 19)
|
||||
]
|
||||
)
|
||||
else:
|
||||
n = 0
|
||||
for worker in workers:
|
||||
n = n + 1
|
||||
worker_name = worker
|
||||
w = workers[worker]
|
||||
site = w["site"]
|
||||
grade = w["grade"]
|
||||
fte = w["fte"]
|
||||
nwd = w["nwd"]
|
||||
end_date = w["end_date"]
|
||||
oop = w["oop"]
|
||||
grade = w["grade"]
|
||||
pair = w["pair"]
|
||||
# name, site, grade, fte, nwd, end_date, oop, twi, twi_we, night, night_we, pw1, pw2, extra = row
|
||||
|
||||
# Ignore trainees if fte == 0 (what are they doing here anyway)
|
||||
if fte == 0 or fte == "0" or fte == "":
|
||||
continue
|
||||
|
||||
nwds = None
|
||||
if nwd:
|
||||
nwds = []
|
||||
for i in nwd.split(","):
|
||||
nwd_start_date = Rota.start_date
|
||||
nwd_end_date = Rota.rota_end_date
|
||||
|
||||
if "[" in i:
|
||||
a, b = i.split("[")[1][:-1].split("-")
|
||||
nwd_start_date = datetime.datetime.strptime(a, "%d/%m/%y").date()
|
||||
nwd_end_date = datetime.datetime.strptime(b, "%d/%m/%y").date()
|
||||
|
||||
nwds.append(
|
||||
(
|
||||
i.split("[")[0].strip()[:3].capitalize(),
|
||||
nwd_start_date,
|
||||
nwd_end_date,
|
||||
)
|
||||
)
|
||||
# nwds = [i.strip()[:3].capitalize() for i in nwd.split("/")] if nwd else None
|
||||
end_date = end_date if end_date else None
|
||||
oop = oop.split("-") if oop else None
|
||||
|
||||
previous_shifts = {}
|
||||
# if twi:
|
||||
# worked = twi.split(" ")[0]
|
||||
# allocated = twi.split(" ")[1][1:-1]
|
||||
# previous_shifts["{}_twilight".format(site.lower())] = (worked, allocated)
|
||||
# if twi_we:
|
||||
# worked = twi_we.split(" ")[0]
|
||||
# allocated = twi_we.split(" ")[1][1:-1]
|
||||
# previous_shifts["weekend_{}".format(site.lower())] = (worked, allocated)
|
||||
# if night:
|
||||
# worked = night.split(" ")[0]
|
||||
# allocated = night.split(" ")[1][1:-1]
|
||||
# previous_shifts["night_weekday".format(site.lower())] = (worked, allocated)
|
||||
# if night_we:
|
||||
# worked = night_we.split(" ")[0]
|
||||
# allocated = night_we.split(" ")[1][1:-1]
|
||||
# previous_shifts["night_weekend".format(site.lower())] = (worked, allocated)
|
||||
# if pw1:
|
||||
# worked = pw1.split(" ")[0]
|
||||
# allocated = pw1.split(" ")[1][1:-1]
|
||||
# previous_shifts["weekend_plymouth1"] = (worked, allocated)
|
||||
# if pw2:
|
||||
# worked = pw2.split(" ")[0]
|
||||
# allocated = pw2.split(" ")[1][1:-1]
|
||||
# previous_shifts["weekend_plymouth2"] = (worked, allocated)
|
||||
|
||||
leave_requests = w["leave"]
|
||||
work_requests = w["requests"]
|
||||
site_pref = w["site_pref"]
|
||||
|
||||
Rota.add_worker(
|
||||
Worker(
|
||||
Rota,
|
||||
worker_name,
|
||||
site.lower(),
|
||||
int(grade[2]),
|
||||
n,
|
||||
int(fte),
|
||||
nwds,
|
||||
end_date,
|
||||
oop,
|
||||
not_available_to_work=leave_requests,
|
||||
work_requests=work_requests,
|
||||
home_site=site_pref,
|
||||
previous_shifts=previous_shifts,
|
||||
pair=pair,
|
||||
shift_balance_extra=w["shift_balance_extra"],
|
||||
bank_holiday_extra=w["bank_holiday_extra"],
|
||||
)
|
||||
)
|
||||
|
||||
print("Building model")
|
||||
Rota.build_workers()
|
||||
|
||||
|
||||
Rota.sites.add("plymouth_twilights")
|
||||
|
||||
Rota.build_model()
|
||||
|
||||
print(Rota.get_worker_details())
|
||||
|
||||
if not solve:
|
||||
ResultsHolder = RotaResults(Rota)
|
||||
ResultsHolder.export_rota_to_html()
|
||||
sys.exit(0)
|
||||
|
||||
solver_options = {"ratio": ratio, "seconds": time_to_run, "threads": 10}
|
||||
|
||||
Rota.solve_model(options=solver_options)
|
||||
|
||||
# print(Rota.get_worker_details())
|
||||
|
||||
# optimizer = SolverFactory('cbc')
|
||||
# result = optimizer.solve(prob,tee=True)
|
||||
# result.Solver.Status = SolverStatus.warning
|
||||
# prob.solutions.load_from(result)
|
||||
|
||||
#ResultsHolder = RotaResults(Rota)
|
||||
|
||||
#worker_timetable_brief = ResultsHolder.get_worker_timetable_brief(
|
||||
# show_prefs=False, show_unavailable=False
|
||||
#)
|
||||
#
|
||||
#print(worker_timetable_brief)
|
||||
|
||||
|
||||
Rota.export_rota_to_html()
|
||||
Rota.export_rota_to_csv("rota")
|
||||
|
||||
if suspend_on_finish:
|
||||
os.system("systemctl suspend")
|
||||
@@ -323,7 +323,7 @@ else:
|
||||
oop,
|
||||
not_available_to_work=leave_requests,
|
||||
work_requests=work_requests,
|
||||
home_site=site_pref,
|
||||
remote_site=site_pref,
|
||||
previous_shifts=previous_shifts,
|
||||
pair=pair,
|
||||
shift_balance_extra=w["shift_balance_extra"],
|
||||
|
||||
+97
-104
@@ -10,7 +10,7 @@ import datetime
|
||||
from pyomo.environ import *
|
||||
from pyomo.opt import SolverFactory
|
||||
|
||||
from .workers import Worker
|
||||
from rota.workers import Worker
|
||||
|
||||
import json
|
||||
|
||||
@@ -100,10 +100,14 @@ class SingleShift(object):
|
||||
|
||||
self.workers_required = workers_required
|
||||
self.rota_on_nwds = rota_on_nwds
|
||||
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
|
||||
|
||||
if force_as_block_unless_nwd:
|
||||
self.assign_as_block = True
|
||||
|
||||
self.assign_as_block = assign_as_block
|
||||
|
||||
self.constraint_options = {}
|
||||
self.constraints = []
|
||||
for c in constraints:
|
||||
@@ -207,9 +211,11 @@ class RotaBuilder(object):
|
||||
"max_weekends": 100,
|
||||
"max_shifts_per_week": 7,
|
||||
"max_shifts_per_month": 40,
|
||||
# The following will cause an unsolvable problem if a shift
|
||||
# is forced as a block and spans the time peroid
|
||||
"prevent_monday_after_full_weekends": [],
|
||||
"prevent_monday_and_tuesday_after_full_weekends": [],
|
||||
"prevent_fridays_before_full_weekends": [],
|
||||
"prevent_fridays_before_full_weekends": [],
|
||||
"prevent_thursdays_before_full_weekends": [],
|
||||
# This may be better defined on the shift?
|
||||
"require_presence_at_site_overnight": [],
|
||||
@@ -804,9 +810,9 @@ class RotaBuilder(object):
|
||||
# rule=nightShiftDerrifordRule,
|
||||
# )
|
||||
|
||||
def presenceAtHomeSite(model, week, shift, required_site, required_number):
|
||||
def presenceAtRemoteSite(model, week, shift, required_site, required_number):
|
||||
required_site_workers = [
|
||||
w for w in self.workers if required_site == w.home_site
|
||||
w for w in self.workers if required_site == w.remote_site
|
||||
]
|
||||
|
||||
return (
|
||||
@@ -817,20 +823,20 @@ class RotaBuilder(object):
|
||||
>= required_number
|
||||
)
|
||||
|
||||
for shift in self.get_shifts_with_constraint("require_home_site_presence"):
|
||||
for shift in self.get_shifts_with_constraint("require_remote_site_presence"):
|
||||
site, required_number = shift.constraint_options[
|
||||
"require_home_site_presence"
|
||||
"require_remote_site_presence"
|
||||
]
|
||||
# self.model.require_presence_at_site_overnight_rule = Constraint(
|
||||
setattr(
|
||||
self.model,
|
||||
f"require_home_site_presence_{shift.name}",
|
||||
f"require_remote_site_presence_{shift.name}",
|
||||
Constraint(
|
||||
[week for week in self.weeks],
|
||||
[shift.name],
|
||||
[site],
|
||||
[required_number],
|
||||
rule=presenceAtHomeSite,
|
||||
rule=presenceAtRemoteSite,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -905,74 +911,35 @@ class RotaBuilder(object):
|
||||
)
|
||||
)
|
||||
|
||||
if shift.force_as_block:
|
||||
if shift.force_as_block_unless_nwd:
|
||||
workers = self.get_workers_for_shift(shift)
|
||||
# Get workers who have a nwd on the shift
|
||||
nwd_workers = []
|
||||
full_workers = []
|
||||
|
||||
if shift.force_as_block_unless_nwd:
|
||||
workers = self.get_workers_for_shift(shift)
|
||||
# Get workers who have a nwd on the shift
|
||||
nwd_workers = []
|
||||
full_workers = []
|
||||
|
||||
for w in workers:
|
||||
if w.nwd is not None:
|
||||
l = []
|
||||
# This should take into account dates!
|
||||
for nwd, d1, d2 in w.nwd:
|
||||
if nwd in shift.shift_days:
|
||||
l.append(w)
|
||||
|
||||
if l:
|
||||
nwd_workers.append(w)
|
||||
else:
|
||||
full_workers.append(w)
|
||||
for w in workers:
|
||||
if w.nwd:
|
||||
l = []
|
||||
# This should take into account dates!
|
||||
for nwd, start_nwd_date, end_nwd_date in w.nwd:
|
||||
if nwd in shift.shift_days:
|
||||
if start_nwd_date > self.get_week_start_date(week):
|
||||
continue
|
||||
if end_nwd_date < self.get_week_start_date(week):
|
||||
continue
|
||||
l.append(w)
|
||||
|
||||
if l:
|
||||
nwd_workers.append(w)
|
||||
else:
|
||||
full_workers.append(w)
|
||||
|
||||
if not nwd_workers: # Just do the usual
|
||||
self.model.constraints.add(
|
||||
sum(
|
||||
self.model.blocks_worker_shift_assigned[
|
||||
worker.id, week, shift.name
|
||||
]
|
||||
for worker in self.workers
|
||||
)
|
||||
<= 1
|
||||
)
|
||||
|
||||
else:
|
||||
self.model.constraints.add(
|
||||
sum(
|
||||
self.model.blocks_worker_shift_assigned[
|
||||
worker.id, week, shift.name
|
||||
]
|
||||
for worker in full_workers
|
||||
)
|
||||
<= 1
|
||||
)
|
||||
full_workers.append(w)
|
||||
|
||||
self.model.constraints.add(
|
||||
sum(
|
||||
self.model.blocks_worker_shift_assigned[
|
||||
worker.id, week, shift.name
|
||||
]
|
||||
for worker in workers
|
||||
)
|
||||
<= 2
|
||||
)
|
||||
print(nwd_workers)
|
||||
|
||||
# self.model.constraints.add(
|
||||
# self.model.worker_block_shift_split[week, shift.name] <= 2 - sum(self.model.blocks_worker_shift_assigned[worker.id,
|
||||
# week,
|
||||
# shift.name]
|
||||
# for worker in workers))
|
||||
#
|
||||
# self.model.constraints.add(
|
||||
# sum(self.model.works[w.id, week, day,shift.name]
|
||||
# for w in nwd_workers for day in shift.shift_days) - sum(self.model.works[w.id, week, day,shift.name]
|
||||
# for w in full_workers for day in shift.shift_days) >= self.model.worker_block_shift_split[week, shift.name] )
|
||||
|
||||
else:
|
||||
if not nwd_workers: # Just do the usual
|
||||
self.model.constraints.add(
|
||||
sum(
|
||||
self.model.blocks_worker_shift_assigned[
|
||||
@@ -983,6 +950,28 @@ class RotaBuilder(object):
|
||||
<= shift.workers_required
|
||||
)
|
||||
|
||||
else:
|
||||
self.model.constraints.add(
|
||||
sum(
|
||||
self.model.blocks_worker_shift_assigned[
|
||||
worker.id, week, shift.name
|
||||
]
|
||||
for worker in full_workers
|
||||
)
|
||||
<= shift.workers_required + 1
|
||||
)
|
||||
|
||||
elif shift.force_as_block:
|
||||
self.model.constraints.add(
|
||||
sum(
|
||||
self.model.blocks_worker_shift_assigned[
|
||||
worker.id, week, shift.name
|
||||
]
|
||||
for worker in self.workers
|
||||
)
|
||||
<= shift.workers_required
|
||||
)
|
||||
|
||||
# Most of our constraints apply per worker
|
||||
for worker in self.workers:
|
||||
|
||||
@@ -1355,7 +1344,7 @@ class RotaBuilder(object):
|
||||
for n, start, end in worker.nwd:
|
||||
if not shift.rota_on_nwds and day == n:
|
||||
# print(start, self.week_day_date_map[(week, day)], end)
|
||||
if start <= self.week_day_date_map[(week, day)] <= end:
|
||||
if start <= self.week_day_date_map[(week, day)] < end:
|
||||
self.model.constraints.add(
|
||||
0
|
||||
== self.model.works[
|
||||
@@ -1405,16 +1394,7 @@ class RotaBuilder(object):
|
||||
)
|
||||
)
|
||||
|
||||
# TODO: generic implementation
|
||||
# for shift in self.get_shifts_with_constraint("max_2_shifts_per_week"):
|
||||
# self.model.constraints.add(
|
||||
# 2
|
||||
# >= sum(
|
||||
# self.model.works[worker.id, week, day, shift.name]
|
||||
# for day in self.days
|
||||
# )
|
||||
# )
|
||||
|
||||
# TODO: consider excluding shifts that span relevant period
|
||||
if (
|
||||
worker.site
|
||||
in self.constraint_options["prevent_monday_after_full_weekends"]
|
||||
@@ -1463,21 +1443,18 @@ class RotaBuilder(object):
|
||||
for shift in self.get_shift_names_by_week_day(
|
||||
week, "Sun"
|
||||
)
|
||||
# for shift in self.get_shifts()
|
||||
)
|
||||
+ sum(
|
||||
self.model.works[worker.id, week + 1, "Mon", shift]
|
||||
for shift in self.get_shift_names_by_week_day(
|
||||
week + 1, "Mon"
|
||||
)
|
||||
# for shift in self.get_shifts()
|
||||
)
|
||||
+ sum(
|
||||
self.model.works[worker.id, week + 1, "Tue", shift]
|
||||
for shift in self.get_shift_names_by_week_day(
|
||||
week + 1, "Tue"
|
||||
)
|
||||
# for shift in self.get_shifts()
|
||||
)
|
||||
)
|
||||
if (
|
||||
@@ -1488,18 +1465,21 @@ class RotaBuilder(object):
|
||||
full_weekend_count
|
||||
>= sum(
|
||||
self.model.works[worker.id, week, "Sat", shift.name]
|
||||
for shift in self.get_shifts()
|
||||
if "night" not in shift.constraints
|
||||
for shift in self.get_shift_names_by_week_day(
|
||||
week, "Sat"
|
||||
)
|
||||
)
|
||||
+ sum(
|
||||
self.model.works[worker.id, week, "Sun", shift.name]
|
||||
for shift in self.get_shifts()
|
||||
if "night" not in shift.constraints
|
||||
for shift in self.get_shift_names_by_week_day(
|
||||
week, "Sat"
|
||||
)
|
||||
)
|
||||
+ sum(
|
||||
self.model.works[worker.id, week, "Fri", shift.name]
|
||||
for shift in self.get_shifts()
|
||||
if "night" not in shift.constraints
|
||||
for shift in self.get_shift_names_by_week_day(
|
||||
week, "Sat"
|
||||
)
|
||||
)
|
||||
)
|
||||
if (
|
||||
@@ -1510,18 +1490,21 @@ class RotaBuilder(object):
|
||||
full_weekend_count
|
||||
>= sum(
|
||||
self.model.works[worker.id, week, "Sat", shift.name]
|
||||
for shift in self.get_shifts()
|
||||
if "night" not in shift.constraints
|
||||
for shift in self.get_shift_names_by_week_day(
|
||||
week, "Sat"
|
||||
)
|
||||
)
|
||||
+ sum(
|
||||
self.model.works[worker.id, week, "Sun", shift.name]
|
||||
for shift in self.get_shifts()
|
||||
if "night" not in shift.constraints
|
||||
for shift in self.get_shift_names_by_week_day(
|
||||
week, "Sun"
|
||||
)
|
||||
)
|
||||
+ sum(
|
||||
self.model.works[worker.id, week, "Thu", shift.name]
|
||||
for shift in self.get_shifts()
|
||||
if "night" not in shift.constraints
|
||||
for shift in self.get_shift_names_by_week_day(
|
||||
week, "Thu"
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -2243,7 +2226,7 @@ class RotaBuilder(object):
|
||||
return [shift.name for shift in self.get_shifts() if shift.assign_as_block]
|
||||
|
||||
def shifts_to_force_as_blocks(self):
|
||||
return [shift.name for shift in self.get_shifts() if shift.force_as_block]
|
||||
return [shift.name for shift in self.get_shifts() if (shift.force_as_block or shift.force_as_block_unless_nwd)]
|
||||
|
||||
def shifts_to_assign_or_force_as_blocks(self):
|
||||
s = self.shifts_to_assign_as_blocks()
|
||||
@@ -2293,6 +2276,9 @@ class RotaBuilder(object):
|
||||
if self.week_day_date_map[(week, day)] in bank_holiday_map
|
||||
]
|
||||
|
||||
def get_week_start_date(self, week: int):
|
||||
return self.start_date + datetime.timedelta(weeks=week)
|
||||
|
||||
# RESULTS
|
||||
def export_rota_to_html(self, filename: str = "rota"):
|
||||
output_file = Path("output", f"{filename}.html")
|
||||
@@ -2300,7 +2286,7 @@ class RotaBuilder(object):
|
||||
f.write(self.get_worker_timetable_html(True))
|
||||
|
||||
def export_rota_to_csv(self, filename: str = "rota"):
|
||||
output_file = Path("output", f"{filename}.html")
|
||||
output_file = Path("output", f"{filename}.csv")
|
||||
works = self.model.works
|
||||
with open(output_file, "w", newline="") as f:
|
||||
wr = csv.writer(f, quoting=csv.QUOTE_ALL)
|
||||
@@ -2436,7 +2422,12 @@ class RotaBuilder(object):
|
||||
n = 0
|
||||
for week, day in self.get_week_day_combinations():
|
||||
d = self.start_date + datetime.timedelta(n)
|
||||
date_row.append(f"<th title='{d}'>Week {week}: {day}</th>")
|
||||
|
||||
th_class = ""
|
||||
if d in bank_holiday_map:
|
||||
th_class = "bank-holiday"
|
||||
|
||||
date_row.append(f"<th title='{d}' class='{th_class}' data-date='{d}'>Week {week}: {day}</th>")
|
||||
n = n + 1
|
||||
timetable.append(f"<tr class='data-row'>{''.join(date_row)}</tr>")
|
||||
|
||||
@@ -2453,11 +2444,12 @@ class RotaBuilder(object):
|
||||
current_site = worker.site
|
||||
|
||||
shifts = []
|
||||
if worker.nwd is not None:
|
||||
# TODO: limit to dates
|
||||
nwds = ", ".join([i[0] for i in worker.nwd])
|
||||
else:
|
||||
nwds = None
|
||||
nwds = json.dumps(worker.nwd, default=str)
|
||||
#if worker.nwd:
|
||||
# # TODO: limit to dates
|
||||
# nwds = ", ".join([i[0] for i in worker.nwd])
|
||||
#else:
|
||||
# nwds = None
|
||||
|
||||
worker_targets = json.dumps(worker.shift_target_number)
|
||||
|
||||
@@ -2501,6 +2493,7 @@ class RotaBuilder(object):
|
||||
|
||||
bank_holiday = ""
|
||||
if d in bank_holiday_map:
|
||||
title = f"{title} [Bank Holiday]"
|
||||
css_class = " ".join((css_class, "bank-holiday"))
|
||||
bank_holiday = f" data-bank-holiday='{bank_holiday_map[d]}'"
|
||||
|
||||
@@ -2536,7 +2529,7 @@ class RotaBuilder(object):
|
||||
data-shift-balance-extra='{shift_balance_extra}'
|
||||
data-bank-holiday-extra='{bank_holiday_extra}'
|
||||
data-shift-diff='{shift_diff}'
|
||||
data-shift-diff_summed='{shift_diff_summed}'
|
||||
data-shift-diff-summed='{shift_diff_summed}'
|
||||
>
|
||||
<span class='name' title='{name}'>{name}</span> ({grade}) [{fte}]</td>""".format(
|
||||
site=worker.site,
|
||||
|
||||
+23
-10
@@ -1,26 +1,33 @@
|
||||
import datetime
|
||||
from collections import defaultdict
|
||||
from typing import List
|
||||
|
||||
# from shifts import RotaBuilder, days, sites
|
||||
#from .shifts import RotaBuilder, days, sites
|
||||
import uuid
|
||||
|
||||
#from rota.shifts import RotaBuilder
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from rota.shifts import RotaBuilder
|
||||
|
||||
|
||||
class Worker:
|
||||
def __init__(
|
||||
self,
|
||||
Rota, # Aim to remove (workers should be independent of rotas)
|
||||
Rota: "RotaBuilder", # Aim to remove (workers should be independent of rotas)
|
||||
name: str,
|
||||
site: str,
|
||||
grade: int,
|
||||
id=None,
|
||||
fte: int = 100,
|
||||
nwd=None,
|
||||
end_date=None,
|
||||
nwd: list[str] | list[str, datetime.datetime, datetime.datetime]=[],
|
||||
end_date: datetime.datetime | None =None,
|
||||
oop=None,
|
||||
not_available_to_work=None,
|
||||
pref_not_to_work=None,
|
||||
work_requests=None,
|
||||
home_site: str = "plymouth", # We set a default proc_site
|
||||
remote_site: str = "plymouth", # We set a default proc_site
|
||||
previous_shifts: dict = {},
|
||||
shift_balance_extra: dict = {},
|
||||
bank_holiday_extra: int = 0,
|
||||
@@ -38,14 +45,14 @@ class Worker:
|
||||
# rules is easier.
|
||||
self.grade = grade
|
||||
self.fte = fte
|
||||
self.nwd = nwd
|
||||
self.nwd = []
|
||||
self.proportion_rota_to_work = 1
|
||||
self.pair = pair
|
||||
self.shift_balance_extra = shift_balance_extra
|
||||
self.bank_holiday_extra = bank_holiday_extra
|
||||
|
||||
self.home_site = home_site
|
||||
if home_site == "plymouth" or home_site == "":
|
||||
self.remote_site = remote_site
|
||||
if remote_site == "plymouth" or remote_site == "":
|
||||
self.night_at_derriford = 1
|
||||
else:
|
||||
self.night_at_derriford = 0
|
||||
@@ -155,7 +162,13 @@ class Worker:
|
||||
if self.fte_adj > 100:
|
||||
raise ValueError("{} : fte_ajd = {}".format(self.name, self.fte_adj))
|
||||
|
||||
# print(self.name, self.nwd)
|
||||
assert type(nwd) == list, f"nwd must be a list: {nwd=}"
|
||||
for item in nwd:
|
||||
match item:
|
||||
case (nwd_day, start_date, end_date):
|
||||
self.nwd.append((nwd_day, start_date, end_date))
|
||||
case nwd_day:
|
||||
self.nwd.append((nwd_day, Rota.start_date, Rota.rota_end_date))
|
||||
|
||||
def __lt__(self, other) -> bool:
|
||||
return (self.site, self.grade, self.fte_adj, self.name) < (
|
||||
@@ -172,7 +185,7 @@ class Worker:
|
||||
self.name,
|
||||
(self.site, self.grade, self.fte_adj),
|
||||
nwd,
|
||||
self.home_site,
|
||||
self.remote_site,
|
||||
self.night_at_derriford,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user