Route model: first attempts are their own shadow roll; experiment plan and automated bot cycle

- game.yaml: `givens` (first or later attempt) and `distinct` levels that are not pooled; the shadow roll
  depends on the attempt first. Form submissions show the shadow on 9% of first attempts and 0.3% of later ones.
- Reporting bias: a roll whose bias cannot be measured can borrow another roll's (`borrow: {shadow: meadow}`),
  so form submissions inform the shadow again. An assumption, stated in planner/policy.yaml.
- policy.json: targets.<colour>.firstAttempt (this attempt, eventually). Black: about 1.4% on a first attempt,
  0.07% on a later one.
- solver.js: advisor options `attempt`, `fix`, sampling "uniform"; assign() and armFor() for the plan.
- planner/experiments.yaml + build_experiments.py -> data/public/experiments.json (progress and status per
  experiment, rebuilt from the bot's runs).
- python -m collection cycle: import bot runs, refit, re-plan, deliver the kit; .gitea/workflows/bot-cycle.yml.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014jqCC2LZ4x1WHQ1V57nTsv
This commit is contained in:
ekcSchiano
2026-11-12 03:31:33 +00:00
co-authored by Claude Fable 5.2
parent ee07ee143e
commit ab3664f369
25 changed files with 1938 additions and 573 deletions
+17 -1
View File
@@ -62,6 +62,10 @@ def estimate(runs, prepared, cfg, rare_goals, seed=20261109, sims=4000):
e = {"rare": y, "n": n, "completeRare": ry, "completeN": rn}
if g.get("complete"):
e.update(rho=1.0, lo=1.0, hi=1.0, used="complete: counted as reported")
elif ry < cfg.get("min_complete_rare", 5) and (cfg.get("borrow") or {}).get(node):
# complete logging has (almost) never seen it, so the bias cannot be estimated for this roll: assume
# the group over-reports it as much as it over-reports another rare roll (filled in below)
e.update(rho=None, borrow=cfg["borrow"][node])
elif ry < cfg.get("min_complete_rare", 5):
# complete logging has (almost) never seen it: the bias cannot be estimated, so this roll is learned
# from complete logging only (its range then shows how little is known)
@@ -83,10 +87,22 @@ def estimate(runs, prepared, cfg, rare_goals, seed=20261109, sims=4000):
for g in groups:
for node in rolls:
e = out[g["name"]][node]
if e.get("rho") is None and not e.get("exclude"):
if e.get("rho") is None and not e.get("exclude") and not e.get("borrow"):
src = out.get(g.get("same_as") or "", {}).get(node, {})
e["rho"] = src.get("rho") or 1.0
e["lo"], e["hi"] = src.get("lo", e["rho"]), src.get("hi", e["rho"])
# a roll whose bias cannot be estimated borrows the same group's estimate for another roll (`borrow`)
for g in groups:
for node in rolls:
e = out[g["name"]][node]
if e.get("borrow"):
src = out[g["name"]].get(e["borrow"], {})
if src.get("rho"):
e.update(rho=src["rho"], lo=src.get("lo"), hi=src.get("hi"),
used=f"assumed equal to this group's bias for the {e['borrow']} roll: complete logs have only "
f"{e['completeRare']} rare outcomes, too few to estimate it")
else:
e.update(lo=None, hi=None, exclude=True, used="left out: no bias estimate to borrow")
return out, names
+24 -5
View File
@@ -28,6 +28,8 @@ ALONE = "(alone)"
ALPHA = 20
ALPHA_GRID = [1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 20000, 100000]
ENC_CATS = G["encounter_outcomes"]
GIVENS = G.get("givens") or {}
DEFAULT_GIVEN = {k: g["default"] for k, g in GIVENS.items()}
def enc_colour(ctx):
@@ -62,6 +64,8 @@ def prepare_run(r):
v = {"company": r["company"] if r["company"] in DECISIONS["company"] else None,
"direction": r["direction"] if r["direction"] in DECISIONS["direction"] else None,
"fs": r["first_sighting"] if r["first_sighting"] in ("Red", "Brown", "Green", "Orange") else None}
# the givens (game.yaml): first or later attempt, from the run's attempt number
v["attempt"] = r.get("attempt_group") if r.get("attempt_group") in GIVENS.get("attempt", {}).get("values", ()) else None
lf = r.get("left_squadmates")
known = lf is not None and not (isinstance(lf, float) and lf != lf) and lf != ""
v["left"] = ALONE if v["company"] == "Alone" else (("Leave them" if lf is True or str(lf).lower() == "true" else "Stay together") if v["company"] and known else None)
@@ -279,7 +283,7 @@ def fit_alpha(T, node):
pairs = []
for key in t:
L = int(key.split(":")[0])
if L == 0:
if L <= CHANCE[node].get("distinct", 0): # the top level, and levels that are not pooled (game.yaml)
continue
ctx = key[key.index(":") + 1:].split("|")
parent = f"{L - 1}:" + "|".join(ctx[:L - 1])
@@ -368,10 +372,14 @@ class Params:
In draw mode each world draws its numbers in the order its own walk of the game first needs them (a branch with
probability exactly zero in that world is never walked), so call solver.draw_worlds() before using it.
chance(node, state) -> {outcome: array(D)}; encounter(ctx) -> {outcome: array(D)}.
given: the run's givens (game.yaml `givens`, e.g. first or later attempt); having() gives the same worlds for
other givens.
"""
def __init__(self, T, mode="mean", seeds=(), alphas=None):
def __init__(self, T, mode="mean", seeds=(), alphas=None, given=None):
self.T, self.mode, self.alphas = T, mode, alphas or {}
self.given = {**DEFAULT_GIVEN, **(given or {})}
self.rngs = [Rng(s) for s in seeds] if mode == "draw" else []
self.D = len(self.rngs) if mode == "draw" else 1
self.memo = {} # (node, level key) or ("enc", ctx) -> array (K, D)
@@ -379,6 +387,14 @@ class Params:
self.world = None # while drawing one world (see draw_world), its index
self._cache = {}
def having(self, **given):
"""The same odds (and the same posterior draws) for a run with other givens."""
import copy
p = copy.copy(self)
p.given = {**self.given, **given}
p._cache = {}
return p
def _levels(self, node, state):
sp = CHANCE[node]
out = []
@@ -408,6 +424,7 @@ class Params:
if not gate_open(node, state): # a closed gate: the roll always comes out the same way
other = CHANCE[node]["otherwise"]
return {k: np.full(self.D, 1.0 if k == other else 0.0) for k in CHANCE[node]["outcomes"]}
state = {**self.given, **state}
# once every world has been drawn, the answer depends only on the context values: cache it
ck = (node, tuple(state.get(k) for k in CHANCE[node]["context"]))
if self.world is None and ck in self._cache:
@@ -419,13 +436,15 @@ class Params:
def _chance(self, node, state):
cats = CHANCE[node]["outcomes"]
al = self.alphas.get(node) or ALPHA
fitted = self.alphas.get(node) or ALPHA
distinct = CHANCE[node].get("distinct", 0)
p = None
for key in self._levels(node, state):
for L, key in enumerate(self._levels(node, state)):
c = self.T[node].get(key, {})
parent = p
al = 1.0 if L <= distinct else fitted # a level that is not pooled keeps a token prior of one run
def make_a(d, c=c, parent=parent):
def make_a(d, c=c, parent=parent, al=al):
if parent is None:
return [c.get(k, 0) + 0.5 for k in cats]
return [c.get(k, 0) + al * (parent[i] if d is None else parent[i][d]) for i, k in enumerate(cats)]
+41 -1
View File
@@ -250,13 +250,30 @@ def draw_worlds(T, seeds, alphas, support):
"""Posterior draws, one world per seed, each drawn in its own walk of the game (see Params)."""
p = model.Params(T, "draw", seeds=seeds, alphas=alphas)
p.reached = {}
default = dict(p.given)
for d in range(p.D):
p.world = d
_Drawer(p, support, d, p.reached).root()
# the same world for runs with other givens (a first attempt): only the rolls that depend on them are new
for given in other_givens():
p.given = {**default, **given}
_Drawer(p, support, d, {}).root()
p.given = default
p.world = None
return p
def other_givens():
"""Every non-default value of every given in game.yaml, one at a time: [{"attempt": "First attempt"}]."""
return [{k: v} for k, g in model.GIVENS.items() for v in g["values"] if v != g["default"]]
def given_key(given):
"""The name a non-default given's results are published under: {"attempt": "First attempt"} -> "firstAttempt"."""
words = " ".join(given.values()).split()
return words[0].lower() + "".join(w.capitalize() for w in words[1:])
# ---------------------------------------------------------------- objectives
def solve(params, X, support, lam=0.0, record=False, only=None):
@@ -364,6 +381,28 @@ def build_policy(runs, draws=200, seed=20261101, min_support=None, alpha=None, c
"crossFit": {"inSample": (cross[0]["inSample"] + cross[1]["inSample"]) / 2,
"heldOut": (cross[0]["heldOut"] + cross[1]["heldOut"]) / 2},
}
# Runs with another given (a first attempt). "This attempt" is solved as above. "Eventually": every attempt
# after it is an ordinary one, worth the default's ratio (lam), so the best play maximises
# P(X now) + P(no bond now) * lam, which is the same score with lam fixed.
later = [xi / (xi + oi) if xi + oi else 0.0 for xi, oi in zip(ev_ratio[0], ev_ratio[1])]
for given in other_givens():
m1, d1 = mean.having(**given), drawp.having(**given)
g_one = solve(m1, X, support, record=True)
g_ev = solve(m1, X, support, lam=ratio.lam, record=True)
e_one = evaluate(d1, X, support, g_one.decide)
e_ev = evaluate(d1, X, support, g_ev.decide)
def gblock(sol, ev, dr, m1=m1):
x, o = ev[0], ev[1]
return {"pX": {**summarise(x), "point": float(sol.value[0][0])},
"pOther": {**summarise(o), "point": float(sol.value[1][0])},
"tree": Exporter(m1, X, support, sol, dr).root()}
ev_block = gblock(g_ev, e_ev, solve(d1, X, support, lam=d_ratio.lam))
x0, o0 = float(g_ev.value[0][0]), float(g_ev.value[1][0])
ev_block["pEventually"] = {**summarise([x + (1 - x - o) * r for x, o, r in zip(e_ev[0], e_ev[1], later)]),
"point": x0 + (1 - x0 - o0) * float(np.ravel(ratio.lam)[0])}
targets[X][given_key(given)] = {"given": given, "thisAttempt": gblock(g_one, e_one, solve(d1, X, support)),
"eventually": ev_block}
return {
"schema": 1,
"model": {
@@ -371,12 +410,13 @@ def build_policy(runs, draws=200, seed=20261101, min_support=None, alpha=None, c
"alphas": alphas, "alphaFit": {k: f["logLik"] for k, f in fitted.items()}, "draws": draws, "seed": seed,
"minSupport": min_support,
"chanceContext": {k: s["context"] for k, s in model.CHANCE.items()},
"given": model.DEFAULT_GIVEN,
"decisions": model.DECISIONS, "support": support, "supportCounts": counts,
"tables": T,
# how much each kind of source over-reports each rare roll, and the correction applied (bias.py)
"reportingBias": bias_est,
# the game itself (core/spec/game.yaml), so the advisor in core/solver.js can re-solve from any point
"game": {k: G[k] for k in ("version", "colours", "decisions", "chance", "encounters", "encounter_outcomes",
"game": {k: G[k] for k in ("version", "colours", "givens", "decisions", "chance", "encounters", "encounter_outcomes",
"by_colour", "start", "steps", "from_form", "bot_text")},
},
"data": {"runs": len(runs)},