#!/usr/bin/env python3 """Route 7, job 975: the complete one-anchor phase-conditioning family at N52. Instance (frozen, #370/#379): p = 97, old primes <= 97, support a = 9409, the first n = 52 slots s >= a with gcd(s(s+2), 97#) = 1, Q = the 19 primes 101..193. A phase b of q kills s when q | s + b or q | s + b + 2. For an anchor prime q0 and each phase b of q0: residual R_b = slots not killed by (q0, b), primes Q' = Q minus q0. The exact LP value v_b = min_{w >= 0, sum w = 1} sum_{q in Q'} max_b' W(q, b') is solved with HiGHS (it equals the max z of a fractional phase cover with coverage z on every slot of R_b). Every verdict is rechecked in integers: STRICT (v_b < 1): LP dual w rounded over 10^7 gives sum max W < W(R_b); SILENT (v_b >= 1): LP primal y floored over 10^9 per prime (mass <= 10^9) covers every slot of R_b with total >= 10^9, i.e. a fractional cover. All q0 branches STRICT = a depth-1 tree certificate (tree959 format) that the prefix has no phase cover. Route 7 pre-registers q0 = 101; the other 18 anchors are reported as the "change the anchor prime" alternative. .venv/bin/python anchor975.py > anchor975.out # writes tree975-101.cert.jsonl stdout is the artifact; timing goes to stderr. """ import hashlib import importlib.util import json import os import sys import time from collections import Counter import numpy as np from scipy.optimize import linprog from scipy.sparse import coo_matrix HERE = os.path.dirname(os.path.abspath(__file__)) s = importlib.util.spec_from_file_location('rw', os.path.join(HERE, 'r370-route4-weighted.py')) rw = importlib.util.module_from_spec(s) s.loader.exec_module(rw) sys.stdout.reconfigure(newline='\n') WDEN, YDEN = 10 ** 7, 10 ** 9 A, N, ANCHOR = 9409, 52, 101 OLD, QS0 = rw.old_primes(97), rw.new_primes(97) def killed(D, q, b): return {x for x in D if (x + b) % q == 0 or (x + b + 2) % q == 0} def lp(D, QS): """Return (v, w dual slot weights, y primal phase masses {q: [..]}).""" off, o = {}, 0 for q in QS: off[q] = o o += q rows, cols, vals = [], [], [] for qi, q in enumerate(QS): for b in range(q): rows.append(qi); cols.append(off[q] + b); vals.append(1.0) for i, x in enumerate(D): r = len(QS) + i rows.append(r); cols.append(o); vals.append(1.0) for q in QS: rows.append(r); cols.append(off[q] + (-x) % q); vals.append(-1.0) rows.append(r); cols.append(off[q] + (-x - 2) % q); vals.append(-1.0) M = coo_matrix((vals, (rows, cols)), shape=(len(QS) + len(D), o + 1)).tocsr() rhs = np.concatenate([np.ones(len(QS)), np.zeros(len(D))]) c = np.zeros(o + 1); c[o] = -1.0 res = linprog(c, A_ub=M, b_ub=rhs, bounds=[(0, None)] * (o + 1), method='highs') assert res.status == 0 y = {q: list(res.x[off[q]:off[q] + q]) for q in QS} return -res.fun, list(-res.ineqlin.marginals[len(QS):]), y def strict_cert(D, QS, w): wi = [max(0, int(round(x * WDEN))) for x in w] tot, sm = sum(wi), 0 for q in QS: W = Counter() for x, v in zip(D, wi): if v: W[(-x) % q] += v W[(-x - 2) % q] += v sm += max(W.values()) if W else 0 return (sm, tot, wi) if tot > 0 and sm < tot else None def silent_cert(D, QS, y): """Integer fractional cover: returns min slot coverage (>= YDEN) or None.""" yi = {q: [max(0, int(v * YDEN)) for v in y[q]] for q in QS} if any(sum(yi[q]) > YDEN for q in QS): return None cov = min(sum(yi[q][(-x) % q] + yi[q][(-x - 2) % q] for q in QS) for x in D) return cov if cov >= YDEN else None def branch(D, q0, b): R = [x for x in D if x not in killed(D, q0, b)] rest = [q for q in QS0 if q != q0] v, w, y = lp(R, rest) if v < 1: c = strict_cert(R, rest, w) return R, v, ('STRICT', c) if c else ('UNCHECKED', None) c = silent_cert(R, rest, y) return R, v, ('SILENT', c) if c else ('UNCHECKED', None) def main(): t0 = time.time() D = rw.slots_from(A, N, OLD) src = json.load(open(os.path.join(HERE, 'n52-cnt.json'))) assert D == src['slots'], 'slots differ from #379 n52-cnt.json' print(f'# support a = {A}, n = {N}, first {D[0]}, last {D[-1]}, L = {D[-1] - A + 1}; ' f'Q = {QS0[0]}..{QS0[-1]} ({len(QS0)} primes); slots equal #379 n52-cnt.json') v19, _, y19 = lp(D, QS0) print(f'root, all 19 primes: v = {v19:.8f}; integer fractional cover check: ' f'{"PASS" if silent_cert(D, QS0, y19) else "FAIL"}') rest = [q for q in QS0 if q != ANCHOR] v18, w18, _ = lp(D, rest) print(f'root without {ANCHOR} (18 primes): v = {v18:.8f}; integer deficit check: ' f'{"PASS" if strict_cert(D, rest, w18) else "FAIL"}') print() print(f'## anchor {ANCHOR}: all {ANCHOR} phases') print('b\t|R_b|\tkilled\tv_b\tverdict\tinteger check') cert = [{'path': [], 'branch': ANCHOR}] rows = [] for b in range(ANCHOR): R, v, (verdict, c) = branch(D, ANCHOR, b) if verdict == 'STRICT': chk = f'{c[0]} < {c[1]}' cert.append({'path': [[ANCHOR, b]], 'w': c[2]}) elif verdict == 'SILENT': chk = f'min coverage {c} >= {YDEN}' else: chk = '-' rows.append((b, len(R), v, verdict)) print(f'{b}\t{len(R)}\t{N - len(R)}\t{v:.8f}\t{verdict}\t{chk}') cnt = Counter(r[3] for r in rows) vac = [r[0] for r in rows if r[1] == N] bind = max(rows, key=lambda r: r[2]) print() print(f'anchor {ANCHOR} summary: ' + ', '.join(f'{k} {cnt[k]}' for k in sorted(cnt)) + f'; vacuous (R_b = D) {len(vac)}; binding b = {bind[0]} (|R_b| = {bind[1]}, v = {bind[2]:.8f}, ' f'margin 1 - v = {1 - bind[2]:.8f}); min v = {min(r[2] for r in rows):.8f}') if cnt['STRICT'] == ANCHOR: body = ''.join(json.dumps(r, separators=(',', ':')) + '\n' for r in cert) fn = f'tree975-{ANCHOR}.cert.jsonl' open(os.path.join(HERE, fn), 'w', newline='\n').write(body) print(f'depth-1 certificate {fn}: {len(cert)} records, sha256 ' f'{hashlib.sha256(body.encode()).hexdigest()}') print() print('## every anchor q0 in Q (depth 1)') print('q0\tSTRICT\tSILENT\tUNCHECKED\tvacuous\tmax v_b (binding b)\tclass closes') for q0 in QS0: vs = [] for b in range(q0): R, v, (verdict, _) = branch(D, q0, b) vs.append((v, b, verdict, len(R))) c = Counter(t[2] for t in vs) mv = max(vs) print(f'{q0}\t{c["STRICT"]}\t{c["SILENT"]}\t{c["UNCHECKED"]}\t{sum(t[3] == N for t in vs)}\t' f'{mv[0]:.8f} (b = {mv[1]})\t{"yes" if c["STRICT"] == q0 else "no"}') print(f'elapsed {time.time() - t0:.1f} s', file=sys.stderr) if __name__ == '__main__': main()