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
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
46ee08a56b
commit
d2ffb84b5e
@@ -0,0 +1,647 @@
|
||||
/*
|
||||
* solver.js: best decisions for bonding each dragon colour.
|
||||
*
|
||||
* Model: the threshing as a decision tree with perfect recall (an influence diagram).
|
||||
* decision nodes company, direction, cadet encounter, fork in the road, tree or valley, how to climb down,
|
||||
* whether to face the valley Red, and at every bonding encounter: attempt the bond or die on purpose
|
||||
* chance nodes first dragon colour, dying at the sighting / in the bully fight / before the hour,
|
||||
* the shadow at the tree and its colour, which dragon waits in the meadow, the encounter outcome
|
||||
*
|
||||
* Chance probabilities depend on the history that came before them (not Markov). Each one is estimated with
|
||||
* hierarchical Dirichlet back-off: the estimate for a full history is shrunk toward the estimate for a shorter
|
||||
* history, which is shrunk toward the overall rate (concentration ALPHA). Uncertainty comes from Monte Carlo
|
||||
* draws of all those Dirichlet posteriors; each draw is solved again so we can say how often the recommended
|
||||
* choice is still the best one.
|
||||
*
|
||||
* Two objectives per target colour X, reported side by side:
|
||||
* 1. "this attempt": maximise P(bond X on this attempt).
|
||||
* 2. "eventually": deaths are free (you can always try again after a cooldown), so what matters is not to
|
||||
* end the account on the wrong dragon: maximise P(X) / (P(X) + P(another colour)).
|
||||
* Solved by Dinkelbach iteration; ties broken by P(X), and the expected number of attempts
|
||||
* (1 / P(X)) is reported with it.
|
||||
*
|
||||
* UMD: window.ThreshSolver in the browser, module.exports in Node. No dependencies.
|
||||
*/
|
||||
(function (root, factory) {
|
||||
if (typeof module === "object" && module.exports) module.exports = factory();
|
||||
else root.ThreshSolver = factory();
|
||||
})(typeof self !== "undefined" ? self : this, function () {
|
||||
"use strict";
|
||||
|
||||
const COLOURS = ["Red", "Orange", "Brown", "Green", "Blue", "Black"];
|
||||
const RARE_GOALS = ["Blue", "Black", "Blue or Black"];
|
||||
const ALPHA = 20;
|
||||
|
||||
/* ---------- 1. From coded form runs to solver variables ---------- */
|
||||
|
||||
const HELP = { "Killed the bullies yourself": 1, "Squadmate's revenge": 1, "Left bullies to the dragons": 1 };
|
||||
const IGNORE = { "Tourniquet": 1, "Left injured cadet": 1, "Took their weapons": 1 };
|
||||
const DECISIONS = {
|
||||
company: ["Alone", "1 Squadmate", "2 Squadmates"],
|
||||
direction: ["East", "West", "Center"],
|
||||
cadet: ["Killed the bullies yourself", "Squadmate's revenge", "Left bullies to the dragons",
|
||||
"Tourniquet", "Left injured cadet", "Took their weapons", "Left them to it"],
|
||||
post: ["Forest", "West", "East"],
|
||||
hour: ["Tree", "Valley"],
|
||||
descend: ["Quickly (Red)", "Slowly (two Greens)"],
|
||||
valley: ["Face the Red", "Pass the Red (meadow)"],
|
||||
encounter: ["Attempt the bond", "Die on purpose"],
|
||||
};
|
||||
const LABEL = {
|
||||
company: "Company", direction: "First direction", fs: "First dragon", dieSight: "Survive the first dragon",
|
||||
cadet: "Cadet encounter", dieCombat: "Survive the fight", post: "Fork in the road", dieHour: "Survive the hour",
|
||||
hour: "After an hour", shadow: "Shadow at the tree", shadowColour: "Shadow dragon", descend: "Climb down",
|
||||
valley: "Valley centre", meadow: "Meadow dragon", encounter: "Bonding encounter",
|
||||
};
|
||||
const encColour = (ctx) => (ctx === "tree-Greens" ? "Green" : ctx.split("-")[1]);
|
||||
|
||||
/** The colour (or colours) a rider's stated goal commits them to, for deciding whether an attempt was genuine. */
|
||||
function genuineAttempt(r, ctx) {
|
||||
const c = encColour(ctx);
|
||||
const g = r.goal || "";
|
||||
const matches = g === c || g.startsWith("Any") || (g === "Blue or Black" && (c === "Blue" || c === "Black")) ||
|
||||
(g === "Red (tree)" && ctx === "tree-Red");
|
||||
if (matches) return true;
|
||||
if (r.malek_intent === "No") return true;
|
||||
if (r.malek_intent === "Yes") return false;
|
||||
const specificOther = (COLOURS.includes(g) && g !== c) || (g === "Blue or Black") || g === "Red (tree)";
|
||||
return !specificOther;
|
||||
}
|
||||
|
||||
function prepareRun(r) {
|
||||
const v = { company: DECISIONS.company.includes(r.company) ? r.company : null,
|
||||
direction: DECISIONS.direction.includes(r.direction) ? r.direction : null,
|
||||
fs: ["Red", "Brown", "Green", "Orange"].includes(r.first_sighting) ? r.first_sighting : null };
|
||||
v.diedSight = r.scream === "DIED at first sighting";
|
||||
if (v.diedSight) return v;
|
||||
const res = r.resolution;
|
||||
if (r.scream === "Investigate & help") {
|
||||
v.help = true;
|
||||
v.diedCombat = res === "DIED in combat";
|
||||
v.cadet = HELP[res] ? res : null;
|
||||
if (v.diedCombat) return v;
|
||||
v.post = DECISIONS.post.includes(r.post_path) ? r.post_path : null;
|
||||
} else if (r.scream === "Ignore the scream") {
|
||||
v.cadet = IGNORE[res] ? res : null; v.post = "(no fork)";
|
||||
} else if (r.scream === "Investigate & leave") {
|
||||
v.cadet = "Left them to it"; v.post = "(no fork)";
|
||||
} else return v;
|
||||
v.survivedCadets = true;
|
||||
v.diedHour = r.hour === "DIED before the hour";
|
||||
if (v.diedHour) return v;
|
||||
v.hour = DECISIONS.hour.includes(r.hour) ? r.hour : null;
|
||||
const b = r.branch;
|
||||
if (v.hour === "Tree" && b) {
|
||||
v.shadow = b === "Shadow (rare)";
|
||||
if (v.shadow) { v.shadowColour = r.enc_Black ? "Black" : "Blue"; v.encCtx = "shadow-" + v.shadowColour; }
|
||||
else if (b === "Descend quickly: Red") { v.descend = "Quickly (Red)"; v.encCtx = "tree-Red"; }
|
||||
else if (b === "Descend slowly: two Greens") { v.descend = "Slowly (two Greens)"; v.encCtx = "tree-Greens"; }
|
||||
} else if (v.hour === "Valley" && b) {
|
||||
if (b === "Valley Red") { v.valley = "Face the Red"; v.encCtx = "valley-Red"; }
|
||||
else if (b === "Passed the Red: meadow") {
|
||||
v.valley = "Pass the Red (meadow)";
|
||||
// the meadow dragon: Blue if one appeared, otherwise the one the rider ended the run with
|
||||
const ended = (c) => r["enc_" + c] && r["enc_" + c] !== "Changed my mind";
|
||||
v.meadow = r.enc_Blue || r.rare_reported === "Blue (valley)" ? "Blue" :
|
||||
ended("Orange") ? "Orange" : ended("Brown") ? "Brown" : r.enc_Orange ? "Orange" : r.enc_Brown ? "Brown" : null;
|
||||
if (v.meadow) v.encCtx = "meadow-" + v.meadow;
|
||||
}
|
||||
}
|
||||
if (v.encCtx) {
|
||||
v.encOutcome = r.outcome === "Bonded" && r.outcome_color ? r.outcome_color : r.outcome === "Incinerated" ? "burn" : null;
|
||||
if (v.encOutcome && !genuineAttempt(r, v.encCtx)) v.encOutcome = null;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
/* ---------- 2. Chance nodes with hierarchical Dirichlet back-off ---------- */
|
||||
|
||||
// ctx: earlier variables the probability may depend on, most important first. Back-off drops from the end.
|
||||
const CHANCE = {
|
||||
fs: { cats: ["Red", "Brown", "Green", "Orange"], ctx: ["direction", "company"], get: (v) => v.fs },
|
||||
dieSight: { cats: ["die", "live"], ctx: ["fs", "direction", "company"], get: (v) => (v.fs ? (v.diedSight ? "die" : "live") : null) },
|
||||
dieCombat: { cats: ["die", "live"], ctx: ["fs", "direction"], get: (v) => (v.help ? (v.diedCombat ? "die" : "live") : null) },
|
||||
dieHour: { cats: ["die", "live"], ctx: ["cadet", "post"], get: (v) => (v.survivedCadets ? (v.diedHour ? "die" : "live") : null) },
|
||||
shadow: { cats: ["yes", "no"], ctx: ["cadet", "post", "fs", "direction", "company"], get: (v) => (v.shadow === undefined ? null : v.shadow ? "yes" : "no") },
|
||||
shadowColour: { cats: ["Black", "Blue"], ctx: [], get: (v) => v.shadowColour || null },
|
||||
meadow: { cats: ["Orange", "Brown", "Blue"], ctx: ["cadet", "post", "fs", "direction", "company"], get: (v) => v.meadow || null },
|
||||
};
|
||||
const ENC_CATS = ["burn"].concat(COLOURS);
|
||||
|
||||
const ctxKey = (v, vars) => vars.map((k) => (v[k] == null ? "?" : v[k])).join("|");
|
||||
|
||||
/** Counts for every chance node at every back-off level. */
|
||||
function countTables(prepared) {
|
||||
const T = {};
|
||||
for (const [node, spec] of Object.entries(CHANCE)) {
|
||||
const t = (T[node] = {});
|
||||
for (const v of prepared) {
|
||||
const y = spec.get(v);
|
||||
if (!y) continue;
|
||||
for (let L = 0; L <= spec.ctx.length; L++) {
|
||||
const vars = spec.ctx.slice(0, L);
|
||||
if (vars.some((k) => v[k] == null)) break;
|
||||
const key = L + ":" + ctxKey(v, vars);
|
||||
const c = t[key] || (t[key] = {});
|
||||
c[y] = (c[y] || 0) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
const enc = (T.encounter = {});
|
||||
for (const v of prepared) {
|
||||
if (!v.encCtx) continue;
|
||||
const c = enc[v.encCtx] || (enc[v.encCtx] = { outcomes: {}, seen: {} });
|
||||
if (v.encOutcome) c.outcomes[v.encOutcome] = (c.outcomes[v.encOutcome] || 0) + 1;
|
||||
}
|
||||
// which colours can ever end a run in this context (from all runs, genuine or not)
|
||||
for (const v of prepared) if (v.encCtx && v.encOutcome && v.encOutcome !== "burn") enc[v.encCtx].seen[v.encOutcome] = 1;
|
||||
return T;
|
||||
}
|
||||
|
||||
/* ---------- Empirical-Bayes concentration for each chance node ---------- */
|
||||
|
||||
function lgamma(x) {
|
||||
const c = [76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.1208650973866179e-2, -0.5395239384953e-5];
|
||||
let y = x, tmp = x + 5.5, ser = 1.000000000190015;
|
||||
tmp -= (x + 0.5) * Math.log(tmp);
|
||||
for (const v of c) ser += v / ++y;
|
||||
return -tmp + Math.log(2.5066282746310005 * ser / x);
|
||||
}
|
||||
const ALPHA_GRID = [1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 20000, 100000];
|
||||
|
||||
/**
|
||||
* How much should a specific history be allowed to differ from the shorter history above it?
|
||||
* Choose the concentration alpha that maximises the Dirichlet-multinomial marginal likelihood of the counts
|
||||
* at every back-off level given the level above. A large alpha means the data show little history dependence,
|
||||
* so the solver will not chase noise in small groups (the "optimiser's curse").
|
||||
*/
|
||||
function fitAlpha(T, node) {
|
||||
const spec = CHANCE[node], t = T[node], K = spec.cats.length;
|
||||
const pooled = (key) => { const c = t[key] || {}; const a = spec.cats.map((k) => (c[k] || 0) + 0.5); const n = a.reduce((x, y) => x + y, 0); return a.map((x) => x / n); };
|
||||
const pairs = [];
|
||||
for (const key of Object.keys(t)) {
|
||||
const L = +key.split(":")[0];
|
||||
if (L === 0) continue;
|
||||
const ctx = key.slice(key.indexOf(":") + 1).split("|");
|
||||
const parent = (L - 1) + ":" + ctx.slice(0, L - 1).join("|");
|
||||
pairs.push([spec.cats.map((k) => t[key][k] || 0), pooled(parent)]);
|
||||
}
|
||||
if (!pairs.length) return { alpha: ALPHA, logLik: null };
|
||||
let bestA = ALPHA, bestLL = -Infinity;
|
||||
const lls = {};
|
||||
for (const a of ALPHA_GRID) {
|
||||
let ll = 0;
|
||||
for (const [c, p] of pairs) {
|
||||
const n = c.reduce((x, y) => x + y, 0);
|
||||
ll += lgamma(a) - lgamma(n + a);
|
||||
for (let i = 0; i < K; i++) ll += lgamma(c[i] + a * p[i]) - lgamma(a * p[i]);
|
||||
}
|
||||
lls[a] = ll;
|
||||
if (ll > bestLL) { bestLL = ll; bestA = a; }
|
||||
}
|
||||
return { alpha: bestA, logLik: lls };
|
||||
}
|
||||
|
||||
/* random numbers: seeded, so results are reproducible */
|
||||
function rng(seed) {
|
||||
let s = seed >>> 0 || 1;
|
||||
return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296; };
|
||||
}
|
||||
function gamma(shape, rand) {
|
||||
if (shape < 1) return gamma(shape + 1, rand) * Math.pow(rand() || 1e-12, 1 / shape);
|
||||
const d = shape - 1 / 3, c = 1 / Math.sqrt(9 * d);
|
||||
for (;;) {
|
||||
let x, v;
|
||||
do {
|
||||
const u1 = rand() || 1e-12, u2 = rand();
|
||||
x = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
|
||||
v = 1 + c * x;
|
||||
} while (v <= 0);
|
||||
v = v * v * v;
|
||||
const u = rand();
|
||||
if (u < 1 - 0.0331 * x ** 4 || Math.log(u) < 0.5 * x * x + d * (1 - v + Math.log(v))) return d * v;
|
||||
}
|
||||
}
|
||||
function dirichlet(alpha, rand) {
|
||||
const g = alpha.map((a) => gamma(Math.max(a, 1e-6), rand));
|
||||
const s = g.reduce((a, b) => a + b, 0);
|
||||
return g.map((x) => x / s);
|
||||
}
|
||||
|
||||
/**
|
||||
* A probability source. mode "mean" gives posterior means; mode "draw" gives one joint posterior draw
|
||||
* (each distribution is drawn once and reused, so a draw is a consistent world).
|
||||
*/
|
||||
function makeParams(T, { mode = "mean", seed = 1, alpha = ALPHA, alphas = {} } = {}) {
|
||||
const rand = mode === "draw" ? rng(seed) : null;
|
||||
const memo = new Map();
|
||||
function chance(node, h) {
|
||||
const spec = CHANCE[node];
|
||||
// deepest level with this exact context, then build top-down
|
||||
const levels = [];
|
||||
for (let L = 0; L <= spec.ctx.length; L++) {
|
||||
const vars = spec.ctx.slice(0, L);
|
||||
if (vars.some((k) => h[k] == null)) break;
|
||||
levels.push(L + ":" + ctxKey(h, vars));
|
||||
}
|
||||
let p = null;
|
||||
for (const key of levels) {
|
||||
const mk = node + "#" + key;
|
||||
if (memo.has(mk)) { p = memo.get(mk); continue; }
|
||||
const c = T[node][key] || {};
|
||||
const al = alphas[node] || alpha;
|
||||
const a = spec.cats.map((k, i) => (c[k] || 0) + (p ? al * p[i] : 0.5));
|
||||
p = rand ? dirichlet(a, rand) : a.map((x) => x / a.reduce((s, y) => s + y, 0));
|
||||
memo.set(mk, p);
|
||||
}
|
||||
return Object.fromEntries(spec.cats.map((k, i) => [k, p[i]]));
|
||||
}
|
||||
function encounter(ctx) {
|
||||
const mk = "enc#" + ctx;
|
||||
if (!memo.has(mk)) {
|
||||
const e = T.encounter[ctx] || { outcomes: {}, seen: {} };
|
||||
const cats = ENC_CATS.filter((k) => k === "burn" || e.seen[k] || (e.outcomes[k] || 0) > 0 || k === encColour(ctx));
|
||||
const a = cats.map((k) => (e.outcomes[k] || 0) + 0.5);
|
||||
const p = rand ? dirichlet(a, rand) : a.map((x) => x / a.reduce((s, y) => s + y, 0));
|
||||
memo.set(mk, Object.fromEntries(cats.map((k, i) => [k, p[i]])));
|
||||
}
|
||||
return memo.get(mk);
|
||||
}
|
||||
return { chance, encounter };
|
||||
}
|
||||
|
||||
/* ---------- 3. The decision tree and backward induction ---------- */
|
||||
|
||||
const ZERO = { x: 0, o: 0 };
|
||||
const add = (a, b, w) => ({ x: a.x + w * b.x, o: a.o + w * b.o });
|
||||
|
||||
/**
|
||||
* Solve for target colour X. score(v) = (1 - lam) * P(X) - lam * P(other) (+ tiny tie-break on P(X)).
|
||||
* Returns the value at the root and a decide(name, h) function giving the best option at any history.
|
||||
* `support` lists the options allowed at each decision (options without enough data are left out).
|
||||
*/
|
||||
function solve(params, X, { lam = 0, support = DECISIONS, record = false } = {}) {
|
||||
const score = (v) => (1 - lam) * v.x - lam * v.o + 1e-9 * v.x - 1e-12 * v.o;
|
||||
const memo = new Map();
|
||||
const choices = new Map();
|
||||
const optVals = record ? new Map() : null;
|
||||
|
||||
function best(name, h, options, next) {
|
||||
const key = name + "@" + JSON.stringify(h);
|
||||
if (memo.has(key)) return memo.get(key);
|
||||
let top = null, arg = null;
|
||||
const vals = {};
|
||||
for (const o of options) {
|
||||
const v = next(o);
|
||||
vals[o] = v;
|
||||
if (!top || score(v) > score(top)) { top = v; arg = o; }
|
||||
}
|
||||
memo.set(key, top);
|
||||
choices.set(key, arg);
|
||||
if (optVals) optVals.set(key, vals);
|
||||
return top;
|
||||
}
|
||||
const mix = (dist, f) => Object.entries(dist).reduce((acc, [k, p]) => (p ? add(acc, f(k), p) : acc), ZERO);
|
||||
const die = () => ZERO;
|
||||
|
||||
const encounter = (h, ctx) => best("encounter", { ...h, enc: ctx }, support.encounter, (o) => {
|
||||
if (o === "Die on purpose") return ZERO;
|
||||
const d = params.encounter(ctx);
|
||||
let x = 0, oth = 0;
|
||||
for (const [k, p] of Object.entries(d)) if (k !== "burn") { if (k === X) x += p; else oth += p; }
|
||||
return { x, o: oth };
|
||||
});
|
||||
const tree = (h) => mix(params.chance("shadow", h), (s) => s === "yes"
|
||||
? mix(params.chance("shadowColour", h), (c) => encounter({ ...h, shadow: "yes" }, "shadow-" + c))
|
||||
: best("descend", { ...h, shadow: "no" }, support.descend, (o) => encounter({ ...h, shadow: "no", descend: o }, o.startsWith("Quickly") ? "tree-Red" : "tree-Greens")));
|
||||
const valley = (h) => best("valley", h, support.valley, (o) => o === "Face the Red"
|
||||
? encounter({ ...h, valley: o }, "valley-Red")
|
||||
: mix(params.chance("meadow", h), (c) => encounter({ ...h, valley: o, meadow: c }, "meadow-" + c)));
|
||||
const hour = (h) => mix(params.chance("dieHour", h), (s) => s === "die" ? die() :
|
||||
best("hour", h, support.hour, (o) => (o === "Tree" ? tree : valley)({ ...h, hour: o })));
|
||||
const cadet = (h) => best("cadet", h, support.cadet, (o) => {
|
||||
const h2 = { ...h, cadet: o };
|
||||
if (HELP[o]) return mix(params.chance("dieCombat", h2), (s) => s === "die" ? die() :
|
||||
best("post", h2, support.post, (p) => hour({ ...h2, post: p })));
|
||||
return hour({ ...h2, post: "(no fork)" });
|
||||
});
|
||||
const sighting = (h) => mix(params.chance("fs", h), (c) => {
|
||||
const h2 = { ...h, fs: c };
|
||||
return mix(params.chance("dieSight", h2), (s) => (s === "die" ? die() : cadet(h2)));
|
||||
});
|
||||
const root = best("company", {}, support.company, (c) =>
|
||||
best("direction", { company: c }, support.direction, (d) => sighting({ company: c, direction: d })));
|
||||
|
||||
const decide = (name, h) => choices.get(name + "@" + JSON.stringify(h));
|
||||
return { value: root, decide, optionValues: (name, h) => optVals && optVals.get(name + "@" + JSON.stringify(h)) };
|
||||
}
|
||||
|
||||
/** Objective 2 by Dinkelbach: find lam* = max P(X) / (P(X) + P(other)). */
|
||||
function solveRatio(params, X, opts = {}) {
|
||||
let lam = 0, s = solve(params, X, { ...opts, lam });
|
||||
for (let i = 0; i < 30; i++) {
|
||||
const v = s.value, r = v.x + v.o > 0 ? v.x / (v.x + v.o) : 0;
|
||||
if (Math.abs(r - lam) < 1e-10) break;
|
||||
lam = r;
|
||||
s = solve(params, X, { ...opts, lam });
|
||||
}
|
||||
return { ...s, lam };
|
||||
}
|
||||
|
||||
/* ---------- 4. Evaluate a fixed policy (for credible intervals) ---------- */
|
||||
|
||||
function evaluate(params, X, decide) {
|
||||
// same tree as solve(), but decisions come from `decide` (the policy chosen on the posterior means)
|
||||
const pick = (name, h, f) => f(decide(name, h));
|
||||
const mix = (dist, f) => Object.entries(dist).reduce((acc, [k, p]) => (p ? add(acc, f(k), p) : acc), ZERO);
|
||||
const encounter = (h, ctx) => pick("encounter", { ...h, enc: ctx }, (o) => {
|
||||
if (o === "Die on purpose") return ZERO;
|
||||
const d = params.encounter(ctx);
|
||||
let x = 0, oth = 0;
|
||||
for (const [k, p] of Object.entries(d)) if (k !== "burn") { if (k === X) x += p; else oth += p; }
|
||||
return { x, o: oth };
|
||||
});
|
||||
const tree = (h) => mix(params.chance("shadow", h), (s) => s === "yes"
|
||||
? mix(params.chance("shadowColour", h), (c) => encounter({ ...h, shadow: "yes" }, "shadow-" + c))
|
||||
: pick("descend", { ...h, shadow: "no" }, (o) => encounter({ ...h, shadow: "no", descend: o }, o.startsWith("Quickly") ? "tree-Red" : "tree-Greens")));
|
||||
const valley = (h) => pick("valley", h, (o) => o === "Face the Red"
|
||||
? encounter({ ...h, valley: o }, "valley-Red")
|
||||
: mix(params.chance("meadow", h), (c) => encounter({ ...h, valley: o, meadow: c }, "meadow-" + c)));
|
||||
const hour = (h) => mix(params.chance("dieHour", h), (s) => s === "die" ? ZERO :
|
||||
pick("hour", h, (o) => (o === "Tree" ? tree : valley)({ ...h, hour: o })));
|
||||
const cadet = (h) => pick("cadet", h, (o) => {
|
||||
const h2 = { ...h, cadet: o };
|
||||
if (HELP[o]) return mix(params.chance("dieCombat", h2), (s) => s === "die" ? ZERO :
|
||||
pick("post", h2, (p) => hour({ ...h2, post: p })));
|
||||
return hour({ ...h2, post: "(no fork)" });
|
||||
});
|
||||
const sighting = (h) => mix(params.chance("fs", h), (c) => {
|
||||
const h2 = { ...h, fs: c };
|
||||
return mix(params.chance("dieSight", h2), (s) => (s === "die" ? ZERO : cadet(h2)));
|
||||
});
|
||||
return pick("company", {}, (c) => pick("direction", { company: c }, (d) => sighting({ company: c, direction: d })));
|
||||
}
|
||||
|
||||
/* ---------- 5. The published policy tree ---------- */
|
||||
|
||||
/**
|
||||
* Walk the point-estimate policy and write it out as a tree: at decisions, the chosen option plus the value of
|
||||
* every option (each followed by the best play afterwards) and how often it stays best across posterior draws;
|
||||
* at chance nodes, every branch with its probability. Only the chosen option's subtree is expanded.
|
||||
*/
|
||||
function exportTree(params, X, sol, draws, minShow = 1e-4) {
|
||||
const vals = (name, h) => sol.optionValues(name, h) || {};
|
||||
const conf = (name, h, chosen) => (draws.length ? draws.filter((d) => d.decide(name, h) === chosen).length / draws.length : null);
|
||||
const fmt = (v) => ({ pX: v.x, pOther: v.o });
|
||||
const decision = (name, h, cont) => {
|
||||
const chosen = sol.decide(name, h);
|
||||
const ov = vals(name, h);
|
||||
const xs = Object.values(ov).map((v) => v.x).sort((p, q) => q - p);
|
||||
const tie = xs.length > 1 && xs[0] - xs[1] < 1e-12; // nothing to choose between (e.g. every option ends the run)
|
||||
return {
|
||||
type: "decision", var: name, label: LABEL[name], choice: chosen, tie, stillBest: tie ? null : conf(name, h, chosen),
|
||||
options: Object.fromEntries(Object.entries(ov).map(([o, v]) => [o, fmt(v)])),
|
||||
next: cont(chosen),
|
||||
};
|
||||
};
|
||||
const chance = (name, dist, cont) => ({
|
||||
type: "chance", var: name, label: LABEL[name],
|
||||
branches: Object.fromEntries(Object.entries(dist).filter(([, p]) => p >= minShow).map(([k, p]) => [k, { p, next: cont(k) }])),
|
||||
});
|
||||
const end = (what) => ({ type: "end", what });
|
||||
const encounter = (h, ctx) => decision("encounter", { ...h, enc: ctx }, (o) => o === "Die on purpose" ? end("die on purpose") :
|
||||
({ type: "end", what: `encounter: ${ctx}`, outcomes: params.encounter(ctx) }));
|
||||
const tree = (h) => chance("shadow", params.chance("shadow", h), (s) => s === "yes"
|
||||
? chance("shadowColour", params.chance("shadowColour", h), (c) => encounter({ ...h, shadow: "yes" }, "shadow-" + c))
|
||||
: decision("descend", { ...h, shadow: "no" }, (o) => encounter({ ...h, shadow: "no", descend: o }, o.startsWith("Quickly") ? "tree-Red" : "tree-Greens")));
|
||||
const valley = (h) => decision("valley", h, (o) => o === "Face the Red"
|
||||
? encounter({ ...h, valley: o }, "valley-Red")
|
||||
: chance("meadow", params.chance("meadow", h), (c) => encounter({ ...h, valley: o, meadow: c }, "meadow-" + c)));
|
||||
const hour = (h) => chance("dieHour", params.chance("dieHour", h), (s) => s === "die" ? end("incinerated") :
|
||||
decision("hour", h, (o) => (o === "Tree" ? tree : valley)({ ...h, hour: o })));
|
||||
const cadet = (h) => decision("cadet", h, (o) => {
|
||||
const h2 = { ...h, cadet: o };
|
||||
if (HELP[o]) return chance("dieCombat", params.chance("dieCombat", h2), (s) => s === "die" ? end("killed in the fight") :
|
||||
decision("post", h2, (p) => hour({ ...h2, post: p })));
|
||||
return hour({ ...h2, post: "(no fork)" });
|
||||
});
|
||||
const sighting = (h) => chance("fs", params.chance("fs", h), (c) => {
|
||||
const h2 = { ...h, fs: c };
|
||||
return chance("dieSight", params.chance("dieSight", h2), (s) => (s === "die" ? end("incinerated at the first sighting") : cadet(h2)));
|
||||
});
|
||||
return decision("company", {}, (c) => decision("direction", { company: c }, (d) => sighting({ company: c, direction: d })));
|
||||
}
|
||||
|
||||
const quantile = (xs, q) => {
|
||||
const s = xs.slice().sort((a, b) => a - b);
|
||||
const i = (s.length - 1) * q, lo = Math.floor(i), hi = Math.ceil(i);
|
||||
return s[lo] + (s[hi] - s[lo]) * (i - lo);
|
||||
};
|
||||
const summarise = (xs) => ({ mean: xs.reduce((a, b) => a + b, 0) / xs.length, lo: quantile(xs, 0.025), hi: quantile(xs, 0.975) });
|
||||
|
||||
/** Options seen fewer than `min` times in the data are not recommended. */
|
||||
function supportFrom(prepared, min) {
|
||||
const n = {};
|
||||
const bump = (k, o) => { n[k] = n[k] || {}; n[k][o] = (n[k][o] || 0) + 1; };
|
||||
for (const v of prepared) {
|
||||
for (const k of ["company", "direction", "cadet", "hour", "descend", "valley"]) if (v[k]) bump(k, v[k]);
|
||||
if (v.post && v.post !== "(no fork)") bump("post", v.post);
|
||||
}
|
||||
const s = { encounter: DECISIONS.encounter };
|
||||
for (const k of ["company", "direction", "cadet", "post", "hour", "descend", "valley"])
|
||||
s[k] = DECISIONS[k].filter((o) => ((n[k] || {})[o] || 0) >= min);
|
||||
return { support: s, counts: n };
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the full policy for every colour.
|
||||
* runs: coded runs from engine.codeRows(); opts.draws: Monte Carlo draws for intervals.
|
||||
*/
|
||||
function buildPolicy(runs, { draws = 200, seed = 20261101, minSupport = 20, alpha = null, colours = COLOURS } = {}) {
|
||||
const prepared = runs.map(prepareRun);
|
||||
const T = countTables(prepared);
|
||||
const { support, counts } = supportFrom(prepared, minSupport);
|
||||
const fitted = Object.fromEntries(Object.keys(CHANCE).map((k) => [k, fitAlpha(T, k)]));
|
||||
const alphas = alpha ? Object.fromEntries(Object.keys(CHANCE).map((k) => [k, alpha])) : Object.fromEntries(Object.entries(fitted).map(([k, f]) => [k, f.alpha]));
|
||||
const mean = makeParams(T, { mode: "mean", alphas });
|
||||
const drawParams = Array.from({ length: draws }, (_, i) => makeParams(T, { mode: "draw", seed: seed + i * 7919, alphas }));
|
||||
// Cross-fitting: choose the policy on one random half of the runs and score it on the other half.
|
||||
// The gap between the in-sample and held-out values shows how much of a route's edge is noise.
|
||||
const r = rng(seed);
|
||||
const fold = prepared.map(() => (r() < 0.5 ? 0 : 1));
|
||||
const halves = [0, 1].map((f) => countTables(prepared.filter((_, i) => fold[i] === f)));
|
||||
const halfParams = halves.map((Th) => makeParams(Th, { mode: "mean", alphas }));
|
||||
const targets = {};
|
||||
for (const X of colours) {
|
||||
const one = solve(mean, X, { support, record: true });
|
||||
const ratio = solveRatio(mean, X, { support });
|
||||
const ratioRec = solve(mean, X, { support, lam: ratio.lam, record: true });
|
||||
const dOne = drawParams.map((p) => solve(p, X, { support }));
|
||||
const dRatio = drawParams.map((p) => solveRatio(p, X, { support }));
|
||||
const evOne = drawParams.map((p) => evaluate(p, X, one.decide));
|
||||
const evRatio = drawParams.map((p) => evaluate(p, X, ratioRec.decide));
|
||||
const block = (sol, ev, dr) => ({
|
||||
pX: { ...summarise(ev.map((v) => v.x)), point: sol.value.x },
|
||||
pOther: { ...summarise(ev.map((v) => v.o)), point: sol.value.o },
|
||||
ratio: summarise(ev.map((v) => (v.x + v.o ? v.x / (v.x + v.o) : 0))),
|
||||
expectedAttempts: summarise(ev.map((v) => (v.x > 0 ? 1 / v.x : Infinity))),
|
||||
tree: exportTree(mean, X, sol, dr),
|
||||
});
|
||||
const cross = [0, 1].map((f) => {
|
||||
const pol = solve(halfParams[f], X, { support });
|
||||
return { chosenOn: f, inSample: pol.value.x, heldOut: evaluate(halfParams[1 - f], X, pol.decide).x };
|
||||
});
|
||||
targets[X] = { thisAttempt: block(one, evOne, dOne), eventually: block(ratioRec, evRatio, dRatio),
|
||||
crossFit: { inSample: (cross[0].inSample + cross[1].inSample) / 2, heldOut: (cross[0].heldOut + cross[1].heldOut) / 2 } };
|
||||
}
|
||||
return {
|
||||
schema: 1,
|
||||
model: {
|
||||
description: "Decision tree with perfect recall; chance probabilities depend on earlier choices via hierarchical Dirichlet back-off.",
|
||||
alphas, alphaFit: Object.fromEntries(Object.entries(fitted).map(([k, f]) => [k, f.logLik])), draws, seed, minSupport,
|
||||
chanceContext: Object.fromEntries(Object.entries(CHANCE).map(([k, s]) => [k, s.ctx])),
|
||||
decisions: DECISIONS, support, supportCounts: counts,
|
||||
tables: T,
|
||||
},
|
||||
data: { runs: runs.length },
|
||||
targets,
|
||||
};
|
||||
}
|
||||
|
||||
/* ---------- 6. Bot helper: what to pick on this screen ---------- */
|
||||
|
||||
const SCREAM_OF = (cadet) => (HELP[cadet] ? "help" : IGNORE[cadet] ? "ignore" : cadet === "Left them to it" ? "leave" : null);
|
||||
const OPTION_PATTERNS = {
|
||||
company: { "Alone": /^enter the valley alone/, "1 Squadmate": /^enter the valley with one/, "2 Squadmates": /^enter the valley with two/ },
|
||||
direction: { East: /^head east/, West: /^head west/, Center: /^head toward the center of the forest/ },
|
||||
post: { East: /^walk toward the east/, West: /^walk toward the west/, Forest: /^walk farther into the forest/ },
|
||||
hour: { Valley: /^continue toward the center of the valley/, Tree: /^find the tallest tree/ },
|
||||
descend: { "Quickly (Red)": /^descend quickly/, "Slowly (two Greens)": /^climb down slowly/ },
|
||||
resolution: {
|
||||
"Killed the bullies yourself": /^end the lives of the bullies/, "Squadmate's revenge": /^allow your squadmate to exact/,
|
||||
"Left bullies to the dragons": /^leave the bullies alive/, "Tourniquet": /tourniquet/, "Took their weapons": /weapon/,
|
||||
},
|
||||
scream: { help: /^(investigate the scream|help your squadmate)/, leave: /^(investigate the scream|play it safe and leave them)/, ignore: /^keep walking/ },
|
||||
};
|
||||
const norm = (t) => String(t || "").toLowerCase().replace(/[‘’']/g, "'").replace(/[–—-]/g, " ").replace(/[^a-z0-9'\s]/g, " ").replace(/\s+/g, " ").trim();
|
||||
|
||||
/**
|
||||
* Convert the community-form variables of a (possibly unfinished) run into the solver's history keys.
|
||||
* For the bot: graph.trajectoryVariables(trajectory).vars gives the form variables; add what the bot saw
|
||||
* on screen itself: `meadow` (colour in the meadow), `shadow` ("yes"/"no" at the treetop), `shadowColour`.
|
||||
*/
|
||||
function solverHistory(vars, seen = {}) {
|
||||
const h = {};
|
||||
if (DECISIONS.company.includes(vars.company)) h.company = vars.company;
|
||||
if (DECISIONS.direction.includes(vars.direction)) h.direction = vars.direction;
|
||||
if (["Red", "Brown", "Green", "Orange"].includes(vars.first_sighting)) h.fs = vars.first_sighting;
|
||||
if (DECISIONS.cadet.includes(vars.resolution)) h.cadet = vars.resolution;
|
||||
if (DECISIONS.post.includes(vars.post_path)) h.post = vars.post_path;
|
||||
else if (h.cadet && !HELP[h.cadet]) h.post = "(no fork)";
|
||||
if (DECISIONS.hour.includes(vars.hour)) h.hour = vars.hour;
|
||||
if (vars.branch === "Descend quickly: Red") h.descend = "Quickly (Red)";
|
||||
if (vars.branch === "Descend slowly: two Greens") h.descend = "Slowly (two Greens)";
|
||||
if (vars.branch === "Valley Red") h.valley = "Face the Red";
|
||||
if (vars.branch === "Passed the Red: meadow") h.valley = "Pass the Red (meadow)";
|
||||
for (const k of ["meadow", "shadow", "shadowColour"]) if (seen[k]) h[k] = seen[k];
|
||||
if (h.hour === "Tree" && h.descend && !h.shadow) h.shadow = "no";
|
||||
return h;
|
||||
}
|
||||
|
||||
/** Follow a published policy tree along a partial history; returns the node where the rider now stands. */
|
||||
function locate(tree, h) {
|
||||
let node = tree;
|
||||
while (node && node.type !== "end") {
|
||||
if (node.type === "decision") {
|
||||
const val = h[node.var];
|
||||
if (val == null) return node; // the next decision to make
|
||||
if (val !== node.choice) return { type: "off-policy", var: node.var, recommended: node.choice, actual: val };
|
||||
node = node.next;
|
||||
} else {
|
||||
// a rider who is asking for advice survived every earlier risk
|
||||
const val = node.var.startsWith("die") ? "live" : h[node.var];
|
||||
if (val == null) return node; // waiting for chance to resolve
|
||||
node = node.branches[val] ? node.branches[val].next : { type: "unknown-branch", var: node.var, value: val };
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* The next decision a rider faces after history h, as the (name, key) used inside solve().
|
||||
* Returns null when the next step is chance (e.g. the first dragon has not appeared yet) or the run is over.
|
||||
* Keys are built in the same order as in solve() so they match its memo keys.
|
||||
*/
|
||||
function nextDecision(h) {
|
||||
if (!h.company) return ["company", {}];
|
||||
if (!h.direction) return ["direction", { company: h.company }];
|
||||
if (!h.fs) return null;
|
||||
const a = { company: h.company, direction: h.direction, fs: h.fs };
|
||||
if (!h.cadet) return ["cadet", a];
|
||||
const b = { ...a, cadet: h.cadet };
|
||||
if (HELP[h.cadet] && !h.post) return ["post", b];
|
||||
const c = { ...b, post: HELP[h.cadet] ? h.post : "(no fork)" };
|
||||
if (!h.hour) return ["hour", c];
|
||||
const d = { ...c, hour: h.hour };
|
||||
if (h.hour === "Tree") {
|
||||
if (!h.shadow) return null;
|
||||
if (h.shadow === "yes") return h.shadowColour ? ["encounter", { ...d, shadow: "yes", enc: "shadow-" + h.shadowColour }] : null;
|
||||
if (!h.descend) return ["descend", { ...d, shadow: "no" }];
|
||||
return ["encounter", { ...d, shadow: "no", descend: h.descend, enc: h.descend.startsWith("Quickly") ? "tree-Red" : "tree-Greens" }];
|
||||
}
|
||||
if (!h.valley) return ["valley", d];
|
||||
if (h.valley === "Face the Red") return ["encounter", { ...d, valley: h.valley, enc: "valley-Red" }];
|
||||
return h.meadow ? ["encounter", { ...d, valley: h.valley, meadow: h.meadow, enc: "meadow-" + h.meadow }] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* An advisor for one colour and objective, built from a published policy sample (policy.json -> samples.all).
|
||||
* It re-solves the model locally (a few milliseconds), so it can advise from ANY history, including histories
|
||||
* that already left the recommended route.
|
||||
*/
|
||||
function advisor(sample, X, objective = "eventually") {
|
||||
const m = sample.model;
|
||||
const params = makeParams(m.tables, { mode: "mean", alphas: m.alphas });
|
||||
const sol = objective === "eventually"
|
||||
? (() => { const r = solveRatio(params, X, { support: m.support }); return solve(params, X, { support: m.support, lam: r.lam, record: true }); })()
|
||||
: solve(params, X, { support: m.support, record: true });
|
||||
return {
|
||||
colour: X, objective, value: sol.value,
|
||||
next(h) {
|
||||
const nd = nextDecision(h);
|
||||
if (!nd) return null;
|
||||
const [name, key] = nd;
|
||||
const ov = sol.optionValues(name, key) || {};
|
||||
return { decision: name, label: LABEL[name], choice: sol.decide(name, key),
|
||||
options: Object.fromEntries(Object.entries(ov).map(([o, v]) => [o, { pX: v.x, pOther: v.o }])) };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an advisor, the rider's history and the options on screen, return
|
||||
* { index, decision, choice, reason } where index is null when there is no advice for this screen
|
||||
* (the bot should then use its own rules). The encounter decision ("Attempt the bond" / "Die on purpose")
|
||||
* is returned without an index: the bot knows which of its options attempt the bond and which burn.
|
||||
*/
|
||||
function recommendOption(adv, h, offered) {
|
||||
const rec = adv.next(h);
|
||||
if (!rec) return null;
|
||||
const texts = offered.map(norm);
|
||||
const find = (re) => texts.findIndex((t) => re.test(t));
|
||||
let idx = -1;
|
||||
if (rec.decision === "cadet") {
|
||||
idx = find(OPTION_PATTERNS.resolution[rec.choice] || /$^/);
|
||||
const scream = SCREAM_OF(rec.choice);
|
||||
if (idx < 0 && scream) idx = find(OPTION_PATTERNS.scream[scream]);
|
||||
} else if (OPTION_PATTERNS[rec.decision]) {
|
||||
idx = find(OPTION_PATTERNS[rec.decision][rec.choice] || /$^/);
|
||||
}
|
||||
const values = Object.values(rec.options).map((v) => v.pX).sort((a, b) => b - a);
|
||||
const margin = values.length > 1 ? values[0] - values[1] : null;
|
||||
return { index: idx < 0 ? null : idx, decision: rec.decision, choice: rec.choice, margin, options: rec.options,
|
||||
reason: idx < 0 && rec.decision !== "encounter" ? "the recommended choice is not on this screen; use the bot's own rules" : `${rec.label}: ${rec.choice}` };
|
||||
}
|
||||
|
||||
return { COLOURS, DECISIONS, CHANCE, prepareRun, countTables, makeParams, fitAlpha, solverHistory, nextDecision, advisor, solve, solveRatio, evaluate, buildPolicy, locate, recommendOption, genuineAttempt };
|
||||
});
|
||||
Reference in New Issue
Block a user