Files
threshing-atlas/scripts/policy-report.js
T
ekcSchianoandClaude Opus 5.5 d2ffb84b5e Solver: best decisions per dragon colour, and the bot advisor
- packages/core/src/solver.js: decision tree with perfect recall over the
  community form's choices; chance events (first dragon, deaths, shadow,
  meadow dragon, encounter outcome) depend on earlier choices through
  hierarchical Dirichlet back-off with empirical-Bayes concentration per
  event; backward induction for P(colour this attempt) and Dinkelbach for
  P(colour) / (P(colour) + P(other colour)); 200 posterior draws for
  credible intervals and "still best" rates; cross-fitted held-out check.
- Only genuine attempts inform encounter outcomes (goal / Malek intent).
- data/public/policy.json (all runs and a low-bias sample) and the readable
  docs/POLICY.md; published on Pages with solver.js for the bot.
- Bot advisor: re-solves from any history, maps advice onto screen text,
  reports margins so ties can be randomised. docs/BOT_INTEGRATION.md.
- Tests on a synthetic world with known answers; daily source update now
  rebuilds the policy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ
2026-11-01 22:34:38 +00:00

63 lines
4.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Write docs/POLICY.md: a readable summary of data/public/policy.json.
// node scripts/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 \`scripts/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, "docs/POLICY.md"), L.join("\n") + "\n");
console.log("wrote docs/POLICY.md", L.length, "lines");