from __future__ import annotations import pandas as pd def numeric_reasons( summary: pd.DataFrame, uncovered: pd.DataFrame, opt_kpis: dict[str, float], base_kpis: dict[str, float], notes: list[str] | None = None, lang: str = "en", limit: int = 8, ) -> list[str]: lines: list[str] = [] if notes: lines.extend(notes[:3]) cover_delta = float(opt_kpis.get("coverage_ratio", 0.0)) - float(base_kpis.get("coverage_ratio", 0.0)) ot_delta = float(opt_kpis.get("overtime_hours", 0.0)) - float(base_kpis.get("overtime_hours", 0.0)) spread_delta = float(opt_kpis.get("undesirable_spread", 0.0)) - float(base_kpis.get("undesirable_spread", 0.0)) if lang == "fa": lines.append( f"پوشش {100 * opt_kpis.get('coverage_ratio', 0):.1f}٪ در برابر پایه چرخشی {100 * base_kpis.get('coverage_ratio', 0):.1f}٪ " f"({100 * cover_delta:+.1f} واحد درصد). اضافه‌کاری {ot_delta:+.0f} ساعت و دامنه شیفت نامطلوب {spread_delta:+.0f}." ) else: lines.append( f"Coverage {100 * opt_kpis.get('coverage_ratio', 0):.1f}% vs rotating greedy {100 * base_kpis.get('coverage_ratio', 0):.1f}% " f"({100 * cover_delta:+.1f} pp). Overtime {ot_delta:+.0f} h and undesirable-shift range {spread_delta:+.0f}." ) loaded = summary.sort_values(["hours", "employee_id"], ascending=[False, True]).head(3) for row in loaded.itertuples(index=False): if lang == "fa": lines.append( f"{row.employee_id} ({row.name}): {row.shifts} شیفت / {row.hours:.0f} ساعت، " f"اضافه‌کاری {row.overtime_hours:.0f}، شب {row.night_shifts}، آخرهفته {row.weekend_shifts}." ) else: lines.append( f"{row.employee_id} ({row.name}): {row.shifts} shifts / {row.hours:.0f} h, " f"overtime {row.overtime_hours:.0f}, nights {row.night_shifts}, weekends {row.weekend_shifts}." ) light = summary.sort_values(["hours", "employee_id"]).head(2) for row in light.itertuples(index=False): if float(row.hours) <= 0: continue if lang == "fa": lines.append(f"بار کم: {row.employee_id} فقط {row.hours:.0f} ساعت با {row.undesirable_shifts} شیفت نامطلوب.") else: lines.append(f"Light load: {row.employee_id} has {row.hours:.0f} h and {row.undesirable_shifts} undesirable shifts.") if uncovered is not None and len(uncovered): top = uncovered.sort_values("uncovered", ascending=False).iloc[0] if lang == "fa": lines.append(f"بزرگ‌ترین کمبود پوشش: {top.date} {top.shift_id}/{top.skill} به اندازه {int(top.uncovered)} نفر.") else: lines.append(f"Largest gap: {top.date} {top.shift_id}/{top.skill} short {int(top.uncovered)} people.") return lines[:limit] def status_label(status: str, lang: str = "en") -> str: mapping = { "en": { "optimal": "Proven optimal (within model)", "feasible": "Feasible incumbent within the time limit — not proven optimal", "heuristic": "Heuristic feasible plan — not a proven optimum", "infeasible": "Proven infeasible under the stated constraints", "timeout": "No plan found before the time limit — this is not a proof of infeasibility", "invalid": "Invalid input", "error": "Solver error", }, "fa": { "optimal": "بهینه اثبات‌شده در مدل", "feasible": "جواب شدنی در مهلت — بهینه بودن اثبات نشده", "heuristic": "برنامه شدنی ابتکاری — بهینه اثبات‌شده نیست", "infeasible": "ناممکن اثبات‌شده با قیود فعلی", "timeout": "در مهلت جوابی پیدا نشد — این اثبات ناممکن بودن نیست", "invalid": "ورودی نامعتبر", "error": "خطای حل‌گر", }, } return mapping.get(lang, mapping["en"]).get(status, status)