Core: a small JS advisor reads the game from policy.json; engine.js is gone (#13)
Step 4b of the Python-first core. - core/solver.js is now a 250-line advisor (was 900 lines): it reads the game (core/spec/game.yaml, published inside policy.json as model.game) and the count tables, and re-solves from any point in a run with the same interpreter as the Python model. Same API for the site and Ken's bot (advisor, recommendOption, solverHistory). The bot's on-screen text for each option moves to game.yaml (bot_text); the deadly rolls carry their risk labels there too. - tests/core/advisor.test.js: the advisor gives exactly the Python model's values and choices for every colour and goal (to 1e-12, every decision on every published route), walks a run in the game's order, and maps advice onto the bot's screen. - Removed core/engine.js and everything that only compared it with Python (tests/parity/run_engine, compare_runs, compare_results, compare_routes, compare_solver, compare_stats, analysis/stats/ analyze.py). In their place: tests/core/test_stats.py checks the statistics against known answers, test_solver.py checks the route model on a synthetic world (tests/fixtures/synthetic_policy.py). - analysis/network/build-graph.js reads the Python-coded runs; graph.json is byte-identical. - docs/BOT_INTEGRATION.md: what changed for the bot (history keys, new choices, re-sync all three files). 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
d04b88e7b4
commit
11b4a0398e
@@ -0,0 +1,55 @@
|
||||
"""The route model (core/atlas/solver.py) on a synthetic world whose answers are known."""
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "fixtures"))
|
||||
import synthetic_policy # noqa: E402
|
||||
|
||||
P = synthetic_policy.policy()
|
||||
T = P["targets"]
|
||||
|
||||
|
||||
def first(tree, var):
|
||||
"""The first decision on a variable along the recommended route (following the likeliest branch)."""
|
||||
n = tree
|
||||
while n and n["type"] != "end":
|
||||
if n["type"] == "decision":
|
||||
if n["var"] == var:
|
||||
return n
|
||||
n = n["next"]
|
||||
else:
|
||||
n = max(n["branches"].values(), key=lambda b: b["p"])["next"]
|
||||
return None
|
||||
|
||||
|
||||
class Synthetic(unittest.TestCase):
|
||||
def test_blue_goes_to_the_valley_and_passes_the_red(self):
|
||||
t = T["Blue"]["thisAttempt"]
|
||||
self.assertEqual(first(t["tree"], "hour")["choice"], "Valley")
|
||||
self.assertEqual(first(t["tree"], "valley")["choice"], "Pass the Red (meadow)")
|
||||
# the chance is (share of meadow visits with a Blue) x (Blue bond rate), as logged in the world
|
||||
m = [r for r in synthetic_policy.world() if r.get("branch") == "Passed the Red: meadow"]
|
||||
blue = [r for r in m if r["enc_Blue"]]
|
||||
expect = len(blue) / len(m) * sum(r["enc_Blue"] == "Bonded!" for r in blue) / len(blue)
|
||||
self.assertLess(abs(t["pX"]["point"] - expect), 0.01)
|
||||
|
||||
def test_black_climbs_the_tree(self):
|
||||
t = T["Black"]["thisAttempt"]
|
||||
self.assertEqual(first(t["tree"], "hour")["choice"], "Tree")
|
||||
self.assertTrue(0.01 < t["pX"]["point"] < 0.035, t["pX"]["point"])
|
||||
|
||||
def test_red_faces_the_valley_red(self):
|
||||
self.assertEqual(first(T["Red"]["thisAttempt"]["tree"], "valley")["choice"], "Face the Red")
|
||||
|
||||
def test_eventually_never_risks_another_bond(self):
|
||||
a, e = T["Brown"]["thisAttempt"], T["Brown"]["eventually"]
|
||||
self.assertLessEqual(e["pOther"]["point"], a["pOther"]["point"] + 1e-12)
|
||||
self.assertGreaterEqual(e["ratio"]["mean"], a["ratio"]["mean"] - 1e-9)
|
||||
|
||||
def test_no_history_dependence_gives_a_large_concentration(self):
|
||||
self.assertGreaterEqual(P["model"]["alphas"]["meadow"], 500)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user