Route model: first attempts are their own shadow roll; experiment plan and automated bot cycle
- 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
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
ee07ee143e
commit
ab3664f369
+17
-1
@@ -62,6 +62,10 @@ def estimate(runs, prepared, cfg, rare_goals, seed=20261109, sims=4000):
|
||||
e = {"rare": y, "n": n, "completeRare": ry, "completeN": rn}
|
||||
if g.get("complete"):
|
||||
e.update(rho=1.0, lo=1.0, hi=1.0, used="complete: counted as reported")
|
||||
elif ry < cfg.get("min_complete_rare", 5) and (cfg.get("borrow") or {}).get(node):
|
||||
# complete logging has (almost) never seen it, so the bias cannot be estimated for this roll: assume
|
||||
# the group over-reports it as much as it over-reports another rare roll (filled in below)
|
||||
e.update(rho=None, borrow=cfg["borrow"][node])
|
||||
elif ry < cfg.get("min_complete_rare", 5):
|
||||
# complete logging has (almost) never seen it: the bias cannot be estimated, so this roll is learned
|
||||
# from complete logging only (its range then shows how little is known)
|
||||
@@ -83,10 +87,22 @@ def estimate(runs, prepared, cfg, rare_goals, seed=20261109, sims=4000):
|
||||
for g in groups:
|
||||
for node in rolls:
|
||||
e = out[g["name"]][node]
|
||||
if e.get("rho") is None and not e.get("exclude"):
|
||||
if e.get("rho") is None and not e.get("exclude") and not e.get("borrow"):
|
||||
src = out.get(g.get("same_as") or "", {}).get(node, {})
|
||||
e["rho"] = src.get("rho") or 1.0
|
||||
e["lo"], e["hi"] = src.get("lo", e["rho"]), src.get("hi", e["rho"])
|
||||
# a roll whose bias cannot be estimated borrows the same group's estimate for another roll (`borrow`)
|
||||
for g in groups:
|
||||
for node in rolls:
|
||||
e = out[g["name"]][node]
|
||||
if e.get("borrow"):
|
||||
src = out[g["name"]].get(e["borrow"], {})
|
||||
if src.get("rho"):
|
||||
e.update(rho=src["rho"], lo=src.get("lo"), hi=src.get("hi"),
|
||||
used=f"assumed equal to this group's bias for the {e['borrow']} roll: complete logs have only "
|
||||
f"{e['completeRare']} rare outcomes, too few to estimate it")
|
||||
else:
|
||||
e.update(lo=None, hi=None, exclude=True, used="left out: no bias estimate to borrow")
|
||||
return out, names
|
||||
|
||||
|
||||
|
||||
+24
-5
@@ -28,6 +28,8 @@ ALONE = "(alone)"
|
||||
ALPHA = 20
|
||||
ALPHA_GRID = [1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 20000, 100000]
|
||||
ENC_CATS = G["encounter_outcomes"]
|
||||
GIVENS = G.get("givens") or {}
|
||||
DEFAULT_GIVEN = {k: g["default"] for k, g in GIVENS.items()}
|
||||
|
||||
|
||||
def enc_colour(ctx):
|
||||
@@ -62,6 +64,8 @@ def prepare_run(r):
|
||||
v = {"company": r["company"] if r["company"] in DECISIONS["company"] else None,
|
||||
"direction": r["direction"] if r["direction"] in DECISIONS["direction"] else None,
|
||||
"fs": r["first_sighting"] if r["first_sighting"] in ("Red", "Brown", "Green", "Orange") else None}
|
||||
# the givens (game.yaml): first or later attempt, from the run's attempt number
|
||||
v["attempt"] = r.get("attempt_group") if r.get("attempt_group") in GIVENS.get("attempt", {}).get("values", ()) else None
|
||||
lf = r.get("left_squadmates")
|
||||
known = lf is not None and not (isinstance(lf, float) and lf != lf) and lf != ""
|
||||
v["left"] = ALONE if v["company"] == "Alone" else (("Leave them" if lf is True or str(lf).lower() == "true" else "Stay together") if v["company"] and known else None)
|
||||
@@ -279,7 +283,7 @@ def fit_alpha(T, node):
|
||||
pairs = []
|
||||
for key in t:
|
||||
L = int(key.split(":")[0])
|
||||
if L == 0:
|
||||
if L <= CHANCE[node].get("distinct", 0): # the top level, and levels that are not pooled (game.yaml)
|
||||
continue
|
||||
ctx = key[key.index(":") + 1:].split("|")
|
||||
parent = f"{L - 1}:" + "|".join(ctx[:L - 1])
|
||||
@@ -368,10 +372,14 @@ class Params:
|
||||
In draw mode each world draws its numbers in the order its own walk of the game first needs them (a branch with
|
||||
probability exactly zero in that world is never walked), so call solver.draw_worlds() before using it.
|
||||
chance(node, state) -> {outcome: array(D)}; encounter(ctx) -> {outcome: array(D)}.
|
||||
|
||||
given: the run's givens (game.yaml `givens`, e.g. first or later attempt); having() gives the same worlds for
|
||||
other givens.
|
||||
"""
|
||||
|
||||
def __init__(self, T, mode="mean", seeds=(), alphas=None):
|
||||
def __init__(self, T, mode="mean", seeds=(), alphas=None, given=None):
|
||||
self.T, self.mode, self.alphas = T, mode, alphas or {}
|
||||
self.given = {**DEFAULT_GIVEN, **(given or {})}
|
||||
self.rngs = [Rng(s) for s in seeds] if mode == "draw" else []
|
||||
self.D = len(self.rngs) if mode == "draw" else 1
|
||||
self.memo = {} # (node, level key) or ("enc", ctx) -> array (K, D)
|
||||
@@ -379,6 +387,14 @@ class Params:
|
||||
self.world = None # while drawing one world (see draw_world), its index
|
||||
self._cache = {}
|
||||
|
||||
def having(self, **given):
|
||||
"""The same odds (and the same posterior draws) for a run with other givens."""
|
||||
import copy
|
||||
p = copy.copy(self)
|
||||
p.given = {**self.given, **given}
|
||||
p._cache = {}
|
||||
return p
|
||||
|
||||
def _levels(self, node, state):
|
||||
sp = CHANCE[node]
|
||||
out = []
|
||||
@@ -408,6 +424,7 @@ class Params:
|
||||
if not gate_open(node, state): # a closed gate: the roll always comes out the same way
|
||||
other = CHANCE[node]["otherwise"]
|
||||
return {k: np.full(self.D, 1.0 if k == other else 0.0) for k in CHANCE[node]["outcomes"]}
|
||||
state = {**self.given, **state}
|
||||
# once every world has been drawn, the answer depends only on the context values: cache it
|
||||
ck = (node, tuple(state.get(k) for k in CHANCE[node]["context"]))
|
||||
if self.world is None and ck in self._cache:
|
||||
@@ -419,13 +436,15 @@ class Params:
|
||||
|
||||
def _chance(self, node, state):
|
||||
cats = CHANCE[node]["outcomes"]
|
||||
al = self.alphas.get(node) or ALPHA
|
||||
fitted = self.alphas.get(node) or ALPHA
|
||||
distinct = CHANCE[node].get("distinct", 0)
|
||||
p = None
|
||||
for key in self._levels(node, state):
|
||||
for L, key in enumerate(self._levels(node, state)):
|
||||
c = self.T[node].get(key, {})
|
||||
parent = p
|
||||
al = 1.0 if L <= distinct else fitted # a level that is not pooled keeps a token prior of one run
|
||||
|
||||
def make_a(d, c=c, parent=parent):
|
||||
def make_a(d, c=c, parent=parent, al=al):
|
||||
if parent is None:
|
||||
return [c.get(k, 0) + 0.5 for k in cats]
|
||||
return [c.get(k, 0) + al * (parent[i] if d is None else parent[i][d]) for i, k in enumerate(cats)]
|
||||
|
||||
+41
-1
@@ -250,13 +250,30 @@ def draw_worlds(T, seeds, alphas, support):
|
||||
"""Posterior draws, one world per seed, each drawn in its own walk of the game (see Params)."""
|
||||
p = model.Params(T, "draw", seeds=seeds, alphas=alphas)
|
||||
p.reached = {}
|
||||
default = dict(p.given)
|
||||
for d in range(p.D):
|
||||
p.world = d
|
||||
_Drawer(p, support, d, p.reached).root()
|
||||
# the same world for runs with other givens (a first attempt): only the rolls that depend on them are new
|
||||
for given in other_givens():
|
||||
p.given = {**default, **given}
|
||||
_Drawer(p, support, d, {}).root()
|
||||
p.given = default
|
||||
p.world = None
|
||||
return p
|
||||
|
||||
|
||||
def other_givens():
|
||||
"""Every non-default value of every given in game.yaml, one at a time: [{"attempt": "First attempt"}]."""
|
||||
return [{k: v} for k, g in model.GIVENS.items() for v in g["values"] if v != g["default"]]
|
||||
|
||||
|
||||
def given_key(given):
|
||||
"""The name a non-default given's results are published under: {"attempt": "First attempt"} -> "firstAttempt"."""
|
||||
words = " ".join(given.values()).split()
|
||||
return words[0].lower() + "".join(w.capitalize() for w in words[1:])
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- objectives
|
||||
|
||||
def solve(params, X, support, lam=0.0, record=False, only=None):
|
||||
@@ -364,6 +381,28 @@ def build_policy(runs, draws=200, seed=20261101, min_support=None, alpha=None, c
|
||||
"crossFit": {"inSample": (cross[0]["inSample"] + cross[1]["inSample"]) / 2,
|
||||
"heldOut": (cross[0]["heldOut"] + cross[1]["heldOut"]) / 2},
|
||||
}
|
||||
# Runs with another given (a first attempt). "This attempt" is solved as above. "Eventually": every attempt
|
||||
# after it is an ordinary one, worth the default's ratio (lam), so the best play maximises
|
||||
# P(X now) + P(no bond now) * lam, which is the same score with lam fixed.
|
||||
later = [xi / (xi + oi) if xi + oi else 0.0 for xi, oi in zip(ev_ratio[0], ev_ratio[1])]
|
||||
for given in other_givens():
|
||||
m1, d1 = mean.having(**given), drawp.having(**given)
|
||||
g_one = solve(m1, X, support, record=True)
|
||||
g_ev = solve(m1, X, support, lam=ratio.lam, record=True)
|
||||
e_one = evaluate(d1, X, support, g_one.decide)
|
||||
e_ev = evaluate(d1, X, support, g_ev.decide)
|
||||
|
||||
def gblock(sol, ev, dr, m1=m1):
|
||||
x, o = ev[0], ev[1]
|
||||
return {"pX": {**summarise(x), "point": float(sol.value[0][0])},
|
||||
"pOther": {**summarise(o), "point": float(sol.value[1][0])},
|
||||
"tree": Exporter(m1, X, support, sol, dr).root()}
|
||||
ev_block = gblock(g_ev, e_ev, solve(d1, X, support, lam=d_ratio.lam))
|
||||
x0, o0 = float(g_ev.value[0][0]), float(g_ev.value[1][0])
|
||||
ev_block["pEventually"] = {**summarise([x + (1 - x - o) * r for x, o, r in zip(e_ev[0], e_ev[1], later)]),
|
||||
"point": x0 + (1 - x0 - o0) * float(np.ravel(ratio.lam)[0])}
|
||||
targets[X][given_key(given)] = {"given": given, "thisAttempt": gblock(g_one, e_one, solve(d1, X, support)),
|
||||
"eventually": ev_block}
|
||||
return {
|
||||
"schema": 1,
|
||||
"model": {
|
||||
@@ -371,12 +410,13 @@ def build_policy(runs, draws=200, seed=20261101, min_support=None, alpha=None, c
|
||||
"alphas": alphas, "alphaFit": {k: f["logLik"] for k, f in fitted.items()}, "draws": draws, "seed": seed,
|
||||
"minSupport": min_support,
|
||||
"chanceContext": {k: s["context"] for k, s in model.CHANCE.items()},
|
||||
"given": model.DEFAULT_GIVEN,
|
||||
"decisions": model.DECISIONS, "support": support, "supportCounts": counts,
|
||||
"tables": T,
|
||||
# how much each kind of source over-reports each rare roll, and the correction applied (bias.py)
|
||||
"reportingBias": bias_est,
|
||||
# the game itself (core/spec/game.yaml), so the advisor in core/solver.js can re-solve from any point
|
||||
"game": {k: G[k] for k in ("version", "colours", "decisions", "chance", "encounters", "encounter_outcomes",
|
||||
"game": {k: G[k] for k in ("version", "colours", "givens", "decisions", "chance", "encounters", "encounter_outcomes",
|
||||
"by_colour", "start", "steps", "from_form", "bot_text")},
|
||||
},
|
||||
"data": {"runs": len(runs)},
|
||||
|
||||
+100
-14
@@ -11,6 +11,9 @@
|
||||
*
|
||||
* 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) {
|
||||
@@ -48,22 +51,31 @@
|
||||
}
|
||||
|
||||
/* ---------- the odds: posterior means (or, with rand, one posterior draw) with hierarchical back-off ---------- */
|
||||
function makeParams(T, { alphas = {}, game, rand = null }) {
|
||||
/** 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 memo = new Map();
|
||||
const known = given || givenFor(game);
|
||||
const ctxKey = (s, names) => names.map((k) => (s[k] == null ? "?" : s[k])).join("|");
|
||||
function chance(node, state) {
|
||||
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]) => state[k] !== v)) return Object.fromEntries(cats.map((k) => [k, k === sp.otherwise ? 1 : 0]));
|
||||
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; }
|
||||
const c = T[node][key] || {}, al = alphas[node] || ALPHA;
|
||||
// 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);
|
||||
@@ -174,22 +186,50 @@
|
||||
* 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 } = {}) {
|
||||
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;
|
||||
const solveWith = (params) => (objective === "eventually" ? solveRatio(params, game, m.support, X, only) : solve(params, game, m.support, X, 0, only));
|
||||
// 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 = makeParams(m.tables, { alphas: m.alphas, game, rand: thompson ? rng(seed) : null });
|
||||
const sol = solveWith(params);
|
||||
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(makeParams(m.tables, { alphas: m.alphas, game, rand: rng(seed * 7919 + i + 1) })).choices);
|
||||
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"]);
|
||||
@@ -236,7 +276,7 @@
|
||||
throw new Error("the game has no end");
|
||||
}
|
||||
return {
|
||||
colour: X, objective, only, sampling, seed: thompson ? seed : null, value: sol.value, step, params, game,
|
||||
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) */
|
||||
@@ -258,8 +298,18 @@
|
||||
function recommendOption(adv, h, offered) {
|
||||
const rec = adv.next(h);
|
||||
if (!rec) return null;
|
||||
const pat = ((adv.game.bot_text || {})[rec.decision] || {})[rec.choice];
|
||||
const idx = pat ? offered.map(norm).findIndex((t) => new RegExp(pat).test(t)) : -1;
|
||||
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,
|
||||
@@ -302,8 +352,44 @@
|
||||
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,
|
||||
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; },
|
||||
|
||||
+19
-3
@@ -9,6 +9,16 @@ version: 1
|
||||
# Colours in the order the route model reports them
|
||||
colours: [Red, Orange, Brown, Green, Blue, Black]
|
||||
|
||||
# ---------------------------------------------------------------- givens: what a run starts with
|
||||
# Known before the first choice; neither decided nor rolled. A roll can depend on a given by naming it in its
|
||||
# context. The route model solves the game once per value; `default` is the one used when nothing is said.
|
||||
givens:
|
||||
attempt:
|
||||
label: Attempt
|
||||
# from the run's attempt number (attempt_group in data/public/runs_clean.csv)
|
||||
values: [First attempt, Later attempt]
|
||||
default: Later attempt
|
||||
|
||||
# ---------------------------------------------------------------- decisions: the rider's choices
|
||||
# options: every option the game offers, in a fixed order (ties go to the first). Options seen fewer than
|
||||
# min_support times in the data are not recommended, except where `always_offered` says so.
|
||||
@@ -48,8 +58,10 @@ min_support: 20
|
||||
# ---------------------------------------------------------------- chance events: the game's rolls
|
||||
# outcomes: in a fixed order. context: the earlier steps the odds may depend on, most important first; the
|
||||
# estimate for a full context is shrunk toward the estimate for a shorter one (hierarchical Dirichlet back-off,
|
||||
# strength learned from the data). requires: earlier choices without which the roll always comes out as
|
||||
# `otherwise` (a gate); runs that did not make them say nothing about the roll and are not counted.
|
||||
# strength learned from the data). distinct: the first N context steps are not pooled at all: each of their values
|
||||
# is its own roll (only a token prior of one run ties it to the rest). requires: earlier choices without which the
|
||||
# roll always comes out as `otherwise` (a gate); runs that did not make them say nothing about the roll and are
|
||||
# not counted.
|
||||
chance:
|
||||
fs: { label: First dragon, outcomes: [Red, Brown, Green, Orange], context: [direction, company, left] }
|
||||
# rolls that can kill you: risk_label names the danger when the advisor reports it
|
||||
@@ -59,7 +71,11 @@ chance:
|
||||
shadow:
|
||||
label: Shadow at the tree
|
||||
outcomes: ["yes", "no"]
|
||||
context: [cadet, post, fs, direction, company]
|
||||
# The shadow is close to a first-attempt event (November 2026, form submissions, gated tree runs: 69 shadows in
|
||||
# 748 first attempts, 7 in 1,995 later ones, the same in every week), so first and later attempts are two
|
||||
# different rolls. Routes are then pooled within each.
|
||||
context: [attempt, cadet, post, fs, direction, company]
|
||||
distinct: 1
|
||||
# the "Black Dragon Gates" of the community route PDF (November 2026). The runs agree: in the spreadsheet,
|
||||
# 78 shadows in 4199 tree runs that investigated and helped, 2 (both form submissions) in 646 that did not.
|
||||
requires: { scream: Investigate the scream, clearing: Help your squadmate }
|
||||
|
||||
Reference in New Issue
Block a user