"""Finite exact checks for 2608.24537. Run with python3 audit_nash.py.""" from fractions import Fraction from itertools import combinations, permutations, product from math import prod from random import Random def outcomes(matrix): for owners in product(range(len(matrix)), repeat=len(matrix[0])): utilities = [0] * len(matrix) for j, i in enumerate(owners): utilities[i] += matrix[i][j] yield owners, utilities def welfare_witnesses(): matrix = [(8, 4, 2, 1), (8, 4, 2, 1), (4, 2, 1, 8)] bundles = [(2,), (0, 1), (3,)] assert all(any(row[j] >= sorted(row)[-3] for j in bundle) for row, bundle in zip(matrix, bundles)) residual = [sum(matrix[0][g] for g in bundles[1] if g != removed) for removed in bundles[1]] assert min(residual) > matrix[0][2] assert all(prod(u) == 0 for _, u in outcomes([(2, 1, 0)] * 3)) assert sum([2, 1, 0]) - max([2, 1, 0]) > 0 print("EF1/top-three witness: own", matrix[0][2], "residual values", residual) candidates = [(owners, (1 + x)**10 * (1 + y)) for owners, (x, y) in outcomes([(2, 1)] * 2)] best = max(score for _, score in candidates) restricted = max(score for owners, score in candidates if len(set(owners)) == 2) assert best == 4**10 > restricted == 2 * 3**10 print("OptimalCompleteAllocation, all vs best enumerated:", best, restricted) for m in [4, 8, 12]: v = tuple(2**k for k in reversed(range(m))) leaves = ordered_pruning([v, v], objective="linear")[1] assert leaves == 2**m print("OptimalCompletion with linear welfare, goods/leaves:", m, leaves) for matrix, expected in [ ([(8, 4, 2, 1), (11, 7, 2, 1)], 81), ([(8, 4, 2, 1), (19, 13, 3, 2)], 150), ([(100, 40, 20, 19), (150, 100, 19, 1)], 14161), ]: assert max(prod(u) for _, u in outcomes(matrix)) == expected print("Three positive-valued introductory examples: optima confirmed") def ordered_pruning(matrix, objective): n, m = len(matrix), len(matrix[0]) def visit(j, u): if j == m: return (prod(u) if objective == "nash" else sum(u)), 1 results = [] for i, row in enumerate(matrix): vi = row[j] # For log, compare v_i(g)/u_i to v_k(g)/(u_k+remaining_k). # With all goods positive, zero base corresponds to infinite marginal. dominated = False for k in range(n): if k == i: continue if objective == "linear": dominated |= vi < matrix[k][j] else: dominated |= vi * (u[k] + sum(matrix[k][j+1:])) < matrix[k][j] * u[i] if not dominated: after = list(u) after[i] += vi results.append(visit(j+1, after)) return max(score for score, _ in results), sum(count for _, count in results) return visit(0, [0]*n) def ordered_checks(): rng = Random(37) count = 0 for n in (2, 3): for m in range(n, 7): for _ in range(10): matrix = [] for i in range(n): row = [] for j in range(m): row.insert(0, sum(row) + rng.randint(1, 5)) matrix.append(row) opt = max(prod(u) for _, u in outcomes(matrix)) score, leaves = ordered_pruning(matrix, "nash") assert score == opt and leaves <= (m+1)**(n*(n-1)//2) count += 1 print("Ordered positive Nash instances, exact optimum and leaf bound:", count) def doubling_nash(matrix): n, m = len(matrix), len(matrix[0]) def restricted(u, favorites, left): if not left: return prod(u) legal = [tuple(g for g in left if row[g] < row[top]) for row, top in zip(matrix, favorites)] best = [max(goods, key=row.__getitem__) if goods else None for row, goods in zip(matrix, legal)] s = max((i for i in range(n) if best[i] is not None), key=lambda i: Fraction(matrix[i][best[i]], 2*u[i])) g = best[s] choices = [] for i in range(n): if best[i] != g: continue dominated = any(matrix[i][g] * (u[k] + sum(matrix[k][t] for t in legal[k] if t != g)) < matrix[k][g] * u[i] for k in range(n) if k != i and g in legal[k]) if not dominated: updated = list(u) updated[i] += matrix[i][g] choices.append(restricted(updated, favorites, left-{g})) return max(choices) result = 0 for top in permutations(range(m), n): left = set(range(m))-set(top) if all(any(row[g] < row[j] for row, j in zip(matrix, top)) for g in left): result = max(result, restricted([row[j] for row, j in zip(matrix, top)], top, left)) return result def eptas_nash(matrix, eps): n, m = len(matrix), len(matrix[0]) L = 0 if n <= 2/eps: while 2**L < 2/eps and L < m-n: L += 1 result = 0 for owners in product(range(n), repeat=L): bonus = [0]*n for g, i in enumerate(owners, n): bonus[i] += matrix[i][g] # Enumerating all permutations computes exactly the matching objective. def matchval(p): return prod(row[j]+b for row, j, b in zip(matrix, p, bonus)) best = max(permutations(range(n)), key=matchval) after = [row[j]+b for row, j,b in zip(matrix, best, bonus)] after[0] += sum(matrix[0][n+L:]) result = max(result, prod(after)) return result def special_case_checks(): rng = Random(24537) checked = 0 for n in (2, 3): for m in range(n, 7): for _ in range(6): ordered = [] for i in range(n): row = [rng.randint(1, 3)] for j in range(m-1): row.insert(0, row[0]*2 + rng.randint(0, 3)) ordered.append(row) opt = max(prod(u) for _, u in outcomes(ordered)) for eps in (Fraction(3,4), Fraction(1,4)): assert eptas_nash(ordered, eps) >= (1-eps)**n * opt for row in ordered: rng.shuffle(row) opt = max(prod(u) for _, u in outcomes(ordered)) assert doubling_nash(ordered) == opt checked += 1 print("Positive-valued doubling algorithm / ordered EPTAS families:", checked, "(two EPTAS epsilons per instance)") def clone_products(n, counts, values): # Valid allocations: for each type, choose its set of distinct recipients. best = 0 recipient_sets = [list(combinations(range(n), c)) for c in counts] for distribution in product(*recipient_sets): u = [0]*n for value, recipients in zip(values, distribution): for recipient in recipients: u[recipient] += value best = max(best, prod(u)) # Block-balanced allocations: inject each block into the agents, discarding # assignments that duplicate a type for some agent across adjacent blocks. types = [t for t, c in enumerate(counts) for _ in range(c)] blocks = [types[j:j+n] for j in range(0, len(types), n)] minimum = None def place(b, used, u): nonlocal minimum if b == len(blocks): score = prod(u) minimum = score if minimum is None else min(minimum, score) return block = blocks[b] for owners in permutations(range(n), len(block)): if any(used[i] & (1<= threshold) == matching, (triples, opt, threshold) if matching: assert opt == threshold checked += 1 yes += matching assert Fraction(29,31) < Fraction(99959,100000)**162 print("3DM to Nash, exhaustive n=2 with 2 or 3 triples:", checked, "of which YES:", yes) print("4DM limiting gap: (29/31) < (99959/100000)^162 verified exactly") def config_gap_checks(): from math import exp, log for L in (6, 12, 24, 48): # Scale by 2^(L^2) and exponentiate logged welfare by 2L. # Both the allocation and fractional LP witness can then be compared # exactly by squaring, which clears the half-probability exponent. d = 2**(L*L) matrix = [(L*d, d+4, d, 2, 1), (L*d, 2, 1, d+4, d), (4, d+8, d, 2, 1), (4, 2, 1, d+8, d)] weights = (L-2, L-2, 2, 2) optimum = max(prod(a**w for a,w in zip(u, weights)) for _,u in outcomes(matrix)) fractional = (L*d*(2*d+4))**(2*(L-2)) * ((d+8)*d)**4 assert fractional >= optimum**2 and fractional <= 2**(2*L)*optimum**2 gap = exp((log(fractional)-2*log(optimum))/(4*L)) print(f"Appendix C, L={L}: feasible LP witness / exact integral optimum = {gap:.6f}") if __name__ == "__main__": welfare_witnesses() ordered_checks() special_case_checks() structural_checks() three_d_matching_checks() config_gap_checks()