#!/usr/bin/env python3 # -*- coding: utf-8 -*- """restarts-dist.py — give the negative a scale. `cover90max` reported 1,973 random restarts without beating the served tuple's 2027-run. A count of restarts is not a scale. This measures what a restart actually reaches: for each of many random residue tuples, descend to a coordinate-wise local maximum of the run length and record it. The output is the distribution, so the negative can be quoted as "descent from a random tuple reaches X at the 50th / 99th / best percentile" rather than as "we tried a lot". """ import argparse import json import time import numpy as np import cover90 as C from cover90 import PRIMES, N import cover90max as M def descend(res, X, rng, max_sweeps=40): cnt = M.build(res, X) best, _s = M.score(cnt, X) for _ in range(max_sweeps): improved, best2 = M.sweep(cnt, res, X, best, rng, ties=0.0) if best2 > best: best = best2 if not improved: break r, s = M.score(cnt, X) return max(best, r), s def main(): ap = argparse.ArgumentParser() ap.add_argument("--X", type=int, default=40000) ap.add_argument("--seconds", type=float, default=120) ap.add_argument("--seed", type=int, default=99) ap.add_argument("--out", required=True) a = ap.parse_args() rng = np.random.default_rng(a.seed) t0 = time.time() vals = [] best_any = (0, None) while time.time() - t0 < a.seconds: res = [int(rng.integers(0, p)) for p in PRIMES] r, s = descend(res, a.X, rng) vals.append(r) if r > best_any[0]: best_any = (r, list(res)) v = np.array(vals) out = {"tool": "restarts-dist/1", "at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "X": a.X, "seconds": round(time.time() - t0, 1), "seed": a.seed, "restarts": len(vals), "min": int(v.min()), "p50": int(np.percentile(v, 50)), "p90": int(np.percentile(v, 90)), "p99": int(np.percentile(v, 99)), "max": int(v.max()), "mean": round(float(v.mean()), 1), "served_state_run": 2027, "best_residues": best_any[1]} json.dump(out, open(a.out, "w", encoding="utf-8"), indent=1) print(json.dumps({k: out[k] for k in ("restarts", "min", "p50", "p90", "p99", "max", "mean", "seconds")}, indent=1)) return 0 if __name__ == "__main__": import sys sys.exit(main())