"""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: every run, and a low-bias sample (bulk logs plus form runs from 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 # the same runs the site's statistics use (analysis/stats/measures.yaml `sources`) with open(ROOT / "analysis/stats/measures.yaml", encoding="utf-8") as _f: SOURCES = yaml.safe_load(_f)["sources"] 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 = runs_io.read(a.data, sources=SOURCES) rows = len(runs) samples = { "all": runs, "lowBias": [r for r in runs if r["entry"] != "Form" or r["goal"] not in model.RARE_GOALS], } t0 = time.time() out = {"schema": 1, "source": {"form_rows": rows}, "samples": {}} for name, rs in samples.items(): out["samples"][name] = clean(solver.build_policy(rs, draws=a.draws)) 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()