File size: 1,553 Bytes
7f50cd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""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")