Core: the route model in Python, built from a declarative game.yaml (#10)
Step 2 of the Python-first, declarative core. - core/spec/game.yaml: the game as data: every decision and its options, every roll and the earlier steps its odds may depend on, every bonding encounter, and the flow of a run from the entrance to the dragons (set / keep / drop / go / end / encounter). Also how the form's coded answers map onto the game. - core/atlas/model.py: what each coded run reveals about each step, count tables, empirical-Bayes shrinkage, and the posterior draws (same random numbers as before, reproducible from the seed). - core/atlas/solver.py: one interpreter of game.yaml used three ways (solve, evaluate a fixed policy, export the published tree) instead of the tree written out five times. Vectorised over the 200 posterior draws: the build takes about 2 minutes instead of 4, with no memory flag. - planner/build_policy.py and policy_report.py replace the JS build and report. Checked against the JavaScript model: every number in policy.json agrees to within 4e-12 (last-digit differences in log/cos). Three "still best" shares differ by one draw in 200, at near-ties under the "eventually" objective. POLICY.md is otherwise identical. tests/parity/compare_solver.py keeps checking JS against Python in CI until the site's JS advisor replaces solver.js; tests/core/test_game.py checks game.yaml is complete and consistent. Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
3536007ade
commit
cc86343668
@@ -0,0 +1,404 @@
|
||||
"""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"]
|
||||
|
||||
|
||||
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}
|
||||
lf = r.get("left_squadmates")
|
||||
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"] 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
|
||||
res = r["resolution"]
|
||||
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"] = "Quickly (Red)"
|
||||
# a rider who let the Red pass and met the Greens instead
|
||||
if r.get("enc_Red") == "Changed my mind" and r.get("enc_Green"):
|
||||
v["red"], v["encCtx"] = "Let it pass (two Greens)", "tree-Greens"
|
||||
else:
|
||||
v["red"], v["encCtx"] = "Jump onto the Red", "tree-Red"
|
||||
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 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
|
||||
|
||||
|
||||
# what each chance event observed in a prepared run
|
||||
OBSERVE = {
|
||||
"fs": lambda v: v.get("fs"),
|
||||
"dieSight": lambda v: ("die" if v.get("diedSight") else "live") if v.get("fs") 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 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 count_tables(prepared):
|
||||
"""Counts for every chance event at every back-off level, plus each encounter's outcomes."""
|
||||
T = {}
|
||||
for node, sp in CHANCE.items():
|
||||
t = T[node] = {}
|
||||
for v in prepared:
|
||||
y = OBSERVE[node](v)
|
||||
if not y:
|
||||
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) + 1
|
||||
enc = T["encounter"] = {}
|
||||
for v in prepared:
|
||||
if not v.get("encCtx"):
|
||||
continue
|
||||
c = enc.setdefault(v["encCtx"], {"outcomes": {}, "seen": {}})
|
||||
if v.get("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":
|
||||
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 == 0:
|
||||
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}
|
||||
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)}.
|
||||
"""
|
||||
|
||||
def __init__(self, T, mode="mean", seeds=(), alphas=None):
|
||||
self.T, self.mode, self.alphas = T, mode, alphas 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 _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):
|
||||
# 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"]
|
||||
al = self.alphas.get(node) or ALPHA
|
||||
p = None
|
||||
for key in self._levels(node, state):
|
||||
c = self.T[node].get(key, {})
|
||||
parent = p
|
||||
|
||||
def make_a(d, c=c, parent=parent):
|
||||
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))
|
||||
Reference in New Issue
Block a user