"""Exact checks for 2610.14994. Run with python3 audit_subsidies.py.""" from itertools import permutations, product, combinations from fractions import Fraction as F from random import Random def bits(mask): while mask: bit = mask & -mask yield bit mask ^= bit def value_table(row): return [sum(row[bit.bit_length()-1] for bit in bits(mask)) for mask in range(1 << len(row))] def allocations(n, m): for owners in product(range(n), repeat=m): bundles = [0] * n for g, i in enumerate(owners): bundles[i] |= 1 << g yield tuple(bundles) def outcomes(v, a): return tuple(row[b] for row, b in zip(v, a)) def fair(v, a, efx): for i, row in enumerate(v): for b in a: if row[a[i]] < row[b]: comparisons = [row[a[i]] >= row[b ^ g] for g in bits(b)] if not (all(comparisons) if efx else any(comparisons)): return False return True def envy_freeable(v, a): current = sum(outcomes(v, a)) return all(sum(outcomes(v, b)) <= current for b in permutations(a)) def po(v, a, alternatives): current = outcomes(v, a) return all(u == current or any(x < y for x, y in zip(u, current)) for u in alternatives) def check_limit(v, support, efx, matrix): n = len(v) all_a = list(allocations(n, len(v[0]).bit_length() - 1)) possible = [a for a in all_a if fair(v, a, efx)] assert not any(envy_freeable(v, a) for a in possible) assert all(a in possible for a in support) if efx: utilities = [outcomes(v, a) for a in all_a] assert all(po(v, a, utilities) for a in support) actual = [[sum(v[i][a[i]] - v[i][a[j]] for a in support) for j in range(n)] for i in range(n)] assert actual == matrix print("allocations, fair, fair and envy-freeable:", len(all_a), len(possible), 0) def separations(): t, u = map(value_table, [(5, 8, 11, 0), (9, 1, 0, 10)]) v = [] for i in range(3): row = [] for mask in range(16): rotated = (mask & 8) | sum(1 << ((j-i) % 3) for j in range(3) if mask & (1 << j)) row.append(min(11, t[rotated]) + min(10, u[rotated])) assert all(row[s]+row[q] >= row[s & q]+row[s | q] and row[s | q] >= row[s] for s, q in product(range(16), repeat=2)) v.append(row) check_limit(v, [(5,2,8),(8,3,4),(1,8,6)], False, [[0,4,11],[11,0,4],[4,11,0]]) rows = [(26,1,8,2,12),(16,8,2,4,1),(8,16,4,1,2),(2,28,1,12,8)] for row in rows: ranked = sorted(row, reverse=True) assert all(x > sum(ranked[k+1:]) for k,x in enumerate(ranked)) v = list(map(value_table,rows)) check_limit(v, [(1,24,4,2),(20,1,2,8)], True, [[0,6,37,43],[2,0,11,9],[6,9,0,3],[29,18,11,0]]) def lex_construction(rows): v = list(map(value_table, rows)) m = len(rows[0]) goods = [1 << j for j in range(m)] order = [sorted(goods, key=row.__getitem__, reverse=True) for row in v] first = [r[0] for r in order] if goods else [] if m <= 3: candidates = permutations(goods + [0]*(3-m)) elif len(set(first)) == 3: a = list(first) a[2] |= ((1 << m)-1) ^ sum(first) candidates = [a] else: if len(set(first)) == 2: a, b = sorted(set(first)) else: a = first[0] second = [r[1] for r in order] if len(set(second)) == 3: s = min(range(3), key=lambda i: v[i][a]-v[i][second[i]]) b = second[s] else: b = max(second, key=second.count) candidates = permutations((a,b,((1 << m)-1)^a^b)) candidates = list(candidates) best = max(sum(outcomes(v,a)) for a in candidates) for a in candidates: if sum(outcomes(v,a)) == best: assert fair(v,a,True) and envy_freeable(v,a) # Greedy recognition of a serial picking sequence. Also test PO by # exhaustive utility comparison on the smaller profiles. remaining = (1 << m)-1 while remaining: for i,r in enumerate(order): g = next(g for g in r if g & remaining) if g & a[i]: remaining ^= g break else: raise AssertionError((rows,a)) if m <= 3: assert po(v,a,(outcomes(v,p) for p in allocations(3,m))) def lex_tests(): checked = 0 for m in range(4): perms = list(permutations([2**g for g in range(m)])) for rows in product(perms,repeat=3): lex_construction(rows) checked += 1 rng = Random(14994) for m in (4,5,6,7): for _ in range(300): rows = [] for i in range(3): row = [] for j in range(m): row.append(sum(row) + rng.randint(1,8)) rng.shuffle(row) rows.append(row) lex_construction(rows) checked += 1 print("three-agent lexicographic construction profiles:", checked) def subsidy_tests(): rng = Random(2610) checked = 0 for n in (2,3,4): for _ in range(60): # An arbitrary signed matrix of bundle evaluations and entitlements. w = [rng.randint(1,4) for _ in range(n)] val = [[F(rng.randint(-9,9), w[j]) for j in range(n)] for i in range(n)] for p in permutations(range(n)): v = [[row[j] for j in p] for row in val] identity = sum(v[i][i] for i in range(n)) stable = all(identity >= sum(v[i][q[i]] for i in range(n)) for q in permutations(range(n))) edge = [[v[i][j]-v[i][i] for j in range(n)] for i in range(n)] def longest(i): tails = [j for j in range(n) if j != i] return max(sum((edge[a][b] for a,b in zip((i,)+path, path)), F(0)) for length in range(n) for path in permutations(tails,length)) s = [longest(i) for i in range(n)] assert min(s) >= 0 assert stable == all(s[i] >= edge[i][j]+s[j] for i in range(n) for j in range(n)) checked += 1 print("signed reassignment and longest-path subsidy comparisons:", checked) if __name__ == "__main__": separations() lex_tests() subsidy_tests() def dual_checks(): """Solve restricted lottery LPs, then verify both kinds of certificates exactly.""" from scipy.optimize import linprog rng = Random(343) n, m = 3, 4 edges = [(i,j) for i in range(n) for j in range(n) if i != j] ans = [0,0] for _ in range(100): vals = [[0]+[rng.randint(-9,9) for _ in range(2**m-1)] for _ in range(n)] weights = [rng.randint(1,3) for _ in range(n)] choices = rng.sample(list(allocations(n,m)), rng.randint(1,20)) d = [[F(vals[i][a[i]],weights[i])-F(vals[i][a[j]],weights[j]) for i,j in edges] for a in choices] c = [0]*len(choices)+[-1] r = linprog(c, A_ub=[[-x for x in col]+[1] for col in zip(*d)], b_ub=[0]*len(edges), A_eq=[[1]*len(choices)+[0]], b_eq=[1], bounds=[(0,None)]*len(choices)+[(None,None)]) assert r.success if r.fun <= 1e-8: q = [F(x).limit_denominator(10**8) for x in r.x[:-1]] assert sum(q)==1 and min(q)>=0 and all(sum(t*x for t,x in zip(q,col))>=0 for col in zip(*d)) ans[0] += 1 continue dual = [F(-x).limit_denominator(10**8) for x in r.ineqlin.marginals] dot = lambda a,b: sum(x*y for x,y in zip(a,b)) delta = -max(dot(dual,row) for row in d) assert delta > 0 and sum(dual)==1 u = [F(1,len(edges))]*len(edges) eps = delta/(2*(delta+max(abs(dot(u,row)) for row in d))) lam = [(1-eps)*l+eps*t for l,t in zip(dual,u)] assert min(lam)>0 and max(dot(lam,row) for row in d)<0 rates = [sum(l for l,(k,j) in zip(lam,edges) if k==i) for i in range(n)] Q = {(i,j):l/rates[i] for l,(i,j) in zip(lam,edges)} # The three directed in-trees rooted at each vertex. mass=[] for i in range(n): j,k = (a for a in range(n) if a!=i) mass.append(Q[j,i]*Q[k,i]+Q[j,i]*Q[k,j]+Q[k,i]*Q[j,k]) pi = [a/sum(mass) for a in mass] scale = [r/p for r,p in zip(rates,pi)] flow = {(i,j):pi[i]*Q[i,j] for i,j in edges} for i in range(n): assert sum(flow[i,j] for j in range(n) if i!=j)==sum(flow[j,i] for j in range(n) if i!=j) assert all(l==scale[i]*flow[i,j] for l,(i,j) in zip(lam,edges)) for a in choices: def assigned(p): return sum(scale[i]*F(vals[i][a[j]],weights[j]) for i,j in enumerate(p)) assert max(assigned(p) for p in permutations(range(n)))>assigned(range(n)) ans[1] += 1 print("weighted LPs with exact feasible lotteries / exact scaled impossibility certificates:", *ans) if __name__ == "__main__": dual_checks()