"""Route 19 / job #1295: the N1 Hall generator on #477's REAL block set. The search is a faithful copy of producer1123.py (sha 535f5f2a...), so that `arm=none` (ablation) and `arm=greedy` (#477's Hall arm) reproduce #477's recorded deterministic receipts exactly, and only the Hall candidate generator differs in `arm=n1`. Propagation, force, branching, record format and caps are unchanged. arm=none : empty + cardinality-on-greedy-clique cuts only (== #477 ablation) arm=greedy : + maximum-matching Hall on ascending-greedy clique candidates (== #477 hall) arm=n1 : + maximum-matching Hall on MAXIMAL cliques of the current separated graph (the N1 strengthening of #564: inclusion-minimal violator extraction over the full separated structure, not greedy seeds) """ import json, time, itertools, resource, sys, hashlib from pathlib import Path sys.setrecursionlimit(5000) class Cap(Exception): pass class Sat(Exception): def __init__(self, phases): self.phases = phases def tuples(d): return [[q, b] for q in sorted(d) for b in range(q) if d[q] >> b & 1] def max_cliques(ids, e, tick, cap=20000): adj = {a: set() for a in ids} for a in ids: for b in ids: if b > a and (e[a] >> b) & 1: adj[a].add(b) adj[b].add(a) out = [] def bk(R, P, X): tick() if len(out) >= cap: return if not P and not X: out.append(sorted(R)) return pivot = max(P | X, key=lambda u: len(P & adj[u])) if (P or X) else None for v in sorted(P - (adj[pivot] if pivot is not None else set())): bk(R | {v}, P & adj[v], X & adj[v]) P -= {v} X |= {v} bk(set(), set(ids), set()) return out def solve(slots, primes, arm, cpu_limit=54, node_limit=200000, byte_limit=2000000, clique_cap=10**9, decision_stop=10**9): started = time.process_time() deadline = started + max(0, cpu_limit - min(1, cpu_limit / 20)) records = [] bytes_used = 0 stats = {'decisions': 0, 'nodes': 0, 'hall_leaves': 0, 'noncardinality_hall_leaves': 0, 'cardinality_leaves': 0, 'empty_leaves': 0, 'deleted_values': 0, 'maxcliques_examined': 0, 'nodes_with_pruned_block': 0} def tick(): if time.process_time() >= deadline: raise Cap('cpu') rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss if sys.platform != 'darwin': rss *= 1024 if rss >= 900000000: raise Cap('ram_headroom') blocks = {i: 1 << i for i in range(len(slots))} dom = {i: {q: (1 << ((-s) % q)) | (1 << ((-s - 2) % q)) for q in primes} for i, s in enumerate(slots)} edges = {i: 0 for i in blocks} def force(d, e): ids = sorted(d) for ix, a in enumerate(ids): tick() for b in ids[ix + 1:]: if not any(v & d[b].get(q, 0) for q, v in d[a].items()): e[a] |= 1 << b e[b] |= 1 << a force(dom, edges) def propagate(d, e): delta = [] changed = True while changed: changed = False for b in sorted(d): tick() if not d[b]: return delta, b if len(d[b]) != 1: continue q, v = next(iter(d[b].items())) for a in sorted(d): if a == b or q not in d[a]: continue old = d[a][q] new = 0 if e[a] >> b & 1 else old & v if new != old: removed = old & ~new delta.append([a, q, removed]) stats['deleted_values'] += removed.bit_count() if new: d[a][q] = new else: del d[a][q] changed = True if not d[a]: return delta, a return delta, None def candidates(ids, e): for seed in ids: c = [seed] for b in ids: if b != seed and all(e[b] >> a & 1 for a in c): c.append(b) yield c def matching(c, d): mate = {} def augment(a, seen): for q in sorted(d[a]): if q in seen: continue seen.add(q) if q not in mate or augment(mate[q], seen): mate[q] = a return True return False for a in c: tick() if not augment(a, set()): W = {a} Z = set() todo = [a] while todo: u = todo.pop() for q in d[u]: if q not in Z: Z.add(q) if q in mate and mate[q] not in W: W.add(mate[q]) todo.append(mate[q]) assert len(Z) < len(W) return None, sorted(W) return {a: q for q, a in mate.items()}, None def add(row): nonlocal bytes_used encoded = json.dumps(row, separators=(',', ':')) bytes_used += len(encoded.encode()) + 1 if bytes_used > byte_limit: raise Cap('proof_bytes') records.append(row) return len(records) - 1 def visit(bl, d, e): tick() stats['nodes'] += 1 delta, empty = propagate(d, e) if empty is not None: stats['empty_leaves'] += 1 return add(['e', empty, delta]) force(d, e) ids = sorted(bl) if any(len(d[a]) < len(primes) for a in ids): stats['nodes_with_pruned_block'] += 1 for c in candidates(ids, e): tick() if len(c) > len(primes): stats['cardinality_leaves'] += 1 return add(['k', c, delta]) if arm in ('greedy', 'n1'): assignment, W = matching(c, d) if W: stats['hall_leaves'] += 1 if len(W) <= len(primes): stats['noncardinality_hall_leaves'] += 1 return add(['h', W, delta]) if arm == 'n1': # Test maximal separated sets, not only greedy seeds (bounded per node). seen_cliques = 0 for c in max_cliques(ids, e, tick, cap=clique_cap): stats['maxcliques_examined'] += 1 seen_cliques += 1 tick() if len(c) > len(primes): continue # already covered by the cardinality cut if len(c) < 2: continue assignment, W = matching(c, d) if W: stats['hall_leaves'] += 1 stats['noncardinality_hall_leaves'] += 1 return add(['h', W, delta]) pairs = [] for ix, a in enumerate(ids): for b in ids[ix + 1:]: if not (e[a] >> b & 1): common = {q: d[a][q] & d[b].get(q, 0) for q in d[a] if d[a][q] & d[b].get(q, 0)} pairs.append((len(common), a, b, common)) if not pairs: assignment, W = matching(ids, d) if W: stats['hall_leaves'] += 1 return add(['h', W, delta]) chosen = {q: (d[a][q] & -d[a][q]).bit_length() - 1 for a, q in assignment.items()} for q in primes: chosen.setdefault(q, 0) raise Sat(chosen) if stats['decisions'] >= node_limit or stats['decisions'] >= decision_stop: raise Cap('decisions') stats['decisions'] += 1 _, a, b, common = min(pairs, key=lambda x: x[:3]) merged = tuples(common) lb = dict(bl) lb[a] |= lb.pop(b) ld = {i: dict(v) for i, v in d.items() if i != b} ld[a] = dict(common) le = {i: v for i, v in e.items() if i != b} le[a] = (e[a] | e[b]) & ~((1 << a) | (1 << b)) for i in le: if i != a: if (e[i] >> a & 1) or (e[i] >> b & 1): le[i] |= 1 << a le[i] &= ~(1 << b) left = visit(lb, ld, le) re = dict(e) re[a] |= 1 << b re[b] |= 1 << a right = visit(dict(bl), {i: dict(v) for i, v in d.items()}, re) return add(['b', a, b, left, right, merged, delta]) proof = None phases = None reason = None try: root = visit(blocks, dom, edges) status = 'UNSAT' proof = {'schema_version': 1, 'slots': slots, 'Q': primes, 'nodes': records, 'root': root} except Sat as found: status = 'SAT' phases = {str(q): b for q, b in sorted(found.phases.items())} except Cap as found: status = 'INCONCLUSIVE' reason = str(found) elapsed = time.process_time() - started if elapsed > cpu_limit: status = 'INCONCLUSIVE' reason = 'cpu_overrun' proof = None phases = None if proof and len(json.dumps(proof, separators=(',', ':')).encode()) + 1 > byte_limit: status = 'INCONCLUSIVE' reason = 'proof_bytes' proof = None receipt = {'status': status, 'cap_reason': reason, 'stats': stats, 'proof_record_bytes': bytes_used, 'cpu_seconds': elapsed, 'peak_rss_bytes': resource.getrusage(resource.RUSAGE_SELF).ru_maxrss * (1 if sys.platform == 'darwin' else 1024), 'hall_enabled': arm != 'none', 'limits': {'cpu_seconds': cpu_limit, 'decisions': node_limit, 'proof_bytes': byte_limit}, 'phases': phases} return receipt, proof if __name__ == '__main__': inp = json.loads(Path('coherence974-input.json').read_text()) arm = sys.argv[1] receipt, proof = solve(inp['slots'], inp['Q'], arm) if proof: Path(f'{arm}-proof1123.json').write_text(json.dumps(proof, separators=(',', ':')) + '\n') Path(f'{arm}-resource1123.json').write_text(json.dumps(receipt, sort_keys=True, indent=2) + '\n') deterministic = {k: v for k, v in receipt.items() if k not in ['cpu_seconds', 'peak_rss_bytes']} Path(f'{arm}-receipt1123.json').write_text(json.dumps(deterministic, sort_keys=True, indent=2) + '\n') print(arm, receipt['status'], receipt['cap_reason'] or '-')