forked from lichess-bot-devs/lichess-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine_wrapper.py
208 lines (158 loc) · 6.36 KB
/
engine_wrapper.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import os
import chess
import chess.xboard
import chess.uci
import backoff
import subprocess
@backoff.on_exception(backoff.expo, BaseException, max_time=120)
def create_engine(config, board):
cfg = config["engine"]
engine_path = os.path.join(cfg["dir"], cfg["name"])
engine_type = cfg.get("protocol")
lczero_options = cfg.get("lczero")
commands = [engine_path]
if lczero_options:
if "weights" in lczero_options:
commands.append("-w")
commands.append(lczero_options["weights"])
if "threads" in lczero_options:
commands.append("-t")
commands.append(str(lczero_options["threads"]))
if "gpu" in lczero_options:
commands.append("--gpu")
commands.append(str(lczero_options["gpu"]))
if "tempdecay" in lczero_options:
commands.append("--tempdecay")
commands.append(str(lczero_options["tempdecay"]))
if lczero_options.get("noise"):
commands.append("--noise")
if "log" in lczero_options:
commands.append("--logfile")
commands.append(lczero_options["log"])
silence_stderr = cfg.get("silence_stderr", False)
if engine_type == "xboard":
return XBoardEngine(board, commands, cfg.get("xboard_options", {}) or {}, silence_stderr)
return UCIEngine(board, commands, cfg.get("uci_options", {}) or {}, silence_stderr)
class EngineWrapper:
def __init__(self, board, commands, options=None, silence_stderr=False):
pass
def set_time_control(self, game):
pass
def first_search(self, board, movetime):
pass
def search(self, board, wtime, btime, winc, binc):
pass
def print_stats(self):
pass
def name(self):
return self.engine.name
def quit(self):
self.engine.quit()
def print_handler_stats(self, info, stats):
for stat in stats:
if stat in info:
print(" {}: {}".format(stat, info[stat]))
def get_handler_stats(self, info, stats):
stats_str = []
for stat in stats:
if stat in info:
stats_str.append("{}: {}".format(stat, info[stat]))
return stats_str
class UCIEngine(EngineWrapper):
def __init__(self, board, commands, options, silence_stderr=False):
commands = commands[0] if len(commands) == 1 else commands
self.go_commands = options.get("go_commands", {})
self.engine = chess.uci.popen_engine(commands, stderr = subprocess.DEVNULL if silence_stderr else None)
self.engine.uci()
if options:
self.engine.setoption(options)
self.engine.setoption({
"UCI_Variant": type(board).uci_variant,
"UCI_Chess960": board.chess960
})
self.engine.position(board)
info_handler = chess.uci.InfoHandler()
self.engine.info_handlers.append(info_handler)
def first_search(self, board, movetime):
self.engine.position(board)
best_move, _ = self.engine.go(movetime=movetime)
return best_move
def search(self, board, wtime, btime, winc, binc):
self.engine.position(board)
cmds = self.go_commands
best_move, _ = self.engine.go(
wtime=wtime,
btime=btime,
winc=winc,
binc=binc,
depth=cmds.get("depth"),
nodes=cmds.get("nodes"),
movetime=cmds.get("movetime")
)
return best_move
def stop(self):
self.engine.stop()
def print_stats(self):
self.print_handler_stats(self.engine.info_handlers[0].info, ["string", "depth", "nps", "nodes", "score"])
def get_stats(self):
return self.get_handler_stats(self.engine.info_handlers[0].info, ["depth", "nps", "nodes", "score"])
class XBoardEngine(EngineWrapper):
def __init__(self, board, commands, options=None, silence_stderr=False):
commands = commands[0] if len(commands) == 1 else commands
self.engine = chess.xboard.popen_engine(commands, stderr = subprocess.DEVNULL if silence_stderr else None)
self.engine.xboard()
if board.chess960:
self.engine.send_variant("fischerandom")
elif type(board).uci_variant != "chess":
self.engine.send_variant(type(board).uci_variant)
if options:
self._handle_options(options)
self.engine.setboard(board)
post_handler = chess.xboard.PostHandler()
self.engine.post_handlers.append(post_handler)
def _handle_options(self, options):
for option, value in options.items():
if option == "memory":
self.engine.memory(value)
elif option == "cores":
self.engine.cores(value)
elif option == "egtpath":
for egttype, egtpath in value.items():
try:
self.engine.egtpath(egttype, egtpath)
except EngineStateException:
# If the user specifies more TBs than the engine supports, ignore the error.
pass
else:
try:
self.engine.features.set_option(option, value)
except EngineStateException:
pass
def set_time_control(self, game):
minutes = game.clock_initial / 1000 / 60
seconds = game.clock_initial / 1000 % 60
inc = game.clock_increment / 1000
self.engine.level(0, minutes, seconds, inc)
def first_search(self, board, movetime):
self.engine.setboard(board)
self.engine.level(0, 0, movetime / 1000, 0)
bestmove = self.engine.go()
return bestmove
def search(self, board, wtime, btime, winc, binc):
self.engine.setboard(board)
if board.turn == chess.WHITE:
self.engine.time(wtime / 10)
self.engine.otim(btime / 10)
else:
self.engine.time(btime / 10)
self.engine.otim(wtime / 10)
return self.engine.go()
def print_stats(self):
self.print_handler_stats(self.engine.post_handlers[0].post, ["depth", "nodes", "score"])
def get_stats(self):
return self.get_handler_stats(self.engine.post_handlers[0].post, ["depth", "nodes", "score"])
def name(self):
try:
return self.engine.features.get("myname")
except:
return None