"""Independently certify regular equilibria in rational five-player binary games. Run with a game JSON and a root-box JSON. All tests use exact fractions. For each support, center the multilinear Taylor polynomial on the box and left multiply the mixing equations by the inverse center Jacobian. The affine term of this system is the identity. The remaining terms give explicit self-mapping and contraction tests in the maximum norm. """ import argparse from collections import Counter, defaultdict from fractions import Fraction from hashlib import sha256 import json from pathlib import Path def require(condition, message): if not condition: raise ValueError(message) def centered_payoffs(rows, state, active, center): """Convert exact corner values on the support face to powers of x-center.""" count = 1 << len(active) forms = [[] for _ in rows] for mask in range(count): profile = [int(value) if value != '*' else 0 for value in state] for j, player in enumerate(active): profile[player] = (mask >> j) & 1 for player, row in enumerate(rows): index = 0 for opponent, bit in enumerate(profile): if opponent != player: index = 2 * index + bit forms[player].append(row[index]) # For f linear in one variable, the constant and linear coefficients # at c are f(0)+c*(f(1)-f(0)) and f(1)-f(0). Apply on every axis. for values in forms: for j, coordinate in enumerate(center): bit = 1 << j for mask in range(count): if not mask & bit: delta = values[mask | bit] - values[mask] values[mask] += coordinate * delta values[mask | bit] = delta return forms def normalize_equations(forms, active, masks): """Reduce [J | Taylor coefficients] to [I | J^{-1} coefficients].""" n = len(active) columns = [1 << j for j in range(n)] + masks matrix = [[Fraction(forms[i][m]) for m in columns] for i in active] determinant_sign = 1 for j in range(n): pivot = next((i for i in range(j, n) if matrix[i][j]), None) require(pivot is not None, 'singular center Jacobian') if pivot != j: matrix[j], matrix[pivot] = matrix[pivot], matrix[j] determinant_sign *= -1 divisor = matrix[j][j] if divisor < 0: determinant_sign *= -1 matrix[j] = [x / divisor for x in matrix[j]] for i in range(n): if i != j and matrix[i][j]: factor = matrix[i][j] matrix[i] = [a - factor*b for a, b in zip(matrix[i], matrix[j])] return determinant_sign, [row[n:] for row in matrix] def check_box(rows, state, center, radius): active = [i for i, v in enumerate(state) if v == '*'] n = len(active) require(len(center) == n, 'center dimension disagrees with support') require(all(radius < x < 1-radius for x in center), 'box meets a support boundary') forms = centered_payoffs(rows, state, active, center) degrees = [m.bit_count() for m in range(1 << n)] powers = [radius**k for k in range(n + 1)] for i, (action, values) in enumerate(zip(state, forms)): if action != '*': variation = sum(abs(values[m]) * powers[degrees[m]] for m in range(1, 1 << n)) margin = (2*int(action)-1) * values[0] require(margin > variation, f'pure player {i} lacks a strict sign on the box') nonlinear = [m for m, degree in enumerate(degrees) if degree >= 2] det_sign, normalized = normalize_equations(forms, active, [0] + nonlinear) worst_slope = Fraction(0) for value, *higher in normalized: # The normalized fixed-point map has displacement at most the # constant plus the nonlinear terms. Its linear term cancels. displ = abs(value) slope = Fraction(0) for mask, coeff in zip(nonlinear, higher): degree = degrees[mask] displ += abs(coeff) * powers[degree] slope += abs(coeff) * degree * powers[degree-1] require(displ < radius, 'fixed-point map exits box') require(slope < 1, 'fixed-point map lacks a strict contraction bound') worst_slope = max(worst_slope, slope) # Throughout the box the normalized Jacobian is within norm one of # the identity. Its determinant has the sign at the center. return (-1)**n * det_sign, worst_slope def verify(game, certificate): rows = game['corner_differences'] require(len(rows) == 5 and all(len(row) == 16 for row in rows), 'expected five 16-entry rows') require(all(type(v) is int for row in rows for v in row), 'expected integer payoffs') cb, rb = certificate['center_bits'], certificate['radius_bits'] require(all(type(k) is int and k > 0 for k in (cb, rb)), 'invalid dyadic precision') radius = Fraction(1, 1 << rb) by_support = defaultdict(list) counts = Counter() worst = Fraction(0) for number, box in enumerate(certificate['roots'], 1): state = box['state'] require(len(state) == 5 and set(state) <= set('01*'), 'invalid support') require(all(type(v) is int for v in box['center']), 'invalid center numerator') center = tuple(Fraction(v, 1 << cb) for v in box['center']) try: index, slope = check_box(rows, state, center, radius) for other in by_support[state]: require(any(abs(x-y) > 2*radius for x, y in zip(center, other)), 'two reported boxes on this support overlap') except ValueError as exc: raise ValueError(f'box {number} on support {state}: {exc}') from exc by_support[state].append(center) counts[len(center), index] += 1 worst = max(worst, slope) return counts, worst if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('game', type=Path) parser.add_argument('boxes', type=Path) args = parser.parse_args() game_bytes, box_bytes = args.game.read_bytes(), args.boxes.read_bytes() counts, worst = verify(json.loads(game_bytes), json.loads(box_bytes)) print('Game SHA-256', sha256(game_bytes).hexdigest()) print('Boxes SHA-256', sha256(box_bytes).hexdigest()) for sign in (1, -1): print('Index', sign, 'by number of mixers:', [counts[k, sign] for k in range(6)]) print('Distinct strictly regular equilibria certified:', sum(counts.values())) print('Index sum on these boxes:', sum(sign*n for (_, sign), n in counts.items())) print('Maximum contraction bound (decimal approximation):', float(worst))