#!/usr/bin/env python3 """Job #1068 (route 13 rev5): does the union slack grow with k - is F1/k nondecreasing and positive? Route's method, implemented as stated: on the same three primes (127, 151, 181) and the same four deep starts per prime (a = 10007, 20011, 40009, 60013) and the same definitions as return #439, form every 3-subset and the single 4-subset of the members, compute F1 with w_s = 1, and fit F1/k against k; run the partitioned-phase LP only for unions the counting rule leaves open, with a cap; re-check every certificate with the pinned kernel and an independent integer checker. Definitions, unchanged from #439 (so the numbers are comparable): member at (p, a) = the counting-silent predecessor prefix slots[:N_F - 1], where N_F is the first prefix with F1 = |D| - sum_q max_b |K_D(q,b)| > 0 (so F1(member) <= 0 by construction); k-fold union U = sorted(D_{i1} + ... + D_{ik}) truncated to the common N = min_i |D_i|; Q = the primes in (p, 2p]; decisive = exact uniform weights w_s = 1 with sum_q max_b W(q,b) < sum_s w_s. #439's slot lists are not carried in its artifact, but its member and union sha256 values are, so every regenerated member is verified against those hashes instead of being trusted: the objects are proved identical, not assumed identical. k = 2 is recomputed as well and cross-checked against #439's recorded pair F1 values, which is a direct consistency test between the two returns. The rule is evaluated on the conservative reading: for each prime, is min over k-subsets of F1/k nondecreasing in k = 2, 3, 4 and positive. Means are reported too, but the verdict uses the minimum. stdout is the artifact (deterministic, no timing); progress and timing go to stderr. """ import argparse import hashlib import itertools import json import sys import time from collections import Counter from pathlib import Path sys.path.insert(0, '../tightcert') import tightcert as tc # noqa: E402 T0 = time.process_time() CAP = 20000 STARTS = [10007, 20011, 40009, 60013] PRIMES = [127, 151, 181] def indep_f1(D, qs): return len(D) - sum(max(sum(1 for s in D if (s + b) % q == 0 or (s + b + 2) % q == 0) for b in range(q)) for q in qs) def indep_weights_ok(D, qs, w): total = 0 for q in qs: best = 0 for b in range(q): m = sum(ws for s, ws in zip(D, w) if (s + b) % q == 0 or (s + b + 2) % q == 0) best = max(best, m) total += best return total < sum(w) def load_members(src): """Regenerate the members, then prove each one equals #439's by its recorded sha256.""" ref = {(m['p'], m['a']): m for m in src['members']} mem = {} for p in PRIMES: qs = [n for n in range(p + 1, 2 * p + 1) if tc.is_prime(n)] for a in STARTS: _, qs2, slots = tc.prime_band(p, a, CAP) assert qs2 == qs n_f = None for N in range(1, len(slots) + 1): if tc.budget(slots[:N], qs)[0] > 0: n_f = N break assert n_f >= 2 D = slots[:n_f - 1] sha = hashlib.sha256(','.join(map(str, D)).encode()).hexdigest() r = ref[(p, a)] assert sha == r['slot_sha256'], ('regenerated member differs from #439', p, a) assert len(D) == r['N_member'] and n_f == r['N_F'] f1, _ = tc.budget(D, qs) assert f1 == r['F1_member'] and indep_f1(D, qs) == f1 mem[(p, a)] = {'p': p, 'a': a, 'qs': qs, 'N_F': n_f, 'slots': D, 'F1': f1, 'slot_sha256': sha} print('member p=%d a=%d N_F=%d N=%d F1=%d (sha matches #439)' % (p, a, n_f, len(D), f1), file=sys.stderr) return mem def union_of(mems, qs): N = min(len(m['slots']) for m in mems) parts = [m['slots'][:N] for m in mems] for x, y in itertools.combinations(parts, 2): assert not (set(x) & set(y)), 'members overlap' U = sorted(s for part in parts for s in part) return N, U def main(): ap = argparse.ArgumentParser() ap.add_argument('--cpu-budget-seconds', type=float, default=150.0) ap.add_argument('--lp-iters', type=int, default=1200) ap.add_argument('--lp-dual-iters', type=int, default=1000) ap.add_argument('--source', default='../job1064/pairs1064.json') args = ap.parse_args() src = json.loads(Path(args.source).read_text(encoding='utf-8')) ref_pairs = {(r['p'], tuple(r['pair_starts'])): r['F1_uniform'] for r in src['pairs']} out = {'job': 1068, 'route_id': 13, 'revision': 5, 'question': ('Does the union slack grow with the number of supports - is F1/k ' 'nondecreasing in k and is the per-support coefficient positive at every ' 'prime?'), 'definitions': src.get('definition') or src.get('definitions'), 'source': {'file': '#439 pairs1064.json (regenerated members verified against its ' 'recorded slot_sha256, then hashed again here)', 'retained_f1_gain_range': src['summary']}, 'primes': PRIMES, 'starts': STARTS, 'cap': CAP, 'verdict_rule': ('conservative: min over k-subsets of F1/k must be nondecreasing in ' 'k = 2, 3, 4 and positive at every prime; means reported but not used ' 'for the verdict'), 'cpu_budget_seconds': args.cpu_budget_seconds, 'members': [], 'unions': [], 'fit': {}, 'cross_check_vs_439': []} mem = load_members(src) for key in sorted(mem): m = mem[key] out['members'].append({'p': m['p'], 'a': m['a'], 'N_F': m['N_F'], 'N_member': len(m['slots']), 'F1_member': m['F1'], 'slot_sha256': m['slot_sha256']}) pending = [] for p in PRIMES: qs = mem[(p, STARTS[0])]['qs'] for k in range(2, len(STARTS) + 1): for combo in itertools.combinations(STARTS, k): mems = [mem[(p, a)] for a in combo] N, U = union_of(mems, qs) f1, _ = tc.budget(U, qs) assert indep_f1(U, qs) == f1 rec = {'p': p, 'k': k, 'members': list(combo), 'common_N': N, 'union_size': len(U), 'F1_union': f1, 'F1_per_k': f1 / k, 'member_F1_sum': sum(m['F1'] for m in mems), 'gain_over_members': f1 - sum(m['F1'] for m in mems), 'U_sha256': hashlib.sha256(','.join(map(str, U)).encode()).hexdigest()} if k == 2: ref = ref_pairs.get((p, tuple(sorted(combo)))) rec['F1_cross_check_439'] = ref out['cross_check_vs_439'].append({'p': p, 'starts': list(combo), 'this': f1, 'return_439': ref, 'agree': ref == f1}) if f1 > 0: rec['rule'] = 'uniform_weights' rec['certificate'] = {'kind': 'noncoverable', 'rule': 'counting', 'slots': U, 'weights': [1] * len(U), 'ratio': [len(U) - f1, len(U)]} rec['kernel_verify_ok'] = bool(tc.verify(rec['certificate'], U, qs)[0]) rec['independent_check'] = indep_weights_ok(U, qs, [1] * len(U)) else: rec['rule'] = 'pending_lp' pending.append((p, U, qs, rec)) out['unions'].append(rec) for p, U, qs, rec in pending: if time.process_time() - T0 > args.cpu_budget_seconds: rec['rule'] = 'undecided' rec['note'] = 'capped: LP not attempted within the allocation' continue cert = tc.certify(U, qs, weights=True, iters=args.lp_iters, dual_iters=args.lp_dual_iters, force_weights=True) rec['kernel_rule'] = cert.get('rule') rec['kernel_verify_ok'] = bool(tc.verify(cert, U, qs)[0]) if cert.get('rule') == 'weighted': rec['rule'] = 'weighted_certificate' rec['certificate'] = {'kind': cert['kind'], 'rule': 'weighted', 'slots': cert['slots'], 'weights': cert['weights'], 'ratio': cert['ratio']} rec['independent_check'] = indep_weights_ok(cert['slots'], qs, cert['weights']) else: rec['rule'] = 'undecided' rec['note'] = 'undecided within the LP budget' ok_all = True for p in PRIMES: rows = [r for r in out['unions'] if r['p'] == p] mins, means = {}, {} for k in range(2, len(STARTS) + 1): vals = [r['F1_per_k'] for r in rows if r['k'] == k] mins[k] = min(vals) means[k] = sum(vals) / len(vals) mono = all(mins[k] <= mins[k + 1] for k in range(2, len(STARTS))) pos = all(mins[k] > 0 for k in mins) ok_all = ok_all and mono and pos out['fit'][str(p)] = {'F1_per_k_min': {str(k): mins[k] for k in mins}, 'F1_per_k_mean': {str(k): means[k] for k in means}, 'min_nondecreasing': mono, 'min_positive': pos, 'F1_union_min_by_k': {str(k): min(r['F1_union'] for r in rows if r['k'] == k) for k in range(2, len(STARTS) + 1)}} rules = Counter(r['rule'] for r in out['unions']) out['summary'] = {'unions': len(out['unions']), 'by_k': dict(sorted(Counter(r['k'] for r in out['unions']).items())), 'by_rule': dict(sorted(rules.items())), 'cross_check_vs_439_all_agree': all(c['agree'] for c in out['cross_check_vs_439']), 'all_certificates_pass': all(r.get('kernel_verify_ok') is not False and r.get('independent_check') is not False for r in out['unions']), 'min_nondecreasing_at_every_prime': ok_all} if rules.get('uniform_weights', 0) == len(out['unions']) and ok_all: out['outcome'] = ('success: F1/k is positive and nondecreasing in k at every prime, so a ' 'k-fold union carries a counting certificate whose margin grows with k') elif rules.get('uniform_weights', 0) == len(out['unions']): out['outcome'] = ('failure: every union is counted, but F1/k is NOT nondecreasing in k at ' 'every prime, so the slack has a pairwise ceiling') else: out['outcome'] = ('inconclusive: at least one union needed the LP or stayed undecided ' '(%s)' % dict(rules)) sys.stdout.reconfigure(newline='\n') text = json.dumps(out, sort_keys=True, indent=2) + '\n' Path('kfold1068.json').write_text(text, encoding='utf-8', newline='\n') print(text, end='') print(json.dumps({'cpu_seconds': time.process_time() - T0}), file=sys.stderr) return 0 if __name__ == '__main__': raise SystemExit(main())