Created
January 2, 2019 16:28
-
-
Save mlakolar/698289de6c8accfaf719158c7c35165e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import argparse | |
import sys | |
import chess | |
import chess.pgn | |
import chess.uci | |
from timeit import default_timer as timer | |
def parse_args(): | |
""" | |
Define an argument parser and return the parsed arguments | |
""" | |
parser = argparse.ArgumentParser( | |
prog='analyze', | |
description='takes a list of PGN files and analyzes last position' | |
'of each PGN') | |
parser.add_argument("--file", "-f", | |
help="list of PGN files", | |
required=True, | |
metavar="list_FILE") | |
parser.add_argument("--engine", "-e", | |
help="analysis engine (default: %(default)s)", | |
default="/home/mkolar/prog/stockfish/stockfish_10_x64_bmi2") | |
parser.add_argument("--Threads", "-t", | |
help="number of threads used by engine", | |
default="1", | |
type=int) | |
parser.add_argument("--depth", "-d", | |
help="depth of analysis", | |
default="26", | |
type=int) | |
parser.add_argument("--Hash", "-H", | |
help="hash size (1, ..., 4096, 8192, 16384, 32768, 65536, 131072)", | |
default="4096", | |
type=int) | |
return parser.parse_args() | |
def main(): | |
args = parse_args() | |
try: | |
with open(args.file) as pgn_list: | |
# setup engine | |
try: | |
engine = chess.uci.popen_engine(args.engine) | |
except FileNotFoundError: | |
errormsg = "Engine '{}' was not found. Aborting...".format(args.engine) | |
raise | |
except PermissionError: | |
errormsg = "Engine '{}' could not be executed. Aborting...".format(args.engine) | |
raise | |
engine.uci() | |
handler = chess.uci.InfoHandler() | |
engine.info_handlers.append(handler) | |
# hash: 4096, 8192, 16384, 32768, 65536, 131072 | |
engine.setoption({ | |
"Threads": args.Threads, | |
"Hash": args.Hash | |
}) | |
engine.isready() | |
engine.ucinewgame() | |
engine.setoption({ | |
"UCI_AnalyseMode": True, | |
"Analysis Contempt": "Off" | |
}) | |
# load PGNs | |
for fName in pgn_list.readlines(): | |
print("Processing: %s ..." % fName.rstrip()) | |
start_file = timer() | |
game = chess.pgn.read_game(open(fName.rstrip())) | |
node = game.end() | |
board = node.board() | |
moves = [mv for mv in board.legal_moves] | |
engine.position(board) | |
count = 0 | |
bestEval = 0. | |
while True: | |
count += 1 | |
start_move = timer() | |
print("Number of moves to search: {}" . format(len(moves))) | |
engine.go(depth=args.depth, searchmoves=moves) | |
print("Nodes per second: {}" . format(handler.info["nps"])) | |
print("Took time {}" . format(timer() - start_move)) | |
print("Move: {} Eval: {}/{}" . format(handler.info["pv"][1][0], handler.info["score"][1].cp/100.0, args.depth)) | |
if count == 1: | |
bestEval = handler.info["score"][1].cp/100.0 | |
if bestEval - handler.info["score"][1].cp/100.0 > 0.3: | |
break | |
node_next = node.add_variation(handler.info["pv"][1][0]) | |
node_next.comment = "%s/%s" % (format(handler.info["score"][1].cp/100.0), format(args.depth)) | |
for mv in handler.info["pv"][1][1:]: | |
node_next = node_next.add_variation(mv) | |
moves = [mv for mv in moves if mv != handler.info["pv"][1][0]] | |
with open("%s.done" % (fName.rstrip()), "w") as fOut: | |
print(game.root(), file=fOut, end="\n\n") | |
print("Overall time {}" . format(timer() - start_file)) | |
print("... done") | |
engine.quit() | |
except PermissionError: | |
errormsg = "Input file not readable. Aborting..." | |
raise | |
sys.exit() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment