Route planner: the solver's options and odds drawn on the valley map
- New "Route Planner" tab: pick a dragon colour, a goal (only this dragon / best odds this run) and a data sample. The solver runs in the browser from policy.json and walks the valley: every decision shows each option's chance of bonding the colour (with the best play afterwards) and of reaching an encounter alive; every roll shows its probabilities; deaths show where they happen; "stays best in N% of re-runs" and "no clear winner" flag noisy recommendations. Click any option on the map or in the list to explore another branch. - solver.js: values now carry P(reaching an encounter alive); advisor.step() gives the planner the next decision, roll or outcome at any history. - valley.js shares its painting with the planner. - Fix: route-map target chips no longer capture clicks on other chip rows. - Pages build gets a doctype and charset. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d2ffb84b5e
commit
bdf66afabc
@@ -272,8 +272,10 @@
|
||||
|
||||
/* ---------- 3. The decision tree and backward induction ---------- */
|
||||
|
||||
const ZERO = { x: 0, o: 0 };
|
||||
const add = (a, b, w) => ({ x: a.x + w * b.x, o: a.o + w * b.o });
|
||||
// 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)).
|
||||
@@ -305,11 +307,11 @@
|
||||
const die = () => ZERO;
|
||||
|
||||
const encounter = (h, ctx) => best("encounter", { ...h, enc: ctx }, support.encounter, (o) => {
|
||||
if (o === "Die on purpose") return ZERO;
|
||||
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 };
|
||||
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))
|
||||
@@ -355,11 +357,11 @@
|
||||
const pick = (name, h, f) => f(decide(name, h));
|
||||
const mix = (dist, f) => Object.entries(dist).reduce((acc, [k, p]) => (p ? add(acc, f(k), p) : acc), ZERO);
|
||||
const encounter = (h, ctx) => pick("encounter", { ...h, enc: ctx }, (o) => {
|
||||
if (o === "Die on purpose") return ZERO;
|
||||
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 };
|
||||
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))
|
||||
@@ -392,7 +394,7 @@
|
||||
function exportTree(params, X, sol, draws, minShow = 1e-4) {
|
||||
const vals = (name, h) => sol.optionValues(name, h) || {};
|
||||
const conf = (name, h, chosen) => (draws.length ? draws.filter((d) => d.decide(name, h) === chosen).length / draws.length : null);
|
||||
const fmt = (v) => ({ pX: v.x, pOther: v.o });
|
||||
const 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);
|
||||
@@ -605,15 +607,60 @@
|
||||
const sol = objective === "eventually"
|
||||
? (() => { const r = solveRatio(params, X, { support: m.support }); return solve(params, X, { support: m.support, lam: r.lam, record: true }); })()
|
||||
: solve(params, X, { support: m.support, record: true });
|
||||
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", {});
|
||||
if (!h.direction) return dec("direction", { company: h.company });
|
||||
const a0 = { company: h.company, direction: h.direction };
|
||||
if (!h.fs) return cha("fs", a0);
|
||||
const a = { ...a0, fs: h.fs };
|
||||
const riskSight = params.chance("dieSight", a).die;
|
||||
if (!h.cadet) return dec("cadet", a, { risk: { label: "Killed at the first sighting", p: riskSight } });
|
||||
const b = { ...a, cadet: h.cadet };
|
||||
const riskCombat = HELP[h.cadet] ? params.chance("dieCombat", b).die : 0;
|
||||
if (HELP[h.cadet] && !h.post) return dec("post", b, { risk: { label: "Killed in the fight", p: riskCombat } });
|
||||
const c = { ...b, post: HELP[h.cadet] ? h.post : "(no fork)" };
|
||||
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" });
|
||||
enc = h.descend.startsWith("Quickly") ? "tree-Red" : "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, value: sol.value,
|
||||
colour: X, objective, value: sol.value, step,
|
||||
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 }])) };
|
||||
options: Object.fromEntries(Object.entries(ov).map(([o, v]) => [o, { pX: v.x, pOther: v.o, pReach: v.r }])) };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user