#!/usr/bin/env python3 """BASE_TOPUP deliverable 2, step 2: what the PRE-REGISTERED fixed-H `mpcd` rule prints, beside what the current max-over-H rule prints. The inventory (mpcd_inventory.py) found 105/105 (task x model x H) mpcd arms complete at 9 pairs x 50 episodes, so this comparison is between two FULLY MEASURED rules -- neither side is short of cells and no number here depends on a cell that has yet to run. Pre-registered rule (#287, 2026-09-25 21:3xZ, before any top-up cell existed -- and, as it turned out, before it was known that no top-up was needed at all): column = the `mpcd` arm at a single FIXED H = 100 frames, the same H for all 7 tasks and all three world models, because gamma = 1 - 1/H = 0.99 is the discount the RSM column itself is evaluated at. Reported beside it, never instead: max-over-H mpcd (a SEARCH), and DF's #284 pin H50. No hand-rolled loader: flagship_latex first (sets RSM_EXTRA_EPOCHS), then verdict_table.cells/pool/rate, proposer filtered by flagship_latex.proposer_ok. DF is read on its base world model W3 only (W3s1/W3s2 are a seed axis, gen_q#284). """ import json, os, re, sys ROOT = "/proj/dmfexp/zwhong/rsm_rise" sys.path.insert(0, f"{ROOT}/scripts") import flagship_latex as F # noqa: F401 -- MUST precede the loader import verdict_table as V assert "500" in (os.environ.get("RSM_EXTRA_EPOCHS") or "") TASKS = ["square_sub_20100", "square_expert_play", "two_piece_expert_suboptimal", "two_piece_expert_play", "mug_expert_suboptimal", "threading_expert_play", "two_piece_expert_play_tipping"] SCORERS = [("wm:dinowm", "DINO-WM"), ("wm:lewm", "LeWM"), ("wm:df", "DF")] HGRID = [10, 20, 50, 100, 200] FIXED_H = 100 def h_of(rec): m = re.match(r"H(\d+)\s", rec + " ") return int(m.group(1)) if m else None def model_of(rec): m = re.search(r"\[([A-Za-z0-9_]+)@s[A-Za-z0-9_]+\]$", rec) return m.group(1) if m else None def arm_rate(c, task, sc, H): """(rate, n_episodes, recipe) for the best-covered mpcd arm of `sc` at horizon `H`, or None.""" best = None recs = {a[1] for a in {k[0] for k in c} if a[0] == sc} for rec in recs: if h_of(rec) != H or "mpcd" not in rec.split(): continue if sc == "wm:df" and model_of(rec) != "W3": continue g = V.pool(c, lambda a, p, R=rec, T=task: a == (sc, R) and F.proposer_ok(T, p)) if not g: continue r, n = V.rate(g) cand = (len(g), n, r, rec) if best is None or cand[:2] > best[:2]: best = cand if best is None: return None npair, n, r, rec = best return dict(rate=r, n=n, pairs=npair, recipe=rec) def main(): out, res = [], {} W = out.append W("# The `mpcd` reported-arm rule: pre-registered fixed H=100 vs the max-over-H the columns print today") W("") W("Every (task, model, H) `mpcd` arm is complete at **9 pairs x 50 episodes** " "(`status/basetopup/MPCD_INVENTORY.md`, 105/105), so both rules below are fully measured and " "neither is waiting on a cell.") W("") W("`fixed H=100` is pre-registered on #287 because gamma = 1 - 1/H = **0.99** is the discount the RSM " "column itself is evaluated at -- the rule is fixed by configuration and cannot be tuned by a rate. " "`max over H` is a SEARCH over 5 horizons at one world-model seed and is reported beside it, never " "instead of it.") W("") for sc, label in SCORERS: W(f"## {label} (`{sc}`)") W("") W("| task | fixed H=100 | max over H | H at max | max - fixed | all five H |") W("|---|---|---|---|---|---|") for t in TASKS: c = V.cells(t) per = {H: arm_rate(c, t, sc, H) for H in HGRID} fx = per[FIXED_H] have = {H: v for H, v in per.items() if v} if not have: W(f"| {t} | -- | -- | -- | -- | -- |") continue hmax = max(have, key=lambda H: have[H]["rate"]) mx = have[hmax] fxs = f"{fx['rate']:.1f} ({fx['pairs']}/9)" if fx else "**missing**" d = f"{mx['rate'] - fx['rate']:+.1f}" if fx else "--" allh = " ".join(f"{H}:{have[H]['rate']:.1f}" if H in have else f"{H}:--" for H in HGRID) W(f"| {t} | {fxs} | {mx['rate']:.1f} ({mx['pairs']}/9) | {hmax} | {d} | {allh} |") res.setdefault(sc, {})[t] = dict( fixed=fx, max=mx, h_max=hmax, per_h={H: (have[H]["rate"] if H in have else None) for H in HGRID}) W("") # headline: how much of each column is the search W("## How much of each world-model column is the search?") W("") W("| model | mean over 7 tasks, fixed H=100 | mean, max over H | inflation from the search |") W("|---|---|---|---|") for sc, label in SCORERS: r = res.get(sc, {}) fx = [v["fixed"]["rate"] for v in r.values() if v["fixed"]] mx = [v["max"]["rate"] for v in r.values() if v["max"]] if not fx: continue W(f"| {label} | {sum(fx)/len(fx):.1f} | {sum(mx)/len(mx):.1f} | " f"**{sum(mx)/len(mx) - sum(fx)/len(fx):+.1f}** |") W("") json.dump(res, open(f"{ROOT}/status/basetopup/mpcd_armrule.json", "w"), indent=1, default=str) open(f"{ROOT}/status/basetopup/MPCD_ARMRULE.md", "w").write("\n".join(out) + "\n") print("\n".join(out)) if __name__ == "__main__": main()