"""What is the maximum achievable win rate in the paper's TicTacToe setup? Sec 4.4 reports ST-GFN winning 45+-4% of games as X against "a minimax opponent with 90% optimal play". TicTacToe is a first-player draw under perfect play, so an agent can only win when the opponent takes one of its 10% random moves AND that move is a losing blunder. We measure the ceiling directly by playing a *perfect* minimax agent (the best any method could possibly be) against the same stochastic opponent, over many games and several opponent-optimality levels. """ from __future__ import annotations import json import numpy as np from envs import TicTacToeEnv def play_perfect(env, n_games, rng): wins = draws = losses = 0 for _ in range(n_games): s = env.reset() while True: va = env.valid_actions(s) if not va: break _, move = env._minimax(s, 1) # perfect play for X if move is None: move = va[rng.randint(len(va))] s, done = env.step(s, move, rng) if done: break w = env._winner(s) wins += w == 1 draws += w == 0 losses += w == -1 n = max(n_games, 1) return 100.0 * wins / n, 100.0 * draws / n, 100.0 * losses / n class TicTacToeUniformOptimal(TicTacToeEnv): """Variant B: the opponent's "optimal" move is drawn uniformly from *all* moves preserving its minimax value, rather than a single fixed choice. This is the other natural reading of "90% optimal play".""" def step(self, state, action, rng): b = list(state) b[action] = 1 if self._winner(tuple(b)) is not None: return tuple(b), True empties = [i for i in range(9) if b[i] == 0] if rng.rand() < self.opp_optimal_prob: best, _ = self._minimax(tuple(b), -1) opts = [] for i in empties: nb = list(b) nb[i] = -1 v, _ = self._minimax(tuple(nb), 1) if v == best: opts.append(i) move = opts[rng.randint(len(opts))] if opts else empties[rng.randint(len(empties))] else: move = empties[rng.randint(len(empties))] b[move] = -1 return tuple(b), self._winner(tuple(b)) is not None if __name__ == "__main__": rng = np.random.RandomState(0) out = {} for label, cls in [("A_fixed_optimal_move", TicTacToeEnv), ("B_uniform_among_optimal", TicTacToeUniformOptimal)]: print(f"\nCeiling for a PERFECT agent as X -- opponent variant {label}:\n") print(f"{'opp optimal':>12s} {'win%':>8s} {'draw%':>8s} {'loss%':>8s}") out[label] = {} for op in [0.0, 0.5, 0.8, 0.9, 0.95, 1.0]: env = cls(opp_optimal_prob=op) w, d, l = play_perfect(env, 4000, rng) out[label][f"opp_optimal_{op}"] = {"win_pct": w, "draw_pct": d, "loss_pct": l} print(f"{op:>12.2f} {w:8.2f} {d:8.2f} {l:8.2f}") with open("../outputs/ttt_ceiling.json", "w") as f: json.dump(out, f, indent=2) a = out["A_fixed_optimal_move"]["opp_optimal_0.9"]["win_pct"] b = out["B_uniform_among_optimal"]["opp_optimal_0.9"]["win_pct"] print(f"\nThe paper reports 45+-4% wins at opp_optimal=0.90.") print(f"Measured ceiling at 0.90: variant A {a:.2f}%, variant B {b:.2f}%.") print("Both are far below 45%: no policy, however good, can reach the reported") print("number against a genuinely 90%-optimal opponent (TicTacToe is a draw).")