chart_achievement: 新增「汇总判定数 + 绝赞单列」模式,绝赞精确、其余按容量均摊,得到确切达成率

同步自 mai 分支 651080d。
- score.py: ALLOC_COLUMNS/ALLOC_GROUPS/ALLOC_OPTIONS + alloc_capacity/alloc_columns/_alloc_feasible/_alloc_spread/solve_allocated
  绝赞列按档位精确计算奖励分;其余音符按「剩余容量比例」均摊,EX 音符单列(只能 Critical/Miss),
  分数表行/列合计都正确,所以均摊值一定落在该谱面真正能打出的达成率区间内
- chart_achievement.py: --break/--break-perfect50/--break-great40/--break-great50,print_allocated 输出均摊表,
  交互向导新增「2) 汇总 + 绝赞单列」,JSON 增加 allocated/loss_break/loss_other/bonus_break
- README: 新增第 ③ 节与已知局限说明
This commit is contained in:
Txiaochen committed 2026-11-07 21:19:16 +08:00
1 parent ed7d463519
commit 72dce56dd1
3 files changed
+446 -59

No files matched your search

+181
View File
@@ -530,6 +530,187 @@ def solve_exact(th: Theory, spec: Dict[str, Dict[str, int]]) -> ExactResult:
)
# ------------------------------------------------------------------ 正向:绝赞单列 + 其余均摊
# 绝赞的奖励分只由绝赞音符自己的判定决定,所以把「绝赞」单独给一列就能精确算出奖励分;
# 剩下的非绝赞音符按各自音符数量**均摊**,于是也能得到一个确切数值(而不是区间)。
#
# 均摊必须照顾「容量」:EX 音符(ExTap 判定表)只能 Critical 或 Miss,吃不了 Great/Good。
# 所以把非绝赞音符分成 4 列,每列只允许它真能拿到的判定;再把各判定数按「剩余容量」比例
# 分给这些列(受约束的先分)。得到的分数表行合计 = 各判定数、列合计 = 各类音符数,
# 是运输多面体(含禁格)的一个可行点,所以它的失分一定落在「可达区间」内。
ALLOC_COLUMNS = ("tap", "ex", "hold", "slide")
ALLOC_GROUPS = {
"tap": (GROUP_TAP,),
"ex": (GROUP_EX,),
"hold": (GROUP_HOLD,),
"slide": (GROUP_SLIDE,),
}
ALLOC_LABEL = {
"tap": "Tap/Star/Touch",
"ex": "EX",
"hold": "Hold 系",
"slide": "Slide 系",
}
# 每列允许的判定 -> 单音符失分(与 CLASS_OPTIONS 同源,EX 只有 Critical / Miss)
ALLOC_OPTIONS = {
"tap": {"cp": 0, "perfect": 0, "great": 100, "good": 250, "miss": 500},
"ex": {"cp": 0, "miss": 500},
"hold": {"cp": 0, "perfect": 0, "great": 200, "good": 500, "miss": 1000},
"slide": {"cp": 0, "perfect": 0, "great": 300, "good": 750, "miss": 1500},
}
# 先分「受约束」的判定(Great/Good/Perfect 进不了 EX),最后分到哪都行的 Miss。
# 顺序不能换:Miss 的可用列最多,放最后才能把剩下的容量全部吃掉。
_ALLOC_FILL_ORDER = ("great", "good", "perfect", "cp", "miss")
_BASE_OF_KEY = {"cp": "cp", "perfect": "perfect", "perfect50": "perfect",
"great": "great", "great40": "great", "great50": "great",
"good": "good", "miss": "miss"}
def alloc_capacity(th: Theory, col: str) -> int:
"""这一列有多少个音符。"""
return sum(th.counts.get(g, 0) for g in ALLOC_GROUPS[col])
def alloc_columns(th: Theory) -> List[str]:
"""谱面里实际存在的非绝赞列。"""
return [c for c in ALLOC_COLUMNS if alloc_capacity(th, c)]
def _alloc_feasible(th: Theory, rest: Dict[str, int],
cols: Sequence[str]) -> bool:
"""这些判定数能不能真的落到谱面上(容量 + EX 约束)。"""
total = sum(rest.get(j, 0) for j in JUDGMENTS)
if total <= 0:
return True
caps = {c: alloc_capacity(th, c) for c in cols}
if total != sum(caps.values()):
return False
src = 0
jb = 1
cb = jb + len(JUDGMENTS)
sink = cb + len(cols)
flow = _MinCostFlow(sink + 1)
for i, j in enumerate(JUDGMENTS):
if rest.get(j, 0):
flow.add_edge(src, jb + i, rest[j], 0)
for k, c in enumerate(cols):
if caps[c]:
flow.add_edge(cb + k, sink, caps[c], 0)
for i, j in enumerate(JUDGMENTS):
if not rest.get(j, 0):
continue
for k, c in enumerate(cols):
if j in ALLOC_OPTIONS[c] and caps[c]:
flow.add_edge(jb + i, cb + k, min(rest[j], caps[c]), 0)
return flow.min_cost(src, sink, total) is not None
def _alloc_spread(th: Theory, rest: Dict[str, int],
cols: Sequence[str]) -> Dict[str, Dict[str, Fraction]]:
"""按剩余容量比例把各判定数摊到各列上(Fraction 精确计算)。"""
left = {c: Fraction(alloc_capacity(th, c)) for c in cols}
table: Dict[str, Dict[str, Fraction]] = {c: {} for c in cols}
for j in _ALLOC_FILL_ORDER:
n = rest.get(j, 0)
if not n:
continue
usable = [c for c in cols if j in ALLOC_OPTIONS[c] and left[c] > 0]
avail = sum((left[c] for c in usable), Fraction(0))
if not usable or avail < n:
raise ValueError("判定数在谱面上分不下去(%s 只剩 %s 个位置,却要给 %d 个)"
% (JUDGE_LABEL[j], avail, n))
for c in usable:
share = Fraction(n) * left[c] / avail
if share:
table[c][j] = table[c].get(j, Fraction(0)) + share
left[c] -= share
return table
@dataclass
class AllocatedResult:
theory: Theory
judges: Dict[str, int] # 汇总判定数
break_spec: Dict[str, int] # 绝赞列的判定数(含档位)
allocated: Dict[str, Dict[str, Fraction]] # 列 -> 判定 -> 个数(均摊,可能带分数)
achievement: Fraction
dx: int
loss_break: int
bonus_break: int
loss_other: Fraction
@property
def columns(self) -> List[str]:
return [c for c in ALLOC_COLUMNS if c in self.allocated]
def solve_allocated(th: Theory, judges: Dict[str, int],
break_spec: Dict[str, int]) -> AllocatedResult:
"""汇总判定数 + 绝赞单列,其余音符按列容量均摊 —— 得到一个确切值。
break_spec 的键与 CLASS_OPTIONS["break"] 一致(cp / perfect / perfect50 /
great / great40 / great50 / good / miss);没给满的绝赞音符按 CP 计。
"""
clean = {j: int(judges.get(j, 0) or 0) for j in JUDGMENTS}
total = sum(clean.values())
if total != th.total_notes:
raise ValueError("判定总数 %d 与谱面音符数 %d 不一致" % (total, th.total_notes))
bspec = {k: int(v) for k, v in (break_spec or {}).items() if int(v)}
for key in bspec:
if key not in CLASS_OPTIONS["break"]:
raise ValueError("绝赞列没有「%s」这个判定(可用 cp/perfect/perfect50/"
"great/great40/great50/good/miss)" % (key,))
bcap = class_capacity(th, "break")
bused = sum(bspec.values())
if bused > bcap:
raise ValueError("绝赞只有 %d 个音符,但给了 %d 个判定" % (bcap, bused))
if bcap > bused:
bspec["cp"] = bspec.get("cp", 0) + (bcap - bused) # 没给满的按 CP 算
cols = alloc_columns(th)
nb_total = sum(alloc_capacity(th, c) for c in cols)
break_base = {j: 0 for j in JUDGMENTS}
for key, n in bspec.items():
break_base[_BASE_OF_KEY[key]] += n
rest: Dict[str, int] = {}
for j in JUDGMENTS:
r = clean[j] - break_base[j]
if r < 0:
raise ValueError("绝赞列的 %s 有 %d 个,超过了你给的 %s 总数 %d"
% (JUDGE_LABEL[j], break_base[j], JUDGE_LABEL[j], clean[j]))
rest[j] = r
if sum(rest.values()) != nb_total:
raise ValueError("非绝赞音符有 %d 个,但剩下的判定数是 %d(对不上)"
% (nb_total, sum(rest.values())))
if not _alloc_feasible(th, rest, cols):
raise ValueError("这些判定数在这个谱面上不可能同时出现"
"(EX 音符只能判 Critical 或 Miss,"
"Great/Good/Miss 也受各类音符个数限制)")
loss_break = sum(CLASS_OPTIONS["break"][k][0] * n for k, n in bspec.items())
bonus_break = sum(CLASS_OPTIONS["break"][k][1] * n for k, n in bspec.items())
allocated = _alloc_spread(th, rest, cols) if nb_total else {c: {} for c in cols}
loss_other = Fraction(0)
for c in cols:
for j, cnt in allocated[c].items():
loss_other += cnt * ALLOC_OPTIONS[c][j]
w = th.all_perfect_score
if w <= 0:
raise ValueError("谱面里没有计分音符")
achievement = Fraction(100) - (100 * (loss_break + loss_other)) / w
if th.break_bonus_score:
achievement += Fraction(bonus_break, th.break_bonus_score)
return AllocatedResult(
theory=th, judges=clean, break_spec=bspec, allocated=allocated,
achievement=achievement, dx=sum(DX_PER[j] * clean[j] for j in JUDGMENTS),
loss_break=loss_break, bonus_break=bonus_break, loss_other=loss_other,
)
# ------------------------------------------------------------------ 反向:工具函数
def _judges_for(th: Theory, miss: int, good: int, great: int,
cp: int, perfect: int) -> Dict[str, int]: