Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse, subprocess, sys
- import chess
- parser = argparse.ArgumentParser()
- parser.add_argument("-E", "--engine", help="Name of Engine", required=True, default="")
- parser.add_argument("-S", "--suite", help="Name of Suite", required=True, default="")
- parser.add_argument("-T", "--time", help="Time per Position in seconds", required=True, default="")
- parser.add_argument("-C", "--cores", help="Cores for the Engine", required=False, default=1)
- parser.add_argument("-H", "--hash", help="Table Size in MB", required=False, default=256)
- argument = parser.parse_args()
- logfile = "{0}-{1}-{2}-{3}-{4}.log".format(
- argument.engine,
- argument.suite,
- argument.time,
- argument.cores,
- argument.hash
- )
- with open(logfile, "w") as fout:
- fout.write("ENGINE : {0}\n".format(argument.engine))
- fout.write("SUITE : {0}\n".format(argument.suite ))
- fout.write("TIME : {0}\n".format(argument.time ))
- fout.write("CORES : {0}\n".format(argument.cores ))
- fout.write("HASH : {0}\n".format(argument.hash ))
- engine = subprocess.Popen(
- argument.engine,
- stderr=subprocess.STDOUT,
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- universal_newlines=True
- )
- engine.stdin.write("setoption name Threads value {0}\n".format(argument.cores))
- engine.stdin.write("setoption name Hash value {0}\n".format(argument.hash))
- engine.stdin.flush()
- with open(argument.suite, "r") as fin:
- data = fin.readlines()
- data = [f.split(";")[0] for f in data]
- score = 0
- for i, position in enumerate(data):
- if "am" in position:
- fen = position.split("am")[0]
- move = position.split("am ")[1].split(" ")[0].strip()
- good = False
- else:
- fen = position.split("bm")[0]
- move = position.split("bm ")[1].split(" ")[0].strip()
- good = True
- move = chess.Board(fen + "0 0").parse_san(move)
- engine.stdin.write("ucinewgame\n")
- engine.stdin.write("position fen {0}\n".format(fen + "0 0"))
- engine.stdin.write("go movetime {0}\n".format(float(argument.time) * 1000))
- engine.stdin.flush()
- outputs = []
- while True:
- line = engine.stdout.readline().strip()
- outputs.append(line)
- if "bestmove" in line:
- best = line.split(" ")[1]
- best = chess.Board(fen + "0 0").parse_uci(best)
- break
- with open(logfile, "a") as fout:
- fout.write("Position #%d FEN : %s\n" % (i, fen))
- for line in outputs:
- fout.write(line + "\n")
- scoreLine = None
- for line in outputs:
- if "score mate" in line or "score cp" in line:
- scoreLine = line
- if "mate" in scoreLine: _eval = "M" + scoreLine.split("score mate ")[1].split(" ")[0]
- elif "cp" in scoreLine: _eval = scoreLine.split("score cp ")[1].split(" ")[0]
- else : _eval = "NONE"
- if good:
- print("{0:>3} : FEN={1:>70} BEST={2} GIVEN={3} EVAL={4:>5}".format(i, fen, move, best, _eval))
- score += move == best
- else:
- print("{0:>3} : FEN={1:>70} ANTI={2} GIVEN={3} EVAL={4:>5}".format(i, fen, move, best, _eval))
- score += move != best
- sys.stdout.flush()
- engine.stdin.write("quit\n")
- engine.stdin.flush()
- engine.wait()
- with open(logfile, "a") as fout:
- fout.write("Score [%3d / %3d]" % (score, len(data)))
- print ("Score [%3d / %3d]" % (score, len(data)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement