def test_alpha_zero_player_from_model(): game = OthelloGame(6) some_net = OthelloNNet(game) player = AlphaZeroPlayer(game, some_net) board = game.get_init_board() action = player.play(board) assert action
def test_bare_model_player_from_model(): game = OthelloGame(6) some_net = OthelloNNet(game) player = BareModelPlayer(game, some_net) board = game.get_init_board() action = player.play(board) assert action
def test_human_player_valid_action(): action_valid = "1,2" game = OthelloGame(6) player = HumanPlayer(game) board = game.get_init_board() with patch("builtins.input", side_effect=[action_valid]): action = player.play(board) assert action == 8
def test_alpha_zero_player_reset(): game = OthelloGame(6) player = AlphaZeroPlayer(game, OthelloNNet, num_mcts_sims=4) assert not player.mcts.Qsa board = game.get_init_board() player.play(board) assert player.mcts.Qsa player.reset() assert not player.mcts.Qsa
def test_human_player_invalid_action(capsys): action_valid = "1,2" action_invalid = "0,0" game = OthelloGame(6) player = HumanPlayer(game) board = game.get_init_board() with patch("builtins.input", side_effect=[action_invalid, action_valid]): player.play(board) out, _err = capsys.readouterr() assert out == "Invalid action.\n"
def test_bare_model_player_from_checkpoint(): game = OthelloGame(6) some_net = OthelloNNet(game) folder, filename = "/tmp/", "checkpoint_bare_model_player" some_net.save_checkpoint(folder, filename) del some_net player = BareModelPlayer(game, OthelloNNet, folder=folder, filename=filename) board = game.get_init_board() action = player.play(board) assert action
def test_alpha_zero_player_from_checkpoint(): game = OthelloGame(6) some_net = OthelloNNet(game) folder, filename = "/tmp/", "checkpoint_alpha_zero_player" some_net.save_checkpoint(folder, filename) del some_net player = AlphaZeroPlayer(game, OthelloNNet, folder=folder, filename=filename, num_mcts_sims=4) board = game.get_init_board() action = player.play(board) assert action
def test_league_lazy_loading_players(): game = OthelloGame(6) league = League(game, rounds=1, games=2, cache_size=2) for i in range(10): league.add_player(f"random_{i}", lambda: RandomPlayer(game)) league.start() final_ratings = league.ratings() assert len(final_ratings) == 10
def test_arena(): game = OthelloGame(6) player1 = RandomPlayer(game) player2 = RandomPlayer(game) arena = Arena(player1, player2, game, display=OthelloGame.display) number_of_games = 10 one_won, two_won, draws = arena.play_games(number_of_games, verbose=True) assert one_won + two_won + draws == number_of_games
def test_self_play(local_ray, tmpdir): game = OthelloGame(6) nnet = OthelloNNet(game) s = SharedStorage.remote(nnet.get_weights()) r = ReplayBuffer.remote(games_to_play=1, games_to_use=1, folder=tmpdir) assert ray.get(r.get_number_of_games_played.remote()) == 0 self_play = SelfPlay.remote(r, s, game, nnet.__class__, dict(args)) ray.get(self_play.start.remote()) assert ray.get(r.get_number_of_games_played.remote()) == 1 assert ray.get(r.played_enough.remote()) is True games = ray.get(ray.get(r.get_examples.remote())) assert len(games) == 1 examples = games[0] assert len(examples) > 2 board, policy, winner = examples[0] assert isinstance(board, type(game.get_init_board())) assert len(policy) == game.get_action_size() assert all(0 <= value <= 1 for value in policy) assert winner in [1, -1]
def test_model_trainer_loop(local_ray, tmpdir): game = OthelloGame(6) nnet = OthelloNNet(game) s = SharedStorage.remote(nnet.get_weights()) assert ray.get(s.get_revision.remote()) == 0 r = MockedReplayBuffer.remote(games_to_play=4, games_to_use=4, folder=tmpdir) r.add_game_examples.remote(mock_example_data(game)) model_trainer = ModelTrainer.options(num_gpus=0).remote( r, s, game, nnet.__class__, dict(args), selfplay_training_ratio=1) ray.get(model_trainer.start.remote()) assert ray.get(s.get_revision.remote()) > 0 assert ray.get(s.trained_enough.remote()) is True
def test_model_trainer_pit_reject_model(capsys, local_ray, tmpdir): game = OthelloGame(6) nnet = OthelloNNet(game) s = SharedStorage.remote(nnet.get_weights()) assert ray.get(s.get_revision.remote()) == 0 r = ReplayBuffer.remote(games_to_play=2, games_to_use=2, folder=tmpdir) r.add_game_examples.remote(mock_example_data(game)) # provoke model rejection by tweaking updateThreshold to fail custom_args = dict(args, updateThreshold=1.1) model_trainer = ModelTrainer.options(num_gpus=0).remote( r, s, game, nnet.__class__, custom_args, pit_against_old_model=True) ray.get(model_trainer.train.remote()) assert ray.get(s.get_revision.remote()) == 0 out, _err = capsys.readouterr() assert "PITTING AGAINST PREVIOUS VERSION" in out assert "REJECTING NEW MODEL" in out
def test_league_incrementally_add_players_to_running(): game = OthelloGame(6) rounds = 2 league = League(game, initial_rating=1500, rounds=rounds, games=4) assert league.has_started is False league.start() assert league.has_started is True league.add_player("random_1", RandomPlayer(game)) assert league.ratings() == [(1500, 1, "random_1")] assert len(league.history) == 0 for i in range(2, 6): league.add_player(f"random_{i}", RandomPlayer(game)) assert len(league.ratings()) == i assert len(league.history) == sum(range(i)) * rounds
def test_league_basic_usage(): game = OthelloGame(6) rounds = 2 league = League(game, initial_rating=1500, rounds=rounds, games=4) league.add_player("random_1", RandomPlayer(game), initial_rating=2000) league.add_player("random_2", RandomPlayer(game), initial_rating=1750) league.add_player("random_3", RandomPlayer(game)) league.add_player("random_4", RandomPlayer(game), initial_rating=1000) assert league.has_started is False initial_ratings = league.ratings() assert initial_ratings == [ (2000, 1, "random_1"), (1750, 2, "random_2"), (1500, 3, "random_3"), (1000, 4, "random_4"), ] league.start() assert league.has_started is True assert len(league.history) == sum(range(4)) * rounds final_ratings = league.ratings() assert len(final_ratings) == 4 assert final_ratings != initial_ratings
def test_bare_model_player_invalid_nnet_parameter(): neither_nnet_nor_nnet_class = "something_else" game = OthelloGame(6) with pytest.raises(TypeError) as excinfo: BareModelPlayer(game, neither_nnet_nor_nnet_class) assert "NeuralNet subclass or instance" in str(excinfo.value)
def test_coach(capsys, tmpdir): args.checkpoint = tmpdir game = OthelloGame(6) nnet = OthelloNNet(game) coach = Coach(game, nnet, args) coach.learn()
def test_bare_model_player(): game = OthelloGame(6) player = BareModelPlayer(game, OthelloNNet) board = game.get_init_board() action = player.play(board) assert action
def test_greedy_player(): game = OthelloGame(6) player = GreedyPlayer(game) board = game.get_init_board() action = player.play(board) assert action
def test_random_player(): game = OthelloGame(6) player = RandomPlayer(game) board = game.get_init_board() action = player.play(board) assert action
def test_alpha_zero_player(): game = OthelloGame(6) player = AlphaZeroPlayer(game, OthelloNNet, num_mcts_sims=4) board = game.get_init_board() action = player.play(board) assert action