Files
threshing-atlas/planner/build-policy.js
T
432e04c2c7 Refactor: lay the repo out by section (core, collection, analysis, planner, site) (#8)
Moves only; no behaviour changes. Every file now lives under the part of the project it
belongs to, each section has a README, and docs/PRIVACY.md collects the rules all parts follow.

  core/        engine.js, graph.js, solver.js, python/clean.py
  collection/  sources/ (form, register, PDF check, daily update), bot/ (importer)
  analysis/    stats/ (Python statistics), network/ (build-graph.js), reports/ (POLICY.md)
  planner/     build-policy.js, policy-report.js
  site/        atlas/ (the page), build.js, qa/
  tests/       core/ (unit), parity/ (JS vs Python), fixtures/

Checked against the previous layout: tests pass; coded runs, test outputs, graph.json and
policy.json are byte-identical; the built site differs only in comments that name file paths.
The published file names Ken's bot downloads (solver.js, graph.js, data/policy.json) are unchanged.

Also removes the undefined/ folder of QA screenshots committed by mistake (the QA scripts now
default to a temp folder) and test.txt, and drops the unused npm workspaces.


Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 19:46:33 +00:00

40 lines
2.2 KiB
JavaScript

// 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)}`);
}
}