"""给校园咨询台选档位,先用历史时段决策,再在新时段回放。""" import json # NumPy 向量化比较容量档位,Matplotlib 对照历史成本与留出时段的超载情况。 import matplotlib.pyplot as plt import numpy as np def main(): # 一维数组的每个元素是一个时段的到访人数;历史用于选档,留出用于回放。 historical = np.array([16, 21, 20, 27, 14, 25, 23, 17]) holdout = np.array([15, 25, 32, 21]) desks = np.arange(1, 5) # 依次为每台每时段处理人数、每台每时段成本、每位未服务人员的损失。 capacity_per_desk, staff_cost, missed_cost = 8, 7, 3 # 行为时段,列为档位。未服务量截在零,余量不抵销其他时段短缺。 historical_missed = np.maximum(historical[:, None] - capacity_per_desk * desks, 0) # 沿第 0 轴汇总所有时段,得到每档的人员固定成本加短缺罚金。 costs = staff_cost * desks * len(historical) + missed_cost * historical_missed.sum(axis=0) chosen = int(desks[np.argmin(costs)]) # 保持历史选出的档位,单独统计之后到访高峰造成的未服务人数和成本。 holdout_missed = np.maximum(holdout - capacity_per_desk * chosen, 0).sum() holdout_cost = staff_cost * chosen * len(holdout) + missed_cost * holdout_missed assert chosen == 3 and holdout_missed == 9 and holdout_cost == 111 # 左图每根柱子是一档总成本,右图超过容量虚线的柱段对应未服务量。 fig, axes = plt.subplots(1, 2, figsize=(8, 3.3), layout="constrained") axes[0].bar(desks, costs) axes[0].set(xlabel="Desk count", ylabel="Historical cost", xticks=desks) axes[1].bar(np.arange(1, len(holdout) + 1), holdout, label="Arrivals") axes[1].axhline(capacity_per_desk * chosen, linestyle="--", label="Service capacity") axes[1].set(xlabel="Holdout period", ylabel="People per period") axes[1].legend() print("LAB_RESULT " + json.dumps({ "desks": chosen, "historical_costs": costs.tolist(), "holdout_missed": int(holdout_missed), "holdout_cost": int(holdout_cost), })) plt.show() if __name__ == "__main__": main()