#!/usr/bin/env python3 """Route 132 rescue, instrument A: exact pseudo-Boolean (MIP) decision of the A144311 covering CSP. Route 132's obstruction is specific about its assumptions: "Single core; no symmetry breaking beyond mod-6 and no reflection breaking; no cube-and-conquer, XOR/pseudo-Boolean or BDD reasoning". This file changes exactly one thing -- the solver class: the same CSP as 0/1 MIP, decided by HiGHS with mip_rel_gap = 0, so kInfeasible is a proof of infeasibility and not a timeout. The one-hot domain is expressed natively as a row (sum_r x[p,r] = 1) instead of a sequential-counter CNF, which is the mathematically distinct part: the LP relaxation keeps the convexity constraint. The encoding MIRRORS return #1422's job2806_gate.py clause for clause -- positions 0..R-1, position i covered by p iff r_p + i = +-1 (mod p), start absorbed by the residues (CRT) -- so a per-prime time comparison compares instruments, not encodings. Reflection symmetry (the assumed-but-untried ingredient): every instance is invariant under sigma : r_p -> -r_p - (R - 1) (mod p), because r covers position i iff sigma(r) covers position R-1-i, an involution. So the residue of one chosen prime q can be restricted to the canonical half of sigma-orbits without losing any solution: (2*r_q + R - 1) mod q <= (q - 1) / 2. That is the only extra row family -- one per excluded residue of a single prime. Usage: ilp132.py --n LO HI [--sym none|reflect] [--q Q] [--cap SECONDS] [--solver highspy] Output: one JSON verdict per line on stdout (LF only); progress and timings on stderr. """ import argparse import json import sys import time import numpy as np import highspy as hp # OEIS A144311 as quoted on route 132 (offset 0): a(n) = longest R with a covering assignment. A = [1, 1, 5, 11, 29, 41, 65, 107, 149, 203, 257, 347, 527, 545, 617, 707, 869, 965, 1079, 1283, 1397, 1529, 1709] def primes(k): out, c = [], 2 while len(out) < k: if all(c % q for q in out if q * q <= c): out.append(c) c += 1 return out def canonical_residues(q, R): """Residues of q allowed by the reflection symmetry break r -> -r-(R-1) mod q.""" half = (q - 1) // 2 return [r for r in range(q) if (2 * r + R - 1) % q <= half] def encode(n, R, sym="none", q=None): """0/1 model mirroring job2806_gate.py. Returns (ps, index, arrays, stats).""" ps = primes(n) index, k = {}, 0 for p in ps: for r in range(p): index[p, r] = k k += 1 ncols = k cols = np.arange(ncols, dtype=np.int32) lb = np.zeros(ncols) ub = np.ones(ncols) cost = np.zeros(ncols) integ = np.full(ncols, hp.HighsVarType.kInteger, dtype=np.uint8) idxs, vals, rlb, rub, starts = [], [], [], [], [0] # exactly one residue per prime (native convexity row; gate.py used a sequential counter) for p in ps: for r in range(p): idxs.append(index[p, r]) vals.append(1.0) rlb.append(1.0) rub.append(1.0) starts.append(len(idxs)) # reflection symmetry break on one prime (empty when sym == none) dropped = [] if sym == "reflect": qq = q if q else ps[-1] keep = set(canonical_residues(qq, R)) for r in range(qq): if r not in keep: dropped.append(r) idxs.append(index[qq, r]) vals.append(1.0) rlb.append(0.0) rub.append(0.0) starts.append(len(idxs)) # one cover row per position: sum of covering residues >= 1 (the set dedup collapses p = 2) for i in range(R): seen = set() for p in ps: seen.add(index[p, (1 - i) % p]) seen.add(index[p, (-1 - i) % p]) cs = sorted(seen) idxs.extend(cs) vals.extend([1.0] * len(cs)) rlb.append(1.0) rub.append(hp.kHighsInf) starts.append(len(idxs)) stats = {"ncols": ncols, "nrows": len(rlb), "nnz": len(idxs), "sym": sym, "sym_prime": (ps[-1] if sym == "reflect" and not q else q) if sym == "reflect" else None, "sym_dropped_residues": dropped} return ps, index, (lb, ub, cost, integ, cols), (rlb, rub, starts, idxs, vals), stats def check_witness(ps, res, R): """A SAT claim is accepted only with this independent recheck of every position.""" return all(any((res[p] + i) % p in (1, p - 1) for p in ps) for i in range(R)) def solve(n, R, cap=None, sym="none", q=None, threads=1, seed=0): ps, index, col, rows, stats = encode(n, R, sym, q) lb, ub, cost, integ, cols = col rlb, rub, starts, idxs, vals = rows h = hp.Highs() h.setOptionValue("output_flag", False) h.setOptionValue("mip_rel_gap", 0.0) h.setOptionValue("random_seed", int(seed)) h.setOptionValue("threads", threads) if cap is not None: h.setOptionValue("time_limit", float(cap)) h.addVars(len(lb), np.asarray(lb), np.asarray(ub)) h.changeColsCost(len(cols), cols, np.asarray(cost)) h.changeColsIntegrality(len(cols), cols, np.asarray(integ)) h.addRows(len(rlb), np.asarray(rlb), np.asarray(rub), len(idxs), np.asarray(starts, dtype=np.int32), np.asarray(idxs, dtype=np.int32), np.asarray(vals)) t0 = time.time() h.run() dt = time.time() - t0 st = str(h.getModelStatus()).split(".")[-1] info = h.getInfo() out = dict(stats, n=n, pn=ps[-1], R=R, status=st, sec=round(dt, 3), solver="HiGHS " + h.version(), threads=threads, seed=int(seed), nodes=getattr(info, "mip_node_count", None), lp_iters=getattr(info, "simplex_iteration_count", None)) if st == "kOptimal": x = h.getSolution().col_value res = {p: next(r for r in range(p) if x[index[p, r]] > 0.5) for p in ps} out["sat"] = True out["witness_ok"] = check_witness(ps, res, R) out["residues"] = {str(p): res[p] for p in ps} elif st in ("kInfeasible", "kUnboundedOrInfeasible"): out["sat"] = False else: out["sat"] = None return out def main(): ap = argparse.ArgumentParser() ap.add_argument("--n", nargs=2, type=int, required=True, metavar=("LO", "HI")) ap.add_argument("--sym", default="none", choices=("none", "reflect")) ap.add_argument("--q", type=int, default=None) ap.add_argument("--cap", type=float, default=None) ap.add_argument("--threads", type=int, default=1) ap.add_argument("--seed", type=int, default=0, help="HiGHS random_seed; the search path is seed-dependent, the verdict is not") ap.add_argument("--only", type=int, default=None, help="test only this R (default: R = a(n) and R = a(n)+1)") a = ap.parse_args() for n in range(a.n[0], a.n[1] + 1): if n >= len(A): print("n=%d beyond the quoted ladder" % n, file=sys.stderr) break rs = [a.only] if a.only else [A[n], A[n] + 1] for R in rs: row = solve(n, R, cap=a.cap, sym=a.sym, q=a.q, threads=a.threads, seed=a.seed) print(json.dumps(row, sort_keys=True), flush=True) print("seed=%d n=%d R=%d %s %ss nodes=%s" % (a.seed, n, R, row["status"], row["sec"], row.get("nodes")), file=sys.stderr, flush=True) if __name__ == "__main__": main()