Commit ·
5400274
1
Parent(s): 24b3694
fix: solver comprehensive review - 8 issues resolved
Browse files① Pre-check counts FT employees correctly (off_map awareness)
② P3 removes dead FT branch (FT never has req_map entries)
③ 5b open-prep constraint includes FT employees with can_open
④ 5c cleanup constraint includes FT employees with can_cleanup
⑤ P2b late-night penalty skips FT (dinner is their default duty)
⑥ Best-effort solver gets P5 slot-split soft penalties (500k/300k)
⑦ P3b timeslot penalty removed (P5 hard constraints cover this)
⑧ SolverConfig: remove double_penalty_scale (P2 is now hard constraint)
Generate route and template updated accordingly
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- optimizer/solver.py +47 -33
- routes/generate.py +0 -1
- templates/generate/index.html +4 -9
optimizer/solver.py
CHANGED
|
@@ -30,9 +30,8 @@ class SolverConfig:
|
|
| 30 |
"""最適化の優先度設定(各スケールは PRIORITY_SCALE の値を使用)"""
|
| 31 |
cost_scale: float = 1.0 # 人件費最小化
|
| 32 |
pt_pref_scale: float = 1.0 # アルバイト希望充当
|
| 33 |
-
double_penalty_scale: float = 1.0 # 正社員両掛け持ち回避
|
| 34 |
balance_scale: float = 1.0 # 人員バランス均等化
|
| 35 |
-
late_night_scale: float = 1.0 # 深夜勤務分散
|
| 36 |
|
| 37 |
|
| 38 |
class SolveProgressCallback(cp_model.CpSolverSolutionCallback):
|
|
@@ -168,7 +167,10 @@ def solve(
|
|
| 168 |
base_min += reserv_extra_d
|
| 169 |
avail = []
|
| 170 |
for e in active_employees:
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
| 172 |
ok = ds not in e.fixed_unavailable_dates
|
| 173 |
elif slot == TimeSlot.DINNER and e.always_available_dinner:
|
| 174 |
ok = ds not in e.fixed_unavailable_dates
|
|
@@ -310,9 +312,15 @@ def solve(
|
|
| 310 |
if min_open <= 0 and min_open_ldr <= 0:
|
| 311 |
continue
|
| 312 |
for ds in date_strs:
|
|
|
|
| 313 |
can_open_req = [
|
| 314 |
emp for emp in active_employees
|
| 315 |
-
if emp.can_open and
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
]
|
| 317 |
open_vars = [assign[emp.id][ds][TimeSlot.BREAKFAST.value][pos.value] for emp in can_open_req]
|
| 318 |
if min_open > 0:
|
|
@@ -346,12 +354,15 @@ def solve(
|
|
| 346 |
cleanup_pool = cleanup_emps_by_pos[pos]
|
| 347 |
for ds in date_strs:
|
| 348 |
if cleanup_pool:
|
| 349 |
-
# can_cleanup
|
| 350 |
cln_vars = [
|
| 351 |
assign[emp.id][ds][TimeSlot.BREAKFAST.value][pos.value]
|
| 352 |
for emp in cleanup_pool
|
| 353 |
if req_map.get((emp.id, ds, TimeSlot.BREAKFAST.value))
|
| 354 |
or emp.always_available_breakfast
|
|
|
|
|
|
|
|
|
|
| 355 |
]
|
| 356 |
if min_cln > 0 and cln_vars:
|
| 357 |
model.add(sum(cln_vars) >= min(min_cln, len(cln_vars)))
|
|
@@ -426,26 +437,24 @@ def solve(
|
|
| 426 |
|
| 427 |
# P2: 両時間帯掛け持ちはハード制約で禁止済み(ペナルティ不要)
|
| 428 |
|
| 429 |
-
# P2b: 深夜(
|
| 430 |
-
# ディナー
|
| 431 |
-
#
|
| 432 |
-
# ※ CP-SATは2次項不可のため、線形近似(合計に重み)
|
| 433 |
late_night_w = max(1, int(500 * config.late_night_scale))
|
| 434 |
for emp in active_employees:
|
|
|
|
|
|
|
| 435 |
for ds in date_strs:
|
| 436 |
for pos in positions:
|
| 437 |
v = assign[emp.id][ds][TimeSlot.DINNER.value][pos.value]
|
| 438 |
-
penalty_terms.append(late_night_w * v)
|
| 439 |
|
| 440 |
-
# P3: 希望
|
| 441 |
-
#
|
| 442 |
-
# → 正社員が希望した枠には必ず入れ、余りをアルバイトで補完する
|
| 443 |
-
FT_NOT_WORKED_PENALTY = 200_000 # 正社員: P1コスト(最大〜100000)を上回る重み(固定)
|
| 444 |
PT_NOT_WORKED_PENALTY = max(1, int(100 * config.pt_pref_scale))
|
| 445 |
for emp in active_employees:
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
else PT_NOT_WORKED_PENALTY)
|
| 449 |
for ds in date_strs:
|
| 450 |
for slot in slots:
|
| 451 |
if req_map.get((emp.id, ds, slot.value)):
|
|
@@ -453,20 +462,9 @@ def solve(
|
|
| 453 |
not_worked = model.new_bool_var(f"nw_{emp.id}_{ds}_{slot.value}")
|
| 454 |
model.add(worked == 0).only_enforce_if(not_worked)
|
| 455 |
model.add(worked >= 1).only_enforce_if(not_worked.negated())
|
| 456 |
-
penalty_terms.append(
|
| 457 |
|
| 458 |
-
# P3b: primary_timeslot 専任制約(
|
| 459 |
-
# 専任設定のある従業員が専任外の時間帯に配置された場合に軽微なペナルティ
|
| 460 |
-
TIMESLOT_PENALTY = 80 # PT_NOT_WORKED_PENALTY より低く設定して他の制約を妨げない
|
| 461 |
-
for emp in active_employees:
|
| 462 |
-
if emp.primary_timeslot is None:
|
| 463 |
-
continue
|
| 464 |
-
for s in slots:
|
| 465 |
-
if s == emp.primary_timeslot:
|
| 466 |
-
continue
|
| 467 |
-
for ds in date_strs:
|
| 468 |
-
for pos in positions:
|
| 469 |
-
penalty_terms.append(TIMESLOT_PENALTY * assign[emp.id][ds][s.value][pos.value])
|
| 470 |
|
| 471 |
# P4: 人員バランス = ポジション毎の総シフト数の偏差を最小化
|
| 472 |
# 各日の各(スロット×ポジション)の担当人数を均等にするため、
|
|
@@ -796,10 +794,11 @@ def _solve_best_effort(
|
|
| 796 |
w = max(1, int(1000 * hours * 10 * config.cost_scale))
|
| 797 |
penalty_terms.append(w * var)
|
| 798 |
|
| 799 |
-
|
| 800 |
PT_PENALTY = max(1, int(100 * config.pt_pref_scale))
|
| 801 |
for emp in active_employees:
|
| 802 |
-
|
|
|
|
| 803 |
for ds in date_strs:
|
| 804 |
for slot in slots:
|
| 805 |
if req_map.get((emp.id, ds, slot.value)):
|
|
@@ -807,7 +806,22 @@ def _solve_best_effort(
|
|
| 807 |
not_worked = model.new_bool_var(f"nw_be_{emp.id}_{ds}_{slot.value}")
|
| 808 |
model.add(worked == 0).only_enforce_if(not_worked)
|
| 809 |
model.add(worked >= 1).only_enforce_if(not_worked.negated())
|
| 810 |
-
penalty_terms.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 811 |
|
| 812 |
model.minimize(cp_model.LinearExpr.Sum(penalty_terms))
|
| 813 |
|
|
|
|
| 30 |
"""最適化の優先度設定(各スケールは PRIORITY_SCALE の値を使用)"""
|
| 31 |
cost_scale: float = 1.0 # 人件費最小化
|
| 32 |
pt_pref_scale: float = 1.0 # アルバイト希望充当
|
|
|
|
| 33 |
balance_scale: float = 1.0 # 人員バランス均等化
|
| 34 |
+
late_night_scale: float = 1.0 # 深夜勤務分散(アルバイトのみ)
|
| 35 |
|
| 36 |
|
| 37 |
class SolveProgressCallback(cp_model.CpSolverSolutionCallback):
|
|
|
|
| 167 |
base_min += reserv_extra_d
|
| 168 |
avail = []
|
| 169 |
for e in active_employees:
|
| 170 |
+
# FIX①: FT社員を正しくカウント(実際の可用性チェックと統一)
|
| 171 |
+
if e.employment_type == EmploymentType.FULL_TIME:
|
| 172 |
+
ok = not off_map.get((e.id, ds), False) and ds not in e.fixed_unavailable_dates
|
| 173 |
+
elif slot == TimeSlot.BREAKFAST and e.always_available_breakfast:
|
| 174 |
ok = ds not in e.fixed_unavailable_dates
|
| 175 |
elif slot == TimeSlot.DINNER and e.always_available_dinner:
|
| 176 |
ok = ds not in e.fixed_unavailable_dates
|
|
|
|
| 312 |
if min_open <= 0 and min_open_ldr <= 0:
|
| 313 |
continue
|
| 314 |
for ds in date_strs:
|
| 315 |
+
# FIX③: FT社員も can_open 要件に含める
|
| 316 |
can_open_req = [
|
| 317 |
emp for emp in active_employees
|
| 318 |
+
if emp.can_open and (
|
| 319 |
+
req_map.get((emp.id, ds, TimeSlot.BREAKFAST.value))
|
| 320 |
+
or (emp.employment_type == EmploymentType.FULL_TIME
|
| 321 |
+
and not off_map.get((emp.id, ds), False)
|
| 322 |
+
and ds not in emp.fixed_unavailable_dates)
|
| 323 |
+
)
|
| 324 |
]
|
| 325 |
open_vars = [assign[emp.id][ds][TimeSlot.BREAKFAST.value][pos.value] for emp in can_open_req]
|
| 326 |
if min_open > 0:
|
|
|
|
| 354 |
cleanup_pool = cleanup_emps_by_pos[pos]
|
| 355 |
for ds in date_strs:
|
| 356 |
if cleanup_pool:
|
| 357 |
+
# FIX④: FT社員も can_cleanup 要件に含める
|
| 358 |
cln_vars = [
|
| 359 |
assign[emp.id][ds][TimeSlot.BREAKFAST.value][pos.value]
|
| 360 |
for emp in cleanup_pool
|
| 361 |
if req_map.get((emp.id, ds, TimeSlot.BREAKFAST.value))
|
| 362 |
or emp.always_available_breakfast
|
| 363 |
+
or (emp.employment_type == EmploymentType.FULL_TIME
|
| 364 |
+
and not off_map.get((emp.id, ds), False)
|
| 365 |
+
and ds not in emp.fixed_unavailable_dates)
|
| 366 |
]
|
| 367 |
if min_cln > 0 and cln_vars:
|
| 368 |
model.add(sum(cln_vars) >= min(min_cln, len(cln_vars)))
|
|
|
|
| 437 |
|
| 438 |
# P2: 両時間帯掛け持ちはハード制約で禁止済み(ペナルティ不要)
|
| 439 |
|
| 440 |
+
# P2b: 深夜勤務分散(アルバイトのみ対象)
|
| 441 |
+
# FIX⑤: FT社員はディナーが基本業務のためペナルティ不要
|
| 442 |
+
# PT社員のディナー担当を均等分散させる
|
|
|
|
| 443 |
late_night_w = max(1, int(500 * config.late_night_scale))
|
| 444 |
for emp in active_employees:
|
| 445 |
+
if emp.employment_type == EmploymentType.FULL_TIME:
|
| 446 |
+
continue # 社員はディナー出勤がデフォルト、ペナルティ対象外
|
| 447 |
for ds in date_strs:
|
| 448 |
for pos in positions:
|
| 449 |
v = assign[emp.id][ds][TimeSlot.DINNER.value][pos.value]
|
| 450 |
+
penalty_terms.append(late_night_w * v)
|
| 451 |
|
| 452 |
+
# P3: アルバイトの希望充当(FIX②: FT社員は req_map を持たないため除外)
|
| 453 |
+
# 社員は off_map で管理済みのため、ここではアルバイトのみ対象
|
|
|
|
|
|
|
| 454 |
PT_NOT_WORKED_PENALTY = max(1, int(100 * config.pt_pref_scale))
|
| 455 |
for emp in active_employees:
|
| 456 |
+
if emp.employment_type == EmploymentType.FULL_TIME:
|
| 457 |
+
continue # 社員は希望提出しない(off_map で管理)
|
|
|
|
| 458 |
for ds in date_strs:
|
| 459 |
for slot in slots:
|
| 460 |
if req_map.get((emp.id, ds, slot.value)):
|
|
|
|
| 462 |
not_worked = model.new_bool_var(f"nw_{emp.id}_{ds}_{slot.value}")
|
| 463 |
model.add(worked == 0).only_enforce_if(not_worked)
|
| 464 |
model.add(worked >= 1).only_enforce_if(not_worked.negated())
|
| 465 |
+
penalty_terms.append(PT_NOT_WORKED_PENALTY * not_worked)
|
| 466 |
|
| 467 |
+
# P3b: primary_timeslot 専任制約は P5 のハード制約でカバー済みのため削除(FIX⑦)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
|
| 469 |
# P4: 人員バランス = ポジション毎の総シフト数の偏差を最小化
|
| 470 |
# 各日の各(スロット×ポジション)の担当人数を均等にするため、
|
|
|
|
| 794 |
w = max(1, int(1000 * hours * 10 * config.cost_scale))
|
| 795 |
penalty_terms.append(w * var)
|
| 796 |
|
| 797 |
+
# アルバイト希望充当(FIX②同様: FT除外)
|
| 798 |
PT_PENALTY = max(1, int(100 * config.pt_pref_scale))
|
| 799 |
for emp in active_employees:
|
| 800 |
+
if emp.employment_type == EmploymentType.FULL_TIME:
|
| 801 |
+
continue
|
| 802 |
for ds in date_strs:
|
| 803 |
for slot in slots:
|
| 804 |
if req_map.get((emp.id, ds, slot.value)):
|
|
|
|
| 806 |
not_worked = model.new_bool_var(f"nw_be_{emp.id}_{ds}_{slot.value}")
|
| 807 |
model.add(worked == 0).only_enforce_if(not_worked)
|
| 808 |
model.add(worked >= 1).only_enforce_if(not_worked.negated())
|
| 809 |
+
penalty_terms.append(PT_PENALTY * not_worked)
|
| 810 |
+
|
| 811 |
+
# FIX⑥: ベストエフォートにも P5 スロット分業のソフト誘導を追加
|
| 812 |
+
BE_BREAKFAST_FT_COST = 500_000
|
| 813 |
+
BE_DINNER_PT_COST = 300_000
|
| 814 |
+
for emp in active_employees:
|
| 815 |
+
for ds in date_strs:
|
| 816 |
+
for pos in positions:
|
| 817 |
+
if emp.employment_type == EmploymentType.FULL_TIME:
|
| 818 |
+
penalty_terms.append(
|
| 819 |
+
BE_BREAKFAST_FT_COST * assign[emp.id][ds][TimeSlot.BREAKFAST.value][pos.value]
|
| 820 |
+
)
|
| 821 |
+
else:
|
| 822 |
+
penalty_terms.append(
|
| 823 |
+
BE_DINNER_PT_COST * assign[emp.id][ds][TimeSlot.DINNER.value][pos.value]
|
| 824 |
+
)
|
| 825 |
|
| 826 |
model.minimize(cp_model.LinearExpr.Sum(penalty_terms))
|
| 827 |
|
routes/generate.py
CHANGED
|
@@ -85,7 +85,6 @@ def run():
|
|
| 85 |
config = SolverConfig(
|
| 86 |
cost_scale=_scale("cost_scale"),
|
| 87 |
pt_pref_scale=_scale("pt_pref_scale"),
|
| 88 |
-
double_penalty_scale=_scale("double_penalty_scale"),
|
| 89 |
balance_scale=_scale("balance_scale"),
|
| 90 |
late_night_scale=_scale("late_night_scale"),
|
| 91 |
)
|
|
|
|
| 85 |
config = SolverConfig(
|
| 86 |
cost_scale=_scale("cost_scale"),
|
| 87 |
pt_pref_scale=_scale("pt_pref_scale"),
|
|
|
|
| 88 |
balance_scale=_scale("balance_scale"),
|
| 89 |
late_night_scale=_scale("late_night_scale"),
|
| 90 |
)
|
templates/generate/index.html
CHANGED
|
@@ -83,10 +83,6 @@
|
|
| 83 |
'title': 'アルバイト希望充当',
|
| 84 |
'body': 'アルバイトが希望した時間帯にできるだけ入れるよう配慮します。「高」にすると希望を尊重しやすくなりますが、人件費が増える可能性があります。',
|
| 85 |
},
|
| 86 |
-
'double_penalty_scale': {
|
| 87 |
-
'title': '正社員 両掛け持ち回避',
|
| 88 |
-
'body': '正社員が同じ日に朝食とディナーの両方に入る「両掛け持ち」をできるだけ避けます。「高」にすると特定の人への連続勤務集中を防げます。',
|
| 89 |
-
},
|
| 90 |
'balance_scale': {
|
| 91 |
'title': '人員バランス均等化',
|
| 92 |
'body': '各スタッフの総勤務回数が均等になるよう調整します。「高」にすると特定のスタッフに勤務が偏らないシフトになります。',
|
|
@@ -98,11 +94,10 @@
|
|
| 98 |
} %}
|
| 99 |
|
| 100 |
{% for key, label in [
|
| 101 |
-
('cost_scale',
|
| 102 |
-
('pt_pref_scale',
|
| 103 |
-
('
|
| 104 |
-
('
|
| 105 |
-
('late_night_scale', '深夜勤務分散'),
|
| 106 |
] %}
|
| 107 |
{% set h = help_texts[key] %}
|
| 108 |
<div class="mb-3">
|
|
|
|
| 83 |
'title': 'アルバイト希望充当',
|
| 84 |
'body': 'アルバイトが希望した時間帯にできるだけ入れるよう配慮します。「高」にすると希望を尊重しやすくなりますが、人件費が増える可能性があります。',
|
| 85 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
'balance_scale': {
|
| 87 |
'title': '人員バランス均等化',
|
| 88 |
'body': '各スタッフの総勤務回数が均等になるよう調整します。「高」にすると特定のスタッフに勤務が偏らないシフトになります。',
|
|
|
|
| 94 |
} %}
|
| 95 |
|
| 96 |
{% for key, label in [
|
| 97 |
+
('cost_scale', '人件費最小化'),
|
| 98 |
+
('pt_pref_scale', 'アルバイト希望充当'),
|
| 99 |
+
('balance_scale', '人員バランス均等化'),
|
| 100 |
+
('late_night_scale', '深夜勤務分散'),
|
|
|
|
| 101 |
] %}
|
| 102 |
{% set h = help_texts[key] %}
|
| 103 |
<div class="mb-3">
|