Files
mtga/scripts/tune_weights.py
fukunee 58446ec562 Add TRK set preview and refresh draft analysis
Track the TRK preview package and the newest value artifacts for FRA20261007, move the 17lands API notes under data, and ignore downloaded 17lands, card image and live directories.
2026-11-15 11:37:16 +08:00

121 lines
4.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Ablate the advisor's affinity / affinity_cluster weights on real draft data.
`card_value = W_GIH·gih + W_AFF·affinity + W_AFF_CLUSTER·affinity_cluster`
`affinity` = 该牌在**本颜色对**里的 GIH 相对 P0(组内质量,已按色对去均值)
`affinity_cluster` = 该牌在**本簇**里相对同色对的提升(簇内特异)
这两个都是「子集信号」(pair ⊂ 全局,cluster ⊂ pair),叠在全局 GIH 上会重复计分。
本脚本遍历一组权重,跑 sim_draft 的对拍 + 完整模拟,看 top1/top3 / 牌组强度 /
曲线贴合 / 生物达标 怎么变。
用法:
python scripts/tune_weights.py --snapshot FRA20261011 --per-arch 5
"""
from __future__ import annotations
import argparse
import io
import json
import statistics
import sys
from collections import Counter
from contextlib import redirect_stdout
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "scripts"))
import sim_draft as S # noqa: E402
CONFIGS = [
("base .85/.80/.45", 0.85, 0.80, 0.45),
("aff .85/.60/.45", 0.85, 0.60, 0.45),
("aff .85/.40/.45", 0.85, 0.40, 0.45),
("aff .85/.20/.45", 0.85, 0.20, 0.45),
("clu .85/.80/.30", 0.85, 0.80, 0.30),
("clu .85/.80/.15", 0.85, 0.80, 0.15),
("both .85/.50/.25", 0.85, 0.50, 0.25),
("both .85/.35/.15", 0.85, 0.35, 0.15),
("nogih .85/.40/.00", 0.85, 0.40, 0.00),
("gih↑ 1.20/.40/.20", 1.20, 0.40, 0.20),
("aff .80 clu 0", 0.85, 0.80, 0.00),
("aff .60 clu .15", 0.85, 0.60, 0.15),
("gih1.0 .60/.15", 1.00, 0.60, 0.15),
]
def evaluate(adv, d, drafts, decks, id2name):
with redirect_stdout(io.StringIO()):
rp = S.mode_replay(d, adv, drafts, False)
rows = []
for stem, seq in drafts:
rows.append(S.simulate(d, adv, seq, decks.get(stem), id2name))
legal = sum(1 for r in rows if not r["problems"])
qa = [r["q_advisor"] for r in rows if r["q_advisor"]]
qh = [r["q_human"] for r in rows if r["q_human"]]
fa = [r["fit_adv"] for r in rows if r.get("fit_adv") is not None]
fh = [r["fit_hu"] for r in rows if r.get("fit_hu") is not None]
cd = [r["curve_dev"] for r in rows if r.get("curve_dev") is not None]
cre = sum(1 for r in rows if r["cre_ok"])
return {
"top1": rp["top1"], "top3": rp["top3"], "gih_base": rp["baseline_gih"],
"legal": legal, "n": len(rows),
"q_adv": statistics.mean(qa) if qa else 0.0,
"q_human": statistics.mean(qh) if qh else 0.0,
"fit_adv": statistics.mean(fa) if fa else 0.0,
"fit_hu": statistics.mean(fh) if fh else 0.0,
"curve": statistics.mean(cd) if cd else 0.0,
"cre": cre,
"colors": Counter(r["colors"] for r in rows).most_common(4),
}
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--snapshot", default="FRA20261011")
ap.add_argument("--per-arch", type=int, default=5)
ap.add_argument("--seed", type=int, default=7)
ap.add_argument("--pick", default=None, help="只跑指定下标的配置,如 0,1,4")
ap.add_argument("--json", default=None, help="把结果写成 json")
a = ap.parse_args()
adv = S.load_advisor()
d = adv.Data(a.snapshot)
snap = ROOT / "data" / "17lands" / a.snapshot
decks = S.read_decks(snap)
drafts, rd = S.read_drafts(snap, 0, a.seed, decks, None, adv=adv, data=d,
per_arch=a.per_arch)
id2name = {c["mtga_id"]: nm for nm, c in d.cdata.items() if c.get("mtga_id")}
id2name.update({mid: nm for mid, nm in S.basic_ids(list(decks.values())).items()
if mid not in id2name})
print("抽样 %d 局(%d 个 arch)· snapshot %s\n" % (len(drafts), rd.get("covered", 0), a.snapshot))
base = None
out = []
print("%-18s %6s %6s %8s %8s %7s %5s %5s" % (
"config", "top1", "top3", "q_adv", "fit_adv", "curve", "cre", "legal"))
cfgs = CONFIGS
if a.pick:
idx = [int(x) for x in a.pick.split(",") if x.strip() != ""]
cfgs = [CONFIGS[i] for i in idx]
for tag, wg, wa, wc in cfgs:
adv.W_GIH, adv.W_AFF, adv.W_AFF_CLUSTER = wg, wa, wc
m = evaluate(adv, d, drafts, decks, id2name)
out.append({"tag": tag, "W_GIH": wg, "W_AFF": wa, "W_AFF_CLUSTER": wc, **m})
if base is None:
base = m
print("%-18s %5.1f%% %5.1f%% %8.4f %8.1f %7.2f %3d/%-2d %3d/%d" % (
tag, m["top1"] * 100, m["top3"] * 100, m["q_adv"], m["fit_adv"],
m["curve"], m["cre"], m["n"], m["legal"], m["n"]))
print("\n(top1/top3 = 与人类选牌吻合;q_adv = 配出牌组强度(GIH 均值,偏高是循环指标);"
"fit_adv = 自身思路拟合度(同样循环);curve = 曲线 L1 越低越好;cre = 生物达标)")
if a.json:
Path(a.json).write_text(json.dumps(out, ensure_ascii=False, indent=1), encoding="utf-8")
print("wrote", a.json)
return 0
if __name__ == "__main__":
raise SystemExit(main())