Files
ekcSchianoandClaude Fable 5.2 f8e4f575f4
CI / test (pull_request) Canceled after 0s
Screen tree, register rows in the master list, and bot runs coded from the picks that were played
- analysis/tree/build_tree.py -> data/public/tree.json, analysis/reports/TREE.md: every screen the bot has seen,
  every option, where each led (fixed or dice, with ranges), best known picks per colour chosen cautiously, where
  any picks end, and the untested options. solver.js screenPick(): least-tried and Thompson picks from the tree.
- The bot logs a pick it then overrode as a step: 37 runs were coded with choices they never made. Importer and
  graph.js now use the played steps only.
- runs_clean.csv: the Unofficial Dragon Register's 288 bonded dragons are in the master list as outcome-only
  rows, flagged (no analysis uses them).
- Cycle, refresh and the bot kit rebuild and carry the tree; tests check every generated report is current.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014jqCC2LZ4x1WHQ1V57nTsv
2026-11-16 15:50:12 +00:00

88 lines
4.3 KiB
Python

"""The screen tree (analysis/tree/build_tree.py) on a small game whose answers are known."""
import sys
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "analysis/tree"))
import build_tree as B # noqa: E402
SCENES = {"start": {"label": "Start", "stage": "S", "options": [{"id": "a", "text": "Left"}, {"id": "b", "text": "Right"}, {"id": "c", "text": "Never tried"}]},
"meadow": {"label": "Meadow", "stage": "M", "options": [{"id": "x", "text": "Approach"}]},
"blue": {"label": "Blue", "stage": "B", "options": [{"id": "g", "text": "Hold its gaze"}, {"id": "l", "text": "Lower eyes"}]}}
def step(scene, option, to=None, stage="S"):
return {"scene": scene, "option": option, "optionText": option, "to": to, "stage": stage}
def run(i, steps, kind, colour=None):
return {"id": f"r{i}", "steps": steps, "outcome": {"type": kind, "colour": colour}}
def world():
T = []
for i in range(100): # Left: the meadow; a Blue waits there 1 time in 10; holding its gaze bonds 8 times in 10
s = [step("start", "a", "meadow")]
if i % 10 == 0:
s += [step("meadow", "x", "blue"), step("blue", "g", "BURNED" if i in (0, 50) else "BONDED:blue")]
T.append(run(i, s, "BURNED" if i in (0, 50) else "BONDED", None if i in (0, 50) else "blue"))
else:
s += [step("meadow", "x", "BURNED", stage="INTENTIONAL_BURN")]
T.append(run(i, s, "BURNED"))
T.append(run(200, [step("start", "b", "BURNED")], "BURNED")) # Right: died, once
T.append(run(201, [step("start", "b", "BURNED"), step("start", "a", "meadow")], "ABORTED")) # overridden pick, then aborted
T.append(run(202, [step("blue", "l", "BONDED:blue")], "BONDED", "blue")) # one lucky run with lowered eyes
return T
class Tree(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.t = B.build(world(), SCENES)
def test_counts_kinds_and_overridden_picks(self):
S = self.t["screens"]
a = S["start"]["options"]["a"]
self.assertEqual((a["n"], a["kind"], list(a["to"])), (101, "fixed", ["meadow"])) # 100 runs + the aborted one
self.assertLess(a["missed"], 0.01)
self.assertEqual(S["start"]["options"]["b"]["n"], 1) # the overridden pick is not counted
self.assertEqual(self.t["builtFrom"]["overridden"], 1)
self.assertEqual(S["start"]["options"]["c"]["kind"], "untested")
x = S["meadow"]["options"]["x"]
self.assertEqual(x["kind"], "dice")
self.assertAlmostEqual(x["to"]["blue"]["p"], 0.1)
self.assertEqual(x["deliberate"], 90) # deliberate burns are real results
self.assertTrue(x["to"]["blue"]["lo"] < 0.1 < x["to"]["blue"]["hi"])
g = S["blue"]["options"]["g"]
self.assertEqual((g["to"]["BONDED:Blue"]["n"], g["to"]["DIED"]["n"]), (8, 2))
def test_best_known_chance_is_chosen_cautiously(self):
best = self.t["best"]["Blue"]
# one lucky run with lowered eyes is 100% as observed, but holding its gaze (8 of 10) is the cautious pick
self.assertEqual([k["option"] for k in best["picks"]], ["a", "x", "g"])
self.assertAlmostEqual(best["p"], 0.1 * 0.8)
self.assertLess(best["pCautious"], best["p"])
self.assertAlmostEqual(best["ends"]["BONDED:Blue"], 0.08)
self.assertAlmostEqual(best["ends"]["DIED"], 0.92)
self.assertEqual(self.t["best"]["Black"]["p"], 0.0)
def test_outcomes_of_any_picks(self):
self.assertEqual(B.outcomes(self.t, {"start": "b"}), {"DIED": 1.0})
self.assertEqual(B.outcomes(self.t, {"start": "c"}), {"not mapped": 1.0})
ends = B.outcomes(self.t, {"start": "a", "blue": "l"})
self.assertAlmostEqual(ends["BONDED:Blue"], 0.1)
def test_coverage_lists_the_gaps(self):
c = self.t["coverage"]
self.assertEqual((c["screens"], c["options"], c["tested"]), (3, 6, 5))
self.assertEqual([u["option"] for u in c["untested"]], ["c"])
self.assertEqual(c["endings"], ["BONDED:Blue", "DIED"])
def test_published_tree_is_up_to_date(self):
self.assertEqual(B.main(["--check"]), 0)
if __name__ == "__main__":
unittest.main()