- packages/core/src/solver.js: decision tree with perfect recall over the community form's choices; chance events (first dragon, deaths, shadow, meadow dragon, encounter outcome) depend on earlier choices through hierarchical Dirichlet back-off with empirical-Bayes concentration per event; backward induction for P(colour this attempt) and Dinkelbach for P(colour) / (P(colour) + P(other colour)); 200 posterior draws for credible intervals and "still best" rates; cross-fitted held-out check. - Only genuine attempts inform encounter outcomes (goal / Malek intent). - data/public/policy.json (all runs and a low-bias sample) and the readable docs/POLICY.md; published on Pages with solver.js for the bot. - Bot advisor: re-solves from any history, maps advice onto screen text, reports margins so ties can be randomised. docs/BOT_INTEGRATION.md. - Tests on a synthetic world with known answers; daily source update now rebuilds the policy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ
95 lines
5.3 KiB
JavaScript
95 lines
5.3 KiB
JavaScript
// node --test tests/solver.test.js
|
|
const test = require("node:test");
|
|
const assert = require("node:assert");
|
|
const S = require("../packages/core/src/solver");
|
|
|
|
// A synthetic world: the meadow shows Blue 30% of the time, the shadow appears 2% of the time,
|
|
// facing the valley Red always bonds it, the Greens burn half the time and bond Brown (rescue) a quarter of the time.
|
|
function world(n = 4000, seed = 7) {
|
|
let s = seed;
|
|
const r = () => ((s = (s * 1103515245 + 12345) % 2147483648) / 2147483648);
|
|
const runs = [];
|
|
for (let i = 0; i < n; i++) {
|
|
const run = { company: "Alone", direction: ["East", "West", "Center"][i % 3], first_sighting: ["Red", "Brown"][i % 2],
|
|
scream: "Investigate & help", resolution: ["Killed the bullies yourself", "Squadmate's revenge"][i % 2], post_path: "Forest",
|
|
goal: "Any (for fun)", malek_intent: "No", enc_Red: null, enc_Green: null, enc_Orange: null, enc_Brown: null, enc_Blue: null, enc_Black: null };
|
|
run.hour = r() < 0.5 ? "Tree" : "Valley";
|
|
if (run.hour === "Tree") {
|
|
if (r() < 0.02) { run.branch = "Shadow (rare)"; run.enc_Black = "Bonded!"; run.outcome = "Bonded"; run.outcome_color = "Black"; }
|
|
else if (r() < 0.5) { run.branch = "Descend quickly: Red"; run.enc_Red = "Incinerated"; run.outcome = "Incinerated"; run.outcome_color = "Red"; }
|
|
else {
|
|
run.branch = "Descend slowly: two Greens";
|
|
const u = r();
|
|
if (u < 0.5) { run.enc_Green = "Incinerated"; run.outcome = "Incinerated"; run.outcome_color = "Green"; }
|
|
else if (u < 0.75) { run.enc_Green = "Bonded!"; run.outcome = "Bonded"; run.outcome_color = "Green"; }
|
|
else { run.enc_Green = "Saved by another"; run.enc_Brown = "Bonded!"; run.outcome = "Bonded"; run.outcome_color = "Brown"; }
|
|
}
|
|
} else if (r() < 0.2) { run.branch = "Valley Red"; run.enc_Red = "Bonded!"; run.outcome = "Bonded"; run.outcome_color = "Red"; }
|
|
else {
|
|
run.branch = "Passed the Red: meadow";
|
|
const u = r(), c = u < 0.3 ? "Blue" : u < 0.65 ? "Orange" : "Brown";
|
|
const bond = r() < 0.5;
|
|
run["enc_" + c] = bond ? "Bonded!" : "Incinerated"; run.outcome = bond ? "Bonded" : "Incinerated"; run.outcome_color = c;
|
|
}
|
|
runs.push(run);
|
|
}
|
|
return runs;
|
|
}
|
|
|
|
const policy = S.buildPolicy(world(), { draws: 10, colours: ["Blue", "Black", "Red", "Brown"] });
|
|
const choice = (X, h, obj = "thisAttempt") => S.advisor(policy, X, obj).next(h).choice;
|
|
|
|
test("Blue: go to the valley, pass the Red, attempt only the Blue", () => {
|
|
const t = policy.targets.Blue.thisAttempt;
|
|
const h = { company: "Alone", direction: "Center", fs: "Red", cadet: "Killed the bullies yourself", post: "Forest" };
|
|
assert.strictEqual(choice("Blue", h), "Valley");
|
|
assert.strictEqual(choice("Blue", { ...h, hour: "Valley" }), "Pass the Red (meadow)");
|
|
const meadow = { ...h, hour: "Valley", valley: "Pass the Red (meadow)" };
|
|
assert.strictEqual(choice("Blue", { ...meadow, meadow: "Blue" }), "Attempt the bond");
|
|
assert.strictEqual(choice("Blue", { ...meadow, meadow: "Orange" }), "Die on purpose");
|
|
assert.strictEqual(S.locate(t.tree, {}).var, "company");
|
|
// 30% Blue in the meadow x 50% bond
|
|
assert.ok(Math.abs(t.pX.point - 0.15) < 0.03, t.pX.point);
|
|
});
|
|
|
|
test("Black: climb the tree; the value is about the shadow rate x bond rate", () => {
|
|
const t = policy.targets.Black.thisAttempt;
|
|
const h = { company: "Alone", direction: "East", fs: "Red", cadet: "Squadmate's revenge", post: "Forest" };
|
|
assert.strictEqual(choice("Black", h), "Tree");
|
|
assert.ok(t.pX.point > 0.01 && t.pX.point < 0.035, t.pX.point);
|
|
});
|
|
|
|
test("Red: face the valley Red", () => {
|
|
const h = { company: "Alone", direction: "West", fs: "Brown", cadet: "Killed the bullies yourself", post: "Forest", hour: "Valley" };
|
|
assert.strictEqual(choice("Red", h), "Face the Red");
|
|
});
|
|
|
|
test("Brown 'eventually' never risks a non-Brown bond that 'this attempt' accepts", () => {
|
|
const a = policy.targets.Brown.thisAttempt, e = policy.targets.Brown.eventually;
|
|
assert.ok(e.pOther.point <= a.pOther.point + 1e-12);
|
|
assert.ok(e.ratio.mean >= a.ratio.mean - 1e-9);
|
|
});
|
|
|
|
test("no history dependence in the data -> large fitted concentration", () => {
|
|
assert.ok(policy.model.alphas.meadow >= 500, JSON.stringify(policy.model.alphas));
|
|
});
|
|
|
|
test("recommendOption maps the policy onto the bot's screen text", () => {
|
|
const adv = S.advisor(policy, "Blue", "thisAttempt");
|
|
const rec = S.recommendOption(adv, { company: "Alone", direction: "Center", fs: "Red", cadet: "Killed the bullies yourself", post: "Forest" },
|
|
["Continue toward the center of the valley", "Find the tallest tree and climb for a better view"]);
|
|
assert.strictEqual(rec.index, 0);
|
|
const none = S.recommendOption(adv, {}, ["Something we have never seen"]);
|
|
assert.strictEqual(none.index, null);
|
|
});
|
|
|
|
test("advisor matches the solver's own decisions on the recommended route", () => {
|
|
const adv = S.advisor(policy, "Blue", "eventually");
|
|
assert.strictEqual(adv.next({}).choice, policy.targets.Blue.eventually.tree.choice);
|
|
});
|
|
|
|
test("solverHistory converts form variables", () => {
|
|
const h = S.solverHistory({ company: "Alone", direction: "Center", first_sighting: "Red", resolution: "Tourniquet", hour: "Tree", branch: "Descend slowly: two Greens" });
|
|
assert.deepStrictEqual(h, { company: "Alone", direction: "Center", fs: "Red", cadet: "Tourniquet", post: "(no fork)", hour: "Tree", descend: "Slowly (two Greens)", shadow: "no" });
|
|
});
|