"""Verify rational contraction boxes for regular Nash equilibria in a binary game. Usage: python3 verify_boxes.py game.json boxes.json All arithmetic after reading the input is exact. Python 3.9+ is sufficient. """ import argparse import json from fractions import Fraction from functools import lru_cache from itertools import combinations, product from pathlib import Path def interpolate(corners, point): # The last opponent bit varies fastest. values = corners for x in reversed(point): values = [a+x*(b-a) for a,b in zip(values[::2],values[1::2])] return values[0] def difference(table, dimension, j): stride = 1 << (dimension-1-j) return [table[h+stride]-v for h,v in enumerate(table) if not h & stride] def restrict(rows, state): mixed = [j for j,bit in enumerate(state) if bit == '*'] tables = [[] for row in rows] for bits in product((0,1),repeat=len(mixed)): point = list(state) for j,bit in zip(mixed,bits): point[j] = bit for i,table in enumerate(tables): index = 0 for j,bit in enumerate(point): if j!=i: index = 2*index+int(bit) table.append(rows[i][index]) slopes, curvature = [], [] for table in tables: dim = len(mixed) derivatives = [difference(table,dim,j) for j in range(dim)] slopes.append(derivatives) # Twice the sum over unordered distinct variables counts the ordered # second partials. The repeated second partials vanish by multilinearity. curvature.append(2*sum(max(map(abs,difference(derivatives[j],dim-1,k-1))) for j,k in combinations(range(dim),2))) return mixed,tables,slopes,curvature def inverse_and_sign(matrix): n = len(matrix) work = [[Fraction(v) for v in row]+[Fraction(i==k) for k in range(n)] for i,row in enumerate(matrix)] sign = 1 for j in range(n): p = next((i for i in range(j,n) if work[i][j]),None) if p is None: raise ValueError('Singular center Jacobian') if p!=j: work[j],work[p]=work[p],work[j] sign=-sign diagonal=work[j][j] if diagonal<0: sign=-sign work[j]=[v/diagonal for v in work[j]] for i in range(n): if i!=j and work[i][j]: factor=work[i][j] work[i]=[a-factor*b for a,b in zip(work[i],work[j])] return [row[n:] for row in work],sign def verify(rows, boxes): if len(rows)!=5 or any(len(row)!=16 or any(type(v) is not int for v in row) for row in rows): raise ValueError('Expected five rows of sixteen integer corner payoffs') den = 2**boxes['center_bits'] radius = Fraction(1,2**boxes['radius_bits']) local_data = lru_cache(maxsize=None)(lambda state: restrict(rows,state)) by_state = {} counts,indices = [0]*6,[0]*6 for record in boxes['roots']: state = record['state'] if len(state)!=5 or any(bit not in '01*' for bit in state): raise ValueError('Invalid support') mixed,tables,slopes,curvature=local_data(state) dim=len(mixed) if len(record['center'])!=dim or any(type(v) is not int for v in record['center']): raise ValueError('Invalid dyadic center') point=[Fraction(v,den) for v in record['center']] if not all(radius=1 or offset+q*radius>=radius: raise ValueError('The Newton map fails the contraction test') index=(-1)**dim*orientation counts[dim]+=1 indices[dim]+=index return counts,indices if __name__ == '__main__': parser=argparse.ArgumentParser(description=__doc__) parser.add_argument('game',type=Path) parser.add_argument('boxes',type=Path) args=parser.parse_args() counts,indices=verify(json.loads(args.game.read_text())['corner_differences'], json.loads(args.boxes.read_text())) print(f'{sum(counts)} distinct strict regular equilibria. Layers {counts}') print(f'Index sum {sum(indices)}. Layer indices {indices}')