Files
threshing-atlas/core/atlas/model.py
T
ekcSchianoandClaude Fable 5.2 ab3664f369 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
2026-11-12 03:31:33 +00:00

459 lines
20 KiB
Python

"""From coded runs to the route model's numbers: what each run says about each step of the game (prepare_run),
how often each roll came out in each context (count_tables), how strongly to shrink toward shorter contexts
(fit_alpha), the probability source the solver uses (Params), and which options have enough data (support_from).
The game itself (steps, options, chance contexts) is core/spec/game.yaml.
The random numbers (xorshift32, Marsaglia-Tsang gamma) are the same as the route model has always used, so the
posterior draws are reproducible from the seed.
"""
import math
import numpy as np
from . import spec
G = spec.game()
DECISIONS = {k: v["options"] for k, v in G["decisions"].items()}
CHANCE = G["chance"]
COLOURS = G["colours"]
FROM_FORM = G["from_form"]
HELP = set(FROM_FORM["helped"])
IGNORE = set(FROM_FORM["ignored"])
FIGHT_OF = FROM_FORM["fight"]
DAGGER_OF = FROM_FORM["dagger"]
RARE_GOALS = FROM_FORM["rare_goals"]
NO_DAGGER = "(no dagger choice)"
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):
return "Green" if ctx == "tree-Greens" else ctx.split("-")[1]
def dagger_after(fight, dagger):
"""The fight moves in a history: a run that charged straight in never faces the dagger."""
return None if fight is None else (dagger or None) if fight == "Pause to assess" else NO_DAGGER
# ---------------------------------------------------------------- 1. coded run -> what it says about each step
def genuine_attempt(r, ctx):
"""Whether a run's bonding attempt counts for this encounter, given the rider's stated goal and Malek intent."""
c = enc_colour(ctx)
g = r.get("goal") or ""
matches = g == c or g.startswith("Any") or (g == "Blue or Black" and c in ("Blue", "Black")) or \
(g == "Red (tree)" and ctx == "tree-Red")
if matches:
return True
if r.get("malek_intent") == "No":
return True
if r.get("malek_intent") == "Yes":
return False
specific_other = (g in COLOURS and g != c) or g == "Blue or Black" or g == "Red (tree)"
return not specific_other
def prepare_run(r):
"""One coded run (atlas.coding) as the steps of the game it reveals."""
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)
v["react"] = r["sighting_action"] if r.get("sighting_action") in DECISIONS["react"] else None
v["diedSight"] = r["scream"] == "DIED at first sighting"
if v["diedSight"]:
return v
if r["scream"] not in ("Investigate & help", "Ignore the scream", "Investigate & leave"):
v["sightUnknown"] = True # no answer after the first dragon: whether the rider survived it is not known
return v
res = r["resolution"]
# the choices at the scream, by their names in game.yaml (gates are written with these)
v["scream"] = "Keep walking" if r["scream"] == "Ignore the scream" else "Investigate the scream"
if r["scream"] in ("Investigate & help", "Investigate & leave"):
v["clearing"] = "Help your squadmate" if r["scream"] == "Investigate & help" else "Walk away"
if r["scream"] == "Investigate & help":
v["help"] = True
v["fight"] = FIGHT_OF.get(r.get("combat_first"))
v["dagger"] = dagger_after(v["fight"], DAGGER_OF.get(r.get("combat_second")))
v["diedCombat"] = res == "DIED in combat"
v["cadet"] = res if res in HELP else None
if v["diedCombat"]:
return v
v["post"] = r["post_path"] if r.get("post_path") in DECISIONS["post"] else None
elif r["scream"] == "Ignore the scream":
v["cadet"] = res if res in IGNORE else None
v["post"] = "(no fork)"
elif r["scream"] == "Investigate & leave":
v["cadet"] = "Left them to it"
v["post"] = "(no fork)"
else:
return v
v["survivedCadets"] = True
v["diedHour"] = r["hour"] == "DIED before the hour"
if v["diedHour"]:
return v
v["hour"] = r["hour"] if r.get("hour") in DECISIONS["hour"] else None
b = r.get("branch")
if v["hour"] == "Tree" and b:
v["shadow"] = b == "Shadow (rare)"
if v["shadow"]:
v["shadowColour"] = "Black" if r.get("enc_Black") else "Blue"
v["encCtx"] = "shadow-" + v["shadowColour"]
elif b == "Descend quickly: Red":
v["descend"], v["red"], v["encCtx"] = "Quickly (Red)", "Jump onto the Red", "tree-Red"
elif b == "Descend quickly: let the Red pass":
v["descend"], v["red"], v["encCtx"] = "Quickly (Red)", "Let it pass (two Greens)", "tree-Greens"
elif b == "Descend slowly: two Greens":
v["descend"], v["encCtx"] = "Slowly (two Greens)", "tree-Greens"
elif v["hour"] == "Valley" and b:
if b == "Valley Red":
v["valley"], v["encCtx"] = "Face the Red", "valley-Red"
elif b == "Passed the Red: meadow":
v["valley"] = "Pass the Red (meadow)"
# the meadow dragon: Blue if one appeared, otherwise the one the rider ended the run with
ended = lambda c: r.get("enc_" + c) and r.get("enc_" + c) != "Changed my mind" # noqa: E731
v["meadow"] = "Blue" if (r.get("enc_Blue") or r.get("rare_reported") == "Blue (valley)") else \
"Orange" if ended("Orange") else "Brown" if ended("Brown") else \
"Orange" if r.get("enc_Orange") else "Brown" if r.get("enc_Brown") else \
"Blue" if r.get("saw_blue_meadow") is True else \
r["outcome_color"] if r.get("saw_blue_meadow") is False and r.get("outcome_color") in ("Orange", "Brown") else None
if v["meadow"]:
v["encCtx"] = "meadow-" + v["meadow"]
if v.get("encCtx"):
v["encOutcome"] = r["outcome_color"] if (r["outcome"] == "Bonded" and r.get("outcome_color")) else \
"burn" if r["outcome"] == "Incinerated" else None
if v["encOutcome"] and not genuine_attempt(r, v["encCtx"]):
v["encOutcome"] = None
return v
def gate_open(node, state):
"""Whether a gated roll (game.yaml `requires`) can come out any other way than `otherwise`."""
req = CHANCE[node].get("requires") or {}
return all(state.get(k) == v for k, v in req.items())
# what each chance event observed in a prepared run (a gated roll only where its gate was open)
OBSERVE = {
"fs": lambda v: v.get("fs"),
"dieSight": lambda v: ("die" if v.get("diedSight") else "live") if v.get("fs") and not v.get("sightUnknown") else None,
"dieCombat": lambda v: ("die" if v.get("diedCombat") else "live") if v.get("help") else None,
"dieHour": lambda v: ("die" if v.get("diedHour") else "live") if v.get("survivedCadets") else None,
"shadow": lambda v: None if "shadow" not in v or not gate_open("shadow", v) else ("yes" if v["shadow"] else "no"),
"shadowColour": lambda v: v.get("shadowColour"),
"meadow": lambda v: v.get("meadow"),
}
assert set(OBSERVE) == set(CHANCE), "every chance event in game.yaml needs an OBSERVE rule"
def ctx_key(v, names):
return "|".join("?" if v.get(k) is None else str(v[k]) for k in names)
# ---------------------------------------------------------------- 2. counts
def can_bond(ctx, colour):
"""Whether a bond with this colour is possible at this encounter (game.yaml `encounters`)."""
e = G["encounters"][ctx]
return colour == "burn" or colour == e["colour"] or colour in (e.get("also") or [])
def count_tables(prepared, weights=None):
"""Counts for every chance event at every back-off level, plus each encounter's outcomes. weights: per run,
{chance event: weight of the outcome it observed} (reporting-bias corrections, core/atlas/bias.py)."""
T = {}
for node, sp in CHANCE.items():
t = T[node] = {}
for i, v in enumerate(prepared):
y = OBSERVE[node](v)
if not y:
continue
w = weights[i].get(node, 1) if weights else 1
if w == 0:
continue
for L in range(len(sp["context"]) + 1):
names = sp["context"][:L]
if any(v.get(k) is None for k in names):
break
c = t.setdefault(f"{L}:{ctx_key(v, names)}", {})
c[y] = c.get(y, 0) + w
for t in T.values(): # weighted counts are fractions: keep them short and stable
for c in t.values():
for k, x in c.items():
if isinstance(x, float):
c[k] = round(x, 4)
enc = T["encounter"] = {}
for v in prepared:
if not v.get("encCtx"):
continue
c = enc.setdefault(v["encCtx"], {"outcomes": {}, "seen": {}})
if v.get("encOutcome") and can_bond(v["encCtx"], v["encOutcome"]):
c["outcomes"][v["encOutcome"]] = c["outcomes"].get(v["encOutcome"], 0) + 1
# which colours can ever end a run in this context (from all runs, genuine or not)
for v in prepared:
if v.get("encCtx") and v.get("encOutcome") and v["encOutcome"] != "burn" and can_bond(v["encCtx"], v["encOutcome"]):
enc[v["encCtx"]]["seen"][v["encOutcome"]] = 1
return T
def support_from(prepared, min_n):
"""Options taken at least min_n times; options never recorded at all are not restricted."""
n = {}
def bump(k, o):
n.setdefault(k, {})
n[k][o] = n[k].get(o, 0) + 1
for v in prepared:
for k in ("company", "direction", "cadet", "hour", "descend", "valley"):
if v.get(k):
bump(k, v[k])
if v.get("left") and v["left"] != ALONE:
bump("leave", v["left"])
if v.get("fs") and v.get("react"):
bump("react:" + v["fs"], v["react"])
if v.get("post") and v["post"] != "(no fork)":
bump("post", v["post"])
# the cadet encounter's steps, including riders killed in the fight (they chose to help)
if v.get("help") or v.get("cadet"):
ignore = bool(v.get("cadet") and v["cadet"] in IGNORE)
bump("scream", "Keep walking" if ignore else "Investigate the scream")
if ignore:
bump("wounded", v["cadet"])
else:
bump("clearing", "Walk away" if v.get("cadet") == "Left them to it" else "Help your squadmate")
if v.get("cadet") in HELP:
bump("fate", v["cadet"])
if v.get("help") and v.get("fight"):
bump("fight", v["fight"])
if v.get("dagger") and v["dagger"] != NO_DAGGER:
bump("dagger", v["dagger"])
s = {"encounter": DECISIONS["encounter"]}
for k in ("company", "leave", "direction", "cadet", "scream", "clearing", "fight", "dagger", "fate", "wounded",
"post", "hour", "descend", "valley"):
s[k] = [o for o in DECISIONS[k] if n.get(k, {}).get(o, 0) >= min_n] or list(DECISIONS[k])
for fs in ("Red", "Brown", "Green", "Orange"):
k = "react:" + fs
s[k] = [o for o in DECISIONS["react"] if n.get(k, {}).get(o, 0) >= min_n] or list(DECISIONS["react"])
for k, d in G["decisions"].items():
if d.get("always_offered") and k != "encounter":
s[k] = list(d["options"])
return s, n
# ---------------------------------------------------------------- 3. empirical-Bayes concentration
def lgamma(x):
"""Log-gamma (Lanczos), the same approximation the route model has always used."""
c = [76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.1208650973866179e-2,
-0.5395239384953e-5]
y = x
tmp = x + 5.5
tmp -= (x + 0.5) * math.log(tmp)
ser = 1.000000000190015
for v in c:
y += 1
ser += v / y
return -tmp + math.log(2.5066282746310005 * ser / x)
def fit_alpha(T, node):
"""The concentration that maximises the Dirichlet-multinomial likelihood of each back-off level given the one above."""
cats = CHANCE[node]["outcomes"]
t = T[node]
def pooled(key):
c = t.get(key, {})
a = [c.get(k, 0) + 0.5 for k in cats]
s = 0
for x in a:
s += x
return [x / s for x in a]
pairs = []
for key in t:
L = int(key.split(":")[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])
pairs.append(([t[key].get(k, 0) for k in cats], pooled(parent)))
if not pairs:
return {"alpha": ALPHA, "logLik": None}
# a roll that has only ever come out one way says nothing about whether routes differ: pool fully (the fit
# would otherwise drive every route's chance of the unseen outcome to zero)
if sum(1 for k in cats if t.get("0:", {}).get(k, 0) > 0) < 2:
return {"alpha": max(ALPHA_GRID), "logLik": None}
best_a, best_ll, lls = ALPHA, -math.inf, {}
for a in ALPHA_GRID:
ll = 0
for c, p in pairs:
n = 0
for x in c:
n += x
ll += lgamma(a) - lgamma(n + a)
for i in range(len(cats)):
ll += lgamma(c[i] + a * p[i]) - lgamma(a * p[i])
lls[a] = ll
if ll > best_ll:
best_ll, best_a = ll, a
return {"alpha": best_a, "logLik": lls}
# ---------------------------------------------------------------- 4. random numbers (reproducible from the seed)
class Rng:
"""xorshift32, as in the original route model (JavaScript semantics)."""
def __init__(self, seed):
self.s = (seed & 0xFFFFFFFF) or 1
def __call__(self):
s = self.s
s = (s ^ (s << 13)) & 0xFFFFFFFF
s ^= s >> 17
s = (s ^ (s << 5)) & 0xFFFFFFFF
self.s = s
return s / 4294967296
def gamma(shape, rand):
if shape < 1:
g = gamma(shape + 1, rand)
return g * math.pow(rand() or 1e-12, 1 / shape)
d = shape - 1 / 3
c = 1 / math.sqrt(9 * d)
while True:
while True:
u1 = rand() or 1e-12
u2 = rand()
x = math.sqrt(-2 * math.log(u1)) * math.cos(2 * math.pi * u2)
v = 1 + c * x
if v > 0:
break
v = v * v * v
u = rand()
if u < 1 - 0.0331 * x ** 4 or math.log(u) < 0.5 * x * x + d * (1 - v + math.log(v)):
return d * v
def dirichlet(alpha, rand):
g = [gamma(max(a, 1e-6), rand) for a in alpha]
s = 0
for x in g:
s += x
return [x / s for x in g]
def _normalise(a):
s = 0
for x in a:
s = s + x
return [x / s for x in a]
# ---------------------------------------------------------------- 5. the probability source
class Params:
"""Chance probabilities and encounter outcomes, as arrays over D worlds.
mode "mean": D = 1, posterior means. mode "draw": D = len(seeds), one joint posterior draw per seed (each
distribution is drawn once, the first time it is needed, and reused, so a draw is one consistent world).
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, 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)
self.filled = {} # same keys -> bool array (D,): which worlds have drawn this distribution
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 = []
for L in range(len(sp["context"]) + 1):
names = sp["context"][:L]
if any(state.get(k) is None for k in names):
break
out.append(f"{L}:{ctx_key(state, names)}")
return out
def _get(self, mk, K, make_a):
"""The distribution for key mk: drawn (or computed) the first time it is needed, then reused."""
if self.mode == "mean":
if mk not in self.memo:
self.memo[mk] = np.array(_normalise(make_a(None)), dtype=float).reshape(K, 1)
return self.memo[mk]
if mk not in self.memo:
self.memo[mk] = np.zeros((K, self.D))
self.filled[mk] = np.zeros(self.D, dtype=bool)
d = self.world
if d is not None and not self.filled[mk][d]:
self.memo[mk][:, d] = dirichlet([float(x) for x in make_a(d)], self.rngs[d])
self.filled[mk][d] = True
return self.memo[mk]
def chance(self, node, state):
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:
return self._cache[ck]
out = self._chance(node, state)
if self.world is None:
self._cache[ck] = out
return out
def _chance(self, node, state):
cats = CHANCE[node]["outcomes"]
fitted = self.alphas.get(node) or ALPHA
distinct = CHANCE[node].get("distinct", 0)
p = None
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, 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)]
p = self._get((node, key), len(cats), make_a)
return dict(zip(cats, p))
def encounter(self, ctx):
e = self.T["encounter"].get(ctx, {"outcomes": {}, "seen": {}})
cats = [k for k in ENC_CATS if k == "burn" or e["seen"].get(k) or e["outcomes"].get(k, 0) > 0 or k == enc_colour(ctx)]
p = self._get(("enc", ctx), len(cats), lambda d: [e["outcomes"].get(k, 0) + 0.5 for k in cats])
return dict(zip(cats, p))