#!/usr/bin/env python3 """Route 132, instrument E: how far does the pre-registered n = 13..17 UNSAT gate get within a STATED core-hour budget? The route's obstacle asks for UNSAT growth of at most about 2x per prime on n = 10..14. The gate is pre-registered, so the honest way to spend budget on it is to fix the budget first and report the level reached, not to run until something finishes. Budget accounting is worst-case and explicit: a level with k seeds and a cap of C seconds is charged k*C regardless of how long it actually takes, so the declared budget can never be exceeded by an overrun and the reported cost is an upper bound on what was spent. Levels run in order; a level that no seed closes stops the gate there (the seeds are a portfolio, so "closed" means at least one seed proved UNSAT, never a timeout). Usage: gate132.py --budget-core-hours 1.5 --schedule "13:900:0,1,2,3;14:900:0,1" [--sym none] Output: one JSON document on stdout (LF only); per-solve progress and timings on stderr. """ import argparse import json import os import subprocess import sys import time A = [1, 1, 5, 11, 29, 41, 65, 107, 149, 203, 257, 347, 527, 545, 617, 707, 869, 965, 1079, 1283, 1397, 1529, 1709] HERE = os.path.dirname(os.path.abspath(__file__)) PY = sys.executable def parse_schedule(s): out = [] for part in s.split(";"): part = part.strip() if not part: continue n, cap, seeds = part.split(":") out.append((int(n), float(cap), [int(x) for x in seeds.split(",") if x != ""])) return out def run_level(n, cap, seeds, sym): """Portfolio: k single-threaded MIP solves on R = a(n)+1, one per seed, in parallel.""" R = A[n] + 1 procs = [] for s in seeds: cmd = [PY, os.path.join(HERE, "ilp132.py"), "--n", str(n), str(n), "--only", str(R), "--cap", str(cap), "--sym", sym, "--seed", str(s), "--threads", "1"] procs.append((s, subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, cwd=HERE))) rows = [] for s, p in procs: out, err = p.communicate() row = None for line in (out or "").splitlines(): line = line.strip() if line.startswith("{"): try: row = json.loads(line) except Exception: pass if row is None: row = {"status": "kNoOutput", "sec": None, "raw_stderr_tail": (err or "")[-300:]} row["seed"] = s rows.append(row) print("n=%d R=%d seed=%d %s %ss nodes=%s" % (n, R, s, row.get("status"), row.get("sec"), row.get("nodes")), file=sys.stderr, flush=True) return R, rows def main(): ap = argparse.ArgumentParser() ap.add_argument("--budget-core-hours", type=float, required=True) ap.add_argument("--schedule", required=True, help='levels as "n:capseconds:seed,seed;..." in the order they should be tried') ap.add_argument("--sym", default="none", choices=("none", "reflect")) ap.add_argument("--json", default=None) a = ap.parse_args() budget = a.budget_core_hours * 3600.0 schedule = parse_schedule(a.schedule) rep = {"tool": "gate132.py", "solver": "HiGHS via ilp132.py", "sym": a.sym, "budget_core_hours": a.budget_core_hours, "budget_seconds": budget, "charging_rule": "k seeds x cap seconds per level, worst case, charged whether used or not", "levels": [], "reached_level": None, "levels_not_reached": [n for n, _, _ in schedule]} t0 = time.time() charged = 0.0 for n, cap, seeds in schedule: cost = len(seeds) * cap if charged + cost > budget: rep["stopped_because"] = ("budget: level n=%d would be charged %.0f s and only %.0f s of " "the declared %.0f s remain" % (n, cost, budget - charged, budget)) print(rep["stopped_because"], file=sys.stderr, flush=True) break charged += cost wall = time.time() R, rows = run_level(n, cap, seeds, a.sym) closed = any(r.get("status") == "kInfeasible" for r in rows) lvl = {"n": n, "R": R, "cap_seconds": cap, "seeds": seeds, "charged_seconds": cost, "wall_seconds": round(time.time() - wall, 1), "closed": bool(closed), "verdicts": rows} rep["levels"].append(lvl) if closed: rep["reached_level"] = n rep["levels_not_reached"] = [m for m, _, _ in schedule if m > n] else: rep["reached_level"] = n - 1 rep["levels_not_reached"] = [m for m, _, _ in schedule if m >= n] rep["stopped_because"] = ("level n=%d was not closed by any of %d seeds within %g s each" % (n, len(seeds), cap)) break rep["charged_seconds"] = charged rep["charged_core_hours"] = round(charged / 3600.0, 4) rep["wall_seconds"] = round(time.time() - t0, 1) rep["a_n_reached"] = A[rep["reached_level"]] if rep["reached_level"] and rep["reached_level"] >= 0 \ else None txt = json.dumps(rep, indent=1, sort_keys=True, ensure_ascii=False) if a.json: with open(a.json, "w", encoding="utf-8", newline="\n") as fh: fh.write(txt + "\n") print(txt) if __name__ == "__main__": main()