|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import chess |
| 5 | +import chess.pgn |
| 6 | +import sys |
| 7 | +import traceback |
| 8 | +import io |
| 9 | + |
| 10 | +from subprocess import Popen, PIPE |
| 11 | +from batched_execution_pool import BatchedExecutionPool |
| 12 | + |
| 13 | +class Engine(): |
| 14 | + |
| 15 | + def __init__(self, binary): |
| 16 | + self.engine = Popen([binary], stdin=PIPE, stdout=PIPE, universal_newlines=True, shell=True) |
| 17 | + self.uci_ready() |
| 18 | + |
| 19 | + def write_line(self, line): |
| 20 | + self.engine.stdin.write(line) |
| 21 | + self.engine.stdin.flush() |
| 22 | + |
| 23 | + def read_line(self): |
| 24 | + return self.engine.stdout.readline().rstrip() |
| 25 | + |
| 26 | + def uci_ready(self): |
| 27 | + self.write_line('isready\n') |
| 28 | + while self.read_line() != 'readyok': pass |
| 29 | + |
| 30 | + def quit(self): |
| 31 | + self.write_line('quit\n') |
| 32 | + |
| 33 | +def parse_args(): |
| 34 | + |
| 35 | + p = argparse.ArgumentParser(description='Add UCI options with: --option.Name=Value') |
| 36 | + p.add_argument('--engine', type=str, required=True, help='Path to the engine or engine name') |
| 37 | + p.add_argument('--pgn', type=str, required=True, help='Path to the PGN file') |
| 38 | + p.add_argument('--player', type=str, required=True, help='Name of the player') |
| 39 | + args, unknown = p.parse_known_args() |
| 40 | + |
| 41 | + uci_options = [] |
| 42 | + for value in unknown: |
| 43 | + if '=' in value and value.startswith('--option.'): |
| 44 | + uci_options.append(value[len('--option.'):].split('=')) |
| 45 | + |
| 46 | + return args, uci_options |
| 47 | + |
| 48 | +def convert_uci_to_score(score_type, score_value): |
| 49 | + |
| 50 | + if score_type == 'cp' and int(score_value) == 0: |
| 51 | + return '0.00' |
| 52 | + |
| 53 | + if score_type == 'cp': |
| 54 | + return '%+.2f' % (float(score_value) / 100.0) |
| 55 | + |
| 56 | + if score_type == 'mate' and int(score_value) < 0: |
| 57 | + return '-M%d' % (abs(2 * int(score_value))) |
| 58 | + |
| 59 | + if score_type == 'mate' and int(score_value) > 0: |
| 60 | + return '+M%d' % (abs(2 * int(score_value) - 1)) |
| 61 | + |
| 62 | + raise Exception('Unable to process Score (%s, %s)' % (score_type, score_value)) |
| 63 | + |
| 64 | + |
| 65 | +def game_generator(args): |
| 66 | + with open(args.pgn) as pgn_file: |
| 67 | + while game := chess.pgn.read_game(pgn_file): |
| 68 | + yield (str(game)) |
| 69 | + |
| 70 | +def replay_game(game_str, args): |
| 71 | + |
| 72 | + game = chess.pgn.read_game(io.StringIO(game_str)) |
| 73 | + process_args, uci_options = args |
| 74 | + |
| 75 | + is_white = game.headers.get('White') == process_args.player |
| 76 | + is_black = game.headers.get('Black') == process_args.player |
| 77 | + assert is_white or is_black |
| 78 | + |
| 79 | + fen = game.headers.get('FEN', None) |
| 80 | + pos = 'position fen %s moves' % (fen) if fen else 'position startpos moves' |
| 81 | + |
| 82 | + engine = Engine(process_args.engine) |
| 83 | + for opt, value in uci_options: |
| 84 | + engine.write_line('setoption name %s value %s\n' % (opt, value)) |
| 85 | + |
| 86 | + node = game |
| 87 | + while node.variations: |
| 88 | + |
| 89 | + next_node = node.variation(0) |
| 90 | + |
| 91 | + if (node.turn() and is_white) or (not node.turn() and is_black): |
| 92 | + |
| 93 | + try: |
| 94 | + pgn_score, pgn_depths, pgn_timems, pgn_nodes = next_node.comment.split() |
| 95 | + pgn_nodes = int(pgn_nodes) |
| 96 | + |
| 97 | + except Exception: |
| 98 | + break |
| 99 | + |
| 100 | + engine.uci_ready() |
| 101 | + engine.write_line('%s\ngo nodes %d\n' % (pos, pgn_nodes)) |
| 102 | + |
| 103 | + score = None |
| 104 | + while 'bestmove' not in (line := engine.read_line()): |
| 105 | + if ' score ' in line: |
| 106 | + score = line.split(' score ')[1].split('nodes ')[0].split()[:2] |
| 107 | + best_move = line.split()[1] |
| 108 | + |
| 109 | + try: |
| 110 | + assert best_move == next_node.move.uci() |
| 111 | + assert convert_uci_to_score(*score) == pgn_score |
| 112 | + except AssertionError: |
| 113 | + engine.quit() |
| 114 | + return 'Failed: ' + game_str |
| 115 | + |
| 116 | + pos += ' ' + next_node.move.uci() |
| 117 | + node = next_node |
| 118 | + |
| 119 | + engine.quit() |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + |
| 123 | + args, uci_options = parse_args() |
| 124 | + |
| 125 | + pool = BatchedExecutionPool( |
| 126 | + input_generator = game_generator(args), |
| 127 | + process_function = replay_game, |
| 128 | + process_function_args = [args, uci_options], |
| 129 | + ) |
| 130 | + |
| 131 | + for result in pool.execute(threads=15, batchsize=256): |
| 132 | + if result: |
| 133 | + print (result) |
0 commit comments