コード例 #1
0
def Evaluate_min_max(player, state):
    board = make_matrix(state)
    # material_adv
    mapped_val = {
        '0': 0,
        '1': 1,  # White Pawn
        '-1': -1,  # Black Pawn
        '2': 3,  # White Knight
        '-2': -3,  # Black Knight
        '3': 3,  # White Bishop
        '-3': -3,  # Black Bishop
        '4': 5,  # White Rook
        '-4': -5,  # Black Rook
        '5': 9,  # White Queen
        '-5': -9,  # Black Queen
        '6': 0,  # White King
        '-6': 0  # Black King
    }
    pieces_adv = [mapped_val[str(x)] for x in list(itertools.chain(*board))]
    material_adv = np.sum(pieces_adv)

    if player == 'white':
        return material_adv
    else:
        return -material_adv

    return material_adv
コード例 #2
0
def Evaluate_center_control(player, env, state):
    mapped_val = {
        '0': 0,
        '1': 1,  # White Pawn
        '-1': -1,  # Black Pawn
        '2': 3,  # White Knight
        '-2': -3,  # Black Knight
        '3': 3,  # White Bishop
        '-3': -3,  # Black Bishop
        '4': 5,  # White Rook
        '-4': -5,  # Black Rook
        '5': 9,  # White Queen
        '-5': -9,  # Black Queen
        '6': 0,  # White King
        '-6': 0  # Black King
    }
    board = make_matrix(state)
    #center control
    space_list = list(itertools.chain(*board))
    center_list = [
        space_list[27], space_list[28], space_list[35], space_list[36]
    ]
    center_list_val = [mapped_val[str(x)] for x in center_list]
    center_control = np.sum(center_list_val)
    #print("Center Control: ", center_control)

    return center_control
コード例 #3
0
def calculate_center_control(state):
    board = make_matrix(state)
    #center control
    space_list = list(itertools.chain(*board))
    center_list = [
        space_list[27], space_list[28], space_list[35], space_list[36]
    ]
    center_list_val = [mapped_val[str(x)] for x in center_list]
    center_control = np.sum(center_list_val)
    print("Center Control: ", center_control)
コード例 #4
0
def calculate_space(state):
    board = make_matrix(state)
    #Space https://en.wikipedia.org/wiki/Chess_strategy#Space
    #attacked
    att_space = 0
    #ocupied
    black_space = list(itertools.chain(*board))[:32]
    occ = [x if x > 0 else 0 for x in black_space]
    #space
    space = np.sum(occ) + att_space
    print("Space: ", space)
    return space
コード例 #5
0
def Evaluate_development(player, state):
    board = make_matrix(state)
    #Space https://en.wikipedia.org/wiki/Chess_strategy#Space
    #attacked
    att_space = 0
    #ocupied
    if player == 'white':
        white_space = list(itertools.chain(*board))[33:57]
        occ = [x if x > 0 else 0 for x in black_space]
    else:
        black_space = list(itertools.chain(*board))[8:32]
        occ = [x if x < 0 else 0 for x in white_space]

    #space
    space = abs(np.sum(occ)) + att_space
    #print("Space: ", space)
    return space
コード例 #6
0
def calculate_material_adv(state):
    board = make_matrix(state)
    # material_adv
    mapped_val = {
        '0': 0,
        '1': 1,  # White Pawn
        '-1': -1,  # Black Pawn
        '2': 3,  # White Knight
        '-2': -3,  # Black Knight
        '3': 3,  # White Bishop
        '-3': -3,  # Black Bishop
        '4': 5,  # White Rook
        '-4': -5,  # Black Rook
        '5': 9,  # White Queen
        '-5': -9,  # Black Queen
        '6': 0,  # White King
        '-6': 0  # Black King
    }
    pieces_adv = [mapped_val[str(x)] for x in list(itertools.chain(*board))]
    material_adv = np.sum(pieces_adv)
    #print('Material Adv: ', material_adv)
    return material_adv
コード例 #7
0
from chessRL import make_matrix

env = gym.make('Chess-v0')
print(env.render())
engine = chess.engine.SimpleEngine.popen_uci("../stockfish")

scores_eps = []
done = False
state = env.reset()

cnt = 0
while not done:

    board_txt = state.fen().split()[0]
    board_encoded = ''.join(str(ord(c)) for c in board_txt)
    obs = make_matrix(state)
    if cnt == 0:
        move = "e4"
        action = state.push_san(move)
        state,reward,done,_ = env.step(action)
        
    elif cnt % 2 == 0:
        legal_moves = [state.san(x) for x in env.legal_moves]
        legal_moves_scores = []

        for move in legal_moves:
            action = state.push_san(move) # Make the move
            obs = make_matrix(state)

            mapped_val = {
            '0':0,
コード例 #8
0
def get_moves_by_similarity(state, legal_moves):
    df = pd.read_excel(
        r'C:\Users\Octavio\Desktop\Projetos Python\Chess-RL\data\pgn\chess_deep_memory_excel.xlsx'
    )

    matr = df.loc[:, 's1':]

    #env = gym.make('Chess-v0')
    #state = env.reset()

    #cnt = 0

    #string = "e4 c5 Nc3 Nc6 Qf3 e5"
    #string = "e4 e5 Nf3 Nc6 Bb5 Nf6 O-O Nxe4 Re1 Nd6 Nxe5 Be7 Bf1 Nxe5 Rxe5 O-O d4 Bf6 Re1 Re8 Bf4 Rxe1"
    #game = string.split(' ')
    #for move in game:
    #    #move = state.san(action)
    #    action = state.push_san(move)
    #
    #    #take action
    #    state,reward,done,_ = env.step(action)
    #    cnt +=1

    #print(env.render())
    #print(state.status())

    board_txt = state.fen().split()[0]
    board_encoded = ''.join(str(ord(c)) for c in board_txt)
    obs = make_matrix(state)
    board_state = [list(np.reshape(obs, [1, 64])[0])]
    print("Board state: ", board_state)

    moves = []
    #legal_moves = [state.san(x) for x in env.legal_moves] ##

    #position = [0,0,0,0,0,0,0,0]

    print('COSINE DISTANCE')
    cos_dist = cdist(board_state, matr[:], metric='cosine')
    #print(cos_dist)
    #print(df.iloc[np.argmax(cos_dist),:].next_move)

    df['cos_sim'] = cos_dist[0]
    df = df.sort_values('cos_sim', ascending=False)
    top3 = list(set(list(df[['cos_sim']].iloc[:, :].cos_sim)))[-1:]
    print(top3)
    #n_moves = list(df[['next_move']].iloc[:20,:].next_move)
    #n_moves = [x for x in n_moves if x in legal_moves]
    df2 = df[df['cos_sim'].isin(top3)]
    n_moves = list(df2[['next_move']].iloc[:, :].next_move)
    print(len(n_moves))

    for mv in n_moves:
        moves.append(mv)
    #print(n_moves)

    #print(df.head(10))

    print(" ")
    print('EUCLIDEAN DISTANCE')
    euc_dist = cdist(board_state, matr[:], metric='euclidean')
    #print(euc_dist)
    #print(df.iloc[np.argmax(euc_dist),:].next_move)

    df['euc_sim'] = euc_dist[0]
    df = df.sort_values('euc_sim', ascending=False)
    #print(df.head(10))
    #n_moves = list(df[['next_move']].iloc[:20,:].next_move)
    #n_moves = [x for x in n_moves if x in legal_moves]
    top3 = list(set(list(df[['euc_sim']].iloc[:, :].euc_sim)))[-1:]
    print(top3)
    df2 = df[df['euc_sim'].isin(top3)]
    n_moves = list(df2[['next_move']].iloc[:, :].next_move)
    print(len(n_moves))
    #print(n_moves)
    for mv in n_moves:
        moves.append(mv)

    print(" ")
    print('jaccard DISTANCE')
    dist = cdist(board_state, matr[:], metric='jaccard')
    #print(dist)
    #print(df.iloc[np.argmax(dist),:].next_move)

    df['jacc_sim'] = dist[0]
    df = df.sort_values('jacc_sim', ascending=False)
    #print(df.head(10))
    #n_moves = list(df[['next_move']].iloc[:20,:].next_move)
    #n_moves = [x for x in n_moves if x in legal_moves]
    top3 = list(set(list(df[['jacc_sim']].iloc[:, :].jacc_sim)))[-1:]
    print(top3)
    df2 = df[df['jacc_sim'].isin(top3)]
    n_moves = list(df2[['next_move']].iloc[:, :].next_move)
    print(len(n_moves))
    #print(n_moves)
    for mv in n_moves:
        moves.append(mv)

    print(" ")
    print('correlation DISTANCE')
    dist = cdist(board_state, matr[:], metric='correlation')
    #print(dist)
    #print(df.iloc[np.argmax(dist),:].next_move)

    df['corr_sim'] = dist[0]
    df = df.sort_values('corr_sim', ascending=False)
    #print(df.head(10))
    #n_moves = list(df[['next_move']].iloc[:20,:].next_move)
    #n_moves = [x for x in n_moves if x in legal_moves]
    top3 = list(set(list(df[['corr_sim']].iloc[:, :].corr_sim)))[-1:]
    print(top3)
    df2 = df[df['corr_sim'].isin(top3)]
    n_moves = list(df2[['next_move']].iloc[:, :].next_move)
    print(len(n_moves))
    #print(n_moves)
    for mv in n_moves:
        moves.append(mv)

    print(" ")
    print('chebyshev DISTANCE')
    dist = cdist(board_state, matr[:], metric='chebyshev')
    #print(dist)
    #print(df.iloc[np.argmax(dist),:].next_move)

    df['cheb_sim'] = dist[0]
    df = df.sort_values('cheb_sim', ascending=False)
    #print(df.head(10))
    #n_moves = list(df[['next_move']].iloc[:20,:].next_move)
    #n_moves = [x for x in n_moves if x in legal_moves]
    top3 = list(set(list(df[['cheb_sim']].iloc[:, :].cheb_sim)))[-1:]
    print(top3)
    df2 = df[df['cheb_sim'].isin(top3)]
    n_moves = list(df2[['next_move']].iloc[:, :].next_move)
    print(len(n_moves))
    #print(n_moves)
    for mv in n_moves:
        moves.append(mv)
    '''print(" ")
    print("King Position")
    king_now = board_state[0].index(-6)

    def get_king_pos(row, king_now):
        list_pieces = list(row['s1':'s64'])
        idx = list_pieces.index(-6)
        if idx == king_now:
            return 2
        elif idx >= (king_now-1) and idx <= (king_now+1):
            return 1
        else:
            return 0
        
    df['king_pos'] = df.apply(lambda row: get_king_pos(row, king_now) , axis=1)
    df = df.sort_values('king_pos', ascending=False)
    #print(df.head(10))
    #n_moves = list(df[['next_move']].iloc[:20,:].next_move)
    #n_moves = [x for x in n_moves if x in legal_moves]
    top3 = [2]
    df2 = df[df['king_pos'].isin(top3)]
    n_moves = list(df2[['next_move']].iloc[:,:].next_move)
    print(len(n_moves))
    #print(n_moves)
    for mv in n_moves:
        moves.append(mv)'''

    moves = list(set(moves))
    moves = [x for x in moves if x in legal_moves]
    return moves
コード例 #9
0
def act(cnt,df,seq_len, model, tokenizer, obs, legal_moves, engine, state, opening: str=None, idx_open: int=0, debug = False, choice = 2):
    #start
    print("Choice: ",choice)
    if cnt == 0: 
        if opening == None:
            #random opening
            idx = random.choice([x for x in range(0,len(df['moves']))])
            moves = df['moves'][idx].split()
            move = moves[0]
        else:
            moves = df[df['opening_name'] == opening]['moves'].iloc[idx_open].split()
            move = moves[0]
            
        return move

    else:
        
        if choice == 4:  
            if opening != None:
                if (cnt <= int(df[df['opening_name'] == opening]['opening_ply'].iloc[idx_open])):
                    moves = df[df['opening_name'] == opening]['moves'].iloc[idx_open].split()
                    move = moves[cnt]
                    print("Option 0 opening")
                    return move
                else:
                    print("Option 0 Material Adv")
                    result = engine.play(state, chess.engine.Limit(time=0.1))
                    move = state.san(result.move)

                    legal_moves_scores = []
                    geral_scores = []
                    for move in legal_moves:
                        action = state.push_san(move) # Make the move
                        obs = make_matrix(state)
                        #material adv
                        pieces_adv = [mapped_val[str(x)] for x in list(itertools.chain(*obs))]
                        material_adv = np.sum(pieces_adv)
                        #print('Material Adv: ', material_adv)
                        legal_moves_scores.append(material_adv)

                        info = engine.analyse(state, chess.engine.Limit(time=0.1))
                        mate = info["score"].white().mate()
                        if mate != None:
                            if mate < 0:
                                score = -((20-(-mate)) * 100)
                            else:
                                score = (20-mate) * 100
                        else:
                            score = info["score"].white().score()
                            
                        score = score if score != None else 0
                        geral_scores.append(score)

                        state.pop()  # Unmake the last move
                        
                    #do the best move
                    #print(legal_moves_scores)
                    a = min(legal_moves_scores)
                    b = max(legal_moves_scores)
                    c = min(geral_scores)
                    d = max(geral_scores)
                    n_legal_score = [(x-a)/(b-a) if (b-a) != 0 else 0 for x in legal_moves_scores]
                    n_geral_score = [(x-c)/(d-c) if (d-c) != 0 else 0 for x in geral_scores]
                    zipped_lists = zip(n_legal_score, n_geral_score)
                    final_score = [x + y for (x, y) in zipped_lists]
                    #move = legal_moves[legal_moves_scores.index(max(legal_moves_scores))]
                    move = legal_moves[final_score.index(max(final_score))]
                    return move

   
        elif choice == 5:
            df2 = df.copy()
            df2['similarities'] = df2['moves'].apply(lambda x: similar(x.split(), obs))
            df2 = df2.sort_values(by=['similarities'], ascending = False)
            #moves = moves[cnt-1:]
            for i in range(0,len(df2['moves'])):
                moves = df2['moves'][i].split()
                if debug:
                    print("Similarity of , ", df2['similarities'][i] * 100, "%")
                    print(obs[-1])
                if obs[-1] in moves:
                    try:
                        if debug:
                            #similarity
                            print("Option 1 Similarity")
                            print("Next move: ",moves[moves.index(obs[-1]) + 1])
                            print("game: ",moves[moves.index(obs[-1])-2:moves.index(obs[-1])+2])
                            
                        move = moves[moves.index(obs[-1]) + 1]
                        if move in legal_moves:
                            return move
                            #break
                    except:
                        #ML predict
                        print("Option 1 Machine Learning")
                        board = ' '.join([str(elem) for elem in obs])
                        next_moves = predict(seq_len,model, tokenizer, board)
                        move = next_moves
                        move = [x.capitalize() if len(x) > 2 else x for x in move]
                        for i in move:
                            if i in legal_moves:
                                move = i
                                return move
                                #break
                else:
                    #ML predict
                    #print("Machine Learning")
                    print("Option 1 Machine Learning")
                    board = ' '.join([str(elem) for elem in obs])
                    next_moves = predict(seq_len,model, tokenizer, board)
                    move = next_moves
                    move = [x.capitalize() if len(x) > 2 else x for x in move]
                    for i in move:
                        if i in legal_moves:
                            move = i
                            return move
                        
                    move = random.choice(legal_moves)
                    return move
                        

        elif choice == 3:
            if opening != None:
                if (cnt <= int(df[df['opening_name'] == opening]['opening_ply'].iloc[idx_open])):
                    moves = df[df['opening_name'] == opening]['moves'].iloc[idx_open].split()
                    move = moves[cnt]
                    print("Option 0 opening")
                    return move
                else:
                    print("Option 2 Space")
                    result = engine.play(state, chess.engine.Limit(time=0.1))
                    move = state.san(result.move)

                    legal_moves_scores = []
                    geral_scores = []
                    for move in legal_moves:
                        action = state.push_san(move) # Make the move
                        obs = make_matrix(state)
                        #Space https://en.wikipedia.org/wiki/Chess_strategy#Space
                        #attacked
                        att_space = 0
                        #ocupied
                        black_space = list(itertools.chain(*obs))[:32]
                        occ = [x if x>0 else 0 for x in black_space]
                        #space
                        space = np.sum(occ) + att_space
                        #print("Space: ", space)
                        legal_moves_scores.append(space)

                        info = engine.analyse(state, chess.engine.Limit(time=0.1))
                        mate = info["score"].white().mate()
                        if mate != None:
                            if mate < 0:
                                score = -((20-(-mate)) * 100)
                            else:
                                score = (20-mate) * 100
                        else:
                            score = info["score"].white().score()
                            
                        score = score if score != None else 0
                        geral_scores.append(score)

                        state.pop()  # Unmake the last move
                        
                    #do the best move
                    #print(legal_moves_scores)
                    a = min(legal_moves_scores)
                    b = max(legal_moves_scores)
                    c = min(geral_scores)
                    d = max(geral_scores)
                    n_legal_score = [(x-a)/(b-a) if (b-a) != 0 else 0 for x in legal_moves_scores]
                    n_geral_score = [(x-c)/(d-c) if (d-c) != 0 else 0 for x in geral_scores]
                    zipped_lists = zip(n_legal_score, n_geral_score)
                    final_score = [x + y for (x, y) in zipped_lists]

                    print("Final score: ",final_score)
                    print("Legal moves: ",legal_moves)
                    move = legal_moves[final_score.index(max(final_score))]
                    
                    #move = legal_moves[legal_moves_scores.index(max(legal_moves_scores))]
                    return move

        elif choice == 0: #stockfish backup
            print("Option 0 - Exploration Stockfish")
            result = engine.play(state, chess.engine.Limit(time=0.1))
            move = state.san(result.move)
            return move

        elif choice == 1: #Komodo
            print("Option 1 - Exploration Komodo")
            result = engine.play(state, chess.engine.Limit(time=0.1))
            move = state.san(result.move)
            return move
        
        elif choice == 2: #Komodo Ribka
            print("Option 2 - Exploration Ribka")
            result = engine.play(state, chess.engine.Limit(time=0.1))
            move = state.san(result.move)
            return move
コード例 #10
0
        except:
            black_rating = 0

        board = first_game.board()

        # Iterate through all moves and play them on a board.
        for move in first_game.mainline_moves():
            #print(env.render())

            print(" ")
            print("NEXT MOVE: ", move, ',', board.san(move))
            print(" ")
            if first_game.is_end():
                #state = env.reset()
                break
            obs = make_matrix(board)
            #X.append(obs)
            #y.append(board.san(move))
            #print(board.san(move))

            state = board
            #action = state.push_san(move)

            player = 'white'
            print("Material Advantage")
            move1 = calculate_min_max_tree(state,
                                           env,
                                           player,
                                           depth=0,
                                           mode='material_adv',
                                           engine=engine)