Example #1
0
def min_select(boards, model, history):
    if len(boards) == 0:
        boards.append(chess.Board())

    curr_board = boards[-1]
    possible = list(curr_board.legal_moves)

    min_val = float('inf')
    for move in possible:
        t_boards = copy.deepcopy(boards)
        t_curr = copy.copy(t_boards[-1])
        t_curr.push(move)

        if t_curr.result() == '1-0':
            return move

        t_boards.append(t_curr)

        val = model.evaluate(
            np.expand_dims(get_simple_input(t_boards, history), 0))
        if val < min_val:
            min_val = val
            best_move = move

    return best_move
Example #2
0
 def __init__(self, model, boards, history):
     self.history = history
     self.model = model
     self.boards = boards
     self.leading_edge = None
     self.edges = []
     self.value = model.evaluate(
         np.expand_dims(get_simple_input(self.boards, self.history), 0))
Example #3
0
def max_select(boards, model, history):

    curr_board = boards[-1]
    possible = list(curr_board.legal_moves)

    max_val = -1 * float('inf')
    for move in possible:
        t_boards = copy.deepcopy(boards)
        t_curr = copy.deepcopy(t_boards[-1])
        t_curr = t_curr.push(move)
        t_boards.append(t_curr)

        val = model.evaluate(
            np.expand_dims(get_simple_input(t_boards, history), 0))
        if val > max_val:
            max_val = val
            best_move = move

    return best_move
Example #4
0
def evaluate(boards, model, history):
    inputs = np.expand_dims(get_simple_input(boards, history), 0)
    return model.evaluate(inputs)
Example #5
0
        '''
        if self.check_win() is not None:
            return []

        possible = np.where(self.board == 0)
        return [(possible[0][i], possible[1][i])
                for i in range(len(possible[0]))]


if __name__ == '__main__':
    game = TicTacToe()
    boards = [copy.deepcopy(game)]
    while game.check_win() is None:
        possible_moves = game.legal_moves
        print(game.board)
        print(possible_moves)
        move_ind = int(input('Select: '))
        move = possible_moves[move_ind]

        game = game.push(move)
        boards.append(game)

    for b in boards:
        print(b.board)

    for i in range(1, len(boards) + 1):
        inp = get_simple_input(boards[:i], 2)
        print(inp[:, :, 0])
        print(inp[:, :, 1])
        print()
Example #6
0
def get_value(model, t_boards, history):
    return model.evaluate(
        np.expand_dims(get_simple_input(t_boards, history), 0))
Example #7
0
def main(args):
    model = DQN()
    model.load(args.path)

    engine = chess.uci.popen_engine(os.environ['SFSH'])
    engine.uci()

    board = chess.Board()
    player = 1

    boards = [copy.deepcopy(board)]

    while not board.is_game_over(claim_draw=True):

        print('CURRENT BOARD')
        print(board)

        if player == 1:

            engine.position(board)
            stk_move, _ = engine.go()

            vals = []
            board_moves = []
            possible_moves = list(board.legal_moves)

            for move in possible_moves:
                t_boards = copy.deepcopy(boards)
                t_curr = copy.copy(t_boards[-1])
                t_curr.push(move)
                t_boards.append(t_curr)

                board_moves.append(t_curr)

                vals.append(
                    model.evaluate(
                        np.expand_dims(
                            get_simple_input(t_boards, args.history), 0)))

                if stk_move == move:
                    stk_val = vals[-1]
                    stk_board = t_curr

            top_5 = heapq.nlargest(3, vals)
            bottom_5 = heapq.nsmallest(3, vals)

            print('Stockfish')
            print(stk_board)
            print(stk_val)

            print('Top 5')
            for v in top_5:
                print(board_moves[vals.index(v)])
                print(v)
                print()

            print()

            print('Bottom 5')
            for v in bottom_5:
                print(board_moves[vals.index(v)])
                print(v)
                print()

            print()

            if args.max_select:
                move = possible_moves[vals.index(max(vals))]
            if args.min_select:
                move = possible_moves[vals.index(min(vals))]

            cont = input('Continue?')
        else:
            #move = random.choice(list(board.legal_moves))
            engine.position(board)
            move, _ = engine.go()

        player = (player % 2) + 1

        board.push(move)
        boards.append(copy.deepcopy(board))

        if len(boards) > 8:
            boards.pop(0)

        res = board.result()