"""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()