Commit ·
332d1dc
1
Parent(s): e772992
fix: 期間境界をまたぐ連続勤務日数のチェックを実装 (#23)
Browse files直前期間の末尾(最大6日分)の出勤状況をDBから取得し、新しい期間の
7日間スライディングウィンドウ制約に連結することで、期間をまたいで
最大連続勤務日数を超えるシフトが組まれないようにした。
solve()のPhase1・_solve_best_effortのPhase2の両方で
_load_prev_period_tail() / _add_consecutive_day_constraints() を
共通利用する。
- optimizer/solver.py +68 -10
optimizer/solver.py
CHANGED
|
@@ -26,6 +26,9 @@ class SolveResult:
|
|
| 26 |
# スケール係数のプリセット
|
| 27 |
PRIORITY_SCALE = {"低": 0.1, "中": 1.0, "高": 10.0}
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
@dataclass
|
| 31 |
class SolverConfig:
|
|
@@ -102,6 +105,58 @@ class Phase2ProgressCallback(cp_model.CpSolverSolutionCallback):
|
|
| 102 |
pass
|
| 103 |
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
def solve(
|
| 106 |
period: SchedulePeriod,
|
| 107 |
employees: list[Employee],
|
|
@@ -518,11 +573,11 @@ def solve(
|
|
| 518 |
worked_day[emp.id][ds] = wd
|
| 519 |
|
| 520 |
# 6c. 連続勤務日数を制限(7日間スライディングウィンドウ・ハード制約 / Issue #5)
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
|
| 527 |
# 6d. 正社員は期間内(半月=2週間)で希望休含め最低日数の休みを取得(ハード制約 / Issue #4)
|
| 528 |
if len(date_strs) > min_days_off:
|
|
@@ -817,6 +872,7 @@ def solve(
|
|
| 817 |
period_id=period_id or (progress_callback._period_id if progress_callback else None),
|
| 818 |
max_consecutive_days=max_consecutive_days,
|
| 819 |
min_days_off=min_days_off,
|
|
|
|
| 820 |
)
|
| 821 |
_log_employee_analysis(best_assignments, active_employees, date_strs, slots, positions,
|
| 822 |
req_map, req_hours, shift_constraints)
|
|
@@ -1010,6 +1066,7 @@ def _solve_best_effort(
|
|
| 1010 |
period_id: int | None = None,
|
| 1011 |
max_consecutive_days: int = 6,
|
| 1012 |
min_days_off: int = 5,
|
|
|
|
| 1013 |
) -> tuple[list[ShiftAssignment], list[str]]:
|
| 1014 |
"""人数・リーダー制約をソフト化してベストエフォートのシフトを生成する"""
|
| 1015 |
import time
|
|
@@ -1026,6 +1083,8 @@ def _solve_best_effort(
|
|
| 1026 |
reserv_tiers_d = []
|
| 1027 |
if long_breakfast_set is None:
|
| 1028 |
long_breakfast_set = set()
|
|
|
|
|
|
|
| 1029 |
model = cp_model.CpModel()
|
| 1030 |
|
| 1031 |
assign: dict = {}
|
|
@@ -1126,11 +1185,10 @@ def _solve_best_effort(
|
|
| 1126 |
worked_day[emp.id][ds] = wd
|
| 1127 |
|
| 1128 |
# 連続勤務日数を制限(7日間スライディングウィンドウ・ハード制約 / Issue #5)
|
| 1129 |
-
|
| 1130 |
-
|
| 1131 |
-
|
| 1132 |
-
|
| 1133 |
-
model.add(sum(worked_day[emp.id][d] for d in window) <= max_consecutive_days)
|
| 1134 |
|
| 1135 |
# 正社員は期間内(半月=2週間)で希望休含め最低日数の休みを取得(ハード制約 / Issue #4)
|
| 1136 |
if len(date_strs) > min_days_off:
|
|
|
|
| 26 |
# スケール係数のプリセット
|
| 27 |
PRIORITY_SCALE = {"低": 0.1, "中": 1.0, "高": 10.0}
|
| 28 |
|
| 29 |
+
# 連続勤務日数チェックのスライディングウィンドウ幅(Issue #5・#23)
|
| 30 |
+
CONSECUTIVE_WINDOW = 7
|
| 31 |
+
|
| 32 |
|
| 33 |
@dataclass
|
| 34 |
class SolverConfig:
|
|
|
|
| 105 |
pass
|
| 106 |
|
| 107 |
|
| 108 |
+
def _load_prev_period_tail(
|
| 109 |
+
period: SchedulePeriod, active_employees: list[Employee], tail_len: int
|
| 110 |
+
) -> dict[int, list[int]]:
|
| 111 |
+
"""直前期間(period.start_dateの前日がend_dateの期間)の末尾 tail_len 日分について、
|
| 112 |
+
各従業員の出勤有無(1/0)を古い日付順のリストで返す(Issue #23)。
|
| 113 |
+
直前期間が存在しない、または確定シフトが未生成の場合は各日0として扱う。"""
|
| 114 |
+
from datetime import timedelta
|
| 115 |
+
from db import repositories as repo
|
| 116 |
+
|
| 117 |
+
prev_start = date.fromisoformat(period.start_date) - timedelta(days=1)
|
| 118 |
+
prev = next(
|
| 119 |
+
(p for p in repo.get_all_periods()
|
| 120 |
+
if p.id != period.id and p.end_date == prev_start.isoformat()),
|
| 121 |
+
None,
|
| 122 |
+
)
|
| 123 |
+
if prev is None:
|
| 124 |
+
return {}
|
| 125 |
+
|
| 126 |
+
tail_dates = [d.isoformat() for d in prev.date_range()[-tail_len:]]
|
| 127 |
+
if not tail_dates:
|
| 128 |
+
return {}
|
| 129 |
+
worked_set = {(a.employee_id, a.date) for a in repo.get_assignments(prev.id)}
|
| 130 |
+
return {
|
| 131 |
+
emp.id: [1 if (emp.id, ds) in worked_set else 0 for ds in tail_dates]
|
| 132 |
+
for emp in active_employees
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def _add_consecutive_day_constraints(
|
| 137 |
+
model: cp_model.CpModel,
|
| 138 |
+
worked_day: dict[int, dict[str, object]],
|
| 139 |
+
active_employees: list[Employee],
|
| 140 |
+
date_strs: list[str],
|
| 141 |
+
max_consecutive_days: int,
|
| 142 |
+
prev_tail: dict[int, list[int]],
|
| 143 |
+
):
|
| 144 |
+
"""7日間スライディングウィンドウで連続勤務日数を制限する(ハード制約 / Issue #5)。
|
| 145 |
+
直前期間末尾との連結ウィンドウもチェックし、期間境界をまたぐ連続勤務を防ぐ(Issue #23)。"""
|
| 146 |
+
for emp in active_employees:
|
| 147 |
+
for i in range(len(date_strs) - CONSECUTIVE_WINDOW + 1):
|
| 148 |
+
window = date_strs[i:i + CONSECUTIVE_WINDOW]
|
| 149 |
+
model.add(sum(worked_day[emp.id][d] for d in window) <= max_consecutive_days)
|
| 150 |
+
|
| 151 |
+
tail = prev_tail.get(emp.id, [])
|
| 152 |
+
for i in range(len(tail)):
|
| 153 |
+
new_days_needed = CONSECUTIVE_WINDOW - (len(tail) - i)
|
| 154 |
+
if new_days_needed > len(date_strs):
|
| 155 |
+
continue
|
| 156 |
+
terms = tail[i:] + [worked_day[emp.id][d] for d in date_strs[:new_days_needed]]
|
| 157 |
+
model.add(sum(terms) <= max_consecutive_days)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
def solve(
|
| 161 |
period: SchedulePeriod,
|
| 162 |
employees: list[Employee],
|
|
|
|
| 573 |
worked_day[emp.id][ds] = wd
|
| 574 |
|
| 575 |
# 6c. 連続勤務日数を制限(7日間スライディングウィンドウ・ハード制約 / Issue #5)
|
| 576 |
+
# 直前期間末尾の出勤状況も連結してチェックし、期間境界をまたぐ連続勤務を防ぐ(Issue #23)
|
| 577 |
+
prev_tail = _load_prev_period_tail(period, active_employees, CONSECUTIVE_WINDOW - 1)
|
| 578 |
+
_add_consecutive_day_constraints(
|
| 579 |
+
model, worked_day, active_employees, date_strs, max_consecutive_days, prev_tail
|
| 580 |
+
)
|
| 581 |
|
| 582 |
# 6d. 正社員は期間内(半月=2週間)で希望休含め最低日数の休みを取得(ハード制約 / Issue #4)
|
| 583 |
if len(date_strs) > min_days_off:
|
|
|
|
| 872 |
period_id=period_id or (progress_callback._period_id if progress_callback else None),
|
| 873 |
max_consecutive_days=max_consecutive_days,
|
| 874 |
min_days_off=min_days_off,
|
| 875 |
+
prev_tail=prev_tail,
|
| 876 |
)
|
| 877 |
_log_employee_analysis(best_assignments, active_employees, date_strs, slots, positions,
|
| 878 |
req_map, req_hours, shift_constraints)
|
|
|
|
| 1066 |
period_id: int | None = None,
|
| 1067 |
max_consecutive_days: int = 6,
|
| 1068 |
min_days_off: int = 5,
|
| 1069 |
+
prev_tail: dict[int, list[int]] | None = None,
|
| 1070 |
) -> tuple[list[ShiftAssignment], list[str]]:
|
| 1071 |
"""人数・リーダー制約をソフト化してベストエフォートのシフトを生成する"""
|
| 1072 |
import time
|
|
|
|
| 1083 |
reserv_tiers_d = []
|
| 1084 |
if long_breakfast_set is None:
|
| 1085 |
long_breakfast_set = set()
|
| 1086 |
+
if prev_tail is None:
|
| 1087 |
+
prev_tail = {}
|
| 1088 |
model = cp_model.CpModel()
|
| 1089 |
|
| 1090 |
assign: dict = {}
|
|
|
|
| 1185 |
worked_day[emp.id][ds] = wd
|
| 1186 |
|
| 1187 |
# 連続勤務日数を制限(7日間スライディングウィンドウ・ハード制約 / Issue #5)
|
| 1188 |
+
# 直前期間末尾の出勤状況も連結してチェックする(Issue #23)
|
| 1189 |
+
_add_consecutive_day_constraints(
|
| 1190 |
+
model, worked_day, active_employees, date_strs, max_consecutive_days, prev_tail
|
| 1191 |
+
)
|
|
|
|
| 1192 |
|
| 1193 |
# 正社員は期間内(半月=2週間)で希望休含め最低日数の休みを取得(ハード制約 / Issue #4)
|
| 1194 |
if len(date_strs) > min_days_off:
|