forked from jnlt3/weather-factory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcutechess.py
85 lines (73 loc) · 2.53 KB
/
cutechess.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from dataclasses import dataclass
from subprocess import PIPE, Popen
@dataclass
class MatchResult:
w: int
l: int
d: int
elo_diff: float
class CutechessMan:
def __init__(
self,
engine: str,
book: str,
games: int = 120,
tc: float = 5.0,
hash: int = 8,
threads: int = 1,
save_rate: int = 10
):
self.engine = engine
self.book = book
self.games = games
self.tc = tc
self.inc = tc / 100
self.hash_size = hash
self.threads = threads
self.save_rate = save_rate
def get_cutechess_cmd(
self,
params_a: list[str],
params_b: list[str]
) -> str:
return (
"./tuner/cutechess/cutechess-cli "
f"-engine cmd=./tuner/{self.engine} name={self.engine} proto=uci "
f"option.Hash={self.hash_size} {' '.join(params_a)} "
f"-engine cmd=./tuner/{self.engine} name={self.engine} proto=uci "
f"option.Hash={self.hash_size} {' '.join(params_b)} "
"-resign movecount=3 score=500 twosided=true "
"-draw movenumber=40 movecount=8 score=10 "
"-repeat "
"-recover "
f"-concurrency {self.threads} "
f"-each tc={self.tc}+{self.inc} "
f"-openings file=tuner/{self.book} "
f"format={self.book.split('.')[-1]} order=random "
f"-games {self.games} "
"-pgnout tuner/games.pgn"
)
def run(self, params_a: list[str], params_b: list[str]) -> MatchResult:
cmd = self.get_cutechess_cmd(params_a, params_b)
print(cmd)
cutechess = Popen(cmd.split(), stdout=PIPE)
score = [0, 0, 0]
elo_diff = 0.0
while True:
# Read each line of output until the pipe closes
line = cutechess.stdout.readline().strip().decode('ascii')
print(line)
if not line:
cutechess.wait()
return MatchResult(*score, elo_diff)
# Parse WLD score
if line.startswith("Score of"):
start_index = line.find(":") + 1
end_index = line.find("[")
split = line[start_index:end_index].split(" - ")
score = [int(i) for i in split]
# Parse Elo Difference
if line.startswith("Elo difference"):
start_index = line.find(":") + 1
end_index = line.find("+")
elo_diff = float(line[start_index:end_index])