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
@@ -48,7 +48,7 @@ run it by hand from the Actions tab, or locally with `npm run check-sources`.
|
||||
|
||||
## Best routes (the solver)
|
||||
|
||||
`npm run policy` rebuilds `data/public/policy.json` and the readable `analysis/reports/POLICY.md` (about 4 minutes:
|
||||
`npm run policy` (Python: `planner/build_policy.py`, from the game in `core/spec/game.yaml`) rebuilds `data/public/policy.json` and the readable `analysis/reports/POLICY.md` (about 2 minutes:
|
||||
200 posterior draws for two data samples). For every colour it gives the best decision at every point under two
|
||||
objectives: the chance of bonding that colour on this attempt, and the chance the account eventually ends on that
|
||||
colour when you die on purpose whenever a run goes wrong. Chance events (which dragon appears, whether the shadow
|
||||
|
||||
+1
-1
@@ -6,4 +6,4 @@ Making sense of the data.
|
||||
|---|---|
|
||||
| `stats/` | General statistics in Python (`analyze.py`, `stats_core.py`): rates with Wilson intervals, odds ratios with Fisher's test and Benjamini–Hochberg, ridge logistic models that adjust for data source and chase goal. `core/engine.js` computes the same numbers for the site, and `tests/parity/` checks they match. |
|
||||
| `network/` | `build-graph.js` combines every source into `data/public/graph.json` (the decision-variable graph and the bot's scene graph). CI fails if the committed graph is out of date. |
|
||||
| `reports/` | Generated reports. `POLICY.md` is written by `planner/policy-report.js`; do not edit it by hand. |
|
||||
| `reports/` | Generated reports. `POLICY.md` is written by `planner/policy_report.py`; do not edit it by hand. |
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Best routes by dragon colour
|
||||
|
||||
Generated from 5988 community runs by `planner/build-policy.js`. Machine-readable version: `data/public/policy.json`.
|
||||
Generated from 5988 community runs by `planner/build_policy.py` from the game in `core/spec/game.yaml`. Machine-readable version: `data/public/policy.json`.
|
||||
|
||||
**How to read this.** "This attempt" maximises the chance of bonding the colour on one run. "Eventually" assumes you will die on purpose and try again whenever the run goes wrong, so it avoids any choice that can end the account on a different dragon. Percentages are posterior means with 95% credible intervals. "Held-out" is the same policy chosen on half of the runs and scored on the other half: when it is much lower than the in-sample number, the route's advantage is mostly noise.
|
||||
|
||||
|
||||
@@ -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. |
|
||||
|
||||
@@ -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))
|
||||
@@ -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,
|
||||
}
|
||||
@@ -16,3 +16,7 @@ def load(name):
|
||||
|
||||
def form():
|
||||
return load("form")
|
||||
|
||||
|
||||
def game():
|
||||
return load("game")
|
||||
|
||||
@@ -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]
|
||||
File diff suppressed because one or more lines are too long
+1
-1
@@ -31,7 +31,7 @@ only where it has to run: the site's drawing and interaction, and a small route
|
||||
share.
|
||||
|
||||
1. Coding rules: `core/spec/form.yaml` + `core/atlas/coding.py`, with rule tests. **Done.**
|
||||
2. Route model in Python, built from `core/spec/game.yaml`; bot text in `core/spec/crosswalk.yaml`.
|
||||
2. Route model in Python, built from `core/spec/game.yaml`. **Done** (the bot's text, `core/spec/crosswalk.yaml`, moves with the bot importer and the JS advisor in step 4).
|
||||
3. Fix the coding bugs found in the core review (letting the Red pass, "left your squadmates" before the
|
||||
redesigned form, missing bot text), each with a rule test.
|
||||
4. The site shows precomputed statistics with fixed filter presets; the upload button and `engine.js` go; a small
|
||||
|
||||
+2
-2
@@ -8,13 +8,13 @@
|
||||
"dev": "node site/build.js && python3 -m http.server 8000 -d dist",
|
||||
"test:js": "node tests/parity/run_engine.js data/public/form_responses.json tests/out",
|
||||
"test:py": "PYTHONPATH=core python3 -m atlas.coding && cd analysis/stats && PYTHONPATH=../../core python3 analyze.py && cd ../.. && PYTHONPATH=core python3 -m unittest discover -s tests/core -p 'test_*.py'",
|
||||
"test:parity": "python3 tests/parity/compare_runs.py && python3 tests/parity/compare_results.py && python3 tests/parity/compare_routes.py",
|
||||
"test:parity": "python3 tests/parity/compare_runs.py && python3 tests/parity/compare_results.py && python3 tests/parity/compare_routes.py && python3 tests/parity/compare_solver.py",
|
||||
"test:unit": "node --test tests/core/graph.test.js tests/core/solver.test.js",
|
||||
"test": "npm run test:js && npm run test:py && npm run test:parity && npm run test:unit",
|
||||
"graph": "node analysis/network/build-graph.js",
|
||||
"import-bot": "node collection/bot/import-bot.js",
|
||||
"check-sources": "python3 collection/sources/update_sources.py",
|
||||
"policy": "node --max-old-space-size=6000 planner/build-policy.js && node planner/policy-report.js"
|
||||
"policy": "python3 planner/build_policy.py && python3 planner/policy_report.py"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
|
||||
+2
-2
@@ -4,8 +4,8 @@ The route planner: the best route to every dragon, and what goes back to Ken's b
|
||||
|
||||
| File | What it does |
|
||||
|---|---|
|
||||
| `build-policy.js` | Solves the decision model (`core/solver.js`) for every colour and both goals ("this attempt" and "eventually"), with 200 posterior draws and a cross-fit check. Writes `data/public/policy.json`, which has a content id so each bot run can record which policy it followed. About 4 minutes. |
|
||||
| `policy-report.js` | Writes the readable version, `analysis/reports/POLICY.md`. |
|
||||
| `build_policy.py` | Solves the route model (`core/atlas/solver.py`, built from the game in `core/spec/game.yaml`) for every colour and both goals ("this attempt" and "eventually"), with 200 posterior draws and a cross-fit check. Writes `data/public/policy.json`, which has a content id so each bot run can record which policy it followed. About 2 minutes. |
|
||||
| `policy_report.py` | Writes the readable version, `analysis/reports/POLICY.md`. |
|
||||
|
||||
Run both with `npm run policy`. The site's valley map and The Odds read `policy.json`; the bot downloads it with
|
||||
`solver.js` and `graph.js` (see `docs/BOT_INTEGRATION.md`).
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
// Build data/public/policy.json: the best decisions for each dragon colour, under both objectives.
|
||||
// node planner/build-policy.js [--draws 200]
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const E = require("../core/engine.js");
|
||||
const S = require("../core/solver.js");
|
||||
const root = path.resolve(__dirname, "..");
|
||||
const arg = (k, d) => { const i = process.argv.indexOf("--" + k); return i > 0 ? process.argv[i + 1] : d; };
|
||||
const draws = +arg("draws", 200);
|
||||
|
||||
const raw = JSON.parse(fs.readFileSync(path.join(root, "data/public/form_responses.json"), "utf8"));
|
||||
const { runs } = E.codeRows(raw.rows, raw.headers);
|
||||
const isRare = (g) => ["Blue", "Black", "Blue or Black"].includes(g);
|
||||
const samples = {
|
||||
// every run
|
||||
all: runs,
|
||||
// runs least affected by selective reporting: bulk logs, plus form runs from riders not chasing a rare dragon
|
||||
lowBias: runs.filter((r) => r.source !== "Form" || !isRare(r.goal)),
|
||||
};
|
||||
const t0 = Date.now();
|
||||
const out = { schema: 1, source: { form_rows: raw.rows.length }, samples: {} };
|
||||
for (const [name, rs] of Object.entries(samples)) {
|
||||
out.samples[name] = S.buildPolicy(rs, { draws });
|
||||
console.log(`${name}: ${rs.length} runs, ${((Date.now() - t0) / 1000).toFixed(1)} s`);
|
||||
}
|
||||
// a content id, so the bot can record which policy produced each run (no timestamps: the build is reproducible)
|
||||
out.id = require("crypto").createHash("sha1").update(JSON.stringify(out.samples)).digest("hex").slice(0, 12);
|
||||
fs.writeFileSync(path.join(root, "data/public/policy.json"), JSON.stringify(out));
|
||||
console.log("policy id", out.id);
|
||||
const pct = (p) => (100 * p).toFixed(p < 0.01 ? 2 : 1) + "%";
|
||||
for (const [name, s] of Object.entries(out.samples)) {
|
||||
console.log(`\n${name} alphas ${JSON.stringify(s.model.alphas)}`);
|
||||
for (const [X, t] of Object.entries(s.targets)) {
|
||||
const a = t.thisAttempt, b = t.eventually;
|
||||
console.log(` ${X.padEnd(6)} this attempt ${pct(a.pX.mean)} [${pct(a.pX.lo)}, ${pct(a.pX.hi)}] other bond ${pct(a.pOther.mean)}` +
|
||||
` | eventually: ratio ${pct(b.ratio.mean)}, P(X) ${pct(b.pX.mean)}, ~${b.expectedAttempts.mean.toFixed(0)} attempts` +
|
||||
` | cross-fit ${pct(t.crossFit.inSample)} -> held-out ${pct(t.crossFit.heldOut)}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
"""Build data/public/policy.json: the best decisions for every dragon colour, under both objectives.
|
||||
|
||||
python planner/build_policy.py [--draws 200] [--out data/public/policy.json]
|
||||
|
||||
Two samples: every run, and a low-bias sample (bulk logs plus form runs from riders not chasing a rare dragon).
|
||||
The output has a content id, so each bot run can record which policy it followed.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "core"))
|
||||
from atlas import coding, model, solver # noqa: E402
|
||||
|
||||
|
||||
def clean(o):
|
||||
"""JSON-safe: numpy numbers to floats, infinities and NaN to null (as JavaScript's JSON.stringify does)."""
|
||||
if isinstance(o, dict):
|
||||
return {str(k): clean(v) for k, v in o.items()}
|
||||
if isinstance(o, (list, tuple)):
|
||||
return [clean(v) for v in o]
|
||||
if hasattr(o, "item"):
|
||||
o = o.item()
|
||||
if isinstance(o, float) and (math.isinf(o) or math.isnan(o)):
|
||||
return None
|
||||
return o
|
||||
|
||||
|
||||
def coded_runs(path):
|
||||
df, _ = coding.load(path)
|
||||
runs = df.drop(columns=["run_id"]).astype(object).where(df.drop(columns=["run_id"]).notna(), None).to_dict("records")
|
||||
return runs
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
ap = argparse.ArgumentParser(description=__doc__)
|
||||
ap.add_argument("--draws", type=int, default=200)
|
||||
ap.add_argument("--data", default=str(ROOT / "data/public/form_responses.json"))
|
||||
ap.add_argument("--out", default=str(ROOT / "data/public/policy.json"))
|
||||
a = ap.parse_args(argv)
|
||||
|
||||
rows = len(json.load(open(a.data, encoding="utf-8"))["rows"])
|
||||
runs = coded_runs(a.data)
|
||||
samples = {
|
||||
"all": runs,
|
||||
"lowBias": [r for r in runs if r["source"] != "Form" or r["goal"] not in model.RARE_GOALS],
|
||||
}
|
||||
t0 = time.time()
|
||||
out = {"schema": 1, "source": {"form_rows": rows}, "samples": {}}
|
||||
for name, rs in samples.items():
|
||||
out["samples"][name] = clean(solver.build_policy(rs, draws=a.draws))
|
||||
print(f"{name}: {len(rs)} runs, {time.time() - t0:.1f} s")
|
||||
# a content id (no timestamps: the build is reproducible)
|
||||
out["id"] = hashlib.sha1(json.dumps(out["samples"], separators=(",", ":"), sort_keys=True).encode()).hexdigest()[:12]
|
||||
Path(a.out).write_text(json.dumps(out, separators=(",", ":")), encoding="utf-8")
|
||||
print("policy id", out["id"])
|
||||
pct = lambda p: "–" if p is None else f"{100 * p:.{2 if p < 0.01 else 1}f}%" # noqa: E731
|
||||
for name, s in out["samples"].items():
|
||||
print(f"\n{name} alphas {json.dumps(s['model']['alphas'])}")
|
||||
for X, t in s["targets"].items():
|
||||
x, e = t["thisAttempt"], t["eventually"]
|
||||
att = e["expectedAttempts"]["mean"]
|
||||
print(f" {X:<6} this attempt {pct(x['pX']['mean'])} [{pct(x['pX']['lo'])}, {pct(x['pX']['hi'])}] other bond {pct(x['pOther']['mean'])}"
|
||||
f" | eventually: ratio {pct(e['ratio']['mean'])}, P(X) {pct(e['pX']['mean'])}, ~{'–' if att is None else round(att)} attempts"
|
||||
f" | cross-fit {pct(t['crossFit']['inSample'])} -> held-out {pct(t['crossFit']['heldOut'])}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,62 +0,0 @@
|
||||
// Write analysis/reports/POLICY.md: a readable summary of data/public/policy.json.
|
||||
// node planner/policy-report.js
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const root = path.resolve(__dirname, "..");
|
||||
const P = JSON.parse(fs.readFileSync(path.join(root, "data/public/policy.json"), "utf8"));
|
||||
const pct = (p) => (p == null ? "–" : (100 * p).toFixed(p < 0.1 ? 1 : 0) + "%");
|
||||
const conf = (n) => (n.stillBest == null ? "" : ` (still best in ${pct(n.stillBest)} of posterior draws)`);
|
||||
|
||||
function walk(n, out, indent = "") {
|
||||
if (!n) return;
|
||||
if (n.type === "decision") {
|
||||
const alts = Object.entries(n.options).sort((a, b) => b[1].pX - a[1].pX)
|
||||
.map(([k, v]) => `${k} ${pct(v.pX)}`).join(", ");
|
||||
if (n.tie) out.push(`${indent}- ${n.label}: any (no difference for this colour)`);
|
||||
else out.push(`${indent}- **${n.label}: ${n.choice}**${conf(n)}${n.stillBest != null && n.stillBest < 0.75 ? " — no clear winner" : ""}. If chosen, then best play after: ${alts}`);
|
||||
walk(n.next, out, indent);
|
||||
} else if (n.type === "chance") {
|
||||
const br = Object.entries(n.branches).sort((a, b) => b[1].p - a[1].p);
|
||||
const live = br.filter(([k]) => !["die"].includes(k));
|
||||
if (br.some(([k]) => k === "die")) {
|
||||
const d = br.find(([k]) => k === "die");
|
||||
out.push(`${indent}- ${n.label}: ${pct(1 - d[1].p)} survive`);
|
||||
for (const [, b] of live) walk(b.next, out, indent);
|
||||
return;
|
||||
}
|
||||
if (live.length === 1) return walk(live[0][1].next, out, indent);
|
||||
out.push(`${indent}- ${n.label}: ${br.map(([k, b]) => `${k} ${pct(b.p)}`).join(", ")}`);
|
||||
for (const [k, b] of br) {
|
||||
out.push(`${indent} - if **${k}**:`);
|
||||
walk(b.next, out, indent + " ");
|
||||
}
|
||||
} else if (n.outcomes) {
|
||||
out.push(`${indent}- Outcome if you attempt: ${Object.entries(n.outcomes).sort((a, b) => b[1] - a[1]).map(([k, p]) => `${k === "burn" ? "incinerated" : "bond " + k} ${pct(p)}`).join(", ")}`);
|
||||
} else out.push(`${indent}- ${n.what}`);
|
||||
}
|
||||
|
||||
const s = P.samples.all, lb = P.samples.lowBias;
|
||||
const L = [];
|
||||
L.push("# Best routes by dragon colour", "");
|
||||
L.push(`Generated from ${s.data.runs} community runs by \`planner/build-policy.js\`. Machine-readable version: \`data/public/policy.json\`.`, "");
|
||||
L.push("**How to read this.** \"This attempt\" maximises the chance of bonding the colour on one run. \"Eventually\" assumes you will die on purpose and try again whenever the run goes wrong, so it avoids any choice that can end the account on a different dragon. Percentages are posterior means with 95% credible intervals. \"Held-out\" is the same policy chosen on half of the runs and scored on the other half: when it is much lower than the in-sample number, the route's advantage is mostly noise.", "");
|
||||
L.push("**Caveats.** The form records choices only at the level of company, direction, first dragon, cadet encounter, fork, tree or valley, and which dragon you met. Choices inside each encounter are not recorded, so the bond chance at an encounter is the community's average with whatever answers people used. Riders chasing a rare dragon report sightings more often than riders who log every run; the \"low-bias\" columns use only bulk logs and non-chasers.", "");
|
||||
L.push("| Colour | This attempt | Low-bias sample | Held-out | Eventually: chance the account ends on this colour | Attempts needed (about) |");
|
||||
L.push("|---|---|---|---|---|---|");
|
||||
for (const X of Object.keys(s.targets)) {
|
||||
const t = s.targets[X], a = t.thisAttempt, e = t.eventually, l = lb.targets[X].thisAttempt;
|
||||
L.push(`| ${X} | ${pct(a.pX.mean)} (${pct(a.pX.lo)}–${pct(a.pX.hi)}) | ${pct(l.pX.mean)} (${pct(l.pX.lo)}–${pct(l.pX.hi)}) | ${pct(t.crossFit.heldOut)} | ${pct(e.ratio.mean)} | ${Math.round(e.expectedAttempts.mean)} |`);
|
||||
}
|
||||
L.push("", "## How much do earlier choices matter?", "");
|
||||
L.push("Each chance event can depend on the choices before it. The model learns how much from the data (empirical-Bayes concentration; larger means the history matters less):", "");
|
||||
L.push("| Chance event | Depends on (most important first) | Concentration |", "|---|---|---|");
|
||||
for (const [k, ctx] of Object.entries(s.model.chanceContext)) L.push(`| ${k} | ${ctx.join(", ") || "nothing"} | ${s.model.alphas[k]} |`);
|
||||
for (const X of Object.keys(s.targets)) {
|
||||
for (const [obj, title] of [["thisAttempt", "this attempt"], ["eventually", "eventually"]]) {
|
||||
const out = [];
|
||||
walk(s.targets[X][obj].tree, out);
|
||||
L.push("", `## ${X}: ${title}`, "", ...out);
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(path.join(root, "analysis/reports/POLICY.md"), L.join("\n") + "\n");
|
||||
console.log("wrote analysis/reports/POLICY.md", L.length, "lines");
|
||||
@@ -0,0 +1,93 @@
|
||||
"""Write analysis/reports/POLICY.md: a readable summary of data/public/policy.json.
|
||||
|
||||
python planner/policy_report.py
|
||||
"""
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def js_round(x):
|
||||
"""Math.round: halves round up (toward +infinity), unlike Python's round()."""
|
||||
import math
|
||||
return math.floor(x + 0.5)
|
||||
|
||||
|
||||
def to_fixed(x, digits):
|
||||
"""Number.prototype.toFixed: round half away from zero on the decimal value."""
|
||||
from decimal import Decimal, ROUND_HALF_UP
|
||||
return str(Decimal(repr(x)).quantize(Decimal(1).scaleb(-digits), rounding=ROUND_HALF_UP))
|
||||
|
||||
|
||||
def pct(p):
|
||||
return "–" if p is None else to_fixed(100 * p, 1 if p < 0.1 else 0) + "%"
|
||||
|
||||
|
||||
def conf(n):
|
||||
return "" if n.get("stillBest") is None else f" (still best in {pct(n['stillBest'])} of posterior draws)"
|
||||
|
||||
|
||||
def walk(n, out, indent=""):
|
||||
if not n:
|
||||
return
|
||||
if n["type"] == "decision":
|
||||
alts = ", ".join(f"{k} {pct(v['pX'])}" for k, v in sorted(n["options"].items(), key=lambda kv: -kv[1]["pX"]))
|
||||
if n["tie"]:
|
||||
out.append(f"{indent}- {n['label']}: any (no difference for this colour)")
|
||||
else:
|
||||
weak = " — no clear winner" if n.get("stillBest") is not None and n["stillBest"] < 0.75 else ""
|
||||
out.append(f"{indent}- **{n['label']}: {n['choice']}**{conf(n)}{weak}. If chosen, then best play after: {alts}")
|
||||
walk(n["next"], out, indent)
|
||||
elif n["type"] == "chance":
|
||||
br = sorted(n["branches"].items(), key=lambda kv: -kv[1]["p"])
|
||||
live = [(k, b) for k, b in br if k != "die"]
|
||||
die = next((b for k, b in br if k == "die"), None)
|
||||
if die is not None:
|
||||
out.append(f"{indent}- {n['label']}: {pct(1 - die['p'])} survive")
|
||||
for _, b in live:
|
||||
walk(b["next"], out, indent)
|
||||
return
|
||||
if len(live) == 1:
|
||||
return walk(live[0][1]["next"], out, indent)
|
||||
out.append(f"{indent}- {n['label']}: " + ", ".join(f"{k} {pct(b['p'])}" for k, b in br))
|
||||
for k, b in br:
|
||||
out.append(f"{indent} - if **{k}**:")
|
||||
walk(b["next"], out, indent + " ")
|
||||
elif n.get("outcomes"):
|
||||
items = sorted(n["outcomes"].items(), key=lambda kv: -kv[1])
|
||||
out.append(f"{indent}- Outcome if you attempt: " + ", ".join(f"{'incinerated' if k == 'burn' else 'bond ' + k} {pct(p)}" for k, p in items))
|
||||
else:
|
||||
out.append(f"{indent}- {n['what']}")
|
||||
|
||||
|
||||
def main():
|
||||
P = json.load(open(ROOT / "data/public/policy.json", encoding="utf-8"))
|
||||
s, lb = P["samples"]["all"], P["samples"]["lowBias"]
|
||||
L = ["# Best routes by dragon colour", ""]
|
||||
L += [f"Generated from {s['data']['runs']} community runs by `planner/build_policy.py` from the game in `core/spec/game.yaml`. Machine-readable version: `data/public/policy.json`.", ""]
|
||||
L += ["**How to read this.** \"This attempt\" maximises the chance of bonding the colour on one run. \"Eventually\" assumes you will die on purpose and try again whenever the run goes wrong, so it avoids any choice that can end the account on a different dragon. Percentages are posterior means with 95% credible intervals. \"Held-out\" is the same policy chosen on half of the runs and scored on the other half: when it is much lower than the in-sample number, the route's advantage is mostly noise.", ""]
|
||||
L += ["**Caveats.** The form records choices only at the level of company, direction, first dragon, cadet encounter, fork, tree or valley, and which dragon you met. Choices inside each encounter are not recorded, so the bond chance at an encounter is the community's average with whatever answers people used. Riders chasing a rare dragon report sightings more often than riders who log every run; the \"low-bias\" columns use only bulk logs and non-chasers.", ""]
|
||||
L += ["| Colour | This attempt | Low-bias sample | Held-out | Eventually: chance the account ends on this colour | Attempts needed (about) |",
|
||||
"|---|---|---|---|---|---|"]
|
||||
for X, t in s["targets"].items():
|
||||
a, e, l = t["thisAttempt"], t["eventually"], lb["targets"][X]["thisAttempt"]
|
||||
att = e["expectedAttempts"]["mean"]
|
||||
L.append(f"| {X} | {pct(a['pX']['mean'])} ({pct(a['pX']['lo'])}–{pct(a['pX']['hi'])}) | {pct(l['pX']['mean'])} ({pct(l['pX']['lo'])}–{pct(l['pX']['hi'])}) | {pct(t['crossFit']['heldOut'])} | {pct(e['ratio']['mean'])} | {'–' if att is None else js_round(att)} |")
|
||||
L += ["", "## How much do earlier choices matter?", ""]
|
||||
L += ["Each chance event can depend on the choices before it. The model learns how much from the data (empirical-Bayes concentration; larger means the history matters less):", ""]
|
||||
L += ["| Chance event | Depends on (most important first) | Concentration |", "|---|---|---|"]
|
||||
for k, ctx in s["model"]["chanceContext"].items():
|
||||
L.append(f"| {k} | {', '.join(ctx) or 'nothing'} | {s['model']['alphas'][k]} |")
|
||||
for X in s["targets"]:
|
||||
for obj, title in (("thisAttempt", "this attempt"), ("eventually", "eventually")):
|
||||
out = []
|
||||
walk(s["targets"][X][obj]["tree"], out)
|
||||
L += ["", f"## {X}: {title}", "", *out]
|
||||
path = ROOT / "analysis/reports/POLICY.md"
|
||||
path.write_text("\n".join(L) + "\n", encoding="utf-8")
|
||||
print(f"wrote {path.relative_to(ROOT)} {len(L)} lines")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,56 @@
|
||||
"""The game tree in core/spec/game.yaml must be complete and consistent."""
|
||||
import unittest
|
||||
|
||||
from atlas import spec
|
||||
|
||||
G = spec.game()
|
||||
|
||||
|
||||
class GameSpec(unittest.TestCase):
|
||||
def test_every_step_is_reachable_and_every_go_exists(self):
|
||||
seen, todo = set(), [G["start"]]
|
||||
while todo:
|
||||
s = todo.pop()
|
||||
if s in seen:
|
||||
continue
|
||||
seen.add(s)
|
||||
self.assertIn(s, G["steps"], f"go: {s} has no step")
|
||||
for t in G["steps"][s]["then"].values():
|
||||
if "go" in t:
|
||||
todo.append(t["go"])
|
||||
self.assertEqual(seen, set(G["steps"]), "steps nobody goes to")
|
||||
|
||||
def test_decisions_and_rolls_are_defined(self):
|
||||
for name, st in G["steps"].items():
|
||||
if "decision" in st:
|
||||
d = G["decisions"][st["decision"]]
|
||||
for opt in st["then"]:
|
||||
self.assertTrue(opt == "default" or opt in d["options"], f"{name}: unknown option {opt}")
|
||||
else:
|
||||
c = G["chance"][st["chance"]]
|
||||
for out in st["then"]:
|
||||
self.assertTrue(out == "default" or out in c["outcomes"], f"{name}: unknown outcome {out}")
|
||||
|
||||
def test_every_branch_says_what_happens(self):
|
||||
for name, st in G["steps"].items():
|
||||
for k, t in st["then"].items():
|
||||
self.assertEqual(sum(x in t for x in ("go", "end", "encounter")), 1, f"{name}/{k}")
|
||||
|
||||
def test_encounters_exist(self):
|
||||
for name, st in G["steps"].items():
|
||||
for t in st["then"].values():
|
||||
if "encounter" in t and "$" not in t["encounter"]:
|
||||
self.assertIn(t["encounter"], G["encounters"])
|
||||
for c, encs in G["by_colour"].items():
|
||||
for e in encs:
|
||||
self.assertIn(e, G["encounters"])
|
||||
|
||||
def test_chance_contexts_are_earlier_steps(self):
|
||||
known = {"company", "left", "direction", "fs", "react", "scream", "clearing", "fight", "dagger", "cadet", "post",
|
||||
"hour", "shadow", "descend", "red", "valley", "meadow"}
|
||||
for k, c in G["chance"].items():
|
||||
self.assertTrue(set(c["context"]) <= known, k)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,46 @@
|
||||
"""Compare two policy.json files structurally: same keys, same choices and labels, numbers equal within a tolerance.
|
||||
|
||||
python tests/parity/compare_policy.py A.json B.json [--tol 1e-9]
|
||||
"""
|
||||
import json
|
||||
import math
|
||||
import sys
|
||||
|
||||
|
||||
def walk(a, b, path, out, tol):
|
||||
if isinstance(a, dict) and isinstance(b, dict):
|
||||
for k in sorted(set(a) | set(b)):
|
||||
if k == "id" and path == "":
|
||||
continue
|
||||
if k not in a or k not in b:
|
||||
out["keys"].append(f"{path}/{k} only in {'B' if k not in a else 'A'}")
|
||||
continue
|
||||
walk(a[k], b[k], f"{path}/{k}", out, tol)
|
||||
elif isinstance(a, list) and isinstance(b, list):
|
||||
if len(a) != len(b):
|
||||
out["other"].append(f"{path}: length {len(a)} vs {len(b)}")
|
||||
for i, (x, y) in enumerate(zip(a, b)):
|
||||
walk(x, y, f"{path}[{i}]", out, tol)
|
||||
elif isinstance(a, (int, float)) and isinstance(b, (int, float)) and not isinstance(a, bool) and not isinstance(b, bool):
|
||||
d = abs(a - b) / max(1.0, abs(a), abs(b))
|
||||
out["max"] = max(out["max"], d)
|
||||
if d > tol:
|
||||
out["numbers"].append(f"{path}: {a} vs {b}")
|
||||
elif a != b:
|
||||
out["other"].append(f"{path}: {a!r} vs {b!r}")
|
||||
|
||||
|
||||
def compare(fa, fb, tol=1e-9):
|
||||
out = {"keys": [], "numbers": [], "other": [], "max": 0.0}
|
||||
walk(json.load(open(fa)), json.load(open(fb)), "", out, tol)
|
||||
return out
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
tol = float(sys.argv[sys.argv.index("--tol") + 1]) if "--tol" in sys.argv else 1e-9
|
||||
o = compare(sys.argv[1], sys.argv[2], tol)
|
||||
for k in ("keys", "other", "numbers"):
|
||||
if o[k]:
|
||||
print(f"{len(o[k])} {k} differ, e.g.:", *o[k][:8], sep="\n ")
|
||||
print(f"largest relative difference: {o['max']:.3g}")
|
||||
sys.exit(1 if o["keys"] or o["other"] or o["numbers"] else 0)
|
||||
@@ -0,0 +1,37 @@
|
||||
"""The Python route model (core/atlas/solver.py, built from core/spec/game.yaml) must give the same policy as the
|
||||
JavaScript one the site and the bot still use (core/solver.js): same choices everywhere, same numbers.
|
||||
|
||||
A quick version for CI: one colour, both samples' coding, 3 posterior draws.
|
||||
python tests/parity/compare_solver.py
|
||||
"""
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path.insert(0, str(ROOT / "core"))
|
||||
sys.path.insert(0, str(ROOT / "planner"))
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
import build_policy # noqa: E402
|
||||
from atlas import solver # noqa: E402
|
||||
from compare_policy import compare # noqa: E402
|
||||
|
||||
COLOURS, DRAWS = ["Blue"], 3
|
||||
runs = build_policy.coded_runs(ROOT / "data/public/form_responses.json")
|
||||
py = build_policy.clean(solver.build_policy(runs, draws=DRAWS, colours=COLOURS))
|
||||
js = json.loads(subprocess.check_output(["node", "-e", f"""
|
||||
const S=require('./core/solver.js'),E=require('./core/engine.js'),fs=require('fs');
|
||||
const raw=JSON.parse(fs.readFileSync('data/public/form_responses.json'));const {{runs}}=E.codeRows(raw.rows,raw.headers);
|
||||
console.log(JSON.stringify(S.buildPolicy(runs,{{draws:{DRAWS},colours:{json.dumps(COLOURS)}}})));"""], cwd=ROOT))
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
a, b = Path(d, "js.json"), Path(d, "py.json")
|
||||
a.write_text(json.dumps(js))
|
||||
b.write_text(json.dumps(py))
|
||||
o = compare(a, b, 1e-9)
|
||||
bad = o["keys"] + o["other"] + o["numbers"]
|
||||
print(f"route model ({', '.join(COLOURS)}, {DRAWS} draws): " + ("ALL MATCH" if not bad else f"{len(bad)} DIFFER"), f"(largest relative difference {o['max']:.2g})")
|
||||
for x in bad[:10]:
|
||||
print(" ", x)
|
||||
sys.exit(1 if bad else 0)
|
||||
Reference in New Issue
Block a user