Ejemplo n.º 1
0
def build_board(player_boards):
    num_players = len(player_boards)
    needed_board_size_x = ceil(sqrt(num_players))
    needed_board_size_y = ceil(num_players / needed_board_size_x)

    single_board_y = player_boards[0][1].shape[0]
    single_board_x = player_boards[0][1].shape[1]

    for ii, board in player_boards:
        if (board.shape[0] != single_board_y
                or board.shape[1] != single_board_x):
            raise Exception(
                f"Player id {ii} has a board that does not match other board size, unable to build"
                f"a game board.")

    y = single_board_y * needed_board_size_y
    x = single_board_x * needed_board_size_x

    gs = GameState(y, x)

    y_counter = 0
    for ii in range(0, num_players):
        sub_board = GameState.from_numpy_array(player_boards[ii][1] * (ii + 1))
        gs = gs.apply_subboard(y_counter * single_board_y,
                               (ii - y_counter * needed_board_size_x) *
                               single_board_x, sub_board)
        if ii - y_counter * needed_board_size_x >= needed_board_size_x - 1:
            y_counter += 1

    return gs
Ejemplo n.º 2
0
    def _check_policy_once(self):
        device = self._devices[0]
        mcts_agent = MCTSAgent(Connect5Game.ASSIGNED_PLAYER_ID_1, "MCTSAgent",
                               self._basic_mcts_rounds_per_move, self._basic_mcts_c_puct)
        az_agent = AlphaZeroAgent(Connect5Game.ASSIGNED_PLAYER_ID_2, "AlphaZeroAgent", self._encoder, self._model,
                                  self._az_mcts_rounds_per_move, self._c_puct, self._az_mcts_temperature, device=device)

        board = Board(self._board_size)
        players = {}
        players[mcts_agent.id] = mcts_agent
        players[az_agent.id] = az_agent

        start_game_state = GameState(board, mcts_agent.id, None)

        # MCTS agent always plays first
        game = Connect5Game(start_game_state, [mcts_agent.id, az_agent.id], self._number_of_planes, is_self_play=False)

        while not game.is_over():
            move = players[game.working_game_state.player_in_action].select_move(
                game)
            if players[0].id == game.working_game_state.player_in_action:
                players[1].mcts_tree.go_down(game, move)
            else:
                players[0].mcts_tree.go_down(game, move)

            game.apply_move(move)

        winner = game.final_winner
        score = 0
        if winner == az_agent.id:
            score = 1
        print('This score is {}'.format(score))
        return score
Ejemplo n.º 3
0
    def _simulate_random_game_for_state(self, game_state):
        random_agent_0 = RandomAgent(Connect5Game.ASSIGNED_PLAYER_ID_1,
                                     "RandomAgent0")
        random_agent_1 = RandomAgent(Connect5Game.ASSIGNED_PLAYER_ID_2,
                                     "RandomAgent1")

        bots = {}
        bots[random_agent_0.id] = random_agent_0
        bots[random_agent_1.id] = random_agent_1

        # current board status
        board = Board(game_state.board.board_size, game_state.board.grid)
        init_game_state = GameState(board, game_state.player_in_action, None)

        game = Connect5Game(init_game_state,
                            [random_agent_0.id, random_agent_1.id], 0)

        # game.working_game_state.board.print_board()

        while not game.is_over():
            move = bots[game.working_game_state.player_in_action].select_move(
                game)
            game.apply_move(move)

        winner = game.final_winner
        # game.working_game_state.board.print_board()

        if winner is None:
            return 0.0
        else:
            return 1.0 if winner == game_state.player_in_action else -1.0
Ejemplo n.º 4
0
 def look_ahead_next_move(self, game_state, move):
     piece = Piece(game_state.player_in_action, move.point)
     new_board = Board(game_state.board.board_size, game_state.board.grid)
     new_board.place_piece(piece)
     return GameState(
         new_board, self.get_player_after_move(game_state.player_in_action),
         move)
Ejemplo n.º 5
0
def evaluate():

    mcts_agent = MCTSAgent(Connect5Game.ASSIGNED_PLAYER_ID_1, "MCTSAgent",
                           7000, 5.0)
    human_player = HumanPlayer(Connect5Game.ASSIGNED_PLAYER_ID_2, "Human")

    board = Board(11)

    players = {}
    players[mcts_agent.id] = mcts_agent
    players[human_player.id] = human_player

    init_game_state = GameState(board, 0, None)
    game = Connect5Game(init_game_state, [mcts_agent.id, human_player.id], 0)

    while not game.is_over():
        move = players[game.working_game_state.player_in_action].select_move(
            game)
        if game.working_game_state.player_in_action == Connect5Game.ASSIGNED_PLAYER_ID_2:
            mcts_agent.mcts_tree.go_down(game, move)

        game.apply_move(move)
        print("Last move is {}".format(move.point))

        if game.working_game_state.player_in_action == Connect5Game.ASSIGNED_PLAYER_ID_1:
            game.working_game_state.board.print_board()

    game.working_game_state.board.print_board()
    # self._logger.info(game.final_winner.name)

    print(players[game.final_winner].name)
Ejemplo n.º 6
0
    def _collect_data_once_in_parallel(encoder, model, az_mcts_round_per_moves,
                                       board_size, number_of_planes, c_puct,
                                       az_mcts_temperature, device, pipe):

        agent_1 = AlphaZeroAgent(Connect5Game.ASSIGNED_PLAYER_ID_1,
                                 "AlphaZeroAgent1",
                                 encoder,
                                 model,
                                 az_mcts_round_per_moves,
                                 c_puct,
                                 az_mcts_temperature,
                                 device=device)
        agent_2 = AlphaZeroAgent(Connect5Game.ASSIGNED_PLAYER_ID_2,
                                 "AlphaZeroAgent2",
                                 encoder,
                                 model,
                                 az_mcts_round_per_moves,
                                 c_puct,
                                 az_mcts_temperature,
                                 device=device)
        agent_2.mcts_tree = agent_1.mcts_tree

        board = Board(board_size)
        players = {agent_1.id: agent_1, agent_2.id: agent_2}
        start_game_state = GameState(board,
                                     random.choice([agent_1.id, agent_2.id]),
                                     None)
        game = Connect5Game(start_game_state, [agent_1.id, agent_2.id],
                            number_of_planes,
                            is_self_play=True)

        while not game.is_over():
            move = players[
                game.working_game_state.player_in_action].select_move(game)
            game.apply_move(move)

            # game.working_game_state.board.print_board()

        # game.working_game_state.board.print_board()

        winner = game.final_winner

        if winner is not None:
            if winner == players[0].id:
                players[0].experience_collector.complete_episode(reward=1)
                players[1].experience_collector.complete_episode(reward=-1)
            if winner == players[1].id:
                players[1].experience_collector.complete_episode(reward=1)
                players[0].experience_collector.complete_episode(reward=-1)

            expericence_buffer = ExpericenceBuffer()
            expericence_buffer.combine_experience(
                [agent_1.experience_collector, agent_2.experience_collector])
            pipe.send(expericence_buffer)
            pipe.close()
Ejemplo n.º 7
0
def select_move(bot_name):

    bot_agent = AlphaZeroAgent(Connect5Game.ASSIGNED_PLAYER_ID_2,
                               "Agent_New",
                               encoder,
                               model_new,
                               az_mcts_round_per_moves,
                               c_puct,
                               az_mcts_temperature,
                               device=device)
    human_agent = HumanPlayer(Connect5Game.ASSIGNED_PLAYER_ID_1,
                              "HumanPlayerX")

    board = Board(board_size)
    players = {}
    players[bot_agent.id] = bot_agent
    players[human_agent.id] = human_agent

    start_game_state = GameState(board, human_agent.id, None)

    game = Connect5Game(start_game_state, [bot_agent.id, human_agent.id],
                        number_of_planes, False)

    historic_jboard_positions = [
        point_from_coords([move][0]) for move in (request.json)['moves']
    ]
    historic_moves = [
        Move(Point(historic_jboard_position.row, historic_jboard_position.col))
        for historic_jboard_position in historic_jboard_positions
    ]
    over = False
    bot_move_str = ''

    for move in historic_moves:
        game.apply_move(move)

    if game.is_over():
        over = True
    else:
        bot_move = bot_agent.select_move(game)
        game.apply_move(bot_move)
        game.working_game_state.board.print_board()
        jboard_postion = Point(bot_move.point.row, bot_move.point.col)
        bot_move_str = coords_from_point(jboard_postion)
        over = True if game.is_over() else False

    return jsonify({
        'bot_move': bot_move_str,
        'over': over,
        'diagnostics': None
    })
Ejemplo n.º 8
0
    def _collect_data_once(self):

        agent_1 = AlphaZeroAgent(Connect5Game.ASSIGNED_PLAYER_ID_1,
                                 "AlphaZeroAgent1",
                                 self._encoder,
                                 self._model,
                                 self._az_mcts_rounds_per_move,
                                 self._c_puct,
                                 self._az_mcts_temperature,
                                 device=self._devices[0])
        agent_2 = AlphaZeroAgent(Connect5Game.ASSIGNED_PLAYER_ID_2,
                                 "AlphaZeroAgent2",
                                 self._encoder,
                                 self._model,
                                 self._az_mcts_rounds_per_move,
                                 self._c_puct,
                                 self._az_mcts_temperature,
                                 device=self._devices[0])

        # Two agents use the same tree to save the memory and improve the efficency
        agent_2.mcts_tree = agent_1.mcts_tree

        board = Board(self._board_size)
        players = {agent_1.id: agent_1, agent_2.id: agent_2}
        start_game_state = GameState(board,
                                     random.choice([agent_1.id, agent_2.id]),
                                     None)
        game = Connect5Game(start_game_state, [agent_1.id, agent_2.id],
                            self._number_of_planes,
                            is_self_play=True)
        while not game.is_over():
            move = players[
                game.working_game_state.player_in_action].select_move(game)
            game.apply_move(move)

            # game.working_game_state.board.print_board()

        # game.working_game_state.board.print_board()

        winner = game.final_winner
        if winner is not None:
            if winner == players[0].id:
                players[0].experience_collector.complete_episode(reward=1)
                players[1].experience_collector.complete_episode(reward=-1)
            if winner == players[1].id:
                players[1].experience_collector.complete_episode(reward=1)
                players[0].experience_collector.complete_episode(reward=-1)

            self._experience_buffer.combine_experience(
                [agent_1.experience_collector, agent_2.experience_collector])