#!/usr/bin/env python3 # -*- coding: utf-8 -*- """cover90max.py — job #2731, the mechanism this run changed. The route's instrument minimizes holes inside a FIXED window of the target length, placed in a small offset range. The object it is trying to produce is a MAXIMAL covered run, and that is what this instrument optimizes directly: coordinate ascent on the LENGTH of the longest covered run over a much wider placement range [0, X), with a boundary-extension move aimed at the two holes that bound the current best run, and iterated local search around it. Every reported value is re-derived from scratch (fresh per-offset counts, fresh hole positions) before it is written; a claimed run is additionally re-verified by a separate pure-Python checker (`cover90verify.py`) that shares no code with the search. Semantics are the project's own and are gated: see `cover90.gate`. """ import argparse import json import time import numpy as np import cover90 as C from cover90 import PRIMES, classes def score(cnt, X): """(longest covered run in [0,X), its start). Exact: holes are located by flatnonzero and the run is the largest gap; the two boundary runs are counted.""" holes = np.flatnonzero(cnt == 0) if holes.size == 0: return X, 0 d = np.diff(holes) if d.size: i = int(np.argmax(d)) best = int(d[i]) - 1 start = int(holes[i]) + 1 else: best, start = -1, 0 if int(holes[0]) > best: best, start = int(holes[0]), 0 tail = int(X - 1 - holes[-1]) if tail > best: best, start = tail, int(holes[-1]) + 1 return best, start def apply_move(cnt, k, v_old, v_new, X): p = PRIMES[k] for a in classes(p, v_old): cnt[a:X:p] -= 1 for a in classes(p, v_new): cnt[a:X:p] += 1 def build(res, X): return C.coverage(res, X) def sweep(cnt, res, X, best_run, rng, ties=0.2): """One full coordinate-ascent sweep: every prime, every residue, keep the best run. Ties are accepted with probability `ties` so a sweep can cross a plateau.""" improved = False entry = best_run for k in range(len(PRIMES)): p = PRIMES[k] cur = res[k] for w in range(p): if w == cur: continue apply_move(cnt, k, cur, w, X) r, _s = score(cnt, X) take = r > best_run or (r == best_run and rng.random() < ties) if take: if r > best_run: best_run = r cur = w res[k] = w else: apply_move(cnt, k, w, cur, X) return best_run > entry, best_run def extend(cnt, res, X, run, start, rng): """Boundary extension: the two holes bounding the best run are the only positions that can lengthen it directly. Try every (prime, covering residue) pair for each, keep the best run obtained.""" improved = False for h in (start - 1, start + run): if not (0 <= h < X): continue cands = [] for k in range(len(PRIMES)): p = PRIMES[k] for w in {(1 - h) % p, (-1 - h) % p}: cands.append((k, w)) rng.shuffle(cands) for k, w in cands: cur = res[k] if w == cur: continue apply_move(cnt, k, cur, w, X) r, s = score(cnt, X) if r > run: run, start, res[k] = r, s, w improved = True break apply_move(cnt, k, w, cur, X) return improved, run, start def run(X, seconds, seed, start_res, log): rng = np.random.default_rng(seed) res = list(start_res) cnt = build(res, X) best, bstart = score(cnt, X) best_res = list(res) t0 = time.time() it = kicks = restarts = 0 traj = [(0.0, best, bstart)] log({"event": "start", "run": best, "start": bstart, "X": X, "seed": seed, "residues": list(res)}) while time.time() - t0 < seconds: it += 1 _i, r = sweep(cnt, res, X, best, rng) r, s = score(cnt, X) if r > best: best, bstart, best_res = r, s, list(res) traj.append((round(time.time() - t0, 2), r, s)) log({"event": "improve", "run": r, "start": s, "iter": it, "t": round(time.time() - t0, 1), "residues": list(res)}) for _ in range(3): # boundary extension attempts _i2, r2, s2 = extend(cnt, res, X, best, bstart, rng) if r2 > best: best, bstart, best_res = r2, s2, list(res) traj.append((round(time.time() - t0, 2), r2, s2)) log({"event": "improve_extend", "run": r2, "start": s2, "iter": it, "t": round(time.time() - t0, 1), "residues": list(res)}) # perturb if rng.random() < 0.25: res = [int(rng.integers(0, p)) for p in PRIMES] restarts += 1 else: res = list(best_res) for _ in range(int(rng.integers(1, 4))): k = int(rng.integers(0, len(PRIMES))) res[k] = int(rng.integers(0, PRIMES[k])) kicks += 1 cnt = build(res, X) r3, s3 = score(cnt, X) if r3 > best: best, bstart, best_res = r3, s3, list(res) traj.append((round(time.time() - t0, 2), r3, s3)) log({"event": "improve_kick", "run": r3, "start": s3, "iter": it, "t": round(time.time() - t0, 1), "residues": list(res)}) return best_res, best, bstart, traj, it, kicks, restarts def cmd_run(a): fh = open(a.log, "w", encoding="utf-8") if a.log else None def log(rec): if fh: fh.write(json.dumps(rec) + "\n"); fh.flush() start = json.load(open(a.start, encoding="utf-8")) if a.start else C.STATE_RESIDUES if isinstance(start, dict): start = start.get("best_residues") or start.get("residues") t0 = time.time() res, best, bstart, traj, it, kicks, restarts = run(a.X, a.seconds, a.seed, start, log) # re-derive everything from scratch cnt = C.coverage(res, a.X) r2, s2 = score(cnt, a.X) holes = np.flatnonzero(cnt == 0) out = {"tool": "cover90max/1", "at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "X": a.X, "R_bar": a.R_bar, "seconds_requested": a.seconds, "seconds_elapsed": round(time.time() - t0, 1), "seed": a.seed, "iterations": it, "kicks": kicks, "random_restarts": restarts, "start_residues": start, "best_residues": res, "best_run_incremental": best, "best_run_from_scratch": r2, "best_run_start": s2, "counter_matches_from_scratch": best == r2, "holes_in_range": int(holes.size), "beats_published_2027": r2 > 2027, "meets_bar": r2 >= a.R_bar, "trajectory": traj} need = max(0, a.R_bar - 2) if r2 >= need: out["bounding_holes"] = [int(h) for h in holes if s2 - 1 <= h <= s2 + r2] json.dump(out, open(a.out, "w", encoding="utf-8"), indent=1) if fh: fh.close() print(json.dumps({k: out[k] for k in ("best_run_from_scratch", "best_run_start", "counter_matches_from_scratch", "beats_published_2027", "meets_bar", "iterations", "kicks", "random_restarts", "seconds_elapsed")}, indent=1)) return 0 def main(): ap = argparse.ArgumentParser() ap.add_argument("--start") ap.add_argument("--X", type=int, default=40000) ap.add_argument("--R-bar", type=int, default=2454) ap.add_argument("--seconds", type=float, default=600) ap.add_argument("--seed", type=int, default=1) ap.add_argument("--out", required=True) ap.add_argument("--log") ap.add_argument("--level", type=int, default=25, help="use the first N primes") a = ap.parse_args() if a.level < len(C.PRIMES): C.set_level(a.level) return cmd_run(a) if __name__ == "__main__": import sys sys.exit(main())