Ryo_Shimano commited on
Commit ·
5518892
1
Parent(s): 01a549c
Revert "正社員の休希望をソフト制約化して休日5日を厳守しPT割当を抑制"
Browse filesThis reverts commit 01a549cf94cba0452f07205986e0b2be9880e31a.
- optimizer/solver.py +34 -69
optimizer/solver.py
CHANGED
|
@@ -267,20 +267,14 @@ def solve(
|
|
| 267 |
|
| 268 |
# 希望シフト(アルバイト用)と社員の休希望・有休を分けて管理
|
| 269 |
req_map: dict[tuple[int, str, str], bool] = {}
|
| 270 |
-
off_map: dict[tuple[int, str], bool] = {}
|
| 271 |
-
leave_map: dict[tuple[int, str], bool] = {} # 有休のみ(FTハード禁止)
|
| 272 |
-
off_pref_map: dict[tuple[int, str], bool] = {} # 休希望のみ(FTソフト選好)
|
| 273 |
# 社員のフレックス出勤(朝のみ可→ディナー不可、晩のみ可→朝食不可)
|
| 274 |
slot_block_map: dict[tuple[int, str], TimeSlot] = {}
|
| 275 |
# 朝食「ロング勤務」(〜15:30頃まで)希望の (employee_id, date) 集合
|
| 276 |
long_breakfast_set: set[tuple[int, str]] = set()
|
| 277 |
for r in requests:
|
| 278 |
-
if r.pattern_id
|
| 279 |
off_map[(r.employee_id, r.date)] = True
|
| 280 |
-
leave_map[(r.employee_id, r.date)] = True
|
| 281 |
-
elif r.pattern_id == "off_request":
|
| 282 |
-
off_map[(r.employee_id, r.date)] = True
|
| 283 |
-
off_pref_map[(r.employee_id, r.date)] = True
|
| 284 |
elif r.pattern_id == "am_only":
|
| 285 |
slot_block_map[(r.employee_id, r.date)] = TimeSlot.DINNER
|
| 286 |
elif r.pattern_id == "pm_only":
|
|
@@ -379,7 +373,7 @@ def solve(
|
|
| 379 |
for e in active_employees:
|
| 380 |
# FIX①: FT社員を正しくカウント(実際の可用性チェックと統一)
|
| 381 |
if e.employment_type == EmploymentType.FULL_TIME:
|
| 382 |
-
ok = not
|
| 383 |
elif slot == TimeSlot.BREAKFAST and e.always_available_breakfast:
|
| 384 |
ok = not off_map.get((e.id, ds), False)
|
| 385 |
elif slot == TimeSlot.DINNER and e.always_available_dinner:
|
|
@@ -481,9 +475,9 @@ def solve(
|
|
| 481 |
for ds in date_strs:
|
| 482 |
for slot in slots:
|
| 483 |
if emp.employment_type == EmploymentType.FULL_TIME:
|
| 484 |
-
# 社員:有休の日以外は常時出勤可
|
| 485 |
# (朝のみ可/晩のみ可の日は該当しない時間帯への配置を禁止)
|
| 486 |
-
can_work = not
|
| 487 |
and slot_block_map.get((emp.id, ds)) != slot
|
| 488 |
elif slot == TimeSlot.BREAKFAST and emp.always_available_breakfast:
|
| 489 |
can_work = not off_map.get((emp.id, ds), False)
|
|
@@ -596,7 +590,7 @@ def solve(
|
|
| 596 |
emp for emp in active_employees
|
| 597 |
if emp.can_open and (
|
| 598 |
(emp.employment_type == EmploymentType.FULL_TIME
|
| 599 |
-
and not
|
| 600 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST)
|
| 601 |
or (emp.always_available_breakfast
|
| 602 |
and breakfast_start_hour.get((emp.id, ds), 6.0) <= 6.0)
|
|
@@ -620,7 +614,7 @@ def solve(
|
|
| 620 |
for emp in active_employees
|
| 621 |
if emp.employment_type == EmploymentType.FULL_TIME
|
| 622 |
and emp.id not in can_open_ids
|
| 623 |
-
and not
|
| 624 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST
|
| 625 |
]
|
| 626 |
if ft_fallback_vars:
|
|
@@ -667,7 +661,7 @@ def solve(
|
|
| 667 |
cln_emps = [
|
| 668 |
emp for emp in cleanup_pool
|
| 669 |
if (emp.employment_type == EmploymentType.FULL_TIME
|
| 670 |
-
and not
|
| 671 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST)
|
| 672 |
or (emp.always_available_breakfast
|
| 673 |
and breakfast_end_hour.get((emp.id, ds), 11.0) >= 11.0)
|
|
@@ -690,7 +684,7 @@ def solve(
|
|
| 690 |
for emp in active_employees
|
| 691 |
if emp.employment_type == EmploymentType.FULL_TIME
|
| 692 |
and emp.id not in cleanup_emp_ids
|
| 693 |
-
and not
|
| 694 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST
|
| 695 |
]
|
| 696 |
if ft_fallback_vars:
|
|
@@ -714,7 +708,7 @@ def solve(
|
|
| 714 |
if req_map.get((emp.id, ds, TimeSlot.BREAKFAST.value))
|
| 715 |
or emp.always_available_breakfast
|
| 716 |
or (emp.employment_type == EmploymentType.FULL_TIME
|
| 717 |
-
and not
|
| 718 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST)
|
| 719 |
]
|
| 720 |
if min_cln > 0 and cln_vars:
|
|
@@ -727,7 +721,7 @@ def solve(
|
|
| 727 |
if req_map.get((emp.id, ds, TimeSlot.BREAKFAST.value))
|
| 728 |
or emp.always_available_breakfast
|
| 729 |
or (emp.employment_type == EmploymentType.FULL_TIME
|
| 730 |
-
and not
|
| 731 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST)
|
| 732 |
]
|
| 733 |
if ldr_cln_vars:
|
|
@@ -843,12 +837,11 @@ def solve(
|
|
| 843 |
model.add(sum(worked_day[emp.id][d] for d in date_strs) <= max_work_days)
|
| 844 |
if emp.employment_type == EmploymentType.FULL_TIME:
|
| 845 |
# FT社員: 下限もハード(休日数をちょうど min_days_off 日に強制)
|
| 846 |
-
#
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
1 for d in date_strs if not leave_map.get((emp.id, d), False)
|
| 850 |
)
|
| 851 |
-
hard_lower = min(max_work_days,
|
| 852 |
if hard_lower > 0:
|
| 853 |
model.add(sum(worked_day[emp.id][d] for d in date_strs) >= hard_lower)
|
| 854 |
else:
|
|
@@ -1038,7 +1031,7 @@ def solve(
|
|
| 1038 |
for emp in active_employees:
|
| 1039 |
if emp.employment_type != EmploymentType.FULL_TIME:
|
| 1040 |
continue
|
| 1041 |
-
if
|
| 1042 |
continue
|
| 1043 |
if slot_block_map.get((emp.id, ds)) == slot:
|
| 1044 |
continue
|
|
@@ -1090,22 +1083,6 @@ def solve(
|
|
| 1090 |
model.add(sum(ft_hall_vars) <= 2)
|
| 1091 |
logger.info(" [P6 FTホール最大2名] 全日付・全スロットにハード制約を追加")
|
| 1092 |
|
| 1093 |
-
# P7: 正社員が休希望日に出勤した場合のソフトペナルティ
|
| 1094 |
-
# 有休(leave_map)はハード禁止。休希望(off_pref_map)はPT割当ペナルティより低く設定し、
|
| 1095 |
-
# PT使用(ディナー300k)よりFT出勤(200k)を優先させる。
|
| 1096 |
-
FT_OFF_REQ_PENALTY = 200_000
|
| 1097 |
-
for emp in active_employees:
|
| 1098 |
-
if emp.employment_type != EmploymentType.FULL_TIME:
|
| 1099 |
-
continue
|
| 1100 |
-
for ds in date_strs:
|
| 1101 |
-
if not off_pref_map.get((emp.id, ds), False):
|
| 1102 |
-
continue
|
| 1103 |
-
worked_on_off_req = sum(
|
| 1104 |
-
assign[emp.id][ds][slot.value][pos.value]
|
| 1105 |
-
for slot in slots for pos in positions
|
| 1106 |
-
)
|
| 1107 |
-
penalty_terms.append(FT_OFF_REQ_PENALTY * worked_on_off_req)
|
| 1108 |
-
|
| 1109 |
model.minimize(cp_model.LinearExpr.Sum(penalty_terms))
|
| 1110 |
|
| 1111 |
# ── 求解 ────────────────────────────────────────────────────────────
|
|
@@ -1151,8 +1128,7 @@ def solve(
|
|
| 1151 |
result_assignments = _extract_assignments(solver, assign, active_employees, date_strs, slots, positions)
|
| 1152 |
_log_employee_analysis(result_assignments, active_employees, date_strs, slots, positions,
|
| 1153 |
req_map, req_hours, shift_constraints,
|
| 1154 |
-
off_map=off_map, min_days_off=min_days_off
|
| 1155 |
-
leave_map=leave_map, off_pref_map=off_pref_map)
|
| 1156 |
_log_assignment_summary(result_assignments, date_strs)
|
| 1157 |
_check_warnings(result_assignments, date_strs, warnings,
|
| 1158 |
reservation_counts=reservation_counts,
|
|
@@ -1172,7 +1148,7 @@ def solve(
|
|
| 1172 |
else:
|
| 1173 |
logger.error(f" [INFEASIBLE/UNKNOWN] status={status_name} → フェーズ2へ移行")
|
| 1174 |
# 実行不可能 → 制約違反の診断メッセージを収集
|
| 1175 |
-
errs = _diagnose_infeasible(active_employees, date_strs, req_map, off_map
|
| 1176 |
for e in errs:
|
| 1177 |
logger.error(f" [診断] {e}")
|
| 1178 |
|
|
@@ -1196,12 +1172,10 @@ def solve(
|
|
| 1196 |
prev_tail=prev_tail,
|
| 1197 |
breakfast_start_hour=breakfast_start_hour,
|
| 1198 |
breakfast_end_hour=breakfast_end_hour,
|
| 1199 |
-
leave_map=leave_map,
|
| 1200 |
)
|
| 1201 |
_log_employee_analysis(best_assignments, active_employees, date_strs, slots, positions,
|
| 1202 |
req_map, req_hours, shift_constraints,
|
| 1203 |
-
off_map=off_map, min_days_off=min_days_off
|
| 1204 |
-
leave_map=leave_map, off_pref_map=off_pref_map)
|
| 1205 |
_check_warnings(best_assignments, date_strs, best_warnings,
|
| 1206 |
reservation_counts=reservation_counts,
|
| 1207 |
reserv_tiers_b=reserv_tiers_b, reserv_tiers_d=reserv_tiers_d)
|
|
@@ -1223,8 +1197,7 @@ def solve(
|
|
| 1223 |
|
| 1224 |
def _log_employee_analysis(assignments, active_employees, date_strs, slots, positions,
|
| 1225 |
req_map, req_hours, shift_constraints,
|
| 1226 |
-
off_map: dict | None = None, min_days_off: int = 5
|
| 1227 |
-
leave_map: dict | None = None, off_pref_map: dict | None = None):
|
| 1228 |
"""スタッフ別の割当状況・ペナルティ根拠・リーダー/SI診断を詳細ログ出力"""
|
| 1229 |
from collections import defaultdict
|
| 1230 |
SOCIAL_INSURANCE_MIN_HOURS = 9.0 # 実働8h + 休憩1h(duration_hours は休憩考慮なし総拘束時間)
|
|
@@ -1267,24 +1240,21 @@ def _log_employee_analysis(assignments, active_employees, date_strs, slots, posi
|
|
| 1267 |
if ft_emps:
|
| 1268 |
max_work_days = len(date_strs) - min_days_off if len(date_strs) > min_days_off else len(date_strs)
|
| 1269 |
logger.info(" [FT社員別勤務日数]")
|
| 1270 |
-
logger.info(f" {'名前':<12} {'
|
| 1271 |
-
_lmap = leave_map or {}
|
| 1272 |
-
_pmap = off_pref_map or {}
|
| 1273 |
for emp in sorted(ft_emps, key=lambda e: e.name):
|
| 1274 |
-
|
| 1275 |
-
|
| 1276 |
-
non_leave_days = len(date_strs) - leave_days
|
| 1277 |
worked_dates = {a.date for a in assignments if a.employee_id == emp.id}
|
| 1278 |
work_days = len(worked_dates)
|
| 1279 |
rest_days = len(date_strs) - work_days
|
| 1280 |
-
ok = "✓" if work_days >= max_work_days or
|
| 1281 |
note = ""
|
| 1282 |
-
if
|
| 1283 |
-
note = f"出勤可能日({
|
| 1284 |
elif work_days < max_work_days:
|
| 1285 |
short = max_work_days - work_days
|
| 1286 |
note = f"未達({short}日不足)"
|
| 1287 |
-
logger.info(f" {emp.name:<12} {
|
| 1288 |
|
| 1289 |
# ── PT スタッフ別詳細 ───────────────────────────────────────────────
|
| 1290 |
pt_emps = [e for e in active_employees if e.employment_type != EmploymentType.FULL_TIME]
|
|
@@ -1438,13 +1408,10 @@ def _solve_best_effort(
|
|
| 1438 |
prev_tail: dict[int, list[int]] | None = None,
|
| 1439 |
breakfast_start_hour: dict | None = None,
|
| 1440 |
breakfast_end_hour: dict | None = None,
|
| 1441 |
-
leave_map: dict | None = None,
|
| 1442 |
) -> tuple[list[ShiftAssignment], list[str]]:
|
| 1443 |
"""人数・リーダー制約をソフト化してベストエフォートのシフトを生成する"""
|
| 1444 |
if config is None:
|
| 1445 |
config = SolverConfig()
|
| 1446 |
-
# FTの有休ハード禁止マップ。未提供時はoff_map全体で代替(後方互換)
|
| 1447 |
-
_leave = leave_map if leave_map is not None else off_map
|
| 1448 |
if shift_constraints is None:
|
| 1449 |
from db import repositories as repo
|
| 1450 |
shift_constraints = repo.get_shift_constraints()
|
|
@@ -1492,7 +1459,7 @@ def _solve_best_effort(
|
|
| 1492 |
for ds in date_strs:
|
| 1493 |
for slot in slots:
|
| 1494 |
if emp.employment_type == EmploymentType.FULL_TIME:
|
| 1495 |
-
can_work = not
|
| 1496 |
and slot_block_map.get((emp.id, ds)) != slot
|
| 1497 |
elif slot == TimeSlot.BREAKFAST and emp.always_available_breakfast:
|
| 1498 |
can_work = not off_map.get((emp.id, ds), False)
|
|
@@ -1689,7 +1656,7 @@ def _solve_best_effort(
|
|
| 1689 |
emp for emp in active_employees
|
| 1690 |
if emp.can_open and (
|
| 1691 |
(emp.employment_type == EmploymentType.FULL_TIME
|
| 1692 |
-
and not
|
| 1693 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST)
|
| 1694 |
or (emp.always_available_breakfast
|
| 1695 |
and breakfast_start_hour.get((emp.id, ds), 6.0) <= 6.0)
|
|
@@ -1713,7 +1680,7 @@ def _solve_best_effort(
|
|
| 1713 |
for emp in active_employees
|
| 1714 |
if emp.employment_type == EmploymentType.FULL_TIME
|
| 1715 |
and emp.id not in can_open_ids
|
| 1716 |
-
and not
|
| 1717 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST
|
| 1718 |
]
|
| 1719 |
if ft_fallback_vars:
|
|
@@ -1735,7 +1702,7 @@ def _solve_best_effort(
|
|
| 1735 |
for emp in active_employees
|
| 1736 |
if emp.can_cleanup and (
|
| 1737 |
(emp.employment_type == EmploymentType.FULL_TIME
|
| 1738 |
-
and not
|
| 1739 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST)
|
| 1740 |
or (emp.always_available_breakfast
|
| 1741 |
and breakfast_end_hour.get((emp.id, ds), 11.0) >= 11.0)
|
|
@@ -1758,7 +1725,7 @@ def _solve_best_effort(
|
|
| 1758 |
for emp in active_employees
|
| 1759 |
if emp.employment_type == EmploymentType.FULL_TIME
|
| 1760 |
and emp.id not in cleanup_emp_ids
|
| 1761 |
-
and not
|
| 1762 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST
|
| 1763 |
]
|
| 1764 |
if ft_fallback_vars:
|
|
@@ -1933,7 +1900,6 @@ def _diagnose_infeasible(
|
|
| 1933 |
date_strs: list[str],
|
| 1934 |
req_map: dict,
|
| 1935 |
off_map: dict | None = None,
|
| 1936 |
-
leave_map: dict | None = None,
|
| 1937 |
) -> list[str]:
|
| 1938 |
"""実行不可能の原因を診断してエラーメッセージを返す"""
|
| 1939 |
errors = []
|
|
@@ -1941,7 +1907,6 @@ def _diagnose_infeasible(
|
|
| 1941 |
shift_constraints = repo.get_shift_constraints()
|
| 1942 |
if off_map is None:
|
| 1943 |
off_map = {}
|
| 1944 |
-
_leave = leave_map if leave_map is not None else off_map
|
| 1945 |
|
| 1946 |
for ds in date_strs:
|
| 1947 |
d = date.fromisoformat(ds)
|
|
@@ -1962,8 +1927,8 @@ def _diagnose_infeasible(
|
|
| 1962 |
or e.primary_position.value == pos.value):
|
| 1963 |
continue
|
| 1964 |
if e.employment_type == EmploymentType.FULL_TIME:
|
| 1965 |
-
# 正社員:
|
| 1966 |
-
if
|
| 1967 |
continue
|
| 1968 |
available.append(e)
|
| 1969 |
else:
|
|
|
|
| 267 |
|
| 268 |
# 希望シフト(アルバイト用)と社員の休希望・有休を分けて管理
|
| 269 |
req_map: dict[tuple[int, str, str], bool] = {}
|
| 270 |
+
off_map: dict[tuple[int, str], bool] = {} # 社員の休希望・有休
|
|
|
|
|
|
|
| 271 |
# 社員のフレックス出勤(朝のみ可→ディナー不可、晩のみ可→朝食不可)
|
| 272 |
slot_block_map: dict[tuple[int, str], TimeSlot] = {}
|
| 273 |
# 朝食「ロング勤務」(〜15:30頃まで)希望の (employee_id, date) 集合
|
| 274 |
long_breakfast_set: set[tuple[int, str]] = set()
|
| 275 |
for r in requests:
|
| 276 |
+
if r.pattern_id in ("off_request", "paid_leave"):
|
| 277 |
off_map[(r.employee_id, r.date)] = True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
elif r.pattern_id == "am_only":
|
| 279 |
slot_block_map[(r.employee_id, r.date)] = TimeSlot.DINNER
|
| 280 |
elif r.pattern_id == "pm_only":
|
|
|
|
| 373 |
for e in active_employees:
|
| 374 |
# FIX①: FT社員を正しくカウント(実際の可用性チェックと統一)
|
| 375 |
if e.employment_type == EmploymentType.FULL_TIME:
|
| 376 |
+
ok = not off_map.get((e.id, ds), False)
|
| 377 |
elif slot == TimeSlot.BREAKFAST and e.always_available_breakfast:
|
| 378 |
ok = not off_map.get((e.id, ds), False)
|
| 379 |
elif slot == TimeSlot.DINNER and e.always_available_dinner:
|
|
|
|
| 475 |
for ds in date_strs:
|
| 476 |
for slot in slots:
|
| 477 |
if emp.employment_type == EmploymentType.FULL_TIME:
|
| 478 |
+
# 社員:休希望・有休の日以外は常時出勤可
|
| 479 |
# (朝のみ可/晩のみ可の日は該当しない時間帯への配置を禁止)
|
| 480 |
+
can_work = not off_map.get((emp.id, ds), False) \
|
| 481 |
and slot_block_map.get((emp.id, ds)) != slot
|
| 482 |
elif slot == TimeSlot.BREAKFAST and emp.always_available_breakfast:
|
| 483 |
can_work = not off_map.get((emp.id, ds), False)
|
|
|
|
| 590 |
emp for emp in active_employees
|
| 591 |
if emp.can_open and (
|
| 592 |
(emp.employment_type == EmploymentType.FULL_TIME
|
| 593 |
+
and not off_map.get((emp.id, ds), False)
|
| 594 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST)
|
| 595 |
or (emp.always_available_breakfast
|
| 596 |
and breakfast_start_hour.get((emp.id, ds), 6.0) <= 6.0)
|
|
|
|
| 614 |
for emp in active_employees
|
| 615 |
if emp.employment_type == EmploymentType.FULL_TIME
|
| 616 |
and emp.id not in can_open_ids
|
| 617 |
+
and not off_map.get((emp.id, ds), False)
|
| 618 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST
|
| 619 |
]
|
| 620 |
if ft_fallback_vars:
|
|
|
|
| 661 |
cln_emps = [
|
| 662 |
emp for emp in cleanup_pool
|
| 663 |
if (emp.employment_type == EmploymentType.FULL_TIME
|
| 664 |
+
and not off_map.get((emp.id, ds), False)
|
| 665 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST)
|
| 666 |
or (emp.always_available_breakfast
|
| 667 |
and breakfast_end_hour.get((emp.id, ds), 11.0) >= 11.0)
|
|
|
|
| 684 |
for emp in active_employees
|
| 685 |
if emp.employment_type == EmploymentType.FULL_TIME
|
| 686 |
and emp.id not in cleanup_emp_ids
|
| 687 |
+
and not off_map.get((emp.id, ds), False)
|
| 688 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST
|
| 689 |
]
|
| 690 |
if ft_fallback_vars:
|
|
|
|
| 708 |
if req_map.get((emp.id, ds, TimeSlot.BREAKFAST.value))
|
| 709 |
or emp.always_available_breakfast
|
| 710 |
or (emp.employment_type == EmploymentType.FULL_TIME
|
| 711 |
+
and not off_map.get((emp.id, ds), False)
|
| 712 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST)
|
| 713 |
]
|
| 714 |
if min_cln > 0 and cln_vars:
|
|
|
|
| 721 |
if req_map.get((emp.id, ds, TimeSlot.BREAKFAST.value))
|
| 722 |
or emp.always_available_breakfast
|
| 723 |
or (emp.employment_type == EmploymentType.FULL_TIME
|
| 724 |
+
and not off_map.get((emp.id, ds), False)
|
| 725 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST)
|
| 726 |
]
|
| 727 |
if ldr_cln_vars:
|
|
|
|
| 837 |
model.add(sum(worked_day[emp.id][d] for d in date_strs) <= max_work_days)
|
| 838 |
if emp.employment_type == EmploymentType.FULL_TIME:
|
| 839 |
# FT社員: 下限もハード(休日数をちょうど min_days_off 日に強制)
|
| 840 |
+
# 希望休が min_days_off 超の場合は到達可能な上限に切り下げてINFEASIBLE回避
|
| 841 |
+
avail_days = sum(
|
| 842 |
+
1 for d in date_strs if not off_map.get((emp.id, d), False)
|
|
|
|
| 843 |
)
|
| 844 |
+
hard_lower = min(max_work_days, avail_days)
|
| 845 |
if hard_lower > 0:
|
| 846 |
model.add(sum(worked_day[emp.id][d] for d in date_strs) >= hard_lower)
|
| 847 |
else:
|
|
|
|
| 1031 |
for emp in active_employees:
|
| 1032 |
if emp.employment_type != EmploymentType.FULL_TIME:
|
| 1033 |
continue
|
| 1034 |
+
if off_map.get((emp.id, ds), False):
|
| 1035 |
continue
|
| 1036 |
if slot_block_map.get((emp.id, ds)) == slot:
|
| 1037 |
continue
|
|
|
|
| 1083 |
model.add(sum(ft_hall_vars) <= 2)
|
| 1084 |
logger.info(" [P6 FTホール最大2名] 全日付・全スロットにハード制約を追加")
|
| 1085 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1086 |
model.minimize(cp_model.LinearExpr.Sum(penalty_terms))
|
| 1087 |
|
| 1088 |
# ── 求解 ────────────────────────────────────────────────────────────
|
|
|
|
| 1128 |
result_assignments = _extract_assignments(solver, assign, active_employees, date_strs, slots, positions)
|
| 1129 |
_log_employee_analysis(result_assignments, active_employees, date_strs, slots, positions,
|
| 1130 |
req_map, req_hours, shift_constraints,
|
| 1131 |
+
off_map=off_map, min_days_off=min_days_off)
|
|
|
|
| 1132 |
_log_assignment_summary(result_assignments, date_strs)
|
| 1133 |
_check_warnings(result_assignments, date_strs, warnings,
|
| 1134 |
reservation_counts=reservation_counts,
|
|
|
|
| 1148 |
else:
|
| 1149 |
logger.error(f" [INFEASIBLE/UNKNOWN] status={status_name} → フェーズ2へ移行")
|
| 1150 |
# 実行不可能 → 制約違反の診断メッセージを収集
|
| 1151 |
+
errs = _diagnose_infeasible(active_employees, date_strs, req_map, off_map)
|
| 1152 |
for e in errs:
|
| 1153 |
logger.error(f" [診断] {e}")
|
| 1154 |
|
|
|
|
| 1172 |
prev_tail=prev_tail,
|
| 1173 |
breakfast_start_hour=breakfast_start_hour,
|
| 1174 |
breakfast_end_hour=breakfast_end_hour,
|
|
|
|
| 1175 |
)
|
| 1176 |
_log_employee_analysis(best_assignments, active_employees, date_strs, slots, positions,
|
| 1177 |
req_map, req_hours, shift_constraints,
|
| 1178 |
+
off_map=off_map, min_days_off=min_days_off)
|
|
|
|
| 1179 |
_check_warnings(best_assignments, date_strs, best_warnings,
|
| 1180 |
reservation_counts=reservation_counts,
|
| 1181 |
reserv_tiers_b=reserv_tiers_b, reserv_tiers_d=reserv_tiers_d)
|
|
|
|
| 1197 |
|
| 1198 |
def _log_employee_analysis(assignments, active_employees, date_strs, slots, positions,
|
| 1199 |
req_map, req_hours, shift_constraints,
|
| 1200 |
+
off_map: dict | None = None, min_days_off: int = 5):
|
|
|
|
| 1201 |
"""スタッフ別の割当状況・ペナルティ根拠・リーダー/SI診断を詳細ログ出力"""
|
| 1202 |
from collections import defaultdict
|
| 1203 |
SOCIAL_INSURANCE_MIN_HOURS = 9.0 # 実働8h + 休憩1h(duration_hours は休憩考慮なし総拘束時間)
|
|
|
|
| 1240 |
if ft_emps:
|
| 1241 |
max_work_days = len(date_strs) - min_days_off if len(date_strs) > min_days_off else len(date_strs)
|
| 1242 |
logger.info(" [FT社員別勤務日数]")
|
| 1243 |
+
logger.info(f" {'名前':<12} {'希望休':>4} {'出勤可':>4} {'勤務日':>4} {'上限':>4} {'休日':>4} {'目標達成':>6} 注記")
|
|
|
|
|
|
|
| 1244 |
for emp in sorted(ft_emps, key=lambda e: e.name):
|
| 1245 |
+
off_days = sum(1 for ds in date_strs if off_map.get((emp.id, ds), False))
|
| 1246 |
+
avail_days = len(date_strs) - off_days
|
|
|
|
| 1247 |
worked_dates = {a.date for a in assignments if a.employee_id == emp.id}
|
| 1248 |
work_days = len(worked_dates)
|
| 1249 |
rest_days = len(date_strs) - work_days
|
| 1250 |
+
ok = "✓" if work_days >= max_work_days or avail_days <= max_work_days else "❌"
|
| 1251 |
note = ""
|
| 1252 |
+
if avail_days < max_work_days:
|
| 1253 |
+
note = f"出勤可能日({avail_days})が上限({max_work_days})未満"
|
| 1254 |
elif work_days < max_work_days:
|
| 1255 |
short = max_work_days - work_days
|
| 1256 |
note = f"未達({short}日不足)"
|
| 1257 |
+
logger.info(f" {emp.name:<12} {off_days:>4} {avail_days:>4} {work_days:>4} {max_work_days:>4} {rest_days:>4} {ok:>6} {note}")
|
| 1258 |
|
| 1259 |
# ── PT スタッフ別詳細 ───────────────────────────────────────────────
|
| 1260 |
pt_emps = [e for e in active_employees if e.employment_type != EmploymentType.FULL_TIME]
|
|
|
|
| 1408 |
prev_tail: dict[int, list[int]] | None = None,
|
| 1409 |
breakfast_start_hour: dict | None = None,
|
| 1410 |
breakfast_end_hour: dict | None = None,
|
|
|
|
| 1411 |
) -> tuple[list[ShiftAssignment], list[str]]:
|
| 1412 |
"""人数・リーダー制約をソフト化してベストエフォートのシフトを生成する"""
|
| 1413 |
if config is None:
|
| 1414 |
config = SolverConfig()
|
|
|
|
|
|
|
| 1415 |
if shift_constraints is None:
|
| 1416 |
from db import repositories as repo
|
| 1417 |
shift_constraints = repo.get_shift_constraints()
|
|
|
|
| 1459 |
for ds in date_strs:
|
| 1460 |
for slot in slots:
|
| 1461 |
if emp.employment_type == EmploymentType.FULL_TIME:
|
| 1462 |
+
can_work = not off_map.get((emp.id, ds), False) \
|
| 1463 |
and slot_block_map.get((emp.id, ds)) != slot
|
| 1464 |
elif slot == TimeSlot.BREAKFAST and emp.always_available_breakfast:
|
| 1465 |
can_work = not off_map.get((emp.id, ds), False)
|
|
|
|
| 1656 |
emp for emp in active_employees
|
| 1657 |
if emp.can_open and (
|
| 1658 |
(emp.employment_type == EmploymentType.FULL_TIME
|
| 1659 |
+
and not off_map.get((emp.id, ds), False)
|
| 1660 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST)
|
| 1661 |
or (emp.always_available_breakfast
|
| 1662 |
and breakfast_start_hour.get((emp.id, ds), 6.0) <= 6.0)
|
|
|
|
| 1680 |
for emp in active_employees
|
| 1681 |
if emp.employment_type == EmploymentType.FULL_TIME
|
| 1682 |
and emp.id not in can_open_ids
|
| 1683 |
+
and not off_map.get((emp.id, ds), False)
|
| 1684 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST
|
| 1685 |
]
|
| 1686 |
if ft_fallback_vars:
|
|
|
|
| 1702 |
for emp in active_employees
|
| 1703 |
if emp.can_cleanup and (
|
| 1704 |
(emp.employment_type == EmploymentType.FULL_TIME
|
| 1705 |
+
and not off_map.get((emp.id, ds), False)
|
| 1706 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST)
|
| 1707 |
or (emp.always_available_breakfast
|
| 1708 |
and breakfast_end_hour.get((emp.id, ds), 11.0) >= 11.0)
|
|
|
|
| 1725 |
for emp in active_employees
|
| 1726 |
if emp.employment_type == EmploymentType.FULL_TIME
|
| 1727 |
and emp.id not in cleanup_emp_ids
|
| 1728 |
+
and not off_map.get((emp.id, ds), False)
|
| 1729 |
and slot_block_map.get((emp.id, ds)) != TimeSlot.BREAKFAST
|
| 1730 |
]
|
| 1731 |
if ft_fallback_vars:
|
|
|
|
| 1900 |
date_strs: list[str],
|
| 1901 |
req_map: dict,
|
| 1902 |
off_map: dict | None = None,
|
|
|
|
| 1903 |
) -> list[str]:
|
| 1904 |
"""実行不可能の原因を診断してエラーメッセージを返す"""
|
| 1905 |
errors = []
|
|
|
|
| 1907 |
shift_constraints = repo.get_shift_constraints()
|
| 1908 |
if off_map is None:
|
| 1909 |
off_map = {}
|
|
|
|
| 1910 |
|
| 1911 |
for ds in date_strs:
|
| 1912 |
d = date.fromisoformat(ds)
|
|
|
|
| 1927 |
or e.primary_position.value == pos.value):
|
| 1928 |
continue
|
| 1929 |
if e.employment_type == EmploymentType.FULL_TIME:
|
| 1930 |
+
# 正社員: off_mapのみで可否判断
|
| 1931 |
+
if off_map.get((e.id, ds), False):
|
| 1932 |
continue
|
| 1933 |
available.append(e)
|
| 1934 |
else:
|