"""Verify the 44 interior roots and the nine roots on each of three facets.""" from collections import Counter from fractions import Fraction as Q from itertools import permutations, product import json from math import prod from pathlib import Path from verify_boxes_taylor import centered_payoffs, check_box def derangements(players): return (p for p in permutations(players) if all(i != j for i, j in zip(players, p))) def last_payoff(row, coords): # In lexicographic corner order the last opponent bit varies fastest. values = row for x in reversed(coords[:4]): values = [(1-x)*a + x*b for a, b in zip(values[::2], values[1::2])] return values[0] def candidate_centers(rows): players = tuple(range(5)) thresholds = (Q(1, 2), Q(1, 5), Q(3, 10), Q(2, 5)) for assigned in derangements(players): coords = [None] * 5 for j, threshold in zip(assigned[:4], thresholds): coords[j] = threshold free = assigned[4] coords[free] = 0 left = last_payoff(rows[4], coords) coords[free] = 1 right = last_payoff(rows[4], coords) assert left * right < 0 coords[free] = Q(-left, right-left) yield '*****', tuple(coords) for state in ('0****', '1****', '****0'): active = [i for i in players if state[i] == '*'] roots = (thresholds if state[-1] == '0' else thresholds[1:] + (Q(9, 10) if state[0] == '0' else Q(1, 10),)) for assigned in derangements(active): coords = dict(zip(assigned, roots)) yield state, tuple(coords[i] for i in active) def verify(path): game = json.loads(path.read_text()) denominator = game['threshold_denominator'] rows = [] for i, thresholds in enumerate(game['thresholds']): opponents = [j for j in range(5) if j != i] rows.append([prod(denominator*bit - thresholds[j] for j, bit in zip(opponents, bits)) for bits in product((0, 1), repeat=4)]) rows.extend(game['corner_differences']) seen = set() counts = Counter() for state, center in candidate_centers(rows): assert (state, center) not in seen seen.add((state, center)) active = [i for i, action in enumerate(state) if action == '*'] forms = centered_payoffs(rows, state, active, center) assert all(forms[i][0] == 0 for i in active) index, _ = check_box(rows, state, center, Q(1, 2**40)) counts[state, index] += 1 return counts if __name__ == '__main__': counts = verify(Path(__file__).with_name('payoffs-three-facets.json')) for state, total in (('*****', 44), ('0****', 9), ('1****', 9), ('****0', 9)): positive, negative = counts[state, 1], counts[state, -1] assert positive + negative == total print(f'{state} {total} regular equilibria ({positive} positive, {negative} negative)')