Refactor: lay the repo out by section (core, collection, analysis, planner, site) (#8)
Moves only; no behaviour changes. Every file now lives under the part of the project it belongs to, each section has a README, and docs/PRIVACY.md collects the rules all parts follow. core/ engine.js, graph.js, solver.js, python/clean.py collection/ sources/ (form, register, PDF check, daily update), bot/ (importer) analysis/ stats/ (Python statistics), network/ (build-graph.js), reports/ (POLICY.md) planner/ build-policy.js, policy-report.js site/ atlas/ (the page), build.js, qa/ tests/ core/ (unit), parity/ (JS vs Python), fixtures/ Checked against the previous layout: tests pass; coded runs, test outputs, graph.json and policy.json are byte-identical; the built site differs only in comments that name file paths. The published file names Ken's bot downloads (solver.js, graph.js, data/policy.json) are unchanged. Also removes the undefined/ folder of QA screenshots committed by mistake (the QA scripts now default to a temp folder) and test.txt, and drops the unused npm workspaces. Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
dcd5117373
commit
432e04c2c7
@@ -0,0 +1,16 @@
|
||||
# core
|
||||
|
||||
The shared model. Every other part uses it, so a change here touches everything: run `npm test` after any edit.
|
||||
|
||||
| File | What it does |
|
||||
|---|---|
|
||||
| `engine.js` | Turns a row of the community form (any version of it) into one coded run: the run format, every coding rule, missing answers, email redaction. Also the statistics the site shows. |
|
||||
| `python/clean.py` | The same coding rules in Python. `tests/parity/` fails if the two ever disagree. |
|
||||
| `graph.js` | The route graph: the bot's scene graph, the decision-variable graph shared by all sources, the crosswalk from the bot's on-screen text to form answers, Beta credible intervals. |
|
||||
| `solver.js` | The decision model: every choice and roll in a run, chance estimates with empirical-Bayes back-off, backward induction for each dragon, and the advisor the site and the bot use to get advice from any point in a run. |
|
||||
|
||||
All three JS files have no dependencies and run unchanged in Node and the browser. The site and Ken's bot load
|
||||
the published copies (`https://ekcschiano.github.io/threshing-atlas/solver.js` etc.), so their file names on the
|
||||
site must not change.
|
||||
|
||||
To change a coding rule, edit `codeRow()` in `engine.js` **and** `code_row()` in `python/clean.py`.
|
||||
+846
@@ -0,0 +1,846 @@
|
||||
/*
|
||||
* engine.js — Threshing route analysis engine.
|
||||
*
|
||||
* One file, used two ways:
|
||||
* - in the browser (the route map page loads it and recomputes everything from the raw form export)
|
||||
* - in Node (`node tests/parity/run_engine.js data.json`), so results can be reproduced and checked against
|
||||
* the Python reference implementation in python/.
|
||||
*
|
||||
* Pipeline: raw form rows -> codeRow() (one coded run per row) -> analyse() -> results object
|
||||
* coded runs + a target outcome -> buildGraph() -> nodes/links for the route map
|
||||
*
|
||||
* Every rule here is described in plain language in the Methods section of the page.
|
||||
*/
|
||||
(function (root, factory) {
|
||||
if (typeof module === "object" && module.exports) module.exports = factory();
|
||||
else root.ThreshEngine = factory();
|
||||
})(typeof self !== "undefined" ? self : this, function () {
|
||||
"use strict";
|
||||
// Free-text answers sometimes contain a respondent's email address; never display or export it.
|
||||
const EMAIL_RE = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
|
||||
const redact = (v) => (v == null ? v : String(v).replace(EMAIL_RE, "[email removed]"));
|
||||
|
||||
const COLORS = ["Red", "Green", "Orange", "Brown", "Blue", "Black"];
|
||||
|
||||
// Answers that carry no information about the choice ("Unsure", blank, a question the form did not ask yet,
|
||||
// conflicting answers). They are kept in the coded data for transparency, but every analysis treats them as
|
||||
// missing: they are never shown or compared as if they were a choice.
|
||||
const MISSING = new Set(["Unknown", "Unsure", "Unsure/unknown", "Multiple", "(not recorded)", "(not asked)",
|
||||
"(not asked/blank)", "Unknown/lost count", "Blank/not asked"]);
|
||||
const isMissing = (v) => v === null || v === undefined || v === "" || MISSING.has(v);
|
||||
|
||||
// ---------------------------------------------------------------- column detection
|
||||
const COLUMN_KEYS = {
|
||||
timestamp: ["timestamp"],
|
||||
wing_First: ["wing and section", "[first]"],
|
||||
wing_Second: ["wing and section", "[second]"],
|
||||
wing_Third: ["wing and section", "[third]"],
|
||||
wing_Fourth: ["wing and section", "[fourth]"],
|
||||
squad: ["what squad"],
|
||||
attempt: ["attempt number"],
|
||||
company: ["who accompanied"],
|
||||
dir_East: ["initial direction", "[east"],
|
||||
dir_West: ["initial direction", "[west"],
|
||||
dir_Center: ["initial direction", "[forest center"],
|
||||
cadet: ["outcome of your cadet encounter"],
|
||||
post: ["path did you take after the bully"],
|
||||
hour: ["after walking an hour"],
|
||||
enc_Red: ["attempt to bond", "[red"],
|
||||
enc_Green: ["attempt to bond", "[green"],
|
||||
enc_Orange: ["attempt to bond", "[orange"],
|
||||
enc_Brown: ["attempt to bond", "[brown"],
|
||||
enc_Blue: ["attempt to bond", "[blue"],
|
||||
enc_Black: ["attempt to bond", "[black"],
|
||||
death_note: ["those who died"],
|
||||
bond_note: ["those who bonded"],
|
||||
goal: ["colour dragon are you trying"],
|
||||
combat: ["combat choice"],
|
||||
// Questions added in the redesigned form (late October 2026). "!x" means the header must NOT contain x.
|
||||
first_dir: ["what was your first direction"],
|
||||
first_colour: ["colour was your initial dragon sighting", "!["],
|
||||
leg_West: ["colour was your initial dragon sighting", "[west"],
|
||||
leg_Center: ["colour was your initial dragon sighting", "[forest center"],
|
||||
sighting_action: ["action did you take during the first dragon sighting"],
|
||||
cadet_actions: ["during your cadet encounter"],
|
||||
goal_v2: ["why are you doing this run"],
|
||||
malek: ["seek malek"],
|
||||
rare: ["encounter a rare dragon"],
|
||||
tail: ["what tail does your dragon have"],
|
||||
};
|
||||
// A key is only reported missing when neither it nor its replacement in the other form version was found.
|
||||
const ALTERNATIVE = {
|
||||
dir_East: "first_dir", dir_West: "first_dir", dir_Center: "first_dir", first_dir: "dir_Center", first_colour: "dir_Center",
|
||||
goal: "goal_v2", goal_v2: "goal", combat: "cadet_actions", cadet_actions: "combat",
|
||||
};
|
||||
const OPTIONAL = new Set(["leg_West", "leg_Center", "sighting_action", "malek", "rare", "tail"]);
|
||||
|
||||
function findColumns(headers) {
|
||||
const C = {};
|
||||
for (const [key, words] of Object.entries(COLUMN_KEYS)) {
|
||||
const hit = headers.find((h) => words.every((w) => w[0] === "!" ? !String(h).toLowerCase().includes(w.slice(1)) : String(h).toLowerCase().includes(w)));
|
||||
if (hit !== undefined) C[key] = hit;
|
||||
}
|
||||
const missing = Object.keys(COLUMN_KEYS).filter((k) => !(k in C) && !OPTIONAL.has(k) && !(ALTERNATIVE[k] in C));
|
||||
return { C, missing };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- coding rules
|
||||
const CADET_MAP = {
|
||||
"Bullies > You dealt with them": ["Investigate & help", "Killed the bullies yourself"],
|
||||
"Bullies > Squadmate's revenge": ["Investigate & help", "Squadmate's revenge"],
|
||||
"Bullies > Left to the Dragons": ["Investigate & help", "Left bullies to the dragons"],
|
||||
"Bullies > Assessed the situation": ["Investigate & help", "Unknown"],
|
||||
"Injured Cadet > Tourniquet": ["Ignore the scream", "Tourniquet"],
|
||||
"Injured Cadet > Left them there": ["Ignore the scream", "Left injured cadet"],
|
||||
"Injured Cadet > Took their weapons": ["Ignore the scream", "Took their weapons"],
|
||||
"Avoided combat (Investigated scream > Left them to it)": ["Investigate & leave", "Left them to it"],
|
||||
"You died in combat": ["Investigate & help", "DIED in combat"],
|
||||
"N/A - You died with initial dragon sighting": ["DIED at first sighting", "DIED at first sighting"],
|
||||
};
|
||||
const COMBAT_MAP = {
|
||||
"Bullies > Assessed the situation": "Assess the situation",
|
||||
"Bullies > Even the numbers (Rushed the smaller cadet)": "Even the numbers (smaller cadet)",
|
||||
"Bullies > Put yourself in the middle (Rushed the bigger cadet attacking your squadmate)":
|
||||
"Put yourself in the middle (bigger cadet)",
|
||||
};
|
||||
// Redesigned form: the cadet encounter is asked as a path of actions plus a separate outcome.
|
||||
// value: [scream, first combat move, second combat move, resolution (when the action decides it)]
|
||||
const CADET_ACTIONS = {
|
||||
"Bullies > Assess > Unknown": ["Investigate & help", "Assess the situation", null, null],
|
||||
"Bullies > Assess > Smaller Cadet": ["Investigate & help", "Assess the situation", "Engage the smaller cadet", null],
|
||||
"Bullies > Assess > Daggers or not": ["Investigate & help", "Assess the situation", "Daggers or not (protect squadmate)", null],
|
||||
"Bullies > Unknown": ["Investigate & help", null, null, null],
|
||||
"Bullies > Smaller Cadet (Did not assess)": ["Investigate & help", "Even the numbers (smaller cadet)", null, null],
|
||||
"Bullies > Bigger Cadet (Did not assess)": ["Investigate & help", "Put yourself in the middle (bigger cadet)", null, null],
|
||||
"Injured Cadet > Tourniquet": ["Ignore the scream", null, null, "Tourniquet"],
|
||||
"Injured Cadet > Left them there": ["Ignore the scream", null, null, "Left injured cadet"],
|
||||
"Injured Cadet > Took their weapons": ["Ignore the scream", null, null, "Took their weapons"],
|
||||
"Avoided combat (Investigated scream > Left them to it)": ["Investigate & leave", null, null, "Left them to it"],
|
||||
"N/A - You died with initial dragon sighting": ["DIED at first sighting", null, null, "DIED at first sighting"],
|
||||
"Unsure": ["Unknown", null, null, "Unknown"],
|
||||
};
|
||||
const CADET_OUTCOME = {
|
||||
"Squadmate's revenge": "Squadmate's revenge",
|
||||
"You ended the bullies yourself": "Killed the bullies yourself",
|
||||
"Left to the Dragons": "Left bullies to the dragons",
|
||||
"You died in combat": "DIED in combat",
|
||||
};
|
||||
const DIRECTION_MAP = { "Forest Center": "Center", "West / Scent": "West", "East / Roar": "East" };
|
||||
const GOAL_V2 = {
|
||||
"Hoping for a Rare Dragon (Blue or Black)": "Blue or Black",
|
||||
"Trying to bond the Red dragon under the tree": "Red (tree)",
|
||||
"Daily Run Assignment data": "Daily run data",
|
||||
"Just here for fun, and maybe a dragon": "Any (for fun)",
|
||||
};
|
||||
const SIGHTING_ACTION = {
|
||||
"Hold Ground / Stay Still": "Hold still", "Respect / Face it": "Respect / face it",
|
||||
"Retreat / Run / Back Away": "Retreat / run", "Hide / Duck": "Hide", "Draw / Drop Weapon": "Weapon",
|
||||
"Meet Gaze": "Meet its gaze", "Protect / Help Sqaudmate": "Protect squadmate", "Approach": "Approach",
|
||||
"Stand on your own": "Stand alone", "Unsure": "Unknown", "Unsure - You don't remember": "Unknown",
|
||||
};
|
||||
const RARE_MAP = { "Yes - A blue dragon in the valley": "Blue (valley)", "Yes - Shadow!!!": "Shadow", "No": "No" };
|
||||
const RARE_GOALS = ["Blue", "Black", "Blue or Black"];
|
||||
const isRareGoal = (goal) => RARE_GOALS.includes(goal);
|
||||
|
||||
const HOUR_MAP = {
|
||||
"Continued to Valley Centre": "Valley",
|
||||
"Climbed the tallest tree": "Tree",
|
||||
"N/A - You're dead": "DIED before the hour",
|
||||
};
|
||||
// First-sighting pools per (company, direction) from the community PDF. Only used to FLAG rows.
|
||||
const SIGHTING_POOL = {
|
||||
"Alone|East": ["Red", "Orange"],
|
||||
"Alone|West": ["Red", "Brown", "Green"],
|
||||
"Alone|Center": ["Red", "Brown"],
|
||||
"1 Squadmate|East": ["Green", "Orange", "Red"],
|
||||
"1 Squadmate|West": ["Red", "Brown", "Green"],
|
||||
"1 Squadmate|Center": ["Red", "Brown"],
|
||||
"2 Squadmates|East": ["Red", "Orange"],
|
||||
"2 Squadmates|West": ["Green", "Brown", "Red"],
|
||||
"2 Squadmates|Center": ["Red", "Brown"],
|
||||
};
|
||||
|
||||
const str = (v) => {
|
||||
if (v === null || v === undefined) return null;
|
||||
if (typeof v === "number" && isNaN(v)) return null;
|
||||
const t = String(v).trim();
|
||||
return t === "" || t === "nan" || t === "NaN" ? null : t;
|
||||
};
|
||||
|
||||
// Form submissions carry a real timestamp; rows bulk-pasted into the sheet carry a label
|
||||
// ("Data Import", "Manual Upload", "Historical import") instead.
|
||||
function parseTimestamp(v) {
|
||||
if (v instanceof Date && !isNaN(v)) return v;
|
||||
if (typeof v === "number" && v > 40000 && v < 80000) return new Date(Date.UTC(1899, 11, 30) + Math.floor(v) * 86400000); // Excel serial date
|
||||
const t = str(v);
|
||||
if (!t) return null;
|
||||
let m = t.match(/^(\d{4})-(\d{2})-(\d{2})/);
|
||||
if (m) return new Date(Date.UTC(+m[1], +m[2] - 1, +m[3]));
|
||||
m = t.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})/); // Google Sheets CSV: M/D/YYYY
|
||||
if (m) return new Date(Date.UTC(+m[3], +m[1] - 1, +m[2]));
|
||||
return null;
|
||||
}
|
||||
const isoDate = (d) => d.toISOString().slice(0, 10);
|
||||
|
||||
function parseAttempt(v) {
|
||||
const t0 = str(v);
|
||||
if (!t0) return null;
|
||||
const t = t0.toLowerCase();
|
||||
if (t.includes("lost count")) return null;
|
||||
const words = { first: 1, second: 2, third: 3, fourth: 4, fifth: 5 };
|
||||
for (const [w, n] of Object.entries(words)) if (t.startsWith(w)) return n;
|
||||
const m = t.match(/\d+/);
|
||||
return m ? parseInt(m[0], 10) : null;
|
||||
}
|
||||
|
||||
function codeRow(row, C) {
|
||||
const g = (k) => (k in C ? str(row[C[k]]) : null);
|
||||
const flags = [];
|
||||
const tsRaw = "timestamp" in C ? row[C.timestamp] : null;
|
||||
const ts = parseTimestamp(tsRaw);
|
||||
const source = ts ? "Form" : str(tsRaw) || "Unknown import";
|
||||
const date = ts ? isoDate(ts) : null;
|
||||
|
||||
const wings = ["First", "Second", "Third", "Fourth"].filter((w) => g("wing_" + w));
|
||||
const wing = wings.length === 1 ? wings[0] : wings.length ? "Multiple" : "Unknown";
|
||||
const section = wings.length === 1 ? g("wing_" + wings[0]) : null;
|
||||
if (wings.length > 1) flags.push("multiple wings");
|
||||
|
||||
let direction, first;
|
||||
if ("first_dir" in C) {
|
||||
// Redesigned form: one direction question and one colour question. The older per-direction colour
|
||||
// columns are still in the sheet for some rows and are used only to fill gaps and to cross-check.
|
||||
direction = DIRECTION_MAP[g("first_dir")] || null;
|
||||
first = g("first_colour");
|
||||
const legacy = ["West", "Center"].filter((d) => g("leg_" + d));
|
||||
if (!direction && legacy.length === 1) {
|
||||
direction = legacy[0];
|
||||
flags.push("direction taken from the older sighting column");
|
||||
}
|
||||
const leg = direction ? g("leg_" + direction) : null;
|
||||
if (!first && leg) first = leg;
|
||||
else if (first && leg && first !== leg && first !== "Unsure" && leg !== "Unsure")
|
||||
flags.push("first sighting differs between the old and new columns");
|
||||
if (!direction) {
|
||||
direction = "Unknown";
|
||||
flags.push("direction missing");
|
||||
}
|
||||
} else {
|
||||
const dirs = ["East", "West", "Center"].filter((d) => g("dir_" + d));
|
||||
if (dirs.length === 1) {
|
||||
direction = dirs[0];
|
||||
first = g("dir_" + dirs[0]);
|
||||
} else {
|
||||
direction = dirs.length ? "Multiple" : "Unknown";
|
||||
first = null;
|
||||
flags.push("direction " + (dirs.length ? "multiple" : "missing"));
|
||||
}
|
||||
}
|
||||
const companyRaw = g("company") || "Unknown";
|
||||
const leftSquad = / > Left them$/.test(companyRaw);
|
||||
const company = leftSquad ? companyRaw.replace(/ > Left them$/, "") : companyRaw;
|
||||
// Leaving your squadmates sends you into the solo pools, so either pool is plausible.
|
||||
const pool = (SIGHTING_POOL[company + "|" + direction] || []).concat(leftSquad ? SIGHTING_POOL["Alone|" + direction] || [] : []);
|
||||
if (first && first !== "Unsure" && pool.length && !pool.includes(first))
|
||||
flags.push(`first sighting ${first} not in documented pool for ${company}/${direction}`);
|
||||
|
||||
const cadetRaw = g("cadet");
|
||||
let scream, resolution, combat = null, combat2 = null;
|
||||
if ("cadet_actions" in C) {
|
||||
const act = g("cadet_actions");
|
||||
const a = CADET_ACTIONS[act] || ["Unknown", null, null, "Unknown"];
|
||||
if (act && !CADET_ACTIONS[act]) flags.push("unrecognised cadet answer");
|
||||
[scream, combat, combat2] = a;
|
||||
const out = CADET_OUTCOME[cadetRaw] || null;
|
||||
if (a[3] !== null) {
|
||||
resolution = a[3];
|
||||
if (out === "DIED in combat" && !resolution.startsWith("DIED")) flags.push("died in combat on a path without combat");
|
||||
} else resolution = out || "Unknown";
|
||||
} else {
|
||||
[scream, resolution] = CADET_MAP[cadetRaw] || ["Unknown", "Unknown"];
|
||||
if (cadetRaw && !CADET_MAP[cadetRaw]) flags.push("unrecognised cadet answer");
|
||||
combat = g("combat") ? COMBAT_MAP[g("combat")] || null : null;
|
||||
}
|
||||
|
||||
const postRaw = g("post");
|
||||
let post = null;
|
||||
if (scream === "Investigate & help" && !resolution.startsWith("DIED"))
|
||||
post = ["Forest", "West", "East"].includes(postRaw) ? postRaw : "Unknown";
|
||||
else if (scream === "Ignore the scream" || scream === "Investigate & leave") post = "(no fork)";
|
||||
|
||||
const diedEarly = scream.startsWith("DIED") || resolution.startsWith("DIED");
|
||||
let hour = HOUR_MAP[g("hour")] || null;
|
||||
if (diedEarly) hour = null;
|
||||
else if (hour === null) hour = "Unknown";
|
||||
|
||||
const enc = {};
|
||||
for (const c of COLORS) enc[c] = g("enc_" + c);
|
||||
const rare = RARE_MAP[g("rare")] || null;
|
||||
const bonded = COLORS.filter((c) => enc[c] === "Bonded!");
|
||||
const burned = COLORS.filter((c) => enc[c] === "Incinerated");
|
||||
|
||||
let branch = null;
|
||||
if (hour === "Tree") {
|
||||
if (enc.Blue || enc.Black || rare === "Shadow") branch = "Shadow (rare)";
|
||||
else if (enc.Green) branch = "Descend slowly: two Greens";
|
||||
else if (enc.Red) branch = "Descend quickly: Red";
|
||||
else if (enc.Orange || enc.Brown) {
|
||||
branch = "Descend slowly: two Greens";
|
||||
flags.push("tree run with Orange/Brown but no Green recorded");
|
||||
}
|
||||
} else if (hour === "Valley") {
|
||||
if (enc.Green) flags.push("Green recorded on a Valley run (no Greens in the valley)");
|
||||
const meadow = ["Orange", "Brown", "Blue"].some((c) => enc[c]) || rare === "Blue (valley)";
|
||||
if (meadow || enc.Red === "Changed my mind") branch = "Passed the Red: meadow";
|
||||
else if (enc.Red) branch = "Valley Red";
|
||||
}
|
||||
|
||||
let outcome, outcomeColor = null;
|
||||
if (diedEarly) outcome = "Died before the hour";
|
||||
else if (bonded.length === 1) { outcome = "Bonded"; outcomeColor = bonded[0]; }
|
||||
else if (bonded.length > 1) { outcome = "Ambiguous"; flags.push("more than one dragon marked Bonded"); }
|
||||
else if (burned.length) {
|
||||
const order = hour !== "Valley"
|
||||
? ["Red", "Green", "Orange", "Brown", "Blue", "Black"]
|
||||
: ["Red", "Orange", "Brown", "Blue", "Green", "Black"];
|
||||
const inOrder = order.filter((c) => burned.includes(c));
|
||||
outcome = "Incinerated";
|
||||
outcomeColor = inOrder[inOrder.length - 1];
|
||||
if (burned.length > 1) flags.push("more than one dragon marked Incinerated");
|
||||
} else if (hour === "DIED before the hour") outcome = "Died before the hour";
|
||||
else outcome = "No outcome recorded";
|
||||
|
||||
const rescued = enc.Green === "Saved by another" && outcome === "Bonded" && ["Orange", "Brown"].includes(outcomeColor);
|
||||
const attempt = parseAttempt(g("attempt"));
|
||||
if ((rare === "Blue (valley)" && hour === "Tree") || (rare === "Shadow" && hour === "Valley"))
|
||||
flags.push("rare-dragon answer does not match the path");
|
||||
const goalRaw = g("goal_v2");
|
||||
const goal = g("goal") || (goalRaw && goalRaw !== "Unknown" ? GOAL_V2[goalRaw] || goalRaw : null) || "(not asked/blank)";
|
||||
|
||||
return {
|
||||
source, date, wing, section, squad: g("squad") || "Unknown", attempt,
|
||||
// "I've lost count" means many attempts, so it is a later attempt, not missing
|
||||
attempt_group: attempt === 1 ? "First attempt" : attempt > 1 || /lost count/i.test(g("attempt") || "") ? "Later attempt" : "Unknown",
|
||||
goal, company, left_squadmates: leftSquad, direction, first_sighting: first || "Unknown",
|
||||
sighting_action: "sighting_action" in C ? SIGHTING_ACTION[g("sighting_action")] || g("sighting_action") || "Unknown" : "(not asked)",
|
||||
scream, resolution, combat_first: combat || "(not recorded)", combat_second: combat2 || "(not recorded)",
|
||||
post_path: post, hour, branch,
|
||||
enc_Red: enc.Red, enc_Green: enc.Green, enc_Orange: enc.Orange, enc_Brown: enc.Brown,
|
||||
enc_Blue: enc.Blue, enc_Black: enc.Black,
|
||||
outcome, outcome_color: outcomeColor, rescued,
|
||||
saw_blue_meadow: !!(hour === "Valley" && (enc.Blue || rare === "Blue (valley)")),
|
||||
saw_shadow: !!(hour === "Tree" && (enc.Blue || enc.Black || rare === "Shadow")),
|
||||
rare_reported: "rare" in C ? rare || "Unknown" : "(not asked)",
|
||||
malek_intent: "malek" in C ? g("malek") || "Unknown" : "(not asked)",
|
||||
tail: g("tail"),
|
||||
data_flags: flags.join("; "),
|
||||
death_note: redact(g("death_note")), bond_note: redact(g("bond_note")),
|
||||
};
|
||||
}
|
||||
|
||||
function codeRows(rows, headers) {
|
||||
if (!headers) {
|
||||
const seen = new Set();
|
||||
rows.forEach((r) => Object.keys(r).forEach((k) => seen.add(k)));
|
||||
headers = [...seen];
|
||||
}
|
||||
const { C, missing } = findColumns(headers);
|
||||
const runs = rows.map((r, i) => Object.assign({ run_id: i + 1 }, codeRow(r, C)));
|
||||
return { runs, missing };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- statistics
|
||||
const Z95 = 1.959963984540054;
|
||||
|
||||
function wilson(k, n) {
|
||||
if (!n) return { k, n, p: null, lo: null, hi: null };
|
||||
const z = Z95, p = k / n, d = 1 + (z * z) / n;
|
||||
const c = (p + (z * z) / (2 * n)) / d;
|
||||
const h = (z * Math.sqrt((p * (1 - p)) / n + (z * z) / (4 * n * n))) / d;
|
||||
return { k, n, p, lo: Math.max(0, c - h), hi: Math.min(1, c + h) };
|
||||
}
|
||||
|
||||
const LOGF = [0];
|
||||
function logFact(n) {
|
||||
for (let i = LOGF.length; i <= n; i++) LOGF[i] = LOGF[i - 1] + Math.log(i);
|
||||
return LOGF[n];
|
||||
}
|
||||
// Two-sided Fisher exact test: sum of probabilities of all tables (same margins) no more likely
|
||||
// than the observed one (same rule as scipy.stats.fisher_exact, with a 1e-7 relative tolerance).
|
||||
function fisher(a, b, c, d) {
|
||||
const r1 = a + b, r2 = c + d, c1 = a + c, n = r1 + r2;
|
||||
const lp = (x) => logFact(r1) + logFact(r2) + logFact(c1) + logFact(n - c1) -
|
||||
logFact(n) - logFact(x) - logFact(r1 - x) - logFact(c1 - x) - logFact(r2 - c1 + x);
|
||||
const lo = Math.max(0, c1 - r2), hi = Math.min(r1, c1);
|
||||
const pObs = lp(a);
|
||||
let p = 0;
|
||||
for (let x = lo; x <= hi; x++) {
|
||||
const q = lp(x);
|
||||
if (q <= pObs + 1e-7 * Math.abs(pObs) + 1e-12) p += Math.exp(q);
|
||||
}
|
||||
return Math.min(1, p);
|
||||
}
|
||||
|
||||
function oddsRatio(a, b, c, d) {
|
||||
const corrected = a === 0 || b === 0 || c === 0 || d === 0;
|
||||
const k = corrected ? 0.5 : 0;
|
||||
const A = a + k, B = b + k, Cc = c + k, D = d + k;
|
||||
const or = (A * D) / (B * Cc);
|
||||
const se = Math.sqrt(1 / A + 1 / B + 1 / Cc + 1 / D);
|
||||
return {
|
||||
or, or_lo: Math.exp(Math.log(or) - Z95 * se), or_hi: Math.exp(Math.log(or) + Z95 * se),
|
||||
fisher_p: fisher(a, b, c, d), corrected,
|
||||
};
|
||||
}
|
||||
|
||||
// Mantel-Haenszel common odds ratio across strata (tables [a,b,c,d]), Robins-Breslow-Greenland 95% CI,
|
||||
// and the Cochran-Mantel-Haenszel chi-square test (1 df, with continuity correction).
|
||||
function mantelHaenszel(tables) {
|
||||
let R = 0, S = 0, PR = 0, PSQR = 0, QS = 0, sumA = 0, sumE = 0, sumV = 0;
|
||||
for (const [a, b, c, d] of tables) {
|
||||
const n = a + b + c + d;
|
||||
if (n < 2) continue;
|
||||
const r = (a * d) / n, s_ = (b * c) / n, P = (a + d) / n, Q = (b + c) / n;
|
||||
R += r; S += s_; PR += P * r; PSQR += P * s_ + Q * r; QS += Q * s_;
|
||||
const m1 = a + b, m0 = c + d, n1 = a + c, n0 = b + d;
|
||||
sumA += a; sumE += (m1 * n1) / n;
|
||||
sumV += (m1 * m0 * n1 * n0) / (n * n * (n - 1));
|
||||
}
|
||||
if (!R || !S) return { mh_or: null, mh_lo: null, mh_hi: null, mh_p: null };
|
||||
const or = R / S;
|
||||
const varLog = PR / (2 * R * R) + PSQR / (2 * R * S) + QS / (2 * S * S);
|
||||
const se = Math.sqrt(varLog);
|
||||
const chi = sumV > 0 ? Math.pow(Math.max(0, Math.abs(sumA - sumE) - 0.5), 2) / sumV : 0;
|
||||
return { mh_or: or, mh_lo: Math.exp(Math.log(or) - Z95 * se), mh_hi: Math.exp(Math.log(or) + Z95 * se), mh_p: normSf2(Math.sqrt(chi)) };
|
||||
}
|
||||
|
||||
function bh(ps) {
|
||||
const m = ps.length, order = ps.map((p, i) => [p, i]).sort((x, y) => x[0] - y[0]);
|
||||
const q = new Array(m);
|
||||
let prev = 1;
|
||||
for (let r = m; r >= 1; r--) {
|
||||
const [p, i] = order[r - 1];
|
||||
prev = Math.min(prev, (p * m) / r);
|
||||
q[i] = prev;
|
||||
}
|
||||
return q;
|
||||
}
|
||||
|
||||
// Normal CDF (Abramowitz-Stegun 7.1.26 is too coarse for small p; use erfc via continued fraction)
|
||||
function normSf2(z) { // two-sided p for |z|
|
||||
z = Math.abs(z);
|
||||
const t = 1 / (1 + 0.5 * z / Math.SQRT2);
|
||||
const x = z / Math.SQRT2;
|
||||
const tau = t * Math.exp(-x * x - 1.26551223 + t * (1.00002368 + t * (0.37409196 + t * (0.09678418 +
|
||||
t * (-0.18628806 + t * (0.27886807 + t * (-1.13520398 + t * (1.48851587 +
|
||||
t * (-0.82215223 + t * 0.17087277)))))))));
|
||||
return Math.min(1, tau); // erfc(x) = 2*P(Z>z)
|
||||
}
|
||||
|
||||
function solve(A, b) { // Gaussian elimination with partial pivoting
|
||||
const n = b.length, M = A.map((r, i) => r.concat([b[i]]));
|
||||
for (let i = 0; i < n; i++) {
|
||||
let p = i;
|
||||
for (let r = i + 1; r < n; r++) if (Math.abs(M[r][i]) > Math.abs(M[p][i])) p = r;
|
||||
[M[i], M[p]] = [M[p], M[i]];
|
||||
for (let r = i + 1; r < n; r++) {
|
||||
const f = M[r][i] / M[i][i];
|
||||
for (let c = i; c <= n; c++) M[r][c] -= f * M[i][c];
|
||||
}
|
||||
}
|
||||
const x = new Array(n).fill(0);
|
||||
for (let i = n - 1; i >= 0; i--) {
|
||||
let s = M[i][n];
|
||||
for (let c = i + 1; c < n; c++) s -= M[i][c] * x[c];
|
||||
x[i] = s / M[i][i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
function invert(A) {
|
||||
const n = A.length;
|
||||
return A.map((_, j) => solve(A, A.map((__, i) => (i === j ? 1 : 0)))).reduce((acc, col, j) => {
|
||||
col.forEach((v, i) => (acc[i][j] = v));
|
||||
return acc;
|
||||
}, A.map(() => new Array(n).fill(0)));
|
||||
}
|
||||
|
||||
// Logistic regression, Newton-Raphson with an L2 penalty (lambda) on all slopes (not the intercept).
|
||||
function logisticRidge(X, y, lam = 1, iters = 100) {
|
||||
const n = X.length, k = X[0].length;
|
||||
let beta = new Array(k).fill(0);
|
||||
const info = (b) => {
|
||||
const H = Array.from({ length: k }, () => new Array(k).fill(0));
|
||||
const grad = new Array(k).fill(0);
|
||||
for (let i = 0; i < n; i++) {
|
||||
let eta = 0;
|
||||
for (let j = 0; j < k; j++) eta += X[i][j] * b[j];
|
||||
const mu = 1 / (1 + Math.exp(-eta)), w = mu * (1 - mu);
|
||||
for (let j = 0; j < k; j++) {
|
||||
grad[j] += X[i][j] * (y[i] - mu);
|
||||
if (!X[i][j]) continue;
|
||||
for (let l = 0; l < k; l++) H[j][l] += X[i][j] * X[i][l] * w;
|
||||
}
|
||||
}
|
||||
for (let j = 1; j < k; j++) { H[j][j] += lam; grad[j] -= lam * b[j]; }
|
||||
return { H, grad };
|
||||
};
|
||||
for (let it = 0; it < iters; it++) {
|
||||
const { H, grad } = info(beta);
|
||||
const step = solve(H, grad);
|
||||
beta = beta.map((v, j) => v + step[j]);
|
||||
if (Math.max(...step.map(Math.abs)) < 1e-10) break;
|
||||
}
|
||||
const { H } = info(beta);
|
||||
const Hi = invert(H);
|
||||
const se = Hi.map((r, j) => Math.sqrt(r[j]));
|
||||
return { beta, se };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- analysis helpers
|
||||
const rate = (runs, pred) => wilson(runs.filter(pred).length, runs.length);
|
||||
const groupBy = (arr, key) => arr.reduce((m, r) => {
|
||||
const k = typeof key === "function" ? key(r) : r[key];
|
||||
(m[k] = m[k] || []).push(r);
|
||||
return m;
|
||||
}, {});
|
||||
const sortedKeys = (o) => Object.keys(o).sort();
|
||||
|
||||
const stratum = (r) => r.source !== "Form" ? "Bulk logs (imports)" :
|
||||
r.goal === "Blue" ? "Form: chasing Blue" : r.goal === "Black" ? "Form: chasing Black" :
|
||||
r.goal === "Blue or Black" ? "Form: chasing Blue or Black" : "Form: other/blank goal";
|
||||
const goal2 = (r) => isRareGoal(r.goal) ? r.goal : r.goal === "(not asked/blank)" ? "Blank/not asked" : "Other colour/any";
|
||||
|
||||
// Denominators for each outcome ("who could have had this outcome?")
|
||||
const OUTCOMES = {
|
||||
blue_meadow: {
|
||||
label: "Blue appears in the meadow", denom: (r) => r.branch === "Passed the Red: meadow", event: (r) => r.saw_blue_meadow,
|
||||
},
|
||||
shadow: {
|
||||
label: "Shadow option appears at the tree", denom: (r) => r.hour === "Tree" && r.branch !== null, event: (r) => r.saw_shadow,
|
||||
},
|
||||
bond_blue_meadow: {
|
||||
label: "Bond, given you approached the meadow Blue", denom: (r) => r.branch === "Passed the Red: meadow" && ["Bonded!", "Incinerated"].includes(r.enc_Blue),
|
||||
event: (r) => r.enc_Blue === "Bonded!",
|
||||
},
|
||||
reach_hour: {
|
||||
label: "Survive to the hour (tree/valley choice)", denom: () => true, event: (r) => r.hour === "Tree" || r.hour === "Valley",
|
||||
},
|
||||
};
|
||||
for (const c of COLORS) {
|
||||
OUTCOMES["bonded_" + c] = {
|
||||
label: `Run ends bonded to ${c}`, denom: (r) => r.hour === "Tree" || r.hour === "Valley",
|
||||
event: (r) => r.outcome === "Bonded" && r.outcome_color === c,
|
||||
};
|
||||
}
|
||||
|
||||
const FACTORS = ["company", "direction", "first_sighting", "scream", "resolution", "combat_first",
|
||||
"post_path", "hour", "wing", "squad", "attempt_group", "source", "goal", "sighting_action", "combat_second", "malek_intent"];
|
||||
|
||||
// Encounter contexts: which recorded dragon attempt belongs to which in-game encounter.
|
||||
const CONTEXTS = [
|
||||
["Red (tree, descend quickly)", (r) => r.hour === "Tree" && r.branch === "Descend quickly: Red", "enc_Red", "Red"],
|
||||
["Green (two Greens)", (r) => r.hour === "Tree" && r.branch === "Descend slowly: two Greens", "enc_Green", "Green"],
|
||||
["Blue (shadow)", (r) => r.hour === "Tree" && r.branch === "Shadow (rare)", "enc_Blue", "Blue"],
|
||||
["Black (shadow)", (r) => r.hour === "Tree" && r.branch === "Shadow (rare)", "enc_Black", "Black"],
|
||||
["Red (valley)", (r) => r.hour === "Valley" && r.branch === "Valley Red", "enc_Red", "Red"],
|
||||
["Orange (meadow)", (r) => r.hour === "Valley" && r.branch === "Passed the Red: meadow", "enc_Orange", "Orange"],
|
||||
["Brown (meadow)", (r) => r.hour === "Valley" && r.branch === "Passed the Red: meadow", "enc_Brown", "Brown"],
|
||||
["Blue (meadow)", (r) => r.hour === "Valley" && r.branch === "Passed the Red: meadow", "enc_Blue", "Blue"],
|
||||
];
|
||||
|
||||
function applyFilters(runs, f = {}) {
|
||||
return runs.filter((r) => {
|
||||
if (f.source === "form" && r.source !== "Form") return false;
|
||||
if (f.source === "bulk" && r.source === "Form") return false;
|
||||
if (f.goal === "exclude_rare" && isRareGoal(r.goal)) return false;
|
||||
if (f.goal === "only_rare" && !isRareGoal(r.goal)) return false;
|
||||
if (f.excludeFlagged && r.data_flags) return false;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
function factorTable(runs, outKey, minN = 5) {
|
||||
const O = OUTCOMES[outKey];
|
||||
const D = runs.filter(O.denom);
|
||||
const total = D.length, events = D.filter(O.event).length;
|
||||
const rows = [], missing = {};
|
||||
for (const f of FACTORS) {
|
||||
const groups = groupBy(D.filter((r) => !isMissing(r[f])), f);
|
||||
missing[f] = D.filter((r) => isMissing(r[f])).length;
|
||||
const nf = Object.values(groups).reduce((s, g) => s + g.length, 0);
|
||||
const ef = Object.values(groups).reduce((s, g) => s + g.filter(O.event).length, 0);
|
||||
for (const lv of sortedKeys(groups)) {
|
||||
const g = groups[lv], k = g.filter(O.event).length, n = g.length;
|
||||
const row = Object.assign({ factor: f, level: lv }, wilson(k, n));
|
||||
const rk = ef - k, rn = nf - n; // rest of the same factor
|
||||
if (n >= minN && rn >= minN && ef > 0 && ef < nf) Object.assign(row, oddsRatio(k, n - k, rk, rn - rk));
|
||||
rows.push(row);
|
||||
}
|
||||
}
|
||||
const tested = rows.filter((r) => r.fisher_p !== undefined);
|
||||
bh(tested.map((r) => r.fisher_p)).forEach((q, i) => (tested[i].q = q));
|
||||
return { outcome: outKey, label: O.label, overall: wilson(events, total), rows, missing };
|
||||
}
|
||||
|
||||
// Multivariable model: one row per run in the outcome's denominator; categorical factors dummy-coded
|
||||
// against their most common level.
|
||||
function model(runs, outKey, factors, lam = 1) {
|
||||
const O = OUTCOMES[outKey];
|
||||
const D = runs.filter(O.denom).map((r) => {
|
||||
const o = { y: O.event(r) ? 1 : 0 };
|
||||
for (const [name, fn] of Object.entries(factors)) o[name] = fn(r);
|
||||
return o;
|
||||
}).filter((o) => Object.keys(factors).every((f) => o[f] !== null && o[f] !== undefined));
|
||||
if (D.length < 20) return null;
|
||||
const cols = [["(intercept)", () => 1]], refs = {};
|
||||
for (const f of Object.keys(factors)) {
|
||||
const counts = groupBy(D, f);
|
||||
const levels = sortedKeys(counts).sort((a, b) => counts[b].length - counts[a].length || (a < b ? -1 : 1));
|
||||
refs[f] = levels[0];
|
||||
for (const lv of levels.slice(1)) cols.push([`${f} = ${lv}`, (o) => (o[f] === lv ? 1 : 0), f, lv, counts[lv].length]);
|
||||
}
|
||||
const X = D.map((o) => cols.map((c) => c[1](o)));
|
||||
const y = D.map((o) => o.y);
|
||||
const { beta, se } = logisticRidge(X, y, lam);
|
||||
const terms = cols.slice(1).map((c, i) => {
|
||||
const b = beta[i + 1], s = se[i + 1];
|
||||
return { term: c[0], factor: c[2], level: c[3], n: c[4], or: Math.exp(b), lo: Math.exp(b - Z95 * s), hi: Math.exp(b + Z95 * s), p: normSf2(b / s) };
|
||||
});
|
||||
return { outcome: outKey, label: O.label, n: D.length, events: y.reduce((a, b) => a + b, 0), refs, lambda: lam, terms };
|
||||
}
|
||||
|
||||
const routeKey = (r) => [r.company, r.direction, r.first_sighting, r.scream, r.resolution, r.post_path || "—", r.hour || "—"];
|
||||
const ROUTE_FIELDS = ["company", "direction", "first_sighting", "scream", "resolution", "post_path", "hour"];
|
||||
|
||||
function analyse(allRuns, filters = {}) {
|
||||
const runs = applyFilters(allRuns, filters);
|
||||
const res = { filters, n: runs.length };
|
||||
|
||||
// meta / data quality
|
||||
const flagCounts = {};
|
||||
runs.forEach((r) => r.data_flags && r.data_flags.split("; ").forEach((f) => {
|
||||
const key = f.startsWith("first sighting") ? "first sighting outside documented pool" : f;
|
||||
flagCounts[key] = (flagCounts[key] || 0) + 1;
|
||||
}));
|
||||
const dates = runs.map((r) => r.date).filter(Boolean).sort();
|
||||
res.meta = {
|
||||
sources: Object.fromEntries(Object.entries(groupBy(runs, "source")).map(([k, v]) => [k, v.length])),
|
||||
date_min: dates[0] || null, date_max: dates[dates.length - 1] || null,
|
||||
n_flagged: runs.filter((r) => r.data_flags).length, flag_counts: flagCounts,
|
||||
outcomes: Object.fromEntries(Object.entries(groupBy(runs, (r) => r.outcome + (r.outcome_color ? " " + r.outcome_color : ""))).map(([k, v]) => [k, v.length])),
|
||||
};
|
||||
|
||||
// chance events, overall and by stratum
|
||||
res.chance = {};
|
||||
for (const key of ["blue_meadow", "shadow"]) {
|
||||
const O = OUTCOMES[key], D = runs.filter(O.denom);
|
||||
res.chance[key] = {
|
||||
label: O.label, overall: rate(D, O.event),
|
||||
by_stratum: Object.fromEntries(sortedKeys(groupBy(D, stratum)).map((s) => [s, rate(groupBy(D, stratum)[s], O.event)])),
|
||||
by_goal: Object.fromEntries(sortedKeys(groupBy(D, "goal")).map((s) => [s, rate(groupBy(D, "goal")[s], O.event)])),
|
||||
by_attempt: Object.fromEntries(sortedKeys(groupBy(D, "attempt_group")).map((s) => [s, rate(groupBy(D, "attempt_group")[s], O.event)])),
|
||||
};
|
||||
}
|
||||
// first sighting pools
|
||||
const fs = groupBy(runs.filter((r) => !isMissing(r.direction) && !isMissing(r.company)), (r) => r.company + "|" + r.direction);
|
||||
res.first_sighting = sortedKeys(fs).map((k) => {
|
||||
const g = fs[k], cnt = groupBy(g, "first_sighting");
|
||||
return { key: k, n: g.length, colors: Object.fromEntries(sortedKeys(cnt).map((c) => [c, cnt[c].length])) };
|
||||
});
|
||||
|
||||
// bonding encounters
|
||||
res.encounters = CONTEXTS.map(([name, where, col, color]) => {
|
||||
const A = runs.filter((r) => where(r) && ["Bonded!", "Incinerated"].includes(r[col]));
|
||||
const win = (r) => r[col] === "Bonded!";
|
||||
const intent = A.filter((r) => r.goal === color || r.goal.startsWith("Any") || (r.goal === "Blue or Black" && (color === "Blue" || color === "Black")));
|
||||
const byS = groupBy(A, stratum);
|
||||
return {
|
||||
context: name, color, overall: rate(A, win), intent_matched: rate(intent, win),
|
||||
by_stratum: Object.fromEntries(sortedKeys(byS).map((s) => [s, rate(byS[s], win)])),
|
||||
};
|
||||
});
|
||||
const greens = runs.filter((r) => r.hour === "Tree" && r.branch === "Descend slowly: two Greens" && r.enc_Green);
|
||||
res.green_rescue = {
|
||||
green_encounters: greens.length,
|
||||
bonded_green: greens.filter((r) => r.enc_Green === "Bonded!").length,
|
||||
rescued_orange: greens.filter((r) => r.rescued && r.outcome_color === "Orange").length,
|
||||
rescued_brown: greens.filter((r) => r.rescued && r.outcome_color === "Brown").length,
|
||||
incinerated: greens.filter((r) => r.outcome === "Incinerated").length,
|
||||
};
|
||||
|
||||
// factor tables
|
||||
res.factors = {};
|
||||
for (const key of Object.keys(OUTCOMES)) res.factors[key] = factorTable(runs, key);
|
||||
|
||||
// adjusted models
|
||||
const first2 = (r) => (["Red", "Brown", "Orange", "Green"].includes(r.first_sighting) ? r.first_sighting : null);
|
||||
const known = (f) => (r) => (isMissing(r[f]) ? null : r[f]);
|
||||
const src2 = (r) => (r.source === "Form" ? "Form submission" : "Bulk log (import)");
|
||||
res.models = {
|
||||
blue_meadow: model(runs, "blue_meadow", {
|
||||
source: src2, goal: goal2, direction: known("direction"), first_sighting: first2,
|
||||
resolution: known("resolution"), company: known("company"),
|
||||
}),
|
||||
shadow: model(runs, "shadow", {
|
||||
source: src2, goal: goal2, attempt: known("attempt_group"), direction: known("direction"),
|
||||
}),
|
||||
bond_blue_meadow: model(runs, "bond_blue_meadow", {
|
||||
source: src2, goal: goal2, direction: known("direction"), resolution: known("resolution"),
|
||||
}),
|
||||
};
|
||||
|
||||
// daily trend (form submissions only, since imports have no date)
|
||||
const byDate = groupBy(runs.filter((r) => r.date), "date");
|
||||
res.daily = sortedKeys(byDate).map((d) => {
|
||||
const g = byDate[d];
|
||||
const m = g.filter(OUTCOMES.blue_meadow.denom), t = g.filter(OUTCOMES.shadow.denom);
|
||||
return { date: d, runs: g.length, meadow_n: m.length, blue_k: m.filter((r) => r.saw_blue_meadow).length, tree_n: t.length, shadow_k: t.filter((r) => r.saw_shadow).length };
|
||||
});
|
||||
|
||||
// routes (choices up to the hour) with outcome rates
|
||||
const R = groupBy(runs.filter((r) => r.hour === "Tree" || r.hour === "Valley"), (r) => routeKey(r).join(" › "));
|
||||
res.routes = sortedKeys(R).map((k) => {
|
||||
const g = R[k], m = g.filter(OUTCOMES.blue_meadow.denom), t = g.filter(OUTCOMES.shadow.denom);
|
||||
const o = { route: k, parts: routeKey(g[0]), n: g.length, blue_meadow: rate(m, (r) => r.saw_blue_meadow), shadow: rate(t, (r) => r.saw_shadow) };
|
||||
for (const c of COLORS) o["bonded_" + c] = rate(g, (r) => r.outcome === "Bonded" && r.outcome_color === c);
|
||||
o.bonded_any = rate(g, (r) => r.outcome === "Bonded");
|
||||
return o;
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- route-map graph
|
||||
const STAGES = [
|
||||
{ key: "company", label: "Company" },
|
||||
{ key: "direction", label: "Direction" },
|
||||
{ key: "first_sighting", label: "First sighting" },
|
||||
{ key: "cadet", label: "Cadet encounter" },
|
||||
{ key: "post_path", label: "Fork in the road" },
|
||||
{ key: "hour", label: "After an hour" },
|
||||
{ key: "encounter", label: "Encounter" },
|
||||
{ key: "end", label: "Outcome" },
|
||||
];
|
||||
const CHANCE_STAGES = new Set(["first_sighting", "encounter"]); // decided (partly) by the game, not the player
|
||||
|
||||
function stageValue(r, key) {
|
||||
switch (key) {
|
||||
case "company": return isMissing(r.company) ? null : r.company;
|
||||
case "direction": return isMissing(r.direction) ? null : r.direction;
|
||||
case "first_sighting": return r.scream === "DIED at first sighting" || isMissing(r.first_sighting) ? null : r.first_sighting;
|
||||
case "cadet":
|
||||
if (r.scream.startsWith("DIED") || isMissing(r.scream) || isMissing(r.resolution)) return null;
|
||||
return r.scream === "Investigate & help" ? "Help → " + r.resolution : r.scream + " → " + r.resolution;
|
||||
case "post_path": return isMissing(r.post_path) ? null : r.post_path;
|
||||
case "hour": return r.hour === "Tree" ? "Climb the tree" : r.hour === "Valley" ? "Valley centre" : null;
|
||||
case "encounter": {
|
||||
if (!r.branch) return null;
|
||||
if (r.hour === "Tree") {
|
||||
if (r.branch === "Shadow (rare)") return "Tree: shadow → Blue/Black";
|
||||
if (r.branch === "Descend quickly: Red") return "Tree: Red below";
|
||||
return "Tree: two Greens";
|
||||
}
|
||||
if (r.branch === "Valley Red") return "Valley: bond the Red";
|
||||
if (r.enc_Blue) return "Meadow: Blue";
|
||||
if (r.enc_Orange && r.enc_Brown) return "Meadow: Orange/Brown";
|
||||
if (r.enc_Orange) return "Meadow: Orange";
|
||||
if (r.enc_Brown) return "Meadow: Brown";
|
||||
return "Meadow (no attempt recorded)";
|
||||
}
|
||||
case "end":
|
||||
if (r.outcome === "Bonded") return "Bonded " + r.outcome_color;
|
||||
if (r.outcome === "Incinerated") return "Incinerated";
|
||||
if (r.outcome === "Died before the hour") return "Died before the hour";
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// pool = which runs could have reached this outcome at all (used by rankRoutes as the comparison group)
|
||||
const BOTH = (r) => r.hour === "Tree" || r.hour === "Valley";
|
||||
const TREE = (r) => r.hour === "Tree";
|
||||
const VALLEY = (r) => r.hour === "Valley";
|
||||
const TARGETS = {
|
||||
bond_Blue: { label: "Bond Blue", color: "Blue", fn: (r) => r.outcome === "Bonded" && r.outcome_color === "Blue", pool: VALLEY },
|
||||
bond_Black: { label: "Bond Black", color: "Black", fn: (r) => r.outcome === "Bonded" && r.outcome_color === "Black", pool: TREE },
|
||||
see_Blue: { label: "Meet a Blue (meadow)", color: "Blue", fn: (r) => r.saw_blue_meadow, pool: (r) => r.branch === "Passed the Red: meadow" },
|
||||
see_shadow: { label: "Get the tree shadow", color: "Black", fn: (r) => r.saw_shadow, pool: (r) => r.hour === "Tree" && r.branch !== null },
|
||||
bond_Red: { label: "Bond Red", color: "Red", fn: (r) => r.outcome === "Bonded" && r.outcome_color === "Red", pool: BOTH },
|
||||
bond_Green: { label: "Bond Green", color: "Green", fn: (r) => r.outcome === "Bonded" && r.outcome_color === "Green", pool: TREE },
|
||||
bond_Orange: { label: "Bond Orange", color: "Orange", fn: (r) => r.outcome === "Bonded" && r.outcome_color === "Orange", pool: BOTH },
|
||||
bond_Brown: { label: "Bond Brown", color: "Brown", fn: (r) => r.outcome === "Bonded" && r.outcome_color === "Brown", pool: BOTH },
|
||||
bond_any: { label: "Bond any dragon", color: null, fn: (r) => r.outcome === "Bonded", pool: BOTH },
|
||||
};
|
||||
|
||||
// conditions: { stageKey: Set(values) } — a run passes if, for every stage with a condition, its value is in the set.
|
||||
function buildGraph(runs, targetKey, conditions = {}) {
|
||||
const T = TARGETS[targetKey].fn;
|
||||
const sel = runs.filter((r) => Object.entries(conditions).every(([k, set]) => !set.size || set.has(stageValue(r, k))));
|
||||
const nodes = new Map(), links = new Map();
|
||||
const node = (si, v) => {
|
||||
const id = si + "|" + v;
|
||||
if (!nodes.has(id)) nodes.set(id, { id, stage: si, stageKey: STAGES[si].key, value: v, n: 0, k: 0 });
|
||||
return nodes.get(id);
|
||||
};
|
||||
for (const r of sel) {
|
||||
const hit = T(r) ? 1 : 0;
|
||||
let prev = null;
|
||||
STAGES.forEach((s, si) => {
|
||||
const v = stageValue(r, s.key);
|
||||
if (v === null || v === undefined) return;
|
||||
const nd = node(si, v);
|
||||
nd.n++; nd.k += hit;
|
||||
if (prev) {
|
||||
const id = prev.id + "→" + nd.id;
|
||||
if (!links.has(id)) links.set(id, { id, source: prev.id, target: nd.id, n: 0, k: 0, chance: CHANCE_STAGES.has(s.key) });
|
||||
const l = links.get(id); l.n++; l.k += hit;
|
||||
}
|
||||
prev = nd;
|
||||
});
|
||||
}
|
||||
const out = (o) => Object.assign(o, wilson(o.k, o.n));
|
||||
return {
|
||||
stages: STAGES, selected: sel.length, target: targetKey,
|
||||
nodes: [...nodes.values()].map(out), links: [...links.values()].map(out),
|
||||
};
|
||||
}
|
||||
|
||||
// Rank full routes (every choice up to the hour) for a target outcome.
|
||||
// Each route with at least minN runs is compared with all other runs by Fisher's exact test;
|
||||
// q-values use Benjamini-Hochberg across all ranked routes. Ranking uses the lower 95% Wilson
|
||||
// bound, so a route needs both a high rate and enough runs to rank high.
|
||||
function rankRoutes(runs, targetKey, minN = 10, denomStage = null) {
|
||||
const T = TARGETS[targetKey].fn;
|
||||
const keyStages = STAGES.slice(0, 6).map((s) => s.key);
|
||||
const pool = runs.filter(TARGETS[targetKey].pool);
|
||||
const totK = pool.filter(T).length, totN = pool.length;
|
||||
// a route is only ranked from runs whose every step is known; runs with a gap still count in the comparison group
|
||||
const groups = groupBy(pool.filter((r) => keyStages.every((k) => stageValue(r, k) || (k === "post_path" && r.post_path === null))),
|
||||
(r) => keyStages.map((k) => stageValue(r, k) || "—").join(" › "));
|
||||
const rows = Object.entries(groups).filter(([, g]) => g.length >= minN).map(([key, g]) => {
|
||||
const k = g.filter(T).length, n = g.length;
|
||||
const o = Object.assign({ route: key, values: keyStages.map((s) => stageValue(g[0], s)) }, wilson(k, n));
|
||||
Object.assign(o, oddsRatio(k, n - k, totK - k, totN - n - (totK - k)));
|
||||
// same comparison, adjusted for data source / chase-goal stratum (reporting bias)
|
||||
const inRoute = new Set(g);
|
||||
const tabs = Object.values(groupBy(pool, stratum)).map((sg) => {
|
||||
const a = sg.filter((r) => inRoute.has(r) && T(r)).length, nin = sg.filter((r) => inRoute.has(r)).length;
|
||||
const c = sg.filter((r) => !inRoute.has(r) && T(r)).length, nout = sg.length - nin;
|
||||
return [a, nin - a, c, nout - c];
|
||||
});
|
||||
Object.assign(o, mantelHaenszel(tabs));
|
||||
return o;
|
||||
});
|
||||
bh(rows.map((r) => r.fisher_p)).forEach((q, i) => (rows[i].q = q));
|
||||
const mhIdx = rows.map((r, i) => [r.mh_p, i]).filter(([p]) => p !== null);
|
||||
bh(mhIdx.map(([p]) => p)).forEach((q, j) => (rows[mhIdx[j][1]].mh_q = q));
|
||||
rows.sort((a, b) => b.lo - a.lo || b.n - a.n);
|
||||
return { target: targetKey, overall: wilson(totK, totN), minN, n_routes_tested: rows.length, rows };
|
||||
}
|
||||
|
||||
return {
|
||||
COLORS, MISSING, isMissing, COLUMN_KEYS, STAGES, TARGETS, OUTCOMES, FACTORS, ROUTE_FIELDS, rankRoutes,
|
||||
findColumns, codeRow, codeRows, parseTimestamp,
|
||||
wilson, fisher, oddsRatio, bh, logisticRidge, mantelHaenszel,
|
||||
applyFilters, analyse, factorTable, model, buildGraph, stageValue, stratum,
|
||||
};
|
||||
});
|
||||
+225
@@ -0,0 +1,225 @@
|
||||
/*
|
||||
* graph.js: the route graph shared by every data source.
|
||||
*
|
||||
* Two levels:
|
||||
* 1. Scene graph (bot data): nodes are exact game scenes (fingerprints), edges are (scene, option) -> next scene
|
||||
* or a terminal (BURNED, BONDED:<colour>). Chance shows up as one option leading to several scenes,
|
||||
* e.g. "head toward the centre" -> a Red OR a Brown first sighting.
|
||||
* 2. Decision-variable graph (all sources): the coarse variables the community form asks about
|
||||
* (company, direction, first sighting, ...). Bot trajectories are mapped onto it with CROSSWALK, so bot and
|
||||
* form runs can be counted side by side, per source.
|
||||
*
|
||||
* Trajectories keep the full ordered history of each attempt, so history-dependent (non-Markov) models can be fitted
|
||||
* later without changing the data format.
|
||||
*
|
||||
* UMD: window.ThreshGraph in the browser, module.exports in Node. No dependencies.
|
||||
*/
|
||||
(function (root, factory) {
|
||||
if (typeof module === "object" && module.exports) module.exports = factory();
|
||||
else root.ThreshGraph = factory();
|
||||
})(typeof self !== "undefined" ? self : this, function () {
|
||||
"use strict";
|
||||
|
||||
const norm = (t) => String(t || "").toLowerCase()
|
||||
.replace(/[‘’‛`']/g, "'").replace(/[–—-]/g, " ")
|
||||
.replace(/[^a-z0-9'\s]/g, " ").replace(/\s+/g, " ").trim();
|
||||
const cap = (s) => (s ? s[0].toUpperCase() + s.slice(1).toLowerCase() : s);
|
||||
|
||||
/* ---------- Beta posterior and credible interval ---------- */
|
||||
|
||||
function logGamma(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);
|
||||
}
|
||||
function betacf(a, b, x) {
|
||||
const MAXIT = 300, EPS = 3e-14, FPMIN = 1e-300;
|
||||
let qab = a + b, qap = a + 1, qam = a - 1, c = 1, d = 1 - qab * x / qap;
|
||||
if (Math.abs(d) < FPMIN) d = FPMIN;
|
||||
d = 1 / d; let h = d;
|
||||
for (let m = 1; m <= MAXIT; m++) {
|
||||
const m2 = 2 * m;
|
||||
let aa = m * (b - m) * x / ((qam + m2) * (a + m2));
|
||||
d = 1 + aa * d; if (Math.abs(d) < FPMIN) d = FPMIN;
|
||||
c = 1 + aa / c; if (Math.abs(c) < FPMIN) c = FPMIN;
|
||||
d = 1 / d; h *= d * c;
|
||||
aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2));
|
||||
d = 1 + aa * d; if (Math.abs(d) < FPMIN) d = FPMIN;
|
||||
c = 1 + aa / c; if (Math.abs(c) < FPMIN) c = FPMIN;
|
||||
d = 1 / d; const del = d * c; h *= del;
|
||||
if (Math.abs(del - 1) < EPS) break;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
/** Regularised incomplete beta I_x(a, b). */
|
||||
function ibeta(x, a, b) {
|
||||
if (x <= 0) return 0;
|
||||
if (x >= 1) return 1;
|
||||
const bt = Math.exp(logGamma(a + b) - logGamma(a) - logGamma(b) + a * Math.log(x) + b * Math.log(1 - x));
|
||||
return x < (a + 1) / (a + b + 2) ? bt * betacf(a, b, x) / a : 1 - bt * betacf(b, a, 1 - x) / b;
|
||||
}
|
||||
function betaQuantile(p, a, b) {
|
||||
let lo = 0, hi = 1;
|
||||
for (let i = 0; i < 80; i++) {
|
||||
const mid = (lo + hi) / 2;
|
||||
if (ibeta(mid, a, b) < p) lo = mid; else hi = mid;
|
||||
}
|
||||
return (lo + hi) / 2;
|
||||
}
|
||||
/**
|
||||
* Posterior for a proportion with k successes in n trials under a Beta(a0, b0) prior
|
||||
* (default Jeffreys, a0 = b0 = 0.5). Returns the mean and the equal-tailed 95% credible interval.
|
||||
*/
|
||||
function betaPosterior(k, n, a0 = 0.5, b0 = 0.5) {
|
||||
const a = a0 + k, b = b0 + n - k;
|
||||
return { k, n, mean: a / (a + b), lo: betaQuantile(0.025, a, b), hi: betaQuantile(0.975, a, b) };
|
||||
}
|
||||
|
||||
/* ---------- Crosswalk: bot option text -> community form variables ---------- */
|
||||
|
||||
const CROSSWALK = [
|
||||
[/^enter the valley alone/, "company", "Alone"],
|
||||
[/^enter the valley with one of your squadmates/, "company", "1 Squadmate"],
|
||||
[/^enter the valley with two of your squadmates/, "company", "2 Squadmates"],
|
||||
[/^head east toward the roar/, "direction", "East"],
|
||||
[/^head west toward the scent/, "direction", "West"],
|
||||
[/^head toward the center of the forest/, "direction", "Center"],
|
||||
[/^keep walking the cadets are just as dangerous/, "scream", "Ignore the scream"],
|
||||
[/^run and hope/, "sighting_action", "Retreat / run"],
|
||||
[/hold still/, "sighting_action", "Hold still"],
|
||||
[/lower your eyes in respect|lower your eyes and turn to face it/, "sighting_action", "Respect / face it"],
|
||||
[/meeting its gaze/, "sighting_action", "Meet its gaze"],
|
||||
[/^hide behind a tree/, "sighting_action", "Hide"],
|
||||
[/^investigate the scream/, "scream_investigate", true],
|
||||
[/^help your squadmate/, "scream", "Investigate & help"],
|
||||
[/^play it safe and leave them/, "scream", "Investigate & leave"],
|
||||
[/^pause to assess the situation/, "combat_first", "Assess the situation"],
|
||||
[/^charge the other cadet/, "combat_first", "Even the numbers (smaller cadet)"],
|
||||
[/^unsheathe your weapon and charge between/, "combat_first", "Put yourself in the middle (bigger cadet)"],
|
||||
[/^engage the smaller cadet/, "combat_second", "Engage the smaller cadet"],
|
||||
[/^daggers or not/, "combat_second", "Daggers or not (protect squadmate)"],
|
||||
[/^end the lives of the bullies/, "resolution", "Killed the bullies yourself"],
|
||||
[/^allow your squadmate to exact/, "resolution", "Squadmate's revenge"],
|
||||
[/^leave the bullies alive/, "resolution", "Left bullies to the dragons"],
|
||||
[/tourniquet/, "resolution", "Tourniquet"],
|
||||
[/^walk toward the east/, "post_path", "East"],
|
||||
[/^walk toward the west/, "post_path", "West"],
|
||||
[/^walk farther into the forest/, "post_path", "Forest"],
|
||||
[/^continue toward the center of the valley/, "hour", "Valley"],
|
||||
[/^find the tallest tree/, "hour", "Tree"],
|
||||
[/^descend quickly/, "branch", "Descend quickly: Red"],
|
||||
[/^climb down slowly to pursue the green/, "branch", "Descend slowly: two Greens"],
|
||||
[/^reds are too hot tempered/, "red", "Let it pass (two Greens)"],
|
||||
[/^jump (now|in a few seconds)/, "red", "Jump onto the Red"]
|
||||
];
|
||||
|
||||
/** Map one bot trajectory onto the form's decision variables. Unmapped choices are listed, not guessed. */
|
||||
function trajectoryVariables(traj) {
|
||||
const vars = {}, unmapped = [];
|
||||
for (const s of traj.steps) {
|
||||
const t = norm(s.optionText);
|
||||
const hit = CROSSWALK.find(([re]) => re.test(t));
|
||||
if (hit) { if (!(hit[1] in vars)) vars[hit[1]] = hit[2]; }
|
||||
else unmapped.push(s.optionText);
|
||||
// first sighting = the first tinted scene after the direction choice
|
||||
if (vars.direction && !vars.first_sighting && s.tint) vars.first_sighting = cap(s.tint);
|
||||
}
|
||||
const o = traj.outcome || {};
|
||||
vars.outcome = o.type === "BONDED" ? "Bonded" : o.type === "BURNED" ? "Incinerated" : "No outcome recorded";
|
||||
vars.outcome_color = o.type === "BONDED" && o.colour ? cap(o.colour) : null;
|
||||
return { vars, unmapped };
|
||||
}
|
||||
|
||||
/** A coded community-form run (engine.codeRow output) in the same shape. */
|
||||
function formRunVariables(run) {
|
||||
const keep = ["company", "left_squadmates", "direction", "first_sighting", "sighting_action", "scream", "combat_first", "combat_second", "resolution", "post_path", "hour", "branch", "outcome", "outcome_color"];
|
||||
const vars = {};
|
||||
for (const k of keep) vars[k] = run[k] == null ? null : run[k];
|
||||
return vars;
|
||||
}
|
||||
|
||||
/* ---------- Decision-variable graph (all sources) ---------- */
|
||||
|
||||
const VAR_STAGES = ["company", "direction", "first_sighting", "scream", "resolution", "post_path", "hour", "branch"];
|
||||
// "(no fork)" is a real state (that path has no fork), but it is not a choice node, so it is skipped too
|
||||
const MISSING = new Set([null, undefined, "", "Unknown", "Unsure", "Unsure/unknown", "Multiple", "(no fork)", "(not recorded)", "(not asked)", "(not asked/blank)", "Unknown/lost count"]);
|
||||
|
||||
function endLabel(v) {
|
||||
if (v.outcome === "Bonded") return v.outcome_color ? `Bonded ${v.outcome_color}` : "Bonded (colour unknown)";
|
||||
if (v.outcome === "Incinerated" || v.outcome === "Died before the hour") return "Incinerated";
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* records: [{ source, vars }]. Returns nodes keyed "stage=value" and edges between consecutive KNOWN stages,
|
||||
* each with total and per-source counts, plus P(next | this) with a Beta credible interval.
|
||||
*/
|
||||
function buildVariableGraph(records, stages = VAR_STAGES) {
|
||||
const nodes = {}, edges = {};
|
||||
const bump = (obj, key, src, init) => {
|
||||
const x = obj[key] || (obj[key] = Object.assign({ n: 0, bySource: {} }, init));
|
||||
x.n++; x.bySource[src] = (x.bySource[src] || 0) + 1;
|
||||
return x;
|
||||
};
|
||||
for (const { source, vars } of records) {
|
||||
const path = [];
|
||||
for (const st of stages) if (!MISSING.has(vars[st])) path.push(`${st}=${vars[st]}`);
|
||||
const end = endLabel(vars);
|
||||
if (end) path.push(`end=${end}`);
|
||||
path.forEach((id, i) => {
|
||||
const [stage, value] = id.split("=");
|
||||
bump(nodes, id, source, { stage, value });
|
||||
if (i) bump(edges, `${path[i - 1]}>${id}`, source, { from: path[i - 1], to: id });
|
||||
});
|
||||
}
|
||||
const out = {};
|
||||
for (const e of Object.values(edges)) (out[e.from] = out[e.from] || []).push(e);
|
||||
for (const [from, list] of Object.entries(out)) {
|
||||
const tot = list.reduce((s, e) => s + e.n, 0);
|
||||
for (const e of list) e.p = betaPosterior(e.n, tot);
|
||||
nodes[from].out = tot;
|
||||
}
|
||||
return { stages: stages.concat("end"), nodes, edges: Object.values(edges) };
|
||||
}
|
||||
|
||||
/* ---------- Scene graph (bot) ---------- */
|
||||
|
||||
/**
|
||||
* scenes: catalogue { id: { stage, tint, label, options: [{ id, text }] } }; trajs: bot trajectories.
|
||||
* For every scene option: how often it was taken, where it led, P(incinerated) and P(survived) with
|
||||
* credible intervals. Options never taken are kept and marked untested.
|
||||
*/
|
||||
function buildSceneGraph(scenes, trajs) {
|
||||
const g = {};
|
||||
for (const [id, s] of Object.entries(scenes)) {
|
||||
g[id] = { id, stage: s.stage, tint: s.tint, label: s.label, visits: 0, options: {} };
|
||||
for (const o of s.options) g[id].options[o.id] = { id: o.id, text: o.text, n: 0, to: {}, bySource: {}, intentionalBurns: 0 };
|
||||
}
|
||||
for (const t of trajs) {
|
||||
for (const s of t.steps) {
|
||||
const node = g[s.scene] || (g[s.scene] = { id: s.scene, stage: s.stage, tint: s.tint, label: s.stage, visits: 0, options: {} });
|
||||
node.visits++;
|
||||
const o = node.options[s.option] || (node.options[s.option] = { id: s.option, text: s.optionText, n: 0, to: {}, bySource: {}, intentionalBurns: 0 });
|
||||
o.n++;
|
||||
o.bySource[t.source] = (o.bySource[t.source] || 0) + 1;
|
||||
const to = s.to || "UNKNOWN";
|
||||
o.to[to] = (o.to[to] || 0) + 1;
|
||||
if (to === "BURNED" && t.outcome && t.outcome.intentional) o.intentionalBurns++;
|
||||
}
|
||||
}
|
||||
for (const node of Object.values(g)) {
|
||||
for (const o of Object.values(node.options)) {
|
||||
const burns = o.to.BURNED || 0;
|
||||
const known = o.n - (o.to.UNKNOWN || 0);
|
||||
o.untested = o.n === 0;
|
||||
o.pBurn = known ? betaPosterior(burns, known) : null;
|
||||
o.chance = Object.keys(o.to).filter((k) => k !== "BURNED" && k !== "UNKNOWN").length > 1;
|
||||
}
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
return { betaPosterior, betaQuantile, ibeta, CROSSWALK, trajectoryVariables, formRunVariables, VAR_STAGES, buildVariableGraph, buildSceneGraph, norm };
|
||||
});
|
||||
@@ -0,0 +1,402 @@
|
||||
"""
|
||||
clean.py — turn the raw "Threshing Routes Responses" Google Form export into one
|
||||
coded row per threshing run.
|
||||
|
||||
Reference implementation in Python. The web page (core/engine.js) implements the
|
||||
same rules in JavaScript; tests/parity/compare_runs.py checks that both give identical output.
|
||||
|
||||
Usage:
|
||||
python clean.py ../../data/raw/form/Threshing_Routes_Responses.xlsx -> writes ../../data/derived/runs_clean.csv
|
||||
|
||||
Every coding rule is listed in RULES below and in the "Methods" section of the page.
|
||||
"""
|
||||
import re
|
||||
import sys
|
||||
from datetime import datetime
|
||||
|
||||
import pandas as pd
|
||||
|
||||
EMAIL_RE = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}")
|
||||
|
||||
|
||||
def redact(v):
|
||||
"""Free-text answers sometimes contain a respondent's email address; never display or export it."""
|
||||
return v if v is None else EMAIL_RE.sub("[email removed]", str(v))
|
||||
|
||||
COLORS = ["Red", "Green", "Orange", "Brown", "Blue", "Black"]
|
||||
|
||||
# Columns are found by keywords in the question text, so the script keeps working
|
||||
# if the form owner reorders or adds questions.
|
||||
COLUMN_KEYS = {
|
||||
"timestamp": ["timestamp"],
|
||||
"wing_First": ["wing and section", "[first]"],
|
||||
"wing_Second": ["wing and section", "[second]"],
|
||||
"wing_Third": ["wing and section", "[third]"],
|
||||
"wing_Fourth": ["wing and section", "[fourth]"],
|
||||
"squad": ["what squad"],
|
||||
"attempt": ["attempt number"],
|
||||
"company": ["who accompanied"],
|
||||
"dir_East": ["initial direction", "[east"],
|
||||
"dir_West": ["initial direction", "[west"],
|
||||
"dir_Center": ["initial direction", "[forest center"],
|
||||
"cadet": ["outcome of your cadet encounter"],
|
||||
"post": ["path did you take after the bully"],
|
||||
"hour": ["after walking an hour"],
|
||||
"enc_Red": ["attempt to bond", "[red"],
|
||||
"enc_Green": ["attempt to bond", "[green"],
|
||||
"enc_Orange": ["attempt to bond", "[orange"],
|
||||
"enc_Brown": ["attempt to bond", "[brown"],
|
||||
"enc_Blue": ["attempt to bond", "[blue"],
|
||||
"enc_Black": ["attempt to bond", "[black"],
|
||||
"death_note": ["those who died"],
|
||||
"bond_note": ["those who bonded"],
|
||||
"goal": ["colour dragon are you trying"],
|
||||
"combat": ["combat choice"],
|
||||
# Questions added in the redesigned form (late October 2026). "!x" means the header must NOT contain x.
|
||||
"first_dir": ["what was your first direction"],
|
||||
"first_colour": ["colour was your initial dragon sighting", "!["],
|
||||
"leg_West": ["colour was your initial dragon sighting", "[west"],
|
||||
"leg_Center": ["colour was your initial dragon sighting", "[forest center"],
|
||||
"sighting_action": ["action did you take during the first dragon sighting"],
|
||||
"cadet_actions": ["during your cadet encounter"],
|
||||
"goal_v2": ["why are you doing this run"],
|
||||
"malek": ["seek malek"],
|
||||
"rare": ["encounter a rare dragon"],
|
||||
"tail": ["what tail does your dragon have"],
|
||||
}
|
||||
# A key is only reported missing when neither it nor its replacement in the other form version was found.
|
||||
ALTERNATIVE = {
|
||||
"dir_East": "first_dir", "dir_West": "first_dir", "dir_Center": "first_dir", "first_dir": "dir_Center",
|
||||
"first_colour": "dir_Center", "goal": "goal_v2", "goal_v2": "goal", "combat": "cadet_actions", "cadet_actions": "combat",
|
||||
}
|
||||
OPTIONAL = {"leg_West", "leg_Center", "sighting_action", "malek", "rare", "tail"}
|
||||
|
||||
CADET_MAP = {
|
||||
"Bullies > You dealt with them": ("Investigate & help", "Killed the bullies yourself"),
|
||||
"Bullies > Squadmate's revenge": ("Investigate & help", "Squadmate's revenge"),
|
||||
"Bullies > Left to the Dragons": ("Investigate & help", "Left bullies to the dragons"),
|
||||
"Bullies > Assessed the situation": ("Investigate & help", "Unknown"),
|
||||
"Injured Cadet > Tourniquet": ("Ignore the scream", "Tourniquet"),
|
||||
"Injured Cadet > Left them there": ("Ignore the scream", "Left injured cadet"),
|
||||
"Injured Cadet > Took their weapons": ("Ignore the scream", "Took their weapons"),
|
||||
"Avoided combat (Investigated scream > Left them to it)": ("Investigate & leave", "Left them to it"),
|
||||
"You died in combat": ("Investigate & help", "DIED in combat"),
|
||||
"N/A - You died with initial dragon sighting": ("DIED at first sighting", "DIED at first sighting"),
|
||||
}
|
||||
COMBAT_MAP = {
|
||||
"Bullies > Assessed the situation": "Assess the situation",
|
||||
"Bullies > Even the numbers (Rushed the smaller cadet)": "Even the numbers (smaller cadet)",
|
||||
"Bullies > Put yourself in the middle (Rushed the bigger cadet attacking your squadmate)": "Put yourself in the middle (bigger cadet)",
|
||||
}
|
||||
# Redesigned form: the cadet encounter is asked as a path of actions plus a separate outcome.
|
||||
# value: (scream, first combat move, second combat move, resolution when the action decides it)
|
||||
CADET_ACTIONS = {
|
||||
"Bullies > Assess > Unknown": ("Investigate & help", "Assess the situation", None, None),
|
||||
"Bullies > Assess > Smaller Cadet": ("Investigate & help", "Assess the situation", "Engage the smaller cadet", None),
|
||||
"Bullies > Assess > Daggers or not": ("Investigate & help", "Assess the situation", "Daggers or not (protect squadmate)", None),
|
||||
"Bullies > Unknown": ("Investigate & help", None, None, None),
|
||||
"Bullies > Smaller Cadet (Did not assess)": ("Investigate & help", "Even the numbers (smaller cadet)", None, None),
|
||||
"Bullies > Bigger Cadet (Did not assess)": ("Investigate & help", "Put yourself in the middle (bigger cadet)", None, None),
|
||||
"Injured Cadet > Tourniquet": ("Ignore the scream", None, None, "Tourniquet"),
|
||||
"Injured Cadet > Left them there": ("Ignore the scream", None, None, "Left injured cadet"),
|
||||
"Injured Cadet > Took their weapons": ("Ignore the scream", None, None, "Took their weapons"),
|
||||
"Avoided combat (Investigated scream > Left them to it)": ("Investigate & leave", None, None, "Left them to it"),
|
||||
"N/A - You died with initial dragon sighting": ("DIED at first sighting", None, None, "DIED at first sighting"),
|
||||
"Unsure": ("Unknown", None, None, "Unknown"),
|
||||
}
|
||||
CADET_OUTCOME = {
|
||||
"Squadmate's revenge": "Squadmate's revenge",
|
||||
"You ended the bullies yourself": "Killed the bullies yourself",
|
||||
"Left to the Dragons": "Left bullies to the dragons",
|
||||
"You died in combat": "DIED in combat",
|
||||
}
|
||||
DIRECTION_MAP = {"Forest Center": "Center", "West / Scent": "West", "East / Roar": "East"}
|
||||
GOAL_V2 = {
|
||||
"Hoping for a Rare Dragon (Blue or Black)": "Blue or Black",
|
||||
"Trying to bond the Red dragon under the tree": "Red (tree)",
|
||||
"Daily Run Assignment data": "Daily run data",
|
||||
"Just here for fun, and maybe a dragon": "Any (for fun)",
|
||||
}
|
||||
SIGHTING_ACTION = {
|
||||
"Hold Ground / Stay Still": "Hold still", "Respect / Face it": "Respect / face it",
|
||||
"Retreat / Run / Back Away": "Retreat / run", "Hide / Duck": "Hide", "Draw / Drop Weapon": "Weapon",
|
||||
"Meet Gaze": "Meet its gaze", "Protect / Help Sqaudmate": "Protect squadmate", "Approach": "Approach",
|
||||
"Stand on your own": "Stand alone", "Unsure": "Unknown", "Unsure - You don't remember": "Unknown",
|
||||
}
|
||||
RARE_MAP = {"Yes - A blue dragon in the valley": "Blue (valley)", "Yes - Shadow!!!": "Shadow", "No": "No"}
|
||||
|
||||
HOUR_MAP = {
|
||||
"Continued to Valley Centre": "Valley",
|
||||
"Climbed the tallest tree": "Tree",
|
||||
"N/A - You're dead": "DIED before the hour",
|
||||
}
|
||||
# First-sighting pools per (company, direction) according to the community PDF map.
|
||||
# Used only to FLAG rows that report a dragon outside the documented pool.
|
||||
SIGHTING_POOL = {
|
||||
("Alone", "East"): {"Red", "Orange"},
|
||||
("Alone", "West"): {"Red", "Brown", "Green"},
|
||||
("Alone", "Center"): {"Red", "Brown"},
|
||||
("1 Squadmate", "East"): {"Green", "Orange", "Red"}, # 'leave squadmate' -> alone pools also possible
|
||||
("1 Squadmate", "West"): {"Red", "Brown", "Green"},
|
||||
("1 Squadmate", "Center"): {"Red", "Brown"},
|
||||
("2 Squadmates", "East"): {"Red", "Orange"},
|
||||
("2 Squadmates", "West"): {"Green", "Brown", "Red"},
|
||||
("2 Squadmates", "Center"): {"Red", "Brown"},
|
||||
}
|
||||
|
||||
|
||||
def find_columns(cols):
|
||||
out = {}
|
||||
low = {c: str(c).lower() for c in cols}
|
||||
for key, words in COLUMN_KEYS.items():
|
||||
hits = [c for c in cols if all((w[1:] not in low[c]) if w[0] == "!" else (w in low[c]) for w in words)]
|
||||
if hits:
|
||||
out[key] = hits[0]
|
||||
missing = [k for k in COLUMN_KEYS if k not in out and k not in OPTIONAL and ALTERNATIVE.get(k) not in out]
|
||||
return out, missing
|
||||
|
||||
|
||||
def blank(v):
|
||||
if v is None or v is pd.NaT:
|
||||
return True
|
||||
if not isinstance(v, str):
|
||||
try:
|
||||
if pd.isna(v):
|
||||
return True
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
return str(v).strip() in ("", "nan", "NaN", "NaT")
|
||||
|
||||
|
||||
def s(v):
|
||||
return None if blank(v) else str(v).strip()
|
||||
|
||||
|
||||
def parse_timestamp(v):
|
||||
"""Form submissions carry a timestamp; bulk-pasted rows carry a label such as 'Data Import'.
|
||||
Returns 'YYYY-MM-DD' or None."""
|
||||
if isinstance(v, (datetime, pd.Timestamp)) and not pd.isna(v):
|
||||
return pd.Timestamp(v).strftime("%Y-%m-%d")
|
||||
if isinstance(v, (int, float)) and not isinstance(v, bool) and 40000 < v < 80000: # Excel serial date
|
||||
return (pd.Timestamp("1899-12-30") + pd.Timedelta(days=int(v))).strftime("%Y-%m-%d")
|
||||
t = s(v)
|
||||
if not t:
|
||||
return None
|
||||
m = re.match(r"^(\d{4})-(\d{2})-(\d{2})", t)
|
||||
if m:
|
||||
return f"{m.group(1)}-{m.group(2)}-{m.group(3)}"
|
||||
m = re.match(r"^(\d{1,2})/(\d{1,2})/(\d{4})", t) # Google Sheets CSV export: M/D/YYYY
|
||||
if m:
|
||||
return f"{m.group(3)}-{int(m.group(1)):02d}-{int(m.group(2)):02d}"
|
||||
return None
|
||||
|
||||
|
||||
def parse_attempt(v):
|
||||
v = s(v)
|
||||
if v is None:
|
||||
return None
|
||||
t = v.lower()
|
||||
if "lost count" in t:
|
||||
return None
|
||||
words = {"first": 1, "second": 2, "third": 3, "fourth": 4, "fifth": 5}
|
||||
for w, n in words.items():
|
||||
if t.startswith(w):
|
||||
return n
|
||||
m = re.search(r"\d+", t)
|
||||
return int(m.group()) if m else None
|
||||
|
||||
|
||||
def code_row(r, C):
|
||||
g = lambda k: s(r[C[k]]) if k in C else None
|
||||
flags = []
|
||||
ts = r[C["timestamp"]] if "timestamp" in C else None
|
||||
d = parse_timestamp(ts)
|
||||
if d:
|
||||
source, date = "Form", d
|
||||
else:
|
||||
source, date = (s(ts) or "Unknown import"), None
|
||||
|
||||
wings = [w for w in ("First", "Second", "Third", "Fourth") if g("wing_" + w)]
|
||||
wing = wings[0] if len(wings) == 1 else ("Multiple" if wings else "Unknown")
|
||||
section = g("wing_" + wings[0]) if len(wings) == 1 else None
|
||||
if len(wings) > 1:
|
||||
flags.append("multiple wings")
|
||||
|
||||
if "first_dir" in C:
|
||||
# Redesigned form: one direction question and one colour question. The older per-direction colour
|
||||
# columns are still in the sheet for some rows and are used only to fill gaps and to cross-check.
|
||||
direction = DIRECTION_MAP.get(g("first_dir"))
|
||||
first = g("first_colour")
|
||||
legacy = [d for d in ("West", "Center") if g("leg_" + d)]
|
||||
if not direction and len(legacy) == 1:
|
||||
direction = legacy[0]
|
||||
flags.append("direction taken from the older sighting column")
|
||||
leg = g("leg_" + direction) if direction else None
|
||||
if not first and leg:
|
||||
first = leg
|
||||
elif first and leg and first != leg and first != "Unsure" and leg != "Unsure":
|
||||
flags.append("first sighting differs between the old and new columns")
|
||||
if not direction:
|
||||
direction = "Unknown"
|
||||
flags.append("direction missing")
|
||||
else:
|
||||
dirs = [d for d in ("East", "West", "Center") if g("dir_" + d)]
|
||||
if len(dirs) == 1:
|
||||
direction, first = dirs[0], g("dir_" + dirs[0])
|
||||
else:
|
||||
direction, first = ("Multiple" if dirs else "Unknown"), None
|
||||
flags.append("direction " + ("multiple" if dirs else "missing"))
|
||||
|
||||
company_raw = g("company") or "Unknown"
|
||||
left_squad = company_raw.endswith(" > Left them")
|
||||
company = company_raw[: -len(" > Left them")] if left_squad else company_raw
|
||||
# Leaving your squadmates sends you into the solo pools, so either pool is plausible.
|
||||
pool = list(SIGHTING_POOL.get((company, direction), [])) + (list(SIGHTING_POOL.get(("Alone", direction), [])) if left_squad else [])
|
||||
if first and first != "Unsure" and pool and first not in pool:
|
||||
flags.append(f"first sighting {first} not in documented pool for {company}/{direction}")
|
||||
|
||||
cadet_raw = g("cadet")
|
||||
combat = combat2 = None
|
||||
if "cadet_actions" in C:
|
||||
act = g("cadet_actions")
|
||||
a = CADET_ACTIONS.get(act, ("Unknown", None, None, "Unknown"))
|
||||
if act and act not in CADET_ACTIONS:
|
||||
flags.append("unrecognised cadet answer")
|
||||
scream, combat, combat2 = a[0], a[1], a[2]
|
||||
out = CADET_OUTCOME.get(cadet_raw)
|
||||
if a[3] is not None:
|
||||
resolution = a[3]
|
||||
if out == "DIED in combat" and not resolution.startswith("DIED"):
|
||||
flags.append("died in combat on a path without combat")
|
||||
else:
|
||||
resolution = out or "Unknown"
|
||||
else:
|
||||
scream, resolution = CADET_MAP.get(cadet_raw, ("Unknown", "Unknown"))
|
||||
if cadet_raw and cadet_raw not in CADET_MAP:
|
||||
flags.append("unrecognised cadet answer")
|
||||
combat = COMBAT_MAP.get(g("combat")) if g("combat") else None
|
||||
|
||||
post_raw = g("post")
|
||||
if scream == "Investigate & help" and not resolution.startswith("DIED"):
|
||||
post = post_raw if post_raw in ("Forest", "West", "East") else "Unknown"
|
||||
elif scream in ("Ignore the scream", "Investigate & leave"):
|
||||
post = "(no fork)"
|
||||
if post_raw in ("Forest", "West", "East"):
|
||||
post = "(no fork)" # the form allowed an answer here; the game has no fork on this branch
|
||||
else:
|
||||
post = None
|
||||
|
||||
hour = HOUR_MAP.get(g("hour"))
|
||||
died_early = scream.startswith("DIED") or resolution.startswith("DIED")
|
||||
if died_early:
|
||||
hour = None
|
||||
elif hour is None:
|
||||
hour = "Unknown"
|
||||
|
||||
enc = {c: g("enc_" + c) for c in COLORS}
|
||||
rare = RARE_MAP.get(g("rare"))
|
||||
bonded = [c for c in COLORS if enc[c] == "Bonded!"]
|
||||
burned = [c for c in COLORS if enc[c] == "Incinerated"]
|
||||
|
||||
# Which encounter branch did the run take after the hour?
|
||||
branch = None
|
||||
if hour == "Tree":
|
||||
if enc["Blue"] or enc["Black"] or rare == "Shadow":
|
||||
branch = "Shadow (rare)"
|
||||
elif enc["Green"]:
|
||||
branch = "Descend slowly: two Greens"
|
||||
elif enc["Red"]:
|
||||
branch = "Descend quickly: Red"
|
||||
elif enc["Orange"] or enc["Brown"]:
|
||||
branch = "Descend slowly: two Greens"
|
||||
flags.append("tree run with Orange/Brown but no Green recorded")
|
||||
elif hour == "Valley":
|
||||
if enc["Green"]:
|
||||
flags.append("Green recorded on a Valley run (no Greens in the valley)")
|
||||
red_passed = enc["Red"] in ("Changed my mind",)
|
||||
meadow = [c for c in ("Orange", "Brown", "Blue") if enc[c]] or rare == "Blue (valley)"
|
||||
if meadow or red_passed:
|
||||
branch = "Passed the Red: meadow"
|
||||
elif enc["Red"]:
|
||||
branch = "Valley Red"
|
||||
|
||||
# Final outcome of the run
|
||||
outcome, outcome_color = None, None
|
||||
if died_early:
|
||||
outcome = "Died before the hour"
|
||||
elif len(bonded) == 1:
|
||||
outcome, outcome_color = "Bonded", bonded[0]
|
||||
elif len(bonded) > 1:
|
||||
outcome = "Ambiguous"
|
||||
flags.append("more than one dragon marked Bonded")
|
||||
elif burned:
|
||||
order = ["Red", "Green", "Orange", "Brown", "Blue", "Black"] if hour != "Valley" else \
|
||||
["Red", "Orange", "Brown", "Blue", "Green", "Black"]
|
||||
last = [c for c in order if c in burned][-1]
|
||||
outcome, outcome_color = "Incinerated", last
|
||||
if len(burned) > 1:
|
||||
flags.append("more than one dragon marked Incinerated")
|
||||
elif hour == "DIED before the hour":
|
||||
outcome = "Died before the hour"
|
||||
else:
|
||||
outcome = "No outcome recorded"
|
||||
|
||||
# The dragon whose bonding encounter decided the run (attempted = Bonded or Incinerated)
|
||||
rescued = enc["Green"] == "Saved by another" and outcome == "Bonded" and outcome_color in ("Orange", "Brown")
|
||||
|
||||
attempt = parse_attempt(g("attempt"))
|
||||
if (rare == "Blue (valley)" and hour == "Tree") or (rare == "Shadow" and hour == "Valley"):
|
||||
flags.append("rare-dragon answer does not match the path")
|
||||
goal_raw = g("goal_v2")
|
||||
goal = g("goal") or (GOAL_V2.get(goal_raw, goal_raw) if goal_raw and goal_raw != "Unknown" else None) or "(not asked/blank)"
|
||||
return {
|
||||
"source": source, "date": date, "wing": wing, "section": section,
|
||||
"squad": g("squad") or "Unknown", "attempt": attempt,
|
||||
# "I've lost count" means many attempts, so it is a later attempt, not missing
|
||||
"attempt_group": "First attempt" if attempt == 1 else ("Later attempt" if (attempt and attempt > 1) or "lost count" in (g("attempt") or "").lower() else "Unknown"),
|
||||
"goal": goal, "company": company, "left_squadmates": left_squad,
|
||||
"direction": direction, "first_sighting": first or "Unknown",
|
||||
"sighting_action": (SIGHTING_ACTION.get(g("sighting_action")) or g("sighting_action") or "Unknown") if "sighting_action" in C else "(not asked)",
|
||||
"scream": scream, "resolution": resolution, "combat_first": combat or "(not recorded)",
|
||||
"combat_second": combat2 or "(not recorded)",
|
||||
"post_path": post, "hour": hour, "branch": branch,
|
||||
**{"enc_" + c: enc[c] for c in COLORS},
|
||||
"outcome": outcome, "outcome_color": outcome_color, "rescued": rescued,
|
||||
"saw_blue_meadow": bool(hour == "Valley" and (enc["Blue"] or rare == "Blue (valley)")),
|
||||
"saw_shadow": bool(hour == "Tree" and (enc["Blue"] or enc["Black"] or rare == "Shadow")),
|
||||
"rare_reported": (rare or "Unknown") if "rare" in C else "(not asked)",
|
||||
"malek_intent": (g("malek") or "Unknown") if "malek" in C else "(not asked)",
|
||||
"tail": g("tail"),
|
||||
"data_flags": "; ".join(flags),
|
||||
"death_note": redact(g("death_note")), "bond_note": redact(g("bond_note")),
|
||||
}
|
||||
|
||||
|
||||
def load(path):
|
||||
if str(path).lower().endswith(".json"): # the page's bundled export (data/public/form_responses.json)
|
||||
import json
|
||||
d = json.load(open(path, encoding="utf-8"))
|
||||
raw = pd.DataFrame(d["rows"], columns=d["headers"])
|
||||
elif str(path).lower().endswith(".csv"):
|
||||
raw = pd.read_csv(path)
|
||||
else:
|
||||
xl = pd.ExcelFile(path)
|
||||
sheet = next((n for n in xl.sheet_names if "form responses" in n.lower()), xl.sheet_names[0])
|
||||
raw = pd.read_excel(xl, sheet)
|
||||
C, missing = find_columns(raw.columns)
|
||||
rows = [code_row(r, C) for _, r in raw.iterrows()]
|
||||
df = pd.DataFrame(rows)
|
||||
df.insert(0, "run_id", range(1, len(df) + 1))
|
||||
return df, missing
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
src = sys.argv[1] if len(sys.argv) > 1 else "../../data/public/form_responses.json"
|
||||
out = sys.argv[2] if len(sys.argv) > 2 else "../../data/derived/runs_clean.csv"
|
||||
df, missing = load(src)
|
||||
if missing:
|
||||
print("WARNING: columns not found:", missing)
|
||||
df.to_csv(out, index=False)
|
||||
print(f"{len(df)} runs -> {out}")
|
||||
print(df.outcome.value_counts().to_string())
|
||||
print("rows with flags:", (df["data_flags"] != "").sum())
|
||||
+874
@@ -0,0 +1,874 @@
|
||||
/*
|
||||
* 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, the cadet encounter in the game's order (the scream: investigate or keep walking; the
|
||||
* clearing: help your squadmate or walk away; your first move in the fight and, after pausing, the dagger; the bullies' fate; or the wounded cadet), 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 };
|
||||
// the resolution reached so far, whichever way the history was written down
|
||||
const cadetOf = (h) => h.cadet || h.fate || h.wounded || (h.clearing === "Walk away" ? "Left them to it" : null);
|
||||
const screamOf = (h) => h.scream || (cadetOf(h) ? (IGNORE[cadetOf(h)] ? "Keep walking" : "Investigate the scream") : null);
|
||||
const clearingOf = (h) => h.clearing || (cadetOf(h) && !IGNORE[cadetOf(h)] ? (cadetOf(h) === "Left them to it" ? "Walk away" : "Help your squadmate") : null);
|
||||
const DECISIONS = {
|
||||
company: ["Alone", "1 Squadmate", "2 Squadmates"],
|
||||
// entering with squadmates, you can later leave them and go on alone
|
||||
leave: ["Stay together", "Leave them"],
|
||||
direction: ["East", "West", "Center"],
|
||||
// how you react when the first dragon appears (the form's categories; which ones are offered depends on the dragon)
|
||||
react: ["Hold still", "Respect / face it", "Retreat / run", "Hide", "Weapon", "Meet its gaze", "Protect squadmate"],
|
||||
cadet: ["Killed the bullies yourself", "Squadmate's revenge", "Left bullies to the dragons",
|
||||
"Tourniquet", "Left injured cadet", "Took their weapons", "Left them to it"],
|
||||
// the cadet encounter, in the game's own order: the scream, then (if you investigate) the clearing, then the
|
||||
// bullies' fate, or (if you keep walking) the wounded cadet
|
||||
scream: ["Investigate the scream", "Keep walking"],
|
||||
clearing: ["Help your squadmate", "Walk away"],
|
||||
// the fight itself: your first move, and (if you paused to assess) what to do about the smaller cadet's dagger
|
||||
fight: ["Pause to assess", "Charge the smaller cadet", "Charge between the bigger cadet and your squadmate"],
|
||||
dagger: ["Engage the smaller cadet", "Charge the bigger cadet anyway"],
|
||||
fate: ["Killed the bullies yourself", "Squadmate's revenge", "Left bullies to the dragons"],
|
||||
wounded: ["Tourniquet", "Left injured cadet", "Took their weapons"],
|
||||
post: ["Forest", "West", "East"],
|
||||
hour: ["Tree", "Valley"],
|
||||
descend: ["Quickly (Red)", "Slowly (two Greens)"],
|
||||
// descending quickly puts you above a Red: jump onto it, or let it pass and go on to the two Greens
|
||||
red: ["Jump onto the Red", "Let it pass (two Greens)"],
|
||||
valley: ["Face the Red", "Pass the Red (meadow)"],
|
||||
encounter: ["Attempt the bond", "Die on purpose"],
|
||||
};
|
||||
const LABEL = {
|
||||
company: "Company", leave: "Your squadmates", direction: "First direction", react: "Facing the first dragon", red: "The Red below the tree", fs: "First dragon", dieSight: "Survive the first dragon",
|
||||
cadet: "Cadet encounter", scream: "The scream", clearing: "The clearing (your squadmate vs two bullies)",
|
||||
fight: "The fight: your first move", dagger: "The smaller cadet's dagger", fate: "The bullies' fate", wounded: "The wounded cadet", 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",
|
||||
};
|
||||
// community-form codes for the fight moves -> the solver's option names
|
||||
const FIGHT_OF = { "Assess the situation": "Pause to assess", "Even the numbers (smaller cadet)": "Charge the smaller cadet",
|
||||
"Put yourself in the middle (bigger cadet)": "Charge between the bigger cadet and your squadmate" };
|
||||
const DAGGER_OF = { "Engage the smaller cadet": "Engage the smaller cadet", "Daggers or not (protect squadmate)": "Charge the bigger cadet anyway" };
|
||||
const NO_DAGGER = "(no dagger choice)";
|
||||
// the fight moves in a history; a run that charged straight in never faces the dagger
|
||||
const daggerAfter = (fight, dagger) => (fight == null ? null : fight === "Pause to assess" ? dagger || null : NO_DAGGER);
|
||||
const ALONE = "(alone)";
|
||||
const leftOf = (h) => (h.company === "Alone" ? ALONE : h.left || null);
|
||||
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.left = v.company === "Alone" ? ALONE : v.company ? (String(r.left_squadmates) === "true" || r.left_squadmates === true ? "Leave them" : "Stay together") : null;
|
||||
v.react = DECISIONS.react.includes(r.sighting_action) ? r.sighting_action : 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.fight = FIGHT_OF[r.combat_first] || null;
|
||||
v.dagger = daggerAfter(v.fight, DAGGER_OF[r.combat_second]);
|
||||
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)";
|
||||
// a rider who let the Red pass and met the Greens instead
|
||||
if (r.enc_Red === "Changed my mind" && r.enc_Green) { v.red = "Let it pass (two Greens)"; v.encCtx = "tree-Greens"; }
|
||||
else { v.red = "Jump onto the 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", "left"], get: (v) => v.fs },
|
||||
dieSight: { cats: ["die", "live"], ctx: ["fs", "direction", "company", "left", "react"], get: (v) => (v.fs ? (v.diedSight ? "die" : "live") : null) },
|
||||
dieCombat: { cats: ["die", "live"], ctx: ["fight", "dagger", "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 ---------- */
|
||||
|
||||
// x = P(target colour), o = P(another colour), r = P(reaching a bonding encounter alive)
|
||||
const ZERO = { x: 0, o: 0, r: 0 };
|
||||
const REACHED = { x: 0, o: 0, r: 1 };
|
||||
const add = (a, b, w) => ({ x: a.x + w * b.x, o: a.o + w * b.o, r: a.r + w * b.r });
|
||||
|
||||
/**
|
||||
* 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, only = null } = {}) {
|
||||
// only: bond only at these encounters (e.g. ["meadow-Blue"]); everywhere else the rider dies on purpose
|
||||
const encOptions = (ctx) => (only && !only.includes(ctx) ? ["Die on purpose"] : support.encounter);
|
||||
const reactOptions = (fs) => support["react:" + fs] || DECISIONS.react;
|
||||
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 }, encOptions(ctx), (o) => {
|
||||
if (o === "Die on purpose") return REACHED;
|
||||
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, r: 1 };
|
||||
});
|
||||
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) => o.startsWith("Quickly")
|
||||
? best("red", { ...h, shadow: "no", descend: o }, support.red, (rd) => encounter({ ...h, shadow: "no", descend: o, red: rd }, rd.startsWith("Jump") ? "tree-Red" : "tree-Greens"))
|
||||
: encounter({ ...h, shadow: "no", descend: o }, "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("scream", h, support.scream, (sc) => {
|
||||
const h1 = { ...h, scream: sc };
|
||||
if (sc === "Keep walking") return best("wounded", h1, support.wounded, (o) => hour({ ...h1, cadet: o, post: "(no fork)" }));
|
||||
return best("clearing", h1, support.clearing, (cl) => {
|
||||
const h2 = { ...h1, clearing: cl };
|
||||
if (cl === "Walk away") return hour({ ...h2, cadet: "Left them to it", post: "(no fork)" });
|
||||
const combat = (hf) => mix(params.chance("dieCombat", hf), (s) => s === "die" ? die() :
|
||||
best("fate", hf, support.fate, (o) => { const h3 = { ...h2, cadet: o }; return best("post", h3, support.post, (p) => hour({ ...h3, post: p })); }));
|
||||
return best("fight", h2, support.fight, (f) => f === "Pause to assess"
|
||||
? best("dagger", { ...h2, fight: f }, support.dagger, (d) => combat({ ...h2, fight: f, dagger: d }))
|
||||
: combat({ ...h2, fight: f, dagger: NO_DAGGER }));
|
||||
});
|
||||
});
|
||||
// the first dragon: which one appears (chance), how you react (decision), whether you survive (chance).
|
||||
// Leaving your squadmates and your reaction only matter here, so the rest of the history leaves them out.
|
||||
const sighting = (h) => mix(params.chance("fs", h), (c) => {
|
||||
const h2 = { ...h, fs: c }, rest = { company: h.company, direction: h.direction, fs: c };
|
||||
return best("react", h2, reactOptions(c), (re) =>
|
||||
mix(params.chance("dieSight", { ...h2, react: re }), (s) => (s === "die" ? die() : cadet(rest))));
|
||||
});
|
||||
const company = (c, f) => (c === "Alone" ? f({ company: c, left: ALONE }) : best("leave", { company: c }, support.leave, (l) => f({ company: c, left: l })));
|
||||
const root = best("company", {}, support.company, (c) => company(c, (hc) =>
|
||||
best("direction", hc, support.direction, (d) => sighting({ ...hc, direction: d }))));
|
||||
|
||||
memo.clear(); // the values are no longer needed; posterior draws keep only their choices
|
||||
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 REACHED;
|
||||
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, r: 1 };
|
||||
});
|
||||
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) => o.startsWith("Quickly")
|
||||
? pick("red", { ...h, shadow: "no", descend: o }, (rd) => encounter({ ...h, shadow: "no", descend: o, red: rd }, rd.startsWith("Jump") ? "tree-Red" : "tree-Greens"))
|
||||
: encounter({ ...h, shadow: "no", descend: o }, "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("scream", h, (sc) => {
|
||||
const h1 = { ...h, scream: sc };
|
||||
if (sc === "Keep walking") return pick("wounded", h1, (o) => hour({ ...h1, cadet: o, post: "(no fork)" }));
|
||||
return pick("clearing", h1, (cl) => {
|
||||
const h2 = { ...h1, clearing: cl };
|
||||
if (cl === "Walk away") return hour({ ...h2, cadet: "Left them to it", post: "(no fork)" });
|
||||
const combat = (hf) => mix(params.chance("dieCombat", hf), (s) => s === "die" ? ZERO :
|
||||
pick("fate", hf, (o) => { const h3 = { ...h2, cadet: o }; return pick("post", h3, (p) => hour({ ...h3, post: p })); }));
|
||||
return pick("fight", h2, (f) => f === "Pause to assess"
|
||||
? pick("dagger", { ...h2, fight: f }, (d) => combat({ ...h2, fight: f, dagger: d }))
|
||||
: combat({ ...h2, fight: f, dagger: NO_DAGGER }));
|
||||
});
|
||||
});
|
||||
const sighting = (h) => mix(params.chance("fs", h), (c) => {
|
||||
const h2 = { ...h, fs: c }, rest = { company: h.company, direction: h.direction, fs: c };
|
||||
return pick("react", h2, (re) => mix(params.chance("dieSight", { ...h2, react: re }), (s) => (s === "die" ? ZERO : cadet(rest))));
|
||||
});
|
||||
const company = (c, f) => (c === "Alone" ? f({ company: c, left: ALONE }) : pick("leave", { company: c }, (l) => f({ company: c, left: l })));
|
||||
return pick("company", {}, (c) => company(c, (hc) => pick("direction", hc, (d) => sighting({ ...hc, 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, pReach: v.r });
|
||||
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) => o.startsWith("Quickly")
|
||||
? decision("red", { ...h, shadow: "no", descend: o }, (rd) => encounter({ ...h, shadow: "no", descend: o, red: rd }, rd.startsWith("Jump") ? "tree-Red" : "tree-Greens"))
|
||||
: encounter({ ...h, shadow: "no", descend: o }, "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("scream", h, (sc) => {
|
||||
const h1 = { ...h, scream: sc };
|
||||
if (sc === "Keep walking") return decision("wounded", h1, (o) => hour({ ...h1, cadet: o, post: "(no fork)" }));
|
||||
return decision("clearing", h1, (cl) => {
|
||||
const h2 = { ...h1, clearing: cl };
|
||||
if (cl === "Walk away") return hour({ ...h2, cadet: "Left them to it", post: "(no fork)" });
|
||||
const combat = (hf) => chance("dieCombat", params.chance("dieCombat", hf), (s) => s === "die" ? end("killed in the fight") :
|
||||
decision("fate", hf, (o) => { const h3 = { ...h2, cadet: o }; return decision("post", h3, (p) => hour({ ...h3, post: p })); }));
|
||||
return decision("fight", h2, (f) => f === "Pause to assess"
|
||||
? decision("dagger", { ...h2, fight: f }, (d) => combat({ ...h2, fight: f, dagger: d }))
|
||||
: combat({ ...h2, fight: f, dagger: NO_DAGGER }));
|
||||
});
|
||||
});
|
||||
const sighting = (h) => chance("fs", params.chance("fs", h), (c) => {
|
||||
const h2 = { ...h, fs: c }, rest = { company: h.company, direction: h.direction, fs: c };
|
||||
return decision("react", h2, (re) => chance("dieSight", params.chance("dieSight", { ...h2, react: re }), (s) => (s === "die" ? end("incinerated at the first sighting") : cadet(rest))));
|
||||
});
|
||||
const company = (c, f) => (c === "Alone" ? f({ company: c, left: ALONE }) : decision("leave", { company: c }, (l) => f({ company: c, left: l })));
|
||||
return decision("company", {}, (c) => company(c, (hc) => decision("direction", hc, (d) => sighting({ ...hc, 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.left && v.left !== ALONE) bump("leave", v.left);
|
||||
if (v.fs && v.react) bump("react:" + v.fs, v.react);
|
||||
if (v.post && v.post !== "(no fork)") bump("post", v.post);
|
||||
// the cadet encounter's steps, including riders killed in the fight (they chose to help)
|
||||
if (v.help || v.cadet) {
|
||||
const ignore = !!(v.cadet && IGNORE[v.cadet]);
|
||||
bump("scream", ignore ? "Keep walking" : "Investigate the scream");
|
||||
if (ignore) bump("wounded", v.cadet);
|
||||
else {
|
||||
bump("clearing", v.cadet === "Left them to it" ? "Walk away" : "Help your squadmate");
|
||||
if (HELP[v.cadet]) bump("fate", v.cadet);
|
||||
if (v.help && v.fight) { bump("fight", v.fight); if (v.dagger && v.dagger !== NO_DAGGER) bump("dagger", v.dagger); }
|
||||
}
|
||||
}
|
||||
}
|
||||
const s = { encounter: DECISIONS.encounter };
|
||||
for (const k of ["company", "leave", "direction", "cadet", "scream", "clearing", "fight", "dagger", "fate", "wounded", "post", "hour", "descend", "valley"])
|
||||
{
|
||||
s[k] = DECISIONS[k].filter((o) => ((n[k] || {})[o] || 0) >= min);
|
||||
// a step the data never record (e.g. the fight moves in an old export) is not restricted
|
||||
if (!s[k].length) s[k] = DECISIONS[k].slice();
|
||||
}
|
||||
// reactions to the first dragon, per dragon (each dragon offers its own options)
|
||||
for (const fs of ["Red", "Brown", "Green", "Orange"]) {
|
||||
const k = "react:" + fs, ok = DECISIONS.react.filter((o) => ((n[k] || {})[o] || 0) >= min);
|
||||
s[k] = ok.length ? ok : DECISIONS.react.slice();
|
||||
}
|
||||
// letting the Red pass is rarely logged, but its outcome is the two Greens' encounter, which has plenty of data
|
||||
s.red = DECISIONS.red.slice();
|
||||
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 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: { "Investigate the scream": /^investigate the scream/, "Keep walking": /^keep walking/ },
|
||||
clearing: { "Help your squadmate": /^help your squadmate/, "Walk away": /^play it safe and leave them/ },
|
||||
fight: { "Pause to assess": /^pause to assess/, "Charge the smaller cadet": /^charge the other cadet/,
|
||||
"Charge between the bigger cadet and your squadmate": /^unsheathe your weapon/ },
|
||||
dagger: { "Engage the smaller cadet": /^engage the smaller cadet/, "Charge the bigger cadet anyway": /^daggers or not/ },
|
||||
react: { "Hold still": /hold still/, "Respect / face it": /in respect|turn to face it/, "Retreat / run": /^run and hope/,
|
||||
"Hide": /^hide/, "Meet its gaze": /meeting its gaze/ },
|
||||
red: { "Jump onto the Red": /^jump/, "Let it pass (two Greens)": /^reds are too hot tempered/ },
|
||||
};
|
||||
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 (h.company && h.company !== "Alone" && vars.left_squadmates != null) h.left = vars.left_squadmates === true || vars.left_squadmates === "true" ? "Leave them" : "Stay together";
|
||||
if (DECISIONS.direction.includes(vars.direction)) h.direction = vars.direction;
|
||||
if (DECISIONS.react.includes(vars.sighting_action)) h.react = vars.sighting_action;
|
||||
if (["Red", "Brown", "Green", "Orange"].includes(vars.first_sighting)) h.fs = vars.first_sighting;
|
||||
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 (FIGHT_OF[vars.combat_first]) h.fight = FIGHT_OF[vars.combat_first];
|
||||
if (DAGGER_OF[vars.combat_second]) h.dagger = DAGGER_OF[vars.combat_second];
|
||||
if (DECISIONS.cadet.includes(vars.resolution)) {
|
||||
h.cadet = vars.resolution;
|
||||
if (HELP[h.cadet]) h.fate = h.cadet;
|
||||
if (IGNORE[h.cadet]) h.wounded = h.cadet;
|
||||
}
|
||||
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 (DECISIONS.red.includes(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;
|
||||
}
|
||||
|
||||
/** 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", {}];
|
||||
const left = leftOf(h);
|
||||
if (!left) return ["leave", { company: h.company }];
|
||||
if (!h.direction) return ["direction", { company: h.company, left }];
|
||||
if (!h.fs) return null;
|
||||
const a = { company: h.company, direction: h.direction, fs: h.fs };
|
||||
const sc = screamOf(h), cad = cadetOf(h);
|
||||
// a history past the first dragon without a recorded reaction: no advice is needed for it any more
|
||||
if (!h.react && !sc) return ["react", { company: h.company, left, direction: h.direction, fs: h.fs }];
|
||||
if (!sc) return ["scream", a];
|
||||
const h1 = { ...a, scream: sc };
|
||||
let c;
|
||||
if (sc === "Keep walking") {
|
||||
if (!cad) return ["wounded", h1];
|
||||
c = { ...h1, cadet: cad, post: "(no fork)" };
|
||||
} else {
|
||||
const cl = clearingOf(h);
|
||||
if (!cl) return ["clearing", h1];
|
||||
const h2 = { ...h1, clearing: cl };
|
||||
if (cl === "Walk away") c = { ...h2, cadet: "Left them to it", post: "(no fork)" };
|
||||
else {
|
||||
// older records may skip the fight moves; assume the usual ones (pause, then engage) so advice can continue
|
||||
const f = h.fight || (cad ? "Pause to assess" : null);
|
||||
if (!f) return ["fight", h2];
|
||||
const dg = daggerAfter(f, h.dagger || (cad ? "Engage the smaller cadet" : null));
|
||||
if (!dg) return ["dagger", { ...h2, fight: f }];
|
||||
const hf = { ...h2, fight: f, dagger: dg };
|
||||
if (!cad) return ["fate", hf];
|
||||
const h3 = { ...h2, cadet: cad };
|
||||
if (!h.post) return ["post", h3];
|
||||
c = { ...h3, post: h.post };
|
||||
}
|
||||
}
|
||||
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" }];
|
||||
if (h.descend.startsWith("Quickly")) {
|
||||
if (!h.red) return ["red", { ...d, shadow: "no", descend: h.descend }];
|
||||
return ["encounter", { ...d, shadow: "no", descend: h.descend, red: h.red, enc: h.red.startsWith("Jump") ? "tree-Red" : "tree-Greens" }];
|
||||
}
|
||||
return ["encounter", { ...d, shadow: "no", descend: h.descend, enc: "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", { only = null } = {}) {
|
||||
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, only }); return solve(params, X, { support: m.support, lam: r.lam, record: true, only }); })()
|
||||
: solve(params, X, { support: m.support, record: true, only });
|
||||
const opts = (name, key) => Object.fromEntries(Object.entries(sol.optionValues(name, key) || {}).map(([o, v]) => [o, { pX: v.x, pOther: v.o, pReach: v.r }]));
|
||||
const dec = (name, key, extra = {}) => ({ kind: "decision", var: name, label: LABEL[name], choice: sol.decide(name, key), options: opts(name, key), ...extra });
|
||||
const cha = (name, key, extra = {}) => ({ kind: "chance", var: name, label: LABEL[name], probs: params.chance(name, key), ...extra });
|
||||
/**
|
||||
* Everything the planner needs at history h: the next decision (every option with P(target), P(other colour)
|
||||
* and P(reaching an encounter alive), each followed by the best play afterwards), or the next chance event with
|
||||
* its probabilities, or the end of the run with the encounter's outcome distribution. `risk` is the chance of
|
||||
* being killed at the step just survived. h may also hold `encounter` ("Attempt the bond" / "Die on purpose").
|
||||
*/
|
||||
function step(h) {
|
||||
if (!h.company) return dec("company", {});
|
||||
const left = leftOf(h);
|
||||
if (!left) return dec("leave", { company: h.company });
|
||||
if (!h.direction) return dec("direction", { company: h.company, left });
|
||||
const a0 = { company: h.company, left, direction: h.direction };
|
||||
if (!h.fs) return cha("fs", a0);
|
||||
const sc = screamOf(h), cad = cadetOf(h);
|
||||
if (!h.react && !sc) return dec("react", { ...a0, fs: h.fs });
|
||||
const riskSight = params.chance("dieSight", { ...a0, fs: h.fs, react: h.react || undefined }).die;
|
||||
const a = { company: h.company, direction: h.direction, fs: h.fs };
|
||||
if (!sc) return dec("scream", a, { risk: { label: "Killed at the first sighting", p: riskSight } });
|
||||
const h1 = { ...a, scream: sc };
|
||||
let c;
|
||||
if (sc === "Keep walking") {
|
||||
if (!cad) return dec("wounded", h1);
|
||||
c = { ...h1, cadet: cad, post: "(no fork)" };
|
||||
} else {
|
||||
const cl = clearingOf(h);
|
||||
if (!cl) return dec("clearing", h1);
|
||||
const h2 = { ...h1, clearing: cl };
|
||||
if (cl === "Walk away") c = { ...h2, cadet: "Left them to it", post: "(no fork)" };
|
||||
else {
|
||||
const f = h.fight || (cad ? "Pause to assess" : null);
|
||||
if (!f) return dec("fight", h2);
|
||||
const dg = daggerAfter(f, h.dagger || (cad ? "Engage the smaller cadet" : null));
|
||||
if (!dg) return dec("dagger", { ...h2, fight: f });
|
||||
const hf = { ...h2, fight: f, dagger: dg };
|
||||
if (!cad) return dec("fate", hf, { risk: { label: "Killed in the fight", p: params.chance("dieCombat", hf).die } });
|
||||
// nothing after the fight depends on its moves, so the rest of the history leaves them out (shared subtree)
|
||||
const h3 = { ...h2, cadet: cad };
|
||||
if (!h.post) return dec("post", h3);
|
||||
c = { ...h3, post: h.post };
|
||||
}
|
||||
}
|
||||
const riskHour = params.chance("dieHour", c).die;
|
||||
if (!h.hour) return dec("hour", c, { risk: { label: "Killed before the hour", p: riskHour } });
|
||||
const d = { ...c, hour: h.hour };
|
||||
let enc = null, key = null;
|
||||
if (h.hour === "Tree") {
|
||||
if (!h.shadow) return cha("shadow", d);
|
||||
if (h.shadow === "yes") {
|
||||
if (!h.shadowColour) return cha("shadowColour", d);
|
||||
enc = "shadow-" + h.shadowColour; key = { ...d, shadow: "yes", enc };
|
||||
} else {
|
||||
if (!h.descend) return dec("descend", { ...d, shadow: "no" });
|
||||
if (h.descend.startsWith("Quickly")) {
|
||||
if (!h.red) return dec("red", { ...d, shadow: "no", descend: h.descend });
|
||||
enc = h.red.startsWith("Jump") ? "tree-Red" : "tree-Greens"; key = { ...d, shadow: "no", descend: h.descend, red: h.red, enc };
|
||||
} else { enc = "tree-Greens"; key = { ...d, shadow: "no", descend: h.descend, enc }; }
|
||||
}
|
||||
} else {
|
||||
if (!h.valley) return dec("valley", d);
|
||||
if (h.valley === "Face the Red") { enc = "valley-Red"; key = { ...d, valley: h.valley, enc }; }
|
||||
else {
|
||||
if (!h.meadow) return cha("meadow", d);
|
||||
enc = "meadow-" + h.meadow; key = { ...d, valley: h.valley, meadow: h.meadow, enc };
|
||||
}
|
||||
}
|
||||
if (!h.encounter) return dec("encounter", key, { encounter: enc });
|
||||
return { kind: "end", encounter: enc, action: h.encounter, outcomes: h.encounter === "Die on purpose" ? { burn: 1 } : params.encounter(enc) };
|
||||
}
|
||||
return {
|
||||
colour: X, objective, only, value: sol.value, step,
|
||||
/** 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),
|
||||
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, pReach: v.r }])) };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 === "fate" || rec.decision === "wounded") {
|
||||
idx = find(OPTION_PATTERNS.resolution[rec.choice] || /$^/);
|
||||
} 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}` };
|
||||
}
|
||||
|
||||
/** Where each colour can be bonded (encounter contexts). The Greens can also end in an Orange or Brown rescue. */
|
||||
const ENCOUNTERS = {
|
||||
Red: ["valley-Red", "tree-Red"], Orange: ["meadow-Orange", "tree-Greens"], Brown: ["meadow-Brown", "tree-Greens"],
|
||||
Green: ["tree-Greens"], Blue: ["meadow-Blue", "shadow-Blue"], Black: ["shadow-Black"],
|
||||
};
|
||||
|
||||
return { ENCOUNTERS, COLOURS, DECISIONS, CHANCE, prepareRun, countTables, makeParams, fitAlpha, solverHistory, nextDecision, advisor, solve, solveRatio, evaluate, buildPolicy, locate, recommendOption, genuineAttempt };
|
||||
});
|
||||
Reference in New Issue
Block a user