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