chart_achievement: 均摊改为按键类型的整数分配,可列举多种可能

- score.py:新增 alloc_flow(最小费用流读回整数分配)、_fill_fluff(CP/Perfect 按剩余容量补满)、
  _alloc_greedy_int(最大余数法取整均摊)、_enum_allocations(按列 DFS 枚举坏音符落点,
  带容量剪枝、节点/时间预算与均匀抽样)、alloc_options(取整均摊 + 流最优/最差 + 枚举结果去重,
  按失分升序);AllocatedResult 增加 options/exhaustive 与 achievement_of(),
  可给出每一种整数分配对应的达成率。
- chart_achievement.py:print_allocated 改为打印所有可能的整数分配
  (序号/达成率/失分/各列 CP-P-G-GD-M),新增 --show N(默认 20,0 = 全部),
  JSON 增加 achievement_min/achievement_max/alloc_values/alloc_exhaustive/alloc_options。
- README:③ 节改写为「整数分配 + 多种可能」,补充 --show 与未枚举完时的说明。
This commit is contained in:
Txiaochen committed 2026-11-14 04:05:47 +08:00
1 parent 72dce56dd1
commit d0763fa796
3 files changed
+482 -65

No files matched your search

+96 -25
View File
@@ -15,6 +15,7 @@ import json
import os
import re
import sys
import unicodedata
from fractions import Fraction
from typing import Dict, List, Optional
@@ -162,8 +163,8 @@ def print_forward(chart: Chart, result) -> None:
print(" 区间下界 = 扣分都落在权重最高的音符上,上界 = 都落在权重最低的音符上。")
if th.counts.get("break", 0) + th.counts.get("break_flat", 0) \
+ th.counts.get("break_slide", 0) + th.counts.get("ex_break", 0):
print(" 想要一个确切数值:再加一列 --break \"CP/Perfect/Great/Good/Miss\" 给绝赞的判定数")
print(" (绝赞精确、其余按数量均摊);或者用 --tap / --hold / --slide / --break 全部按类别给。")
print(" 想要确切的数值:加 --break \"CP/Perfect/Great/Good/Miss\" 单列绝赞,")
print(" 其余音符就会按键类型列出所有整数分配;或者用 --tap / --hold / --slide / --break 全按类别给。")
else:
print(" 想要一个确切数值:用 --tap / --hold / --slide / --break 按类别给判定数。")
print(LINE)
@@ -222,8 +223,40 @@ def _num(value) -> str:
return str(v.numerator) if v.denominator == 1 else "%.2f" % float(v)
def print_allocated(chart: Chart, result) -> None:
def _dwidth(text: str) -> int:
"""终端显示宽度(中文算 2 列)。"""
return sum(2 if unicodedata.east_asian_width(ch) in "WF" else 1 for ch in text)
def _ljust(text: str, width: int) -> str:
return text + " " * max(0, width - _dwidth(text))
def _rjust(text: str, width: int) -> str:
return " " * max(0, width - _dwidth(text)) + text
def _alloc_cell(row: Dict[str, int]) -> str:
return "-".join(str(int(row.get(j, 0))) for j in JUDGMENTS)
def _sample_indexes(total: int, show: int) -> List[int]:
if show <= 0 or total <= show:
return list(range(total))
if show == 1:
return [0]
picked: List[int] = []
for i in range(show):
idx = round(i * (total - 1) / (show - 1))
if idx not in picked:
picked.append(idx)
return picked
def print_allocated(chart: Chart, result, show: int = 20) -> None:
th = result.theory
cols = result.columns
options = list(result.options) or [(result.loss_other, result.allocated)]
print(LINE)
print("判定输入(汇总判定数 + 绝赞单列)")
for j in JUDGMENTS:
@@ -236,42 +269,66 @@ def print_allocated(chart: Chart, result) -> None:
print(" 绝赞列 %-10s %s" % ("(%d 个)" % bcap, " · ".join(parts) if parts else "-"))
else:
print(" 绝赞列 %-10s 该谱面没有绝赞音符(达成率上限 100.0000%%)" % "(0 个)")
cols = result.columns
if cols:
nb = sum(alloc_capacity(th, c) for c in cols)
print(" 其余 %d 个音符按音符数量均摊:%s"
print(" 其余 %d 个音符按键类型分列:%s"
% (nb, " · ".join("%s %d" % (_SHORT_TITLE[c], alloc_capacity(th, c))
for c in cols)))
print(" %-8s" % "判定" + "".join("%14s" % _SHORT_TITLE[c] for c in cols))
for j in JUDGMENTS:
row = [result.allocated.get(c, {}).get(j, Fraction(0)) for c in cols]
if not any(row):
continue
print(" %-8s" % _EXACT_KEY_LABEL[j]
+ "".join("%14.2f" % float(v) for v in row))
print(LINE)
print("结果")
print(" 达成率 %s (确切值)" % pct(result.achievement))
print(" 达成率 %s (按容量取整均摊的这一份)" % pct(result.achievement))
if len(options) > 1:
print(" 可能范围 %s ~ %s (%d 种整数分配,前者最好、后者最差)"
% (pct(result.achievement_of(options[0][0])),
pct(result.achievement_of(options[-1][0])), len(options)))
print(" DX 分 %d / %d (%.2f%%)"
% (result.dx, th.max_dx, 100.0 * result.dx / th.max_dx if th.max_dx else 0.0))
loss_all = result.loss_break + result.loss_other
if th.all_perfect_score:
print(" 失分 绝赞 %d + 其余 %s = %s / %d"
% (result.loss_break, _num(result.loss_other), _num(loss_all),
th.all_perfect_score))
print(" 失分 绝赞 %d + 其余 %d = %d / %d"
% (result.loss_break, result.loss_other, loss_all, th.all_perfect_score))
if th.break_bonus_score:
print(" 绝赞奖励 %d / %d (%.4f%%)"
% (result.bonus_break, th.break_bonus_score,
100.0 * result.bonus_break / th.break_bonus_score))
if cols:
print()
print("可能的整数分配(每格 CP-P-G-GD-M,就是该按键类型上的音符个数)")
head = (" " + _rjust("#", 3) + " " + _rjust("达成率", 9) + " "
+ _rjust("失分", 7) + " ")
for c in cols:
head += _ljust(_SHORT_TITLE[c], 16)
print(head.rstrip())
idxs = _sample_indexes(len(options), show)
gi = next((i for i, (loss, _t) in enumerate(options)
if loss == result.loss_other), None)
if gi is not None and gi not in idxs:
idxs = sorted(idxs + [gi])
for n, i in enumerate(idxs, 1):
loss, table = options[i]
note = " <- 按容量取整均摊" if loss == result.loss_other else ""
print((" %3d %9s %7d " % (n, pct(result.achievement_of(loss)), loss)
+ "".join(_ljust(_alloc_cell(table.get(c, {})), 16) for c in cols)).rstrip()
+ note)
if len(idxs) < len(options):
print(" (共 %d 种,这里均匀显示 %d 种;用 --show 0 显示全部,或用 --json 取数据)"
% (len(options), len(idxs)))
if not result.exhaustive:
print(" [注意] 组合太多,只枚举了一部分(按预算抽样),上面的列表不是全部;"
"列出的每一种都是真的,上下界也是精确的。")
print()
print(" 说明:绝赞的奖励分只由绝赞音符自己的判定决定,绝赞单列后这部分是精确的;")
print(" 其余音符按各自音符数量均摊(EX 音符只能判 Critical/Miss,单独一列),")
print(" 失分取加权平均,所以得到的是一个确定数值。")
print(" 说明:同一份汇总判定数可以落在不同的按键类型上,失分不同,达成率也就不同;")
if result.exhaustive:
print(" 上表列出了这个谱面上所有可能的整数分配,DX 分与判定落在哪一类音符无关。")
else:
print(" 上表只列出了枚举到的一部分整数分配,DX 分与判定落在哪一类音符无关。")
ex_notes = th.counts.get("ex", 0)
if ex_notes:
print(" [注意] 谱面里有 %d 个 EX 音符(ExTap/ExStar),判定表只允许 Critical 或 Miss;"
print(" [注意] 谱面里有 %d 个 EX 音符(ExTap/ExStar),判定表只允许 Critical 或 Miss,"
% ex_notes)
print(" 均摊时已把它们单列,Great/Good 不会落到 EX 上。")
print(" 所以 EX 单独成列,Great/Good 不会落到 EX 上。")
print(LINE)
@@ -401,7 +458,6 @@ def interactive(args) -> None:
except SystemExit:
chart = None
th = theory_from_chart(chart)
print_chart(chart, th)
mode = ask("计算模式:1) 汇总判定数(估计值 + 区间)\n"
" 2) 汇总判定数 + 绝赞单列,其余均摊(确切值)\n"
@@ -589,21 +645,33 @@ def run_allocated(chart: Chart, th: Theory, raw: Dict[str, Optional[int]],
except ValueError as exc:
die(str(exc))
if args.json:
options = list(result.options) or [(result.loss_other, result.allocated)]
print(to_json(chart, th, {
"mode": "forward_allocated",
"judges": judges,
"break_judges": result.break_spec,
"allocated": {c: {j: float(v) for j, v in row.items()}
"allocated": {c: {j: int(v) for j, v in row.items()}
for c, row in result.allocated.items()},
"achievement": float(result.achievement),
"achievement_min": float(result.achievement_of(options[-1][0])),
"achievement_max": float(result.achievement_of(options[0][0])),
"dx_score": result.dx,
"loss_break": result.loss_break,
"loss_other": float(result.loss_other),
"loss_other": result.loss_other,
"bonus_break": result.bonus_break,
"alloc_values": len(options),
"alloc_exhaustive": result.exhaustive,
"alloc_options": [
{"loss_other": loss,
"achievement": float(result.achievement_of(loss)),
"columns": {c: {j: int(v) for j, v in row.items()}
for c, row in table.items()}}
for loss, table in options[:200]
],
}))
return
print_chart(chart, th)
print_allocated(chart, result)
print_allocated(chart, result, getattr(args, "show", 20))
def run_forward(chart: Chart, th: Theory,
@@ -711,6 +779,9 @@ def build_parser() -> argparse.ArgumentParser:
ex.add_argument("--break-great50", type=int, metavar="N",
help="绝赞 Great 里失分 50%% 的个数(Great3rd)")
fw.add_argument("--show", type=int, default=20, metavar="N",
help="均摊模式下最多列出多少种整数分配(默认 20;0 = 全部)")
rv = p.add_argument_group("反向计算:已知达成率")
rv.add_argument("--achievement", metavar="A", help="目标达成率(如 99.5 / 100.5)")
rv.add_argument("--mode", choices=("exact", "range"), default="exact",