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
+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()
|
||||
Reference in New Issue
Block a user