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:
ekcSchiano
2026-11-03 21:04:21 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 3536007ade
commit cc86343668
19 changed files with 1291 additions and 110 deletions
+2
View File
@@ -6,6 +6,8 @@ The shared model. Every other part uses it, so a change here touches everything:
|---|---|
| `engine.js` | Turns a row of the community form (any version of it) into one coded run: the run format, every coding rule, missing answers, email redaction. Also the statistics the site shows. |
| `spec/form.yaml` | **Declarative.** Every question of the community form (how to find it in an export) and every answer (what it is coded as), the answers that count as missing, email redaction. |
| `spec/game.yaml` | **Declarative.** The game: every decision and its options in order, every roll and what its odds may depend on, every bonding encounter, and the flow of a run from the entrance to the dragons. |
| `atlas/model.py`, `atlas/solver.py` | The route model in Python: what each coded run reveals about each step, the count tables and shrinkage, and one interpreter of `game.yaml` used to solve, evaluate and export the best routes. `tests/parity/compare_solver.py` checks it against `solver.js`, which the site and the bot use until the JS advisor replaces it. |
| `atlas/coding.py` | The Python coding rules: reads `form.yaml`, codes every row (`python -m atlas.coding`). Rule tests: `tests/core/test_coding.py`. |
| `graph.js` | The route graph: the bot's scene graph, the decision-variable graph shared by all sources, the crosswalk from the bot's on-screen text to form answers, Beta credible intervals. |
| `solver.js` | The decision model: every choice and roll in a run, chance estimates with empirical-Bayes back-off, backward induction for each dragon, and the advisor the site and the bot use to get advice from any point in a run. |
+404
View File
@@ -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))
+372
View File
@@ -0,0 +1,372 @@
"""The route model: best decisions for bonding each dragon colour.
The game tree is core/spec/game.yaml. One interpreter (Walker) walks it; three kinds of walk reuse it:
Solver backward induction: the best option at every decision (vectorised over posterior draws)
Evaluator the value of a fixed policy (the one chosen on the posterior means) in every draw
Exporter the published decision tree, following the chosen options
Two objectives per target colour X:
1. "this attempt": maximise P(bond X on this attempt).
2. "eventually": deaths are free (you can try again), so what matters is not ending the account on the wrong
dragon: maximise P(X) / (P(X) + P(another colour)), by Dinkelbach iteration; ties go to the higher P(X).
Values are triples (x, o, r) of arrays over worlds: P(target), P(another colour), P(reaching an encounter alive).
"""
import numpy as np
from . import model, spec
G = spec.game()
STEPS = G["steps"]
LABEL = {**{k: v["label"] for k, v in G["decisions"].items()}, **{k: v["label"] for k, v in G["chance"].items()}}
def canon(state):
return tuple(sorted(state.items()))
def apply(then, state, value):
"""The state after an option or outcome: keep / drop, then set ($ = the value just taken)."""
s = {k: state[k] for k in then["keep"] if k in state} if "keep" in then else dict(state)
for k in then.get("drop", ()):
s.pop(k, None)
for k, v in (then.get("set") or {}).items():
s[k] = value if v == "$" else v
return s
class Walker:
"""Walks the game tree of game.yaml. Subclasses say what a decision, a roll, an ending and an encounter mean."""
def __init__(self, params, X, support, only=None):
self.params, self.X, self.support, self.only = params, X, support, only
def options(self, name, state):
by = G["decisions"][name].get("support_by")
if by:
return self.support.get(f"{name}:{state.get(by)}") or model.DECISIONS[name]
return self.support[name]
def root(self):
return self.run(G["start"], {})
def run(self, step_id, state):
st = STEPS[step_id]
if "decision" in st:
name = st["decision"]
return self.decision(name, state, self.options(name, state), lambda o: self.follow(st["then"], o, state))
node = st["chance"]
return self.chance(node, state, lambda k: self.follow(st["then"], k, state))
def follow(self, then, value, state):
t = then.get(value) or then["default"]
if "end" in t:
return self.end(t["end"])
s = apply(t, state, value)
if "encounter" in t:
ctx = t["encounter"].replace("$", value)
opts = ["Die on purpose"] if self.only and ctx not in self.only else self.support["encounter"]
return self.decision("encounter", {**s, "enc": ctx}, opts,
lambda o: self.die_on_purpose() if o == "Die on purpose" else self.attempt(ctx))
return self.run(t["go"], s)
class _Values(Walker):
"""Shared by Solver and Evaluator: values are (x, o, r) arrays over the worlds in params."""
def __init__(self, params, X, support, only=None):
super().__init__(params, X, support, only)
D = params.D
self.ZERO = (np.zeros(D), np.zeros(D), np.zeros(D))
self.REACHED = (np.zeros(D), np.zeros(D), np.ones(D))
def end(self, what):
return self.ZERO
def die_on_purpose(self):
return self.REACHED
def attempt(self, ctx):
cache = self.__dict__.setdefault("_attempt", {})
if ctx not in cache:
cache[ctx] = self._attempt_value(ctx)
return cache[ctx]
def _attempt_value(self, ctx):
x = 0
oth = 0
for k, p in self.params.encounter(ctx).items():
if k != "burn":
if k == self.X:
x = x + p
else:
oth = oth + p
D = self.params.D
return (np.broadcast_to(x, D).astype(float), np.broadcast_to(oth, D).astype(float), np.ones(D))
def chance(self, node, state, cont):
acc = self.ZERO
for k, p in self.params.chance(node, state).items():
if not np.any(p):
continue
v = cont(k)
acc = (acc[0] + p * v[0], acc[1] + p * v[1], acc[2] + p * v[2])
return acc
class Solver(_Values):
"""Backward induction. lam: scalar or array (one per world). After root(): .value, .choices, .option_values."""
def __init__(self, params, X, support, lam=0.0, record=False, only=None):
super().__init__(params, X, support, only)
self.lam = lam
self.memo, self.choices = {}, {}
self.option_values = {} if record else None
def score(self, v):
lam = self.lam
return (1 - lam) * v[0] - lam * v[1] + 1e-9 * v[0] - 1e-12 * v[1]
def decision(self, name, state, options, cont):
key = (name, canon(state))
if key in self.memo:
return self.memo[key]
top = arg = None
vals = {}
for i, o in enumerate(options):
v = cont(o)
vals[o] = v
if top is None:
top, arg = v, np.zeros(self.params.D, dtype=int)
else:
better = self.score(v) > self.score(top)
top = tuple(np.where(better, v[j], top[j]) for j in range(3))
arg = np.where(better, i, arg)
self.memo[key] = top
self.choices[key] = (list(options), arg)
if self.option_values is not None:
self.option_values[key] = vals
return top
def solve(self):
self.value = self.root()
self.memo.clear() # the values are no longer needed; posterior draws keep only their choices
return self
def decide(self, name, state, world=0):
opts, arg = self.choices[(name, canon(state))]
return opts[int(arg[world])]
class Evaluator(_Values):
"""The value of a fixed policy: decide(name, state) -> option, in every world of params."""
def __init__(self, params, X, support, decide, only=None):
super().__init__(params, X, support, only)
self.decide = decide
def decision(self, name, state, options, cont):
return cont(self.decide(name, state))
class Exporter(Walker):
"""The published tree: at decisions the chosen option, every option's value (each followed by the best play
afterwards) and how often it stays best across posterior draws; at rolls every branch and its probability.
Only the chosen option's subtree is expanded."""
def __init__(self, params, X, support, sol, draws, min_show=1e-4, only=None):
super().__init__(params, X, support, only)
self.sol, self.draws, self.min_show = sol, draws, min_show
def decision(self, name, state, options, cont):
key = (name, canon(state))
chosen = self.sol.decide(name, state)
ov = self.sol.option_values.get(key, {})
xs = sorted((float(v[0][0]) for v in ov.values()), reverse=True)
tie = len(xs) > 1 and xs[0] - xs[1] < 1e-12 # nothing to choose between (e.g. every option ends the run)
still = None
if not tie and self.draws is not None:
opts, arg = self.draws.choices[key]
ok = arg == opts.index(chosen)
reached = getattr(self.draws.params, "reached", {}).get(key)
if reached is not None:
ok = ok & reached
still = int(np.sum(ok)) / len(arg)
return {
"type": "decision", "var": name, "label": LABEL[name], "choice": chosen, "tie": tie, "stillBest": still,
"options": {o: {"pX": float(v[0][0]), "pOther": float(v[1][0]), "pReach": float(v[2][0])} for o, v in ov.items()},
"next": cont(chosen),
}
def chance(self, node, state, cont):
dist = self.params.chance(node, state)
return {"type": "chance", "var": node, "label": LABEL[node],
"branches": {k: {"p": float(p[0]), "next": cont(k)} for k, p in dist.items() if float(p[0]) >= self.min_show}}
def end(self, what):
return {"type": "end", "what": what}
def die_on_purpose(self):
return self.end("die on purpose")
def attempt(self, ctx):
return {"type": "end", "what": f"encounter: {ctx}", "outcomes": {k: float(p[0]) for k, p in self.params.encounter(ctx).items()}}
class _Drawer(Walker):
"""Walks the game in one world only, drawing its distributions in the order the walk first needs them and
skipping branches with probability exactly zero in that world. Records which decisions the world reaches."""
def __init__(self, params, support, world, reached):
super().__init__(params, None, support)
self.d, self.seen, self.reached = world, set(), reached
def decision(self, name, state, options, cont):
key = (name, canon(state))
if key in self.seen:
return
self.seen.add(key)
self.reached.setdefault(key, np.zeros(self.params.D, dtype=bool))[self.d] = True
for o in options:
cont(o)
def chance(self, node, state, cont):
for k, p in self.params.chance(node, state).items():
if p[self.d]:
cont(k)
def attempt(self, ctx):
self.params.encounter(ctx)
def end(self, what):
return None
def die_on_purpose(self):
return None
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 = {}
for d in range(p.D):
p.world = d
_Drawer(p, support, d, p.reached).root()
p.world = None
return p
# ---------------------------------------------------------------- objectives
def solve(params, X, support, lam=0.0, record=False, only=None):
return Solver(params, X, support, lam=lam, record=record, only=only).solve()
def solve_ratio(params, X, support, record=False, only=None):
"""Dinkelbach: lam* = max P(X) / (P(X) + P(other)), separately in every world (at most 30 updates each)."""
D = params.D
lam = np.zeros(D)
s = solve(params, X, support, lam=lam, only=only)
done = np.zeros(D, dtype=bool)
for _ in range(30):
x, o = s.value[0], s.value[1]
tot = x + o
r = np.where(tot > 0, x / np.where(tot > 0, tot, 1), 0.0)
done = done | (np.abs(r - lam) < 1e-10)
if done.all():
break
lam = np.where(done, lam, r)
s = solve(params, X, support, lam=lam, only=only)
if record:
s = solve(params, X, support, lam=lam, record=True, only=only)
s.lam = lam
return s
def evaluate(params, X, support, decide, only=None):
return Evaluator(params, X, support, decide, only=only).root()
# ---------------------------------------------------------------- summaries
def _sum(xs):
s = 0
for x in xs:
s += x
return s
def quantile(xs, q):
s = sorted(xs)
i = (len(s) - 1) * q
lo, hi = int(np.floor(i)), int(np.ceil(i))
return s[lo] + (s[hi] - s[lo]) * (i - lo)
def summarise(xs):
xs = [float(x) for x in xs]
return {"mean": _sum(xs) / len(xs), "lo": quantile(xs, 0.025), "hi": quantile(xs, 0.975)}
# ---------------------------------------------------------------- the published policy
def build_policy(runs, draws=200, seed=20261101, min_support=None, alpha=None, colours=None):
"""The full policy for every colour. runs: coded runs (atlas.coding), as dicts."""
colours = colours or model.COLOURS
min_support = G["min_support"] if min_support is None else min_support
prepared = [model.prepare_run(r) for r in runs]
T = model.count_tables(prepared)
support, counts = model.support_from(prepared, min_support)
fitted = {k: model.fit_alpha(T, k) for k in model.CHANCE}
alphas = {k: alpha for k in model.CHANCE} if alpha else {k: f["alpha"] for k, f in fitted.items()}
mean = model.Params(T, "mean", alphas=alphas)
drawp = draw_worlds(T, [seed + i * 7919 for i in range(draws)], alphas, support)
# Cross-fitting: choose the policy on one random half of the runs and score it on the other half.
r = model.Rng(seed)
fold = [0 if r() < 0.5 else 1 for _ in prepared]
halves = [model.count_tables([v for v, f in zip(prepared, fold) if f == h]) for h in (0, 1)]
half_params = [model.Params(Th, "mean", alphas=alphas) for Th in halves]
targets = {}
for X in colours:
one = solve(mean, X, support, record=True)
ratio = solve_ratio(mean, X, support, record=True)
d_one = solve(drawp, X, support)
d_ratio = solve_ratio(drawp, X, support)
ev_one = evaluate(drawp, X, support, one.decide)
ev_ratio = evaluate(drawp, X, support, ratio.decide)
def block(sol, ev, dr):
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])},
"ratio": summarise([xi / (xi + oi) if xi + oi else 0 for xi, oi in zip(x, o)]),
"expectedAttempts": summarise([1 / xi if xi > 0 else float("inf") for xi in x]),
"tree": Exporter(mean, X, support, sol, dr).root(),
}
cross = []
for f in (0, 1):
pol = solve(half_params[f], X, support)
cross.append({"inSample": float(pol.value[0][0]),
"heldOut": float(evaluate(half_params[1 - f], X, support, pol.decide)[0][0])})
targets[X] = {
"thisAttempt": block(one, ev_one, d_one), "eventually": block(ratio, ev_ratio, d_ratio),
"crossFit": {"inSample": (cross[0]["inSample"] + cross[1]["inSample"]) / 2,
"heldOut": (cross[0]["heldOut"] + cross[1]["heldOut"]) / 2},
}
return {
"schema": 1,
"model": {
"description": "Decision tree with perfect recall; chance probabilities depend on earlier choices via hierarchical Dirichlet back-off.",
"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()},
"decisions": model.DECISIONS, "support": support, "supportCounts": counts,
"tables": T,
},
"data": {"runs": len(runs)},
"targets": targets,
}
+4
View File
@@ -16,3 +16,7 @@ def load(name):
def form():
return load("form")
def game():
return load("game")
+194
View File
@@ -0,0 +1,194 @@
# The threshing as a game tree: every choice a rider makes (decisions), every roll of the dice (chance events),
# and every bonding encounter, in the game's own order.
#
# This file is the single source of truth for the route model. core/atlas/solver.py builds the decision tree from
# it, and the route advisor used by the site and Ken's bot reads the same file (published as game.json).
# Adding a choice to the game means adding it here (plus, if the form records it, how in core/atlas/model.py).
version: 1
# Colours in the order the route model reports them
colours: [Red, Orange, Brown, Green, Blue, Black]
# ---------------------------------------------------------------- decisions: the rider's choices
# options: every option the game offers, in a fixed order (ties go to the first). Options seen fewer than
# min_support times in the data are not recommended, except where `always_offered` says so.
decisions:
company: { label: Company, options: [Alone, 1 Squadmate, 2 Squadmates] }
leave: { label: Your squadmates, options: [Stay together, Leave them] }
direction: { label: First direction, options: [East, West, Center] }
react:
label: Facing the first dragon
# the form's categories; which ones each dragon offers is learned per first dragon (support key react:<fs>)
options: [Hold still, Respect / face it, Retreat / run, Hide, Weapon, Meet its gaze, Protect squadmate]
support_by: fs
scream: { label: The scream, options: [Investigate the scream, Keep walking] }
clearing: { label: The clearing (your squadmate vs two bullies), options: [Help your squadmate, Walk away] }
fight: { label: "The fight: your first move", options: [Pause to assess, Charge the smaller cadet, Charge between the bigger cadet and your squadmate] }
dagger: { label: The smaller cadet's dagger, options: [Engage the smaller cadet, Charge the bigger cadet anyway] }
fate: { label: The bullies' fate, options: [Killed the bullies yourself, Squadmate's revenge, Left bullies to the dragons] }
wounded: { label: The wounded cadet, options: [Tourniquet, Left injured cadet, Took their weapons] }
post: { label: Fork in the road, options: [Forest, West, East] }
hour: { label: After an hour, options: [Tree, Valley] }
descend: { label: Climb down, options: [Quickly (Red), Slowly (two Greens)] }
red:
label: The Red below the tree
options: [Jump onto the Red, Let it pass (two Greens)]
# rarely logged on its own, but letting it pass leads to the two Greens' encounter, which has plenty of data
always_offered: true
valley: { label: Valley centre, options: [Face the Red, Pass the Red (meadow)] }
encounter: { label: Bonding encounter, options: [Attempt the bond, Die on purpose], always_offered: true }
# the whole cadet encounter as one answer (how the community form records it)
cadet:
label: Cadet encounter
options: [Killed the bullies yourself, Squadmate's revenge, Left bullies to the dragons, Tourniquet, Left injured cadet, Took their weapons, Left them to it]
summary_only: true
min_support: 20
# ---------------------------------------------------------------- chance events: the game's rolls
# outcomes: in a fixed order. context: the earlier steps the odds may depend on, most important first; the
# estimate for a full context is shrunk toward the estimate for a shorter one (hierarchical Dirichlet back-off,
# strength learned from the data).
chance:
fs: { label: First dragon, outcomes: [Red, Brown, Green, Orange], context: [direction, company, left] }
dieSight: { label: Survive the first dragon, outcomes: [die, live], context: [fs, direction, company, left, react] }
dieCombat: { label: Survive the fight, outcomes: [die, live], context: [fight, dagger, fs, direction] }
dieHour: { label: Survive the hour, outcomes: [die, live], context: [cadet, post] }
shadow: { label: Shadow at the tree, outcomes: ["yes", "no"], context: [cadet, post, fs, direction, company] }
shadowColour: { label: Shadow dragon, outcomes: [Black, Blue], context: [] }
meadow: { label: Meadow dragon, outcomes: [Orange, Brown, Blue], context: [cadet, post, fs, direction, company] }
# Bonding encounters: where each colour can be bonded. tree-Greens can also end in an Orange or Brown rescue.
encounters:
valley-Red: { label: The valley Red, colour: Red }
tree-Red: { label: The Red below the tree, colour: Red }
tree-Greens: { label: The two Greens, colour: Green }
meadow-Orange: { label: The meadow Orange, colour: Orange }
meadow-Brown: { label: The meadow Brown, colour: Brown }
meadow-Blue: { label: The meadow Blue, colour: Blue }
shadow-Blue: { label: The Blue at the redwood, colour: Blue }
shadow-Black: { label: The Black (the shadow), colour: Black }
encounter_outcomes: [burn, Red, Orange, Brown, Green, Blue, Black]
by_colour:
Red: [valley-Red, tree-Red]
Orange: [meadow-Orange, tree-Greens]
Brown: [meadow-Brown, tree-Greens]
Green: [tree-Greens]
Blue: [meadow-Blue, shadow-Blue]
Black: [shadow-Black]
# ---------------------------------------------------------------- the flow of a run
# Each step is a decision, a chance event or an encounter, keyed by the history so far (the "state").
# After an option or outcome, `then` says what happens: `set` adds to the state ($ = the option or outcome just
# taken), `keep` / `drop` remove steps nothing later depends on, `go` names the next step, `end` finishes the run
# (with a death, the value is zero). `default` covers every option without its own entry.
start: company
steps:
company:
decision: company
then:
Alone: { set: { company: $, left: (alone) }, go: direction }
default: { set: { company: $ }, go: leave }
leave:
decision: leave
then: { default: { set: { left: $ }, go: direction } }
direction:
decision: direction
then: { default: { set: { direction: $ }, go: first_dragon } }
first_dragon:
chance: fs
then: { default: { set: { fs: $ }, go: react } }
react:
decision: react
then: { default: { set: { react: $ }, go: survive_sighting } }
survive_sighting:
chance: dieSight
then:
die: { end: incinerated at the first sighting }
# leaving your squadmates and your reaction only matter at the first dragon
live: { keep: [company, direction, fs], go: scream }
scream:
decision: scream
then:
Keep walking: { set: { scream: $ }, go: wounded }
Investigate the scream: { set: { scream: $ }, go: clearing }
wounded:
decision: wounded
then: { default: { set: { cadet: $, post: (no fork) }, go: survive_hour } }
clearing:
decision: clearing
then:
Walk away: { set: { clearing: $, cadet: Left them to it, post: (no fork) }, go: survive_hour }
Help your squadmate: { set: { clearing: $ }, go: fight }
fight:
decision: fight
then:
Pause to assess: { set: { fight: $ }, go: dagger }
default: { set: { fight: $, dagger: (no dagger choice) }, go: survive_fight }
dagger:
decision: dagger
then: { default: { set: { dagger: $ }, go: survive_fight } }
survive_fight:
chance: dieCombat
then:
die: { end: killed in the fight }
live: { go: fate }
fate:
decision: fate
# the fight's moves only matter for surviving it
then: { default: { drop: [fight, dagger], set: { cadet: $ }, go: post } }
post:
decision: post
then: { default: { set: { post: $ }, go: survive_hour } }
survive_hour:
chance: dieHour
then:
die: { end: incinerated }
live: { go: hour }
hour:
decision: hour
then:
Tree: { set: { hour: $ }, go: shadow }
Valley: { set: { hour: $ }, go: valley }
shadow:
chance: shadow
then:
"yes": { go: shadow_colour }
"no": { set: { shadow: "no" }, go: descend }
shadow_colour:
chance: shadowColour
then: { default: { set: { shadow: "yes" }, encounter: "shadow-$" } }
descend:
decision: descend
then:
Quickly (Red): { set: { descend: $ }, go: red }
Slowly (two Greens): { set: { descend: $ }, encounter: tree-Greens }
red:
decision: red
then:
Jump onto the Red: { set: { red: $ }, encounter: tree-Red }
Let it pass (two Greens): { set: { red: $ }, encounter: tree-Greens }
valley:
decision: valley
then:
Face the Red: { set: { valley: $ }, encounter: valley-Red }
Pass the Red (meadow): { set: { valley: $ }, go: meadow }
meadow:
chance: meadow
then: { default: { set: { meadow: $ }, encounter: "meadow-$" } }
# ---------------------------------------------------------------- how the community form's answers map onto the game
# (used to count how often each option was taken and how each roll came out; see core/atlas/model.py)
from_form:
fight:
"Assess the situation": Pause to assess
"Even the numbers (smaller cadet)": Charge the smaller cadet
"Put yourself in the middle (bigger cadet)": Charge between the bigger cadet and your squadmate
dagger:
"Engage the smaller cadet": Engage the smaller cadet
"Daggers or not (protect squadmate)": Charge the bigger cadet anyway
# cadet resolutions on each side of the scream
helped: [Killed the bullies yourself, Squadmate's revenge, Left bullies to the dragons]
ignored: [Tourniquet, Left injured cadet, Took their weapons]
# a rider's stated goal that means "chasing a rare dragon"
rare_goals: [Blue, Black, Blue or Black]