1"""Boundary table (CS2 / L4): for p in a range and n in a range, decide whether2some X in P(n,5) with |X| = p has |dX| <= 11, and if so record the exact3minimum beta_p(n) and a witness (an obstruction to the boundary method).45usage: python3 code/run_boundary_table.py PMIN PMAX NMIN NMAX [workers]6 python3 code/run_boundary_table.py --job N P (single instance, prints JSON)7appends to results/boundary_table.jsonl8Parallelism uses subprocesses (ProcessPoolExecutor is unavailable in the sandbox).9"""10import json11import os12import subprocess13import sys14import time15from concurrent.futures import ThreadPoolExecutor1617import common18from graph_constructor import label19from sat_or_ilp_solver import beta202122def job(n, p):23 t0 = time.time()24 r = beta(n, 5, p, start=11)25 r["wall_seconds"] = round(time.time() - t0, 3)26 r["utc"] = common.utcnow()27 if r["X"] is not None:28 r["X_labels"] = [label(w, n) for w in r["X"]]29 r["boundary_labels"] = [label(w, n) for w in r["boundary"]]30 return r313233def run_sub(args):34 n, p = args35 out = subprocess.run([sys.executable, os.path.abspath(__file__), "--job", str(n), str(p)], capture_output=True, text=True)36 if out.returncode != 0:37 return {"n": n, "p": p, "error": out.stderr[-500:]}38 return json.loads(out.stdout.strip().splitlines()[-1])394041if __name__ == "__main__":42 if sys.argv[1] == "--job":43 print(json.dumps(job(int(sys.argv[2]), int(sys.argv[3]))))44 sys.exit(0)45 pmin, pmax, nmin, nmax = (int(a) for a in sys.argv[1:5])46 workers = int(sys.argv[5]) if len(sys.argv) > 5 else 847 out = os.path.join(common.RESULTS, "boundary_table.jsonl")48 tasks = [(n, p) for p in range(pmin, pmax + 1) for n in range(nmin, nmax + 1)]49 common.log_event("D", "boundary_table_start", pmin=pmin, pmax=pmax, nmin=nmin, nmax=nmax, workers=workers)50 with ThreadPoolExecutor(max_workers=workers) as ex:51 for r in ex.map(run_sub, tasks):52 common.append_jsonl(out, r)53 if "error" in r:54 print("n=%d p=%d ERROR %s" % (r["n"], r["p"], r["error"]), flush=True)55 else:56 print("n=%d p=%d beta=%s%s %.1fs" % (r["n"], r["p"], r["beta"], "" if r["exact"] else "+", r["wall_seconds"]), flush=True)57 common.log_event("D", "boundary_table_end", pmin=pmin, pmax=pmax, nmin=nmin, nmax=nmax)58