- 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
40 lines
2.2 KiB
JavaScript
40 lines
2.2 KiB
JavaScript
// Build data/public/policy.json: the best decisions for each dragon colour, under both objectives.
|
|
// node scripts/build-policy.js [--draws 200]
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const E = require("../packages/core/src/engine.js");
|
|
const S = require("../packages/core/src/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)}`);
|
|
}
|
|
}
|