Last active
January 2, 2019 15:49
-
-
Save mlakolar/0fd1557268e575c6b3f0f626bbf50731 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) | |
parser.add_argument("--MultiPV", "-L", | |
help="number of variations to print", | |
default="5", | |
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, | |
"MultiPV": args.MultiPV | |
}) | |
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 = timer() | |
game = chess.pgn.read_game(open(fName.rstrip())) | |
node = game.end() | |
board = node.board() | |
engine.position(board) | |
evaluation = engine.go(depth=args.depth) | |
print("Nodes per second: {}" . format(handler.info["nps"])) | |
print("Took time {}" . format(timer() - start)) | |
for l in range(1, len(handler.info["pv"]) + 1): | |
node_next = node.add_variation(handler.info["pv"][l][0]) | |
node_next.comment = "%s/%s" % (format(handler.info["score"][l].cp/100.0), format(args.depth)) | |
for mv in handler.info["pv"][l][1:]: | |
node_next = node_next.add_variation(mv) | |
with open("%s.done" % (fName.rstrip()), "w") as fOut: | |
print(game.root(), file=fOut, end="\n\n") | |
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