1 import numpy as np
   2 import matplotlib.pyplot as plt
   3 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   4 
   5 setup_style()
   6 np.random.seed(42)
   7 
   8 # === 模拟数据:5 个算法在 30 个问题上的求解时间 ===
   9 n_problems = 30
  10 algos = ['本文方法', 'Baseline A', 'Baseline B', 'Baseline C', 'Heuristic']
  11 # 每行一个问题,每列一个算法的求解时间
  12 times = np.abs(np.random.lognormal(0, 0.6, (n_problems, len(algos))))
  13 times[:, 0] *= 0.7   # 本文方法更快
  14 times[10:15, 2] = np.inf  # Baseline B 有 5 个失败
  15 times[5:8, 3] = np.inf    # Baseline C 失败 3 个
  16 
  17 # === 计算 performance ratio ===
  18 best = np.nanmin(np.where(np.isinf(times), np.nan, times), axis=1, keepdims=True)
  19 rp = times / best  # 每问题每算法的相对比率
  20 rp[np.isinf(times)] = np.inf
  21 
  22 # === 计算累计分布 ρ_s(τ) = #{p : rp(p,s) ≤ τ} / n_problems ===
  23 tau_grid = np.logspace(0, 1.0, 200)  # τ ∈ [1, 10]
  24 rho = np.zeros((len(tau_grid), len(algos)))
  25 for s in range(len(algos)):
  26     for i, tau in enumerate(tau_grid):
  27         rho[i, s] = np.mean(rp[:, s] <= tau)
  28 
  29 fig, ax = plt.subplots(figsize=(7, 4.5))
  30 
  31 markers = ['o', 's', '^', 'D', 'v']
  32 for s, name in enumerate(algos):
  33     is_self = (s == 0)
  34     lw = 2.4 if is_self else 1.4
  35     alpha = 1.0 if is_self else 0.78
  36     # ★ 阶梯函数 — drawstyle='steps-post' 是 Dolan-Moré 标准
  37     ax.step(tau_grid, rho[:, s], where='post',
  38             color=PALETTE[s % len(PALETTE)],
  39             linewidth=lw, alpha=alpha,
  40             marker=markers[s % len(markers)], markersize=5,
  41             markevery=20, markeredgecolor='white', markeredgewidth=0.6,
  42             label=name + (' ★' if is_self else ''))
  43 
  44 # ★ τ=1 处的左端值标注("找到最优的比例")
  45 for s, name in enumerate(algos):
  46     rho0 = rho[0, s]
  47     if rho0 > 0.05:
  48         ax.text(1.02, rho0, f'{rho0:.2f}', fontsize=7,
  49                 color=PALETTE[s % len(PALETTE)], va='center', ha='left',
  50                 bbox=dict(boxstyle='round,pad=0.15', facecolor='white',
  51                           edgecolor=PALETTE[s % len(PALETTE)], alpha=0.85, linewidth=0.4))
  52 
  53 # ★ "完美求解器"参考线(理论上限 ρ=1)
  54 ax.axhline(1.0, color=COLORS['ref_line'], linestyle=':', linewidth=0.8, alpha=0.5)
  55 ax.text(tau_grid[-1] * 0.95, 1.01, 'Ideal solver (ρ=1)',
  56         fontsize=7, color=COLORS['ref_line'], ha='right', va='bottom', style='italic')
  57 
  58 # ★ 灰色背景区暗示"τ=1 = 全部找到最优"是稀有的
  59 ax.axvspan(1.0, 1.05, alpha=0.08, color=COLORS['highlight'], zorder=0)
  60 
  61 ax.set_xscale('log')
  62 ax.set_xlim(1.0, tau_grid[-1])
  63 ax.set_ylim(0, 1.05)
  64 ax.set_xlabel(r'性能比率 $\tau$ (相对最优解的倍数, log scale)', fontsize=10)
  65 ax.set_ylabel(r'$\rho_s(\tau)$  (在 $\tau$ 倍内解出的问题比例)', fontsize=10)
  66 ax.legend(loc='lower right', frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=8)
  67 ax.grid(which='both', alpha=0.12, linestyle='--', color=COLORS['grid'])
  68 ax.spines['top'].set_visible(False)
  69 ax.spines['right'].set_visible(False)
  70 fig.tight_layout()
  71 save_fig(fig, 'figures/fig_perf_profile.pdf')