- game.yaml: `givens` (first or later attempt) and `distinct` levels that are not pooled; the shadow roll
depends on the attempt first. Form submissions show the shadow on 9% of first attempts and 0.3% of later ones.
- Reporting bias: a roll whose bias cannot be measured can borrow another roll's (`borrow: {shadow: meadow}`),
so form submissions inform the shadow again. An assumption, stated in planner/policy.yaml.
- policy.json: targets.<colour>.firstAttempt (this attempt, eventually). Black: about 1.4% on a first attempt,
0.07% on a later one.
- solver.js: advisor options `attempt`, `fix`, sampling "uniform"; assign() and armFor() for the plan.
- planner/experiments.yaml + build_experiments.py -> data/public/experiments.json (progress and status per
experiment, rebuilt from the bot's runs).
- python -m collection cycle: import bot runs, refit, re-plan, deliver the kit; .gitea/workflows/bot-cycle.yml.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014jqCC2LZ4x1WHQ1V57nTsv
399 lines
24 KiB
JavaScript
399 lines
24 KiB
JavaScript
/*
|
|
* solver.js: the route advisor for the site and the DragonKind bot.
|
|
*
|
|
* The route model is built in Python (core/atlas/solver.py) from the game in core/spec/game.yaml and published as
|
|
* data/policy.json. Each sample in it carries the game itself (model.game) and the count tables behind every roll
|
|
* (model.tables, model.alphas). This file reads both and re-solves the game from any point in a run, including
|
|
* points off the recommended route, so a rider (or the bot) always gets the best next choice. It is the same
|
|
* interpreter as the Python one, kept small: a walk of the game's steps, posterior-mean odds with hierarchical
|
|
* back-off, backward induction, and Dinkelbach iteration for the "eventually" goal.
|
|
* tests/core/advisor.test.js checks it gives exactly the Python model's values.
|
|
*
|
|
* Public API (unchanged for the bot): advisor(sample, colour, objective, { only }), recommendOption(adv, h, texts),
|
|
* solverHistory(formVars, seen), makeParams(tables, { alphas, game }), ENCOUNTERS (set from the first sample).
|
|
* Since November 2026: advisor options `attempt` (the account's attempt number: a first attempt has its own
|
|
* shadow odds), `fix` (hold some decisions to given options: an experiment arm) and sampling "uniform" (mapping);
|
|
* assign(plan, { accountSeq, attempt }) reads the experiment plan (experiments.json) and says what an attempt is for.
|
|
* 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 ALPHA = 20;
|
|
const ZERO = { x: 0, o: 0, r: 0 }, REACHED = { x: 0, o: 0, r: 1 };
|
|
const canon = (s) => JSON.stringify(Object.keys(s).sort().map((k) => [k, s[k]]));
|
|
const encColour = (ctx) => (ctx === "tree-Greens" ? "Green" : ctx.split("-")[1]);
|
|
const api = {};
|
|
|
|
/* ---------- posterior draws, for Thompson sampling ---------- */
|
|
function rng(seed) {
|
|
let x = (seed >>> 0) || 0x9e3779b9;
|
|
return () => { x ^= x << 13; x >>>= 0; x ^= x >>> 17; x ^= x << 5; x >>>= 0; return (x + 0.5) / 4294967296; };
|
|
}
|
|
function normal(rand) { return Math.sqrt(-2 * Math.log(rand())) * Math.cos(2 * Math.PI * rand()); }
|
|
function gamma(k, rand) { // Marsaglia and Tsang
|
|
if (k < 1) return gamma(k + 1, rand) * Math.pow(rand(), 1 / k);
|
|
const d = k - 1 / 3, c = 1 / Math.sqrt(9 * d);
|
|
for (;;) {
|
|
let x, v;
|
|
do { x = normal(rand); 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(a, rand) {
|
|
const g = a.map((x) => Math.max(gamma(x, rand), 1e-300));
|
|
let s = 0; for (const x of g) s += x;
|
|
return g.map((x) => x / s);
|
|
}
|
|
|
|
/* ---------- the odds: posterior means (or, with rand, one posterior draw) with hierarchical back-off ---------- */
|
|
/** The givens of a run (game.yaml `givens`): the defaults, with `attempt` set from an attempt number or name. */
|
|
function givenFor(game, { attempt = null, given = null } = {}) {
|
|
const g = Object.fromEntries(Object.entries(game.givens || {}).map(([k, v]) => [k, v.default]));
|
|
if (attempt != null && game.givens && game.givens.attempt) g.attempt = attempt === 1 || attempt === "1" || attempt === "First attempt" ? "First attempt" : "Later attempt";
|
|
return { ...g, ...(given || {}) };
|
|
}
|
|
// given: the run's givens; memo: pass the same Map to get the same posterior draw for another given
|
|
function makeParams(T, { alphas = {}, game, rand = null, given = null, memo = new Map() }) {
|
|
const normalise = (a) => { let sum = 0; for (const x of a) sum += x; return a.map((x) => x / sum); };
|
|
const estimate = rand ? (a) => dirichlet(a, rand) : normalise;
|
|
const known = given || givenFor(game);
|
|
const ctxKey = (s, names) => names.map((k) => (s[k] == null ? "?" : s[k])).join("|");
|
|
function chance(node, walked) {
|
|
const sp = game.chance[node], cats = sp.outcomes;
|
|
// a closed gate (game.yaml `requires`): the roll always comes out as `otherwise`
|
|
if (sp.requires && Object.entries(sp.requires).some(([k, v]) => walked[k] !== v)) return Object.fromEntries(cats.map((k) => [k, k === sp.otherwise ? 1 : 0]));
|
|
const state = { ...known, ...walked };
|
|
let p = null;
|
|
for (let L = 0; L <= sp.context.length; L++) {
|
|
const names = sp.context.slice(0, L);
|
|
if (names.some((k) => state[k] == null)) break;
|
|
const key = L + ":" + ctxKey(state, names), mk = node + "#" + key;
|
|
if (memo.has(mk)) { p = memo.get(mk); continue; }
|
|
// a level that is not pooled (game.yaml `distinct`) keeps a token prior of one run
|
|
const c = T[node][key] || {}, al = L <= (sp.distinct || 0) ? 1 : alphas[node] || ALPHA;
|
|
const a = cats.map((k, i) => (c[k] || 0) + (p ? al * p[i] : 0.5));
|
|
p = estimate(a);
|
|
memo.set(mk, p);
|
|
}
|
|
return Object.fromEntries(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 = game.encounter_outcomes.filter((k) => k === "burn" || e.seen[k] || (e.outcomes[k] || 0) > 0 || k === encColour(ctx));
|
|
const a = estimate(cats.map((k) => (e.outcomes[k] || 0) + 0.5));
|
|
memo.set(mk, Object.fromEntries(cats.map((k, i) => [k, a[i]])));
|
|
}
|
|
return memo.get(mk);
|
|
}
|
|
return { chance, encounter };
|
|
}
|
|
|
|
/* ---------- one walk of the game, used to solve (as Walker in core/atlas/solver.py) ---------- */
|
|
function apply(then, state, value) {
|
|
const s = then.keep ? Object.fromEntries(then.keep.filter((k) => k in state).map((k) => [k, state[k]])) : { ...state };
|
|
for (const k of then.drop || []) delete s[k];
|
|
for (const [k, v] of Object.entries(then.set || {})) s[k] = v === "$" ? value : v;
|
|
return s;
|
|
}
|
|
function walker(game, support, only, hooks) {
|
|
const options = (name, state) => {
|
|
const by = game.decisions[name].support_by;
|
|
return by ? support[`${name}:${state[by]}`] || game.decisions[name].options : support[name];
|
|
};
|
|
function follow(then, value, state) {
|
|
const t = then[value] || then.default;
|
|
if (t.end) return hooks.end(t.end);
|
|
const s = apply(t, state, value);
|
|
if (t.encounter) {
|
|
const ctx = t.encounter.replace("$", value);
|
|
const opts = only && !only.includes(ctx) ? ["Die on purpose"] : support.encounter;
|
|
return hooks.decision("encounter", { ...s, enc: ctx }, opts, (o) => (o === "Die on purpose" ? hooks.dieOnPurpose() : hooks.attempt(ctx)));
|
|
}
|
|
return run(t.go, s);
|
|
}
|
|
function run(id, state) {
|
|
const st = game.steps[id];
|
|
if (st.decision) return hooks.decision(st.decision, state, options(st.decision, state), (o) => follow(st.then, o, state));
|
|
return hooks.chance(st.chance, state, (k) => follow(st.then, k, state));
|
|
}
|
|
return { root: () => run(game.start, {}) };
|
|
}
|
|
|
|
/** Backward induction for colour X: score = (1 - lam) P(X) - lam P(other), ties to the higher P(X). */
|
|
function solve(params, game, support, X, lam = 0, only = null) {
|
|
const memo = new Map(), choices = new Map(), optVals = new Map();
|
|
const score = (v) => (1 - lam) * v.x - lam * v.o + 1e-9 * v.x - 1e-12 * v.o;
|
|
const w = walker(game, support, only, {
|
|
decision(name, state, opts, cont) {
|
|
const key = name + "@" + canon(state);
|
|
if (memo.has(key)) return memo.get(key);
|
|
let top = null, arg = null;
|
|
const vals = {};
|
|
for (const o of opts) {
|
|
const v = cont(o);
|
|
vals[o] = v;
|
|
if (!top || score(v) > score(top)) { top = v; arg = o; }
|
|
}
|
|
memo.set(key, top); choices.set(key, arg); optVals.set(key, vals);
|
|
return top;
|
|
},
|
|
chance(node, state, cont) {
|
|
let acc = ZERO;
|
|
for (const [k, p] of Object.entries(params.chance(node, state))) {
|
|
if (!p) continue;
|
|
const v = cont(k);
|
|
acc = { x: acc.x + p * v.x, o: acc.o + p * v.o, r: acc.r + p * v.r };
|
|
}
|
|
return acc;
|
|
},
|
|
end: () => ZERO,
|
|
dieOnPurpose: () => REACHED,
|
|
attempt(ctx) {
|
|
let x = 0, o = 0;
|
|
for (const [k, p] of Object.entries(params.encounter(ctx))) if (k !== "burn") { if (k === X) x += p; else o += p; }
|
|
return { x, o, r: 1 };
|
|
},
|
|
});
|
|
return { value: w.root(), choices, optVals };
|
|
}
|
|
function solveRatio(params, game, support, X, only) {
|
|
let lam = 0, s = solve(params, game, support, X, 0, only);
|
|
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, game, support, X, lam, only);
|
|
}
|
|
return { ...s, lam };
|
|
}
|
|
|
|
/* ---------- the advisor ---------- */
|
|
/**
|
|
* advisor(sample, X, objective, { only }): sample is policy.samples.all (or .lowBias); X a colour; objective
|
|
* "eventually" (default) or "thisAttempt"; only: bond only at these encounters, die on purpose elsewhere.
|
|
* Returns { value, step(h), next(h), attempts(ctx), params, game }.
|
|
*
|
|
* h is a run so far, keyed by the game's step names: decisions (company, leave, direction, react, scream,
|
|
* clearing, fight, dagger, fate, wounded, post, hour, descend, red, valley, encounter) and rolls (fs, shadow,
|
|
* shadowColour, meadow). A rider asking for advice survived every deadly roll so far; step() reports the risk of
|
|
* the deadly roll just passed on the step after it. When h already has a later step but skips an earlier
|
|
* decision (an older form record, say), the recommended option is assumed for the skipped one.
|
|
*/
|
|
function advisor(sample, X, objective = "eventually", { only = null, sampling = "best", seed = 1, draws = 40, attempt = null, given = null, fix = null } = {}) {
|
|
const m = sample.model, game = m.game;
|
|
api.game = game; api.ENCOUNTERS = game.by_colour;
|
|
// attempt: the account's attempt number (or "First attempt" / "Later attempt"). A first attempt has its own odds
|
|
// for the rolls that depend on it (the shadow); without it, a later attempt is assumed.
|
|
const defaults = givenFor(game), known = givenFor(game, { attempt, given });
|
|
const special = Object.keys(known).some((k) => known[k] !== defaults[k]);
|
|
// "uniform" (mapping) may take any option the game has, so it is solved without the minimum-support rule
|
|
const all = Object.fromEntries(Object.entries(game.decisions).map(([k, d]) => [k, d.options]));
|
|
const base = sampling === "uniform" ? { ...all, encounter: m.support.encounter } : m.support;
|
|
// fix: { decision: option } holds those decisions to one option (an experiment arm); the rest is solved around it
|
|
let support = base;
|
|
if (fix) {
|
|
support = { ...base };
|
|
for (const [k, o] of Object.entries(fix)) {
|
|
if (!game.decisions[k] || !game.decisions[k].options.includes(o)) throw new Error(`fix: ${k} has no option "${o}"`);
|
|
support[k] = [o];
|
|
if (game.decisions[k].support_by) for (const key of Object.keys(support)) if (key.startsWith(k + ":")) support[key] = [o];
|
|
}
|
|
}
|
|
// One world (the posterior means, or one draw): the odds for this run's givens, solved for the objective.
|
|
// "Eventually" on a first attempt: every later attempt is an ordinary one, worth its own ratio (lam), so the
|
|
// best play now maximises P(X now) + P(no bond now) * lam: the same score with lam fixed.
|
|
const solveWith = (rand) => {
|
|
const memo = new Map();
|
|
const mk = (g) => makeParams(m.tables, { alphas: m.alphas, game, rand, given: g, memo });
|
|
if (objective !== "eventually") { const params = mk(known); return { params, sol: solve(params, game, support, X, 0, only) }; }
|
|
if (!special) { const params = mk(known); return { params, sol: solveRatio(params, game, support, X, only) }; }
|
|
const lam = solveRatio(mk(defaults), game, base, X, only).lam;
|
|
const params = mk(known);
|
|
return { params, sol: { ...solve(params, game, support, X, lam, only), lam } };
|
|
};
|
|
// "best": the best route under the posterior means. "thompson": the best route of one posterior draw of the odds
|
|
// (one per attempt: make a new advisor with a new seed for each attempt), so every option is tried about as
|
|
// often as it is likely to be the best one; as the data firm up, sampling settles on the best route.
|
|
// "uniform": every option on the screen is equally likely (recommendOption picks; see there).
|
|
const thompson = sampling === "thompson";
|
|
const { params, sol } = solveWith(thompson ? rng(seed) : null);
|
|
// the chance Thompson sampling picks each option at a history, estimated from `draws` other draws: the
|
|
// propensity to log, so the runs can be re-weighted later
|
|
let worlds = null;
|
|
const propensityAt = (key, choice) => {
|
|
if (!thompson) return 1;
|
|
if (!worlds) worlds = Array.from({ length: draws }, (_, i) => solveWith(rng(seed * 7919 + i + 1)).sol.choices);
|
|
return worlds.filter((c) => c.get(key) === choice).length / draws;
|
|
};
|
|
const VARS = new Set([...Object.keys(game.decisions), ...Object.keys(game.chance), "encounter"]);
|
|
const fmt = (v) => ({ pX: v.x, pOther: v.o, pReach: v.r });
|
|
const label = (k) => (game.decisions[k] || game.chance[k]).label;
|
|
const decisionAt = (name, key, extra) => {
|
|
const vals = sol.optVals.get(key) || {};
|
|
const choice = sol.choices.get(key);
|
|
return { kind: "decision", var: name, label: label(name), choice, propensity: propensityAt(key, choice),
|
|
options: Object.fromEntries(Object.entries(vals).map(([o, v]) => [o, fmt(v)])), ...extra };
|
|
};
|
|
|
|
/** The next thing that happens after history h: a decision, a roll, or the end of the run. */
|
|
function step(h) {
|
|
const used = new Set();
|
|
const later = () => Object.keys(h).some((k) => VARS.has(k) && h[k] != null && !used.has(k));
|
|
let id = game.start, state = {}, risk = null;
|
|
for (let guard = 0; guard < 100; guard++) {
|
|
const st = game.steps[id];
|
|
let value;
|
|
if (st.decision) {
|
|
const name = st.decision, key = name + "@" + canon(state);
|
|
if (h[name] != null) { value = h[name]; used.add(name); }
|
|
else if (later()) value = sol.choices.get(key);
|
|
else return decisionAt(name, key, risk ? { risk } : {});
|
|
risk = null;
|
|
} else {
|
|
const node = st.chance, probs = params.chance(node, state), sp = game.chance[node];
|
|
if (sp.risk_label) { risk = { label: sp.risk_label, p: probs.die }; value = "live"; }
|
|
else if (h[node] != null) { value = h[node]; used.add(node); risk = null; }
|
|
else return { kind: "chance", var: node, label: label(node), probs, ...(risk ? { risk } : {}) };
|
|
}
|
|
const t = st.then[value] || st.then.default;
|
|
if (!t) return { kind: "end", invalid: `${st.decision || st.chance} = ${value}` };
|
|
if (t.end) return { kind: "end", what: t.end };
|
|
const s = apply(t, state, value);
|
|
if (t.encounter) {
|
|
const ctx = t.encounter.replace("$", value);
|
|
if (h.encounter == null) return decisionAt("encounter", "encounter@" + canon({ ...s, enc: ctx }), { encounter: ctx, ...(risk ? { risk } : {}) });
|
|
return { kind: "end", encounter: ctx, action: h.encounter, outcomes: h.encounter === "Die on purpose" ? { burn: 1 } : params.encounter(ctx) };
|
|
}
|
|
id = t.go; state = s;
|
|
}
|
|
throw new Error("the game has no end");
|
|
}
|
|
return {
|
|
colour: X, objective, only, sampling, seed: sampling === "best" ? null : seed, given: known, fix, value: sol.value, step, params, game,
|
|
/** attempts recorded at an encounter (genuine attempts only), to show how much data a bond chance rests on */
|
|
attempts: (ctx) => Object.values(((m.tables.encounter || {})[ctx] || { outcomes: {} }).outcomes).reduce((a, b) => a + b, 0),
|
|
/** the next decision only (null when the next step is a roll or the run is over) */
|
|
next(h) {
|
|
const s = step(h);
|
|
return s.kind === "decision" ? { decision: s.var, label: s.label, choice: s.choice, propensity: s.propensity, options: s.options } : null;
|
|
},
|
|
};
|
|
}
|
|
|
|
/* ---------- the bot ---------- */
|
|
const norm = (t) => String(t || "").toLowerCase().replace(/[‘’‛`']/g, "'").replace(/[–—-]/g, " ").replace(/[^a-z0-9'\s]/g, " ").replace(/\s+/g, " ").trim();
|
|
/**
|
|
* Which of the options on the bot's screen to pick: { index, decision, choice, margin, options, reason }.
|
|
* index is null when there is no advice for this screen (use the bot's own rules). The encounter decision
|
|
* ("Attempt the bond" / "Die on purpose") is returned without an index: the bot knows which option is which.
|
|
* The on-screen text of each option is in game.yaml (bot_text).
|
|
*/
|
|
function recommendOption(adv, h, offered) {
|
|
const rec = adv.next(h);
|
|
if (!rec) return null;
|
|
const texts = offered.map(norm), pats = (adv.game.bot_text || {})[rec.decision] || {};
|
|
// mapping (sampling "uniform"): every option on the screen that the game knows is equally likely, except a
|
|
// decision held by `fix` and the bonding encounter (where the advisor's own choice stands)
|
|
if (adv.sampling === "uniform" && rec.decision !== "encounter" && !(adv.fix && adv.fix[rec.decision])) {
|
|
const on = Object.keys(rec.options).map((o) => [o, pats[o] ? texts.findIndex((t) => new RegExp(pats[o]).test(t)) : -1]).filter(([, i]) => i >= 0);
|
|
if (on.length) {
|
|
const [o, i] = on[Math.floor(rng(hash(`${adv.seed}|${rec.decision}|${canon(h)}`))() * on.length)];
|
|
return { index: i, decision: rec.decision, choice: o, propensity: 1 / on.length, margin: null, options: rec.options, reason: `${rec.label}: ${o} (mapping: 1 of ${on.length} at random)` };
|
|
}
|
|
}
|
|
const pat = pats[rec.choice];
|
|
const idx = pat ? texts.findIndex((t) => new RegExp(pat).test(t)) : -1;
|
|
const values = Object.values(rec.options).map((v) => v.pX).sort((a, b) => b - a);
|
|
return { index: idx < 0 ? null : idx, decision: rec.decision, choice: rec.choice, propensity: rec.propensity,
|
|
margin: values.length > 1 ? values[0] - values[1] : null,
|
|
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}` };
|
|
}
|
|
|
|
/**
|
|
* The community form's variables for a run so far (graph.trajectoryVariables(t).vars, or a coded form run) as a
|
|
* history for advisor().step(), keyed by the game's step names. seen: what the bot saw on screen that the form does
|
|
* not ask: { shadow: "yes"|"no", shadowColour, meadow }. Uses the game of the last advisor made (or pass one).
|
|
*/
|
|
function solverHistory(vars, seen = {}, game = api.game) {
|
|
const D = game.decisions, F = game.from_form, h = {};
|
|
const has = (k, v) => D[k].options.includes(v);
|
|
if (has("company", vars.company)) h.company = vars.company;
|
|
const lf = vars.left_squadmates;
|
|
if (h.company && h.company !== "Alone" && lf != null && lf !== "") h.leave = String(lf).toLowerCase() === "true" ? "Leave them" : "Stay together";
|
|
if (has("direction", vars.direction)) h.direction = vars.direction;
|
|
if (["Red", "Brown", "Green", "Orange"].includes(vars.first_sighting)) h.fs = vars.first_sighting;
|
|
if (has("react", vars.sighting_action)) h.react = vars.sighting_action;
|
|
if (vars.scream === "Ignore the scream") h.scream = "Keep walking";
|
|
else if (vars.scream === "Investigate & help" || vars.scream === "Investigate & leave" || vars.scream_investigate) h.scream = "Investigate the scream";
|
|
if (vars.scream === "Investigate & help") h.clearing = "Help your squadmate";
|
|
if (vars.scream === "Investigate & leave") h.clearing = "Walk away";
|
|
if (F.fight[vars.combat_first]) h.fight = F.fight[vars.combat_first];
|
|
if (F.dagger[vars.combat_second]) h.dagger = F.dagger[vars.combat_second];
|
|
if (F.helped.includes(vars.resolution)) h.fate = vars.resolution;
|
|
if (F.ignored.includes(vars.resolution)) h.wounded = vars.resolution;
|
|
if (has("post", vars.post_path)) h.post = vars.post_path;
|
|
if (has("hour", vars.hour)) h.hour = vars.hour;
|
|
if (vars.branch === "Descend quickly: Red" || vars.branch === "Descend quickly: let the Red pass") h.descend = "Quickly (Red)";
|
|
if (vars.branch === "Descend quickly: let the Red pass") h.red = "Let it pass (two Greens)";
|
|
if (has("red", vars.red)) h.red = vars.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;
|
|
}
|
|
|
|
/* ---------- the experiment plan (planner/experiments.yaml, published as experiments.json) ---------- */
|
|
function hash(s) { let x = 2166136261; for (const ch of String(s)) { x ^= ch.codePointAt(0); x = Math.imul(x, 16777619) >>> 0; } return x || 1; }
|
|
/** The arm of the n-th unit (0-based) of an experiment: balanced blocks, each block a seeded shuffle of the arms. */
|
|
function armFor(experiment, n) {
|
|
const arms = experiment.arms, k = arms.length, rand = rng(hash(`${experiment.id}|${experiment.seed || 0}|${Math.floor(n / k)}`));
|
|
const order = arms.map((_, i) => i);
|
|
for (let i = k - 1; i > 0; i--) { const j = Math.floor(rand() * (i + 1)); [order[i], order[j]] = [order[j], order[i]]; }
|
|
return arms[order[n % k]];
|
|
}
|
|
/**
|
|
* What an attempt is for, under the plan: assign(plan, { accountSeq, attempt }) ->
|
|
* { experimentId, arm, fix, target, objective, sampling, seed, propensity, burn, planId }, or null when no
|
|
* experiment covers the attempt (use the account's own goal).
|
|
* accountSeq: the account's number in the order the accounts were made (0, 1, 2, ...), the same for its whole
|
|
* life; attempt: the account's attempt number (1 = its first threshing). The answer is the same every time for
|
|
* the same plan id, so a restart mid-attempt changes nothing. Pass the result to advisor():
|
|
* advisor(policy.samples.all, a.target, a.objective, { attempt, fix: a.fix, sampling: a.sampling, seed: a.seed })
|
|
* and log experimentId, arm, planId and the propensity on the run.
|
|
*/
|
|
function assign(plan, { accountSeq, attempt }) {
|
|
let open = (plan.experiments || []).filter((e) => e.status === "running" && attempt >= e.attempts.from && (e.attempts.to == null || attempt <= e.attempts.to)
|
|
&& (!e.accounts || (accountSeq % e.accounts.every) < e.accounts.take));
|
|
if (!open.length) return null;
|
|
if (open.some((e) => e.exclusive)) open = open.filter((e) => e.exclusive); // an exclusive experiment takes its attempts whole
|
|
const total = open.reduce((s, e) => s + e.share, 0);
|
|
let u = rng(hash(`${plan.seed || 0}|${accountSeq}|${attempt}`))() * total, exp = open[open.length - 1];
|
|
for (const e of open) { if (u < e.share) { exp = e; break; } u -= e.share; }
|
|
// the unit that is randomised: the account (one arm for life) or the attempt (blocks within the account)
|
|
// (accounts are counted among those the experiment takes, so its blocks stay balanced)
|
|
const nth = exp.accounts ? Math.floor(accountSeq / exp.accounts.every) * exp.accounts.take + (accountSeq % exp.accounts.every) : accountSeq;
|
|
const arm = exp.unit === "account" ? armFor(exp, nth) : armFor({ ...exp, seed: `${exp.seed || 0}|${accountSeq}` }, attempt - exp.attempts.from);
|
|
return { experimentId: exp.id, arm: arm.id, fix: arm.fix || null, target: exp.target, objective: exp.objective || "eventually",
|
|
sampling: exp.sampling || "best", seed: hash(`${plan.seed || 0}|${exp.id}|${accountSeq}|${attempt}`),
|
|
propensity: (exp.share / total) / exp.arms.length, burn: exp.burn || null, planId: plan.id };
|
|
}
|
|
|
|
return Object.assign(api, {
|
|
advisor, recommendOption, solverHistory, norm, assign, armFor, givenFor,
|
|
makeParams: (T, { alphas, game } = {}) => makeParams(T, { alphas, game: game || api.game }),
|
|
/** remember the game of a policy sample (sets ENCOUNTERS) without solving anything */
|
|
useGame(sample) { api.game = sample.model.game; api.ENCOUNTERS = api.game.by_colour; return api.game; },
|
|
ENCOUNTERS: null, game: null,
|
|
});
|
|
});
|