def test_three_permutation(self): game = ThreePlayerLeapFrog() p1, p2, p3 = UninformedMCTSPlayer(game, 1), UninformedMCTSPlayer( game, 1), UninformedMCTSPlayer(game, 1) scores, outcomes = play_match(game, [p1, p2, p3], permute=True) self.assertEqual(scores, {p1: 1 / 3, p2: 1 / 3, p3: 1 / 3}) self.assertEqual(outcomes, {p1: "Tie", p2: "Tie", p3: "Tie"})
def effective_model_power(checkpoint, game, model_class, sims, cuda=False): my_model = NeuralNetwork(game, model_class, cuda=cuda) my_model.load(checkpoint) my_player = DeepMCTSPlayer(game, my_model, sims) strength = 10 num_opponents = game.get_num_players() - 1 lost = False while not lost: contenders = [ UninformedMCTSPlayer(game, strength) for _ in range(num_opponents) ] # Play main game scores, outcomes = play_match(game, [my_player] + contenders, verbose=False, permute=True) score, outcome = scores[my_player], outcomes[my_player] if outcome == "Lose": lost = True print("{} <{}> Opponent strength: {}".format( outcome, round(score, 3), strength), end="") # Play control game control_player = UninformedMCTSPlayer(game, sims) scores, outcomes = play_match(game, [control_player] + contenders, verbose=False, permute=True) score, outcome = scores[control_player], outcomes[control_player] print(" (Control: {} <{}>)".format(outcome, round(score, 3))) strength *= 2 # Opponent strength doubles every turn
def test_three_simple(self): game = ThreePlayerLeapFrog() p1, p2, p3 = UninformedMCTSPlayer(game, 1), UninformedMCTSPlayer( game, 1), UninformedMCTSPlayer(game, 1) scores, outcomes = play_match(game, [p1, p2, p3]) self.assertEqual(scores, {p1: 0.0, p2: 0.0, p3: 1.0}) self.assertEqual(outcomes, {p1: "Lose", p2: "Lose", p3: "Win"})
def test_two_sim_guess_it(self): gi = TwoPlayerGuessIt() p = UninformedMCTSPlayer(gi, 2) init = gi.get_initial_state() s = p.update_state(init) s_gt = init s_gt[0, 0, 0] = 1 s_gt[:, :, -1] = 1 self.assertTrue((s == s_gt).all())
def test_six_sim_guess_it(self): for _ in range(1000): # There should be no stochasticity (temp=0) gi = TwoPlayerGuessIt() p = UninformedMCTSPlayer(gi, 6) init = gi.get_initial_state() s = p.update_state(init) s_gt = init s_gt[-1, -1, 0] = 1 s_gt[:, :, -1] = 1 self.assertTrue((s == s_gt).all())
def test_resets_work(self): game = TicTacToe() p1, p2 = UninformedMCTSPlayer(game, 1), UninformedMCTSPlayer(game, 1) for _ in range(100): scores, outcomes = play_match(game, [p1, p2]) self.assertEqual(scores, {p1: 1.0, p2: 0.0}) self.assertEqual(outcomes, {p1: "Win", p2: "Lose"}) for _ in range(100): scores, outcomes = play_match(game, [p1, p2], permute=True) self.assertEqual(scores, {p1: 0.5, p2: 0.5}) self.assertEqual(outcomes, {p1: "Tie", p2: "Tie"})
def test_memory_guess_it(self): gi = TwoPlayerGuessIt() p = UninformedMCTSPlayer(gi, 2) init = gi.get_initial_state() s = p.update_state(init) s_gt = init.copy() s_gt[0, 0, 0] = 1 s_gt[:, :, -1] = 1 self.assertTrue((s == s_gt).all()) for _ in range(100): s = p.update_state(init) s_gt = init.copy() s_gt[1, 1, 0] = 1 s_gt[:, :, -1] = 1 self.assertTrue((s == s_gt).all())
def evaluate_against_uninformed(checkpoint, game, model_class, my_sims, opponent_sims, cuda=False): my_model = NeuralNetwork(game, model_class, cuda=cuda) my_model.load(checkpoint) num_opponents = game.get_num_players() - 1 uninformeds = [ UninformedMCTSPlayer(game, opponent_sims) for _ in range(num_opponents) ] informed = DeepMCTSPlayer(game, my_model, my_sims) scores = play_match(game, [informed] + uninformeds, permute=True) print("Opponent strength: {} Scores: {}".format(opponent_sims, scores))
if verbose: print("Final scores:", scores) return scores if __name__ == "__main__": from players.human_player import HumanPlayer from neural_network import NeuralNetwork from models.senet import SENet from players.uninformed_mcts_player import UninformedMCTSPlayer from players.deep_mcts_player import DeepMCTSPlayer from games.tictactoe import TicTacToe from games.tictacmo import TicTacMo # Change these variable game = TicTacMo() #ckpt = 15 #nn = NeuralNetwork(game, SENet, cuda=True) #nn.load(ckpt) # HumanPlayer(game), # UninformedMCTSPlayer(game, simulations=1000) # DeepMCTSPlayer(game, nn, simulations=50) players = [ HumanPlayer(game), HumanPlayer(game), UninformedMCTSPlayer(game, simulations=3000) ] for _ in range(1): play_match(game, players, verbose=True, permute=True)
def test_self_play(self): game = TwoPlayerGuessIt() p1 = UninformedMCTSPlayer(game, 4) scores, outcomes = play_match(game, [p1, p1], permute=True) self.assertEqual(len(scores), 1) self.assertEqual(len(outcomes), 1)
def test_permutation(self): game = TicTacToe() p1, p2 = UninformedMCTSPlayer(game, 1), UninformedMCTSPlayer(game, 1) scores, outcomes = play_match(game, [p1, p2], permute=True) self.assertEqual(scores, {p1: 0.5, p2: 0.5}) self.assertEqual(outcomes, {p1: "Tie", p2: "Tie"})
def test_simple(self): game = TicTacToe() p1, p2 = UninformedMCTSPlayer(game, 1), UninformedMCTSPlayer(game, 1) scores, outcomes = play_match(game, [p1, p2]) self.assertEqual(scores, {p1: 1.0, p2: 0.0}) self.assertEqual(outcomes, {p1: "Win", p2: "Lose"})