#!/usr/bin/env python3 """Route 97 triage: measure the LP-relaxation gap of the two-class covering run at small n. LP relaxation of the cover feasibility (fractional set cover): variables x[p][r] >= 0, sum_r x[p][r] = 1 for each prime p (residue r in {1..p-1} \\ {skip}); for each position pos in [1..M]: sum_p ( x[p][pos mod p] * [pos mod p != skip] + x[p][(pos+skip) mod p] * [(pos+skip) mod p != skip] ) >= 1. The max M with a feasible fractional cover is the LP bound (an upper bound on the true maxm, a(n) = 6*maxm+5). The integrality gap = LP bound vs true maxm. Deterministic (scipy linprog, method='highs'). """ from scipy.optimize import linprog import math plist = [5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103] pskip = [2,5,4,9,6,13,8,10,21,25,14,29,16,18,20,41,45,24,49,53,28,30,65,34,69] # true maxm for n=15..19 (a(n)=6*maxm+5): a=707,869,965,1079,1283 true_a = {15:707, 16:869, 17:965, 18:1079, 19:1283} true_maxm = {n:(true_a[n]-5)//6 for n in true_a} def lp_feasible(k, M): """Is there a fractional residue distribution covering positions 1..M by primes plist[0..k-1]?""" # variable index map: (prime idx, residue) -> var index var_idx = {} idx = 0 for ki in range(k): p = plist[ki]; skip = pskip[ki] for r in range(1, p): if r != skip: var_idx[(ki, r)] = idx idx += 1 nv = idx if nv == 0: return False # equality: sum_r x[ki,r] = 1 for each ki -> A_eq A_eq = [] b_eq = [] for ki in range(k): row = [0.0]*nv for (ki2, r), ix in var_idx.items(): if ki2 == ki: row[ix] = 1.0 A_eq.append(row); b_eq.append(1.0) # inequality: coverage >= 1 for each pos in 1..M -> A_ub x >= 1 => -A_ub x <= -1 A_ub = [] b_ub = [] for pos in range(1, M+1): row = [0.0]*nv for (ki, r), ix in var_idx.items(): p = plist[ki]; skip = pskip[ki] a = pos % p if a != skip and a >= 1: row[ix] += 1.0 if a == r else 0.0 b = (pos + skip) % p if b != skip and b >= 1: row[ix] += 1.0 if b == r else 0.0 A_ub.append([-v for v in row]); b_ub.append(-1.0) c = [0.0]*nv res = linprog(c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=[(0,1)]*nv, method='highs') return res.status == 0 def lp_bound(k, lo=0, hi=400): """Binary search the max feasible M.""" while lo < hi: mid = (lo + hi + 1)//2 if lp_feasible(k, mid): lo = mid else: hi = mid - 1 return lo if __name__ == "__main__": print("route97 LP-relaxation gap (fractional set cover of positions 1..M)") for n in (15, 16, 17, 18, 19): k = n - 2 M = lp_bound(k, hi=260) tm = true_maxm[n] print(f"n={n}: LP bound maxm = {M} (a={6*M+5}) vs true maxm={tm} (a={true_a[n]}) gap_factor={M/tm:.3f}")