Download tasks/chess_mate_distance/solution/reference/source_protocol.py from Tokenwave-AI/REE: direct link, hf CLI and curl.
- Browser
- Download file 1.55 kB
-
https://huggingface.co/datasets/Tokenwave-AI/REE/resolve/main/tasks/chess_mate_distance/solution/reference/source_protocol.py
- Command line
-
hf download hf://datasets/Tokenwave-AI/REE/tasks/chess_mate_distance/solution/reference/source_protocol.py
-
curl -L -o source_protocol.py https://huggingface.co/datasets/Tokenwave-AI/REE/resolve/main/tasks/chess_mate_distance/solution/reference/source_protocol.py
1.55 kB
| """Task adapter for the unmodified Crystal v1.1 engine. | |
| Use matetrack's one-million-node evaluation protocol and preserve input order. | |
| """ | |
| import json | |
| import os | |
| import sys | |
| from multiprocessing import Pool | |
| from pathlib import Path | |
| import chess | |
| import chess.engine | |
| sys.path.insert(0, str(Path(os.environ["CORE_DIR"]) / "interface")) | |
| from positions import read_positions | |
| def analyse_many(rows): | |
| results = [] | |
| with chess.engine.SimpleEngine.popen_uci( | |
| str(Path(__file__).with_name("engine")) | |
| ) as engine: | |
| for index, fen in rows: | |
| board = chess.Board(fen) | |
| mate = None | |
| with engine.analysis( | |
| board, chess.engine.Limit(nodes=1_000_000), game=board | |
| ) as analysis: | |
| for info in analysis: | |
| if "score" in info and info.get("multipv", 1) == 1: | |
| mate = None | |
| if "upperbound" not in info and "lowerbound" not in info: | |
| mate = info["score"].pov(board.turn).mate() | |
| results.append((index, mate)) | |
| engine.quit() | |
| return results | |
| if __name__ == "__main__": | |
| rows = list(enumerate(read_positions(sys.argv[1]))) | |
| with Pool(4) as pool: | |
| chunks = pool.map(analyse_many, [rows[i::4] for i in range(4)]) | |
| predictions = [mate for _, mate in sorted(row for chunk in chunks for row in chunk)] | |
| output = Path(sys.argv[2]) | |
| output.mkdir(parents=True, exist_ok=True) | |
| (output / "mates.json").write_text(json.dumps(predictions) + "\n") | |