Files
ekcSchianoandClaude Opus 5.5 46c1f00150
CI / test (pull_request) Successful in 41s
Impossible runs: one definition, left out of the route model and the site statistics (#21, #22)
- core/spec/form.yaml impossible_flags: the flags that mean a run cannot have happened as recorded (a dragon an
  encounter does not have, a Green or the Black in the valley, two bonds, two burns, a cadet path that does not
  follow, an early death that still reached the hour...). atlas.runs.impossible() reads it.
- The route model and the site statistics (and the riders' paths) leave those runs out, from every source; every
  other run is used, however partial. The runs stay in runs_clean.csv, flagged. 80 runs: 75 form, 5 Reddit.
  Route-model odds move by at most 0.2 points.
- collection/etl/runs.py: the community form gets the same game-rule checks as "Report a run" and the Reddit
  comments (10 form rows newly flagged); new rule: the Black in the valley.
- The first-sighting pool stays a flag only: it is what riders have seen, not a rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ
2026-11-19 16:03:45 +00:00

99 lines
4.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Build data/public/policy.json: the best decisions for every dragon colour, under both objectives.
python planner/build_policy.py [--draws 200] [--out data/public/policy.json]
Two samples, from the runs and rules in planner/policy.yaml: every run, and a low-bias sample (bot runs, bulk logs,
and riders not chasing a rare dragon).
The output has a content id, so each bot run can record which policy it followed.
"""
import argparse
import hashlib
import json
import math
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "core"))
from atlas import model, solver # noqa: E402
from atlas import runs as runs_io # noqa: E402
import yaml # noqa: E402
# which runs the model learns from, and the low-bias sample (planner/policy.yaml)
with open(ROOT / "planner/policy.yaml", encoding="utf-8") as _f:
CONFIG = yaml.safe_load(_f)
SOURCES = CONFIG["sources"]
def low_bias(r, lb=CONFIG["low_bias"]):
"""Kept in the low-bias sample: every run of an always-kept source, bulk logs, and riders not chasing a rare dragon."""
if r["source"] in lb["always"]:
return True
if r["source"] in (lb.get("never") or ()):
return False
if r["source"] == "reddit spreadsheet" and "bulk" in lb["entries_kept"] and r["entry"] not in (None, "Form"):
return True
return r["goal"] not in lb["rare_goals"]
def drop_flagged(runs, on=CONFIG.get("drop_impossible", True)):
"""Without the runs that cannot have happened as recorded (atlas.runs.impossible; planner/policy.yaml
drop_impossible): every other run is kept, however partial."""
return [r for r in runs if not (on and runs_io.impossible(r))]
def clean(o):
"""JSON-safe: numpy numbers to floats, infinities and NaN to null (as JavaScript's JSON.stringify does)."""
if isinstance(o, dict):
return {str(k): clean(v) for k, v in o.items()}
if isinstance(o, (list, tuple)):
return [clean(v) for v in o]
if hasattr(o, "item"):
o = o.item()
if isinstance(o, float) and (math.isinf(o) or math.isnan(o)):
return None
return o
def main(argv=None):
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("--draws", type=int, default=200)
ap.add_argument("--data", default=str(runs_io.PATH))
ap.add_argument("--out", default=str(ROOT / "data/public/policy.json"))
a = ap.parse_args(argv)
runs = drop_flagged(runs_io.read(a.data, sources=SOURCES))
rows = len(runs)
samples = {
"all": runs,
"lowBias": [r for r in runs if low_bias(r)],
}
t0 = time.time()
by_source = {}
for r in runs:
by_source[r["source"]] = by_source.get(r["source"], 0) + 1
out = {"schema": 1, "source": {"form_rows": by_source.get("reddit spreadsheet", 0), "runs": rows, "by_source": by_source},
"samples": {}}
for name, rs in samples.items():
out["samples"][name] = clean(solver.build_policy(rs, draws=a.draws, bias_config=CONFIG.get("reporting_bias")))
print(f"{name}: {len(rs)} runs, {time.time() - t0:.1f} s")
# a content id (no timestamps: the build is reproducible)
out["id"] = hashlib.sha1(json.dumps(out["samples"], separators=(",", ":"), sort_keys=True).encode()).hexdigest()[:12]
Path(a.out).write_text(json.dumps(out, separators=(",", ":")), encoding="utf-8")
print("policy id", out["id"])
pct = lambda p: "–" if p is None else f"{100 * p:.{2 if p < 0.01 else 1}f}%" # noqa: E731
for name, s in out["samples"].items():
print(f"\n{name} alphas {json.dumps(s['model']['alphas'])}")
for X, t in s["targets"].items():
x, e = t["thisAttempt"], t["eventually"]
att = e["expectedAttempts"]["mean"]
print(f" {X:<6} this attempt {pct(x['pX']['mean'])} [{pct(x['pX']['lo'])}, {pct(x['pX']['hi'])}] other bond {pct(x['pOther']['mean'])}"
f" | eventually: ratio {pct(e['ratio']['mean'])}, P(X) {pct(e['pX']['mean'])}, ~{'–' if att is None else round(att)} attempts"
f" | cross-fit {pct(t['crossFit']['inSample'])} -> held-out {pct(t['crossFit']['heldOut'])}")
if __name__ == "__main__":
main()