首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python数据(FEN)到Stockfish for Python

Python数据(FEN)到Stockfish for Python
EN

Stack Overflow用户
提问于 2021-11-01 07:27:24
回答 1查看 611关注 0票数 2

我试着用丝袜鱼来评估国际象棋的位置,全部用Python中的分符号表示。我主要使用两个库(我在github上找到的pgnToFen:https://github.com/SindreSvendby/pgnToFen和Stockfish,麻省理工学院授权的两个库:https://github.com/zhelyabuzhsky/stockfish)。在经历了许多错误之后,我遇到了一个又一个问题。河豚不仅不能分析这个位置(3b2k1/1p3pp1/8/3pP1P1/pP3P2/P2pB3/6K1/8b f3 -),而且它无限循环!“别担心!”并且认为修改源代码是可以完成的。更改为_put(),但基本上我无法将虚拟值放入其中,因为一旦给出这些值,stdin.flush()就不会执行!这意味着我甚至不能跳到我的dataframe中的下一行。(我更改的代码如下所示。

代码语言:javascript
复制
def _put(self, command: str, tmp_time) -> None:
        if not self.stockfish.stdin:
            raise BrokenPipeError()
        self.stockfish.stdin.write(f"{command}\n")

        try:
            self.stockfish.stdin.flush()

        except:
            if command != "quit":
                self.stockfish.stdin.write('isready\n')  
                try:   
                    time.sleep(tmp_time)
                    self.stockfish.stdin.flush()
                except:
                    #print ('Imma head out', file=sys.stderr)
                    raise ValueError('Imma head out...')
                    #sys.stderr.close()

代码语言:javascript
复制
def get_evaluation(self) -> dict:
        """Evaluates current position

        Returns:
            A dictionary of the current advantage with "type" as "cp" (centipawns) or "mate" (checkmate in)
        """

        evaluation = dict()
        fen_position = self.get_fen_position()
        if "w" in fen_position:  # w can only be in FEN if it is whites move
            compare = 1
        else:  # stockfish shows advantage relative to current player, convention is to do white positive
            compare = -1
        self._put(f"position {fen_position}", 5)
        self._go()
        x=0
        while True:
            x=x+1
            text = self._read_line()
            #print(text)
            splitted_text = text.split(" ")
            if splitted_text[0] == "info":
                for n in range(len(splitted_text)):
                    if splitted_text[n] == "score":
                        evaluation = {
                            "type": splitted_text[n + 1],
                            "value": int(splitted_text[n + 2]) * compare,
                        }
            elif splitted_text[0] == "bestmove":
                return evaluation

            elif x == 500:
                evaluation = {
                    "type": 'cp',
                    "value": 10000,
                }
                return evaluation

最后但并非最不重要的是更改为下面的init_ contructor:

代码语言:javascript
复制
self._stockfish_major_version: float = float(self._read_line().split(" ")[1])

我将这些代码导入到的代码如下,这是出现错误的地方。

代码语言:javascript
复制
import pandas as pd
import re
import nltk
import numpy as np
from stockfish import Stockfish
import os
import sys

sys.path.insert(0, r'C:\Users\path\to\pgntofen')

import pgntofen

#nltk.download('punkt')

#Changed models.py for major version line 39 in stockfish from int to float

stockfish = Stockfish(r"C:\Users\path\to\Stockfish.exe")

file = r'C:\Users\path\to\selenium-pandas output.csv'

chunksize = 10 ** 6
for chunk in pd.read_csv(file, chunksize=chunksize):
    for index, row in chunk.iterrows():
        FullMovesStr = str(row['FullMoves']) 
        FullMovesStr = FullMovesStr.replace('+', '')
        if "e.p" in FullMovesStr:
            row.to_csv(r'C:\Users\MyName\Logger.csv', header=None, index=False, mode='a')
            
            print('Enpassant')
            continue

        tokens = nltk.word_tokenize(FullMovesStr)
        
        movelist = []
        for tokenit in range(len(tokens)):
            
            if "." in str(tokens[tokenit]):
                try:
                    tokenstripped = re.sub(r"[0-9]+\.", "", tokens[tokenit])
                    token = [tokenstripped, tokens[tokenit+1]]
                    movelist.append(token)
                except:
                    continue
            else:
                continue
        DFMoves = pd.DataFrame(movelist, columns=[['WhiteMove', 'BlackMove']])
        DFMoves['index'] = row['index']
        DFMoves['Date'] = row['Date']
        DFMoves['White'] = row['White']
        DFMoves['Black'] = row['Black']
        DFMoves['W ELO'] = row['W ELO']
        DFMoves['B ELO'] = row['B ELO']
        DFMoves['Av ELO'] = row['Av ELO']
        DFMoves['Event'] = row['Event']
        DFMoves['Site'] = row['Site']
        DFMoves['ECO'] = row['ECO']
        DFMoves['Opening'] = row['Opening']
        pd.set_option('display.max_rows', DFMoves.shape[0]+1)
        print(DFMoves[['WhiteMove', 'BlackMove']])
        seqmoves = []
        #seqmovesBlack = []
        evalmove = []

        pgnConverter = pgntofen.PgnToFen()
        #stockfish.set_fen_position("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
        #rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
        for index, row in DFMoves.iterrows():
            try:
                stockfish.set_fen_position("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
            except:
                evalmove.append("?")
                continue
            #stockfish.set_fen_position("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
            pgnConverter.resetBoard()
            WhiteMove = str(row['WhiteMove'])
            BlackMove = str(row['BlackMove'])

            if index == 0:
                PGNMoves1 = [WhiteMove]
                seqmoves.append(WhiteMove)
                #seqmoves.append(BlackMove)
            else:
                seqmoves.append(WhiteMove)
                #seqmoves.append(BlackMove)
                PGNMoves1 = seqmoves.copy()

            #print(seqmoves)
            try:
                pgnConverter.pgnToFen(PGNMoves1)
                fen = pgnConverter.getFullFen()
            except:
                break

            try:
          
                stockfish.set_fen_position(fen)
                print(stockfish.get_board_visual())
                evalpos = stockfish.get_evaluation()
                evalmove.append(evalpos)
              
            except:
                pass
          
            try:
                stockfish.set_fen_position("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
            except:
                evalmove.append("?")
                continue

            pgnConverter.resetBoard()

            if index == 0:
                PGNMoves2 = [WhiteMove, BlackMove]
                seqmoves.append(BlackMove)
            else:
                seqmoves.append(BlackMove)
                PGNMoves2 = seqmoves.copy()

            try:
                pgnConverter.pgnToFen(PGNMoves2)
                fen = pgnConverter.getFullFen()
            except:
                break

            try:
                stockfish.set_fen_position(fen)
                print(stockfish.get_board_visual())
                evalpos = stockfish.get_evaluation()
                print(evalpos)
                evalmove.append(evalpos)
            except:
                pass

        #DFMoves['EvalWhite'] = evalwhite
        #DFMoves['EvalBlack'] = evalblack
        print(evalmove)

因此,详细的问题是让stockfish.get_evalution()跳过,或者更好地解决这个问题,对于这个(3b2k1/1p3pp1/8/3pP1P1/pP3P2/P2pB3/6K1/8b f3 -) FEN位置。我已经在这个问题上工作了很长一段时间了,所以对这个问题的任何见解都是非常感谢的。

我的规格是Windows10,Python3.9,处理器:Intel(R) Core(TM) i9-10980XEcpu@3.00GHz3.00 GHz,内存为64.0GB。

谢谢:)

EN

回答 1

Stack Overflow用户

发布于 2021-11-03 19:13:45

好的。您的fen似乎是无效的(3b2k1/1p3pp1/8/3pP1P1/pP3P2/P2pB3/6K1/8b f3 -)。所以你去看看。python (https://python-chess.readthedocs.io/en/latest/index.html)库允许您使用FEN和国际象棋引擎。很酷不是吗?下面是这两个幻想工具的例子:

代码语言:javascript
复制
import chess
import chess.engine
import chess.pgn

pgn = open("your_pgn_file.pgn")
game = chess.pgn.read_game(pgn)

engine = chess.engine.SimpleEngine.popen_uci("your_stockfish_path.exe")

 # Iterate through all moves, play them on a board and analyse them.
board = game.board()
for move in game.mainline_moves():
    board.push(move)
    print(engine.analyse(board, chess.engine.Limit(time=0.1))["score"])
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69793774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档