"""Open cases of the three-move game T(a,b): does every game obey its translation rule? For one game this script computes the Sprague-Grundy table on the finite window of the reduction theorem (Lean: ThreeMove.reduceD / certifiedQ) and checks every hypothesis of that theorem. If all hold, the theorem gives, for this game and for ALL positions: (A_D) G(x+p, y+r) = G(x, y) for y >= x + D (B_D) G(x+q, y+q) = G(x, y) for X0 <= x <= y < x + (r-p) + D, q = p + r. So a line "OK" below is a computer-assisted proof for that game (table computed and checked by this script, not by Lean); a line "FAIL" means the rule with these parameters is false or needs other parameters. Near bands (2m-1)a <= b <= 2ma : (p, r) = (2ma+b, 2mb+a), D = 0, X0 = p. Far bands 2ma < b < (2m+1)a : (p, r) = 2(b-ma)(a,b) + a(b,a), D = a((2m+1)a-b), X0 = p + q (conjecture T5). Usage: python experiments/open_cases.py near MLO MHI AMAX | far AMAX MMAX QMAX """ import sys, json, math, time import numpy as np from multiprocessing import Pool sys.path.insert(0, 'experiments') from three_fast import sg_fast def params(a, b): k = b // a if b % a else None if b <= 2 * a: m = 1; kind = 'near' else: m = 1 while True: if (2 * m - 1) * a <= b <= 2 * m * a: kind = 'near'; break if 2 * m * a < b < (2 * m + 1) * a: kind = 'far'; break m += 1 if kind == 'near': p, r = 2 * m * a + b, 2 * m * b + a; D = 0; X0 = p else: al = 2 * (b - m * a); p, r = al * a + a * b, al * b + a * a; D = a * ((2 * m + 1) * a - b); X0 = p + (p + r) return kind, m, p, r, D, X0 def check(ab): a, b = ab; t0 = time.time() kind, m, p, r, D, X0 = params(a, b); q = p + r; c = b - a; W = r - p if not (a + b <= p and p + 2 * c <= r): return dict(a=a, b=b, kind=kind, m=m, ok=False, why='side conditions hp/hr') Y1 = max(a + b + D, max(b * ((x + p) // a) - r for x in range(a + b)), max(b * (x // a) for x in range(a + b))) X = X0 + p + q + (a + b) Y = max(Y1 + r + 1, X0 + q + (a + b) + D + c + r, X0 + p + b + W + D + q) G = sg_fast([(a, b), (b, a), (a + b, a + b)], X, Y) why = None # H1 (walls): x < a+b, x + D <= y <= Y1 (larger y follow from saturation, checked by hsat) for x in range(a + b): if not (b * (x // a) <= Y1 and b * ((x + p) // a) <= Y1 + r): why = 'hsat'; break ys = np.arange(x + D, Y1 + 1) if not np.array_equal(G[x + p, ys + r], G[x, ys]): why = f'H1 at x={x}'; break # H2: a+b <= x < X0 + q + (a+b), x + D <= y < x + D + (b-a) if why is None: xs = np.arange(a + b, X0 + q + (a + b)) for d in range(D, D + c): if not np.array_equal(G[xs + p, xs + d + r], G[xs, xs + d]): why = f'H2 at y-x={d}'; break # H3: X0 <= x < X0 + p + b, x <= y < x + W + D if why is None: xs = np.arange(X0, X0 + p + b) for d in range(0, W + D): if not np.array_equal(G[xs + q, xs + d + q], G[xs, xs + d]): why = f'H3 at y-x={d}'; break return dict(a=a, b=b, kind=kind, m=m, p=p, r=r, D=D, X0=X0, X=X, Y=Y, ok=why is None, why=why, sec=round(time.time() - t0, 1)) if __name__ == '__main__': mode = sys.argv[1] if mode == 'near': mlo, mhi, amax = map(int, sys.argv[2:5]) games = [(a, b) for m in range(mlo, mhi + 1) for a in range(1, amax + 1) for b in range((2 * m - 1) * a, 2 * m * a + 1) if math.gcd(a, b) == 1] tag = f'near_m{mlo}-{mhi}_a{amax}' else: amax, mmax, qmax = map(int, sys.argv[2:5]) games = [] for a in range(2, amax + 1): for m in range(1, mmax + 1): for b in range(2 * m * a + 1, (2 * m + 1) * a): if math.gcd(a, b) == 1: _, _, p, r, _, _ = params(a, b) if p + r <= qmax: games.append((a, b)) tag = f'far_a{amax}_m{mmax}_q{qmax}' games.sort(key=lambda g: g[0] * g[1] * g[1]) print(f'{tag}: {len(games)} games', flush=True) res = [] with Pool(int(sys.argv[5]) if len(sys.argv) > 5 else 60) as P: for k, t in enumerate(P.imap_unordered(check, games, chunksize=1)): res.append(t) if not t['ok']: print('FAIL', t, flush=True) if (k + 1) % 25 == 0: print(f' {k+1}/{len(games)} done, {sum(1 for u in res if u["ok"])} OK', flush=True) res.sort(key=lambda t: (t['m'], t['a'], t['b'])) json.dump(res, open(f'data/open_cases_{tag}.json', 'w')) ok = [t for t in res if t['ok']] print(f'RESULT {tag}: {len(ok)} of {len(res)} games satisfy every hypothesis of the reduction theorem; ' f'{len(res)-len(ok)} fail', flush=True)