Ejemplo n.º 1
0
    def _genmove(self, color, game, flat_board):
        flat_board = flat_board.reshape(1, len(flat_board))

        inp = self.board_to_input(flat_board)
        current_pred = self.model.predict(inp)

        my_index = 0 if color == 'b' else 1
        my_pred = current_pred[0, my_index]
        my_value = BLACK if color == 'b' else WHITE

        # We're still interested in the playable locations
        playable_locations = game.get_playable_locations(color)
        results = np.zeros(game.board.shape)
        for move in playable_locations:
            if move.is_pass:
                continue

            test_board = copy.deepcopy(game.board)
            test_board.place_stone_and_capture_if_applicable_default_values(
                move.to_matrix_location(), my_value)
            inp = self.board_to_input(test_board.flatten())
            pred_result = self.model.predict(inp)
            # pred_result = self.softmax(pred_result)

            results[move.to_matrix_location()] = pred_result[0, my_index]

        results -= my_pred

        row, col = np.unravel_index(results.argmax(), results.shape)

        move = Move(col=col, row=row)
        if (results[move.to_matrix_location()] <= 0):
            move = Move(is_pass=True)

        return move
Ejemplo n.º 2
0
    def genmove(self, color, game) -> Move:
        # We're still interested in the playable locations
        playable_locations = game.get_playable_locations(color)

        # Format the board and make predictions
        inp = self.board_to_input(color, game.board)
        pred_moves = self.model.predict(inp)
        pred_moves = pred_moves.reshape(9, 9)

        # print(pred_moves)
        # print(playable_locations)
        dummy_value = -10
        potential_moves = np.array([[dummy_value] * 9] * 9, dtype=float)
        for move in playable_locations:
            # print(move)
            if move.is_pass:
                continue
            loc = move.to_matrix_location()
            potential_moves[loc[0]][loc[1]] = pred_moves[loc[0]][loc[1]]

        potential_moves = self.softmax(potential_moves)

        row, col = np.unravel_index(potential_moves.argmax(),
                                    potential_moves.shape)

        move = Move(col=col, row=row)
        # if game.board[col,row] != 0:
        #     move = Move(is_pass = True)
        #     return move

        if potential_moves[move.to_matrix_location()] == dummy_value:
            move = Move(is_pass=True)

        return move
Ejemplo n.º 3
0
    def genmove(self, color, game) -> Move:
        # We're still interested in the playable locations
        playable_locations = game.get_playable_locations(color)

        # Format the board and make predictions
        inp = self.board_to_input(color, game.board)
        bot_logger.debug('Input shape:', inp.shape)
        bot_logger.debug('Input:', inp)
        pred_moves = self.model.predict(inp)
        # pred_moves = self.model.predict(np.zeros((1, 162)))
        bot_logger.debug('This worked')
        bot_logger.debug('Predicted moves:', pred_moves)

        pred_moves = pred_moves.reshape(9, 9)
        # print(pred_moves)
        # print(playable_locations)
        dummy_value = -10
        potential_moves = np.array([[dummy_value] * 9] * 9, dtype=float)
        for move in playable_locations:
            # print(move)
            if move.is_pass:
                continue
            loc = move.to_matrix_location()
            potential_moves[loc[0]][loc[1]] = pred_moves[loc[0]][loc[1]]

        # print([i for row in potential_moves for i in row])

        potential_moves = self.softmax(potential_moves)

        row, col = np.unravel_index(potential_moves.argmax(),
                                    potential_moves.shape)

        move = Move(col=col, row=row)
        if (potential_moves[move.to_matrix_location()] == dummy_value
                or potential_moves[move.to_matrix_location()] <
            (1 / 81 + 0.0001)):
            move = Move(is_pass=True)

        return move
Ejemplo n.º 4
0
    def genmove(self, color, game) -> Move:
        my_index = 0 if color == 'b' else 1

        # We're still interested in the playable locations
        playable_locations = game.get_playable_locations(color)

        inp = self.board_to_input(color, game.board)
        current_pred = self.model.predict(inp)
        # print('Current outcome prediction:', current_pred)
        # assert (self.softmax(current_pred) == current_pred).all()
        current_pred = self.softmax(current_pred)
        my_pred = current_pred[0, my_index]

        my_value = BLACK if color == 'b' else WHITE

        results = np.zeros(game.board.shape)

        for move in playable_locations:
            if move.is_pass:
                continue

            test_board = copy.deepcopy(game.board)
            test_board[move.to_matrix_location()] = my_value
            inp = self.board_to_input(color, test_board)
            pred_result = self.model.predict(inp)
            pred_result = self.softmax(pred_result)

            results[move.to_matrix_location()] = pred_result[0, my_index]

        # print(results>0)
        # print(my_pred)
        results -= my_pred
        # print(results>0)
        """ `results` now contains our prediction of our win probabilities
        for each move, adjusted by our current win probability. We can now
        easily check if a move is worth playing by checking the
        sign; If it is negative, our probability to win gets worse. In general
        the higher the number in `results` the better the move."""

        row, col = np.unravel_index(results.argmax(), results.shape)

        move = Move(col=col, row=row)
        if (results[move.to_matrix_location()] <= 0):
            move = Move(is_pass=True)

        # print('Returned move:', move.to_gtp(9))

        return move