-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathengine_wrapper.py
280 lines (228 loc) · 10.9 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import os
import chess.engine
import backoff
import subprocess
import logging
from enum import Enum
logger = logging.getLogger(__name__)
@backoff.on_exception(backoff.expo, BaseException, max_time=120)
def create_engine(config):
cfg = config["engine"]
engine_path = os.path.join(cfg["dir"], cfg["name"])
engine_working_dir = cfg.get("working_dir") or os.getcwd()
engine_type = cfg.get("protocol")
engine_options = cfg.get("engine_options")
draw_or_resign = cfg.get("draw_or_resign") or {}
commands = [engine_path]
if engine_options:
for k, v in engine_options.items():
commands.append(f"--{k}={v}")
stderr = None if cfg.get("silence_stderr", False) else subprocess.DEVNULL
if engine_type == "xboard":
Engine = XBoardEngine
elif engine_type == "uci":
Engine = UCIEngine
elif engine_type == "homemade":
Engine = getHomemadeEngine(cfg["name"])
else:
raise ValueError(
f" Invalid engine type: {engine_type}. Expected xboard, uci, or homemade.")
options = remove_managed_options(cfg.get(f"{engine_type}_options") or {})
logger.debug(f"Starting engine: {' '.join(commands)}")
return Engine(commands, options, stderr, draw_or_resign, cwd=engine_working_dir)
def remove_managed_options(config):
def is_managed(key):
return chess.engine.Option(key, None, None, None, None, None).is_managed()
return {name: value for (name, value) in config.items() if not is_managed(name)}
class Termination(str, Enum):
MATE = "mate"
TIMEOUT = "outoftime"
RESIGN = "resign"
ABORT = "aborted"
DRAW = "draw"
class GameEnding(str, Enum):
WHITE_WINS = "1-0"
BLACK_WINS = "0-1"
DRAW = "1/2-1/2"
INCOMPLETE = "*"
def translate_termination(termination, board, winner_color):
if termination == Termination.MATE:
return f"{winner_color.title()} mates"
elif termination == Termination.TIMEOUT:
return "Time forfeiture"
elif termination == Termination.RESIGN:
resigner = "black" if winner_color == "white" else "white"
return f"{resigner.title()} resigns"
elif termination == Termination.ABORT:
return "Game aborted"
elif termination == Termination.DRAW:
if board.is_fifty_moves():
return "50-move rule"
elif board.is_repetition():
return "Threefold repetition"
else:
return "Draw by agreement"
elif termination:
return termination
else:
return ""
PONDERPV_CHARACTERS = 12 # the length of ", ponderpv: "
MAX_CHAT_MESSAGE_LEN = 140 # maximum characters in a chat message
class EngineWrapper:
def __init__(self, options, draw_or_resign):
self.scores = []
self.draw_or_resign = draw_or_resign
self.go_commands = options.pop("go_commands", {}) or {}
self.last_move_info = {}
self.move_commentary = []
self.comment_start_index = None
def search_for(self, board, movetime, ponder, draw_offered):
return self.search(board, chess.engine.Limit(time=movetime / 1000), ponder, draw_offered)
def first_search(self, board, movetime, draw_offered):
# No pondering after the first move since a different clock is used afterwards.
return self.search_for(board, movetime, False, draw_offered)
def search_with_ponder(self, board, wtime, btime, winc, binc, ponder, draw_offered):
time_limit = chess.engine.Limit(white_clock=wtime / 1000,
black_clock=btime / 1000,
white_inc=winc / 1000,
black_inc=binc / 1000)
return self.search(board, time_limit, ponder, draw_offered)
def add_go_commands(self, time_limit):
movetime = self.go_commands.get("movetime")
if movetime is not None:
movetime_sec = float(movetime) / 1000
if time_limit.time is None or time_limit.time > movetime_sec:
time_limit.time = movetime_sec
time_limit.depth = self.go_commands.get("depth")
time_limit.nodes = self.go_commands.get("nodes")
return time_limit
def offer_draw_or_resign(self, result, board):
if self.draw_or_resign.get("offer_draw_enabled", False) and len(self.scores) >= self.draw_or_resign.get("offer_draw_moves", 5):
scores = self.scores[-self.draw_or_resign.get("offer_draw_moves", 5):]
pieces_on_board = chess.popcount(board.occupied)
scores_near_draw = lambda score: abs(score.relative.score(mate_score=40000)) <= self.draw_or_resign.get("offer_draw_score", 0)
if len(scores) == len(list(filter(scores_near_draw, scores))) and pieces_on_board <= self.draw_or_resign.get("offer_draw_pieces", 10):
result.draw_offered = True
if self.draw_or_resign.get("resign_enabled", False) and len(self.scores) >= self.draw_or_resign.get("resign_moves", 3):
scores = self.scores[-self.draw_or_resign.get("resign_moves", 3):]
scores_near_loss = lambda score: score.relative.score(mate_score=40000) <= self.draw_or_resign.get("resign_score", -1000)
if len(scores) == len(list(filter(scores_near_loss, scores))):
result.resigned = True
return result
def search(self, board, time_limit, ponder, draw_offered):
time_limit = self.add_go_commands(time_limit)
result = self.engine.play(board, time_limit, info=chess.engine.INFO_ALL, ponder=ponder, draw_offered=draw_offered)
self.last_move_info = result.info.copy()
self.move_commentary.append(self.last_move_info.copy())
if self.comment_start_index is None:
self.comment_start_index = len(board.move_stack)
self.scores.append(self.last_move_info.get("score", chess.engine.PovScore(chess.engine.Mate(1), board.turn)))
result = self.offer_draw_or_resign(result, board)
self.last_move_info["ponderpv"] = board.variation_san(self.last_move_info.get("pv", []))
self.print_stats()
return result
def comment_index(self, move_stack_index):
if self.comment_start_index is None:
return -1
else:
return move_stack_index - self.comment_start_index
def comment_for_board_index(self, index):
comment_index = self.comment_index(index)
if comment_index < 0 or comment_index % 2 != 0:
return None
try:
return self.move_commentary[comment_index // 2]
except IndexError:
return None
def add_null_comment(self):
if self.comment_start_index is not None:
self.move_commentary.append(None)
def print_stats(self):
for line in self.get_stats():
logger.info(f"{line}")
def get_stats(self, for_chat=False):
info = self.last_move_info.copy()
stats = ["depth", "nps", "nodes", "score", "ponderpv"]
if for_chat:
bot_stats = [f"{stat}: {info[stat]}" for stat in stats if stat in info and stat != "ponderpv"]
len_bot_stats = len(", ".join(bot_stats)) + PONDERPV_CHARACTERS
ponder_pv = info["ponderpv"]
ponder_pv = ponder_pv.split()
try:
while len(" ".join(ponder_pv)) + len_bot_stats > MAX_CHAT_MESSAGE_LEN:
ponder_pv.pop()
if ponder_pv[-1].endswith("."):
ponder_pv.pop()
info["ponderpv"] = " ".join(ponder_pv)
except IndexError:
pass
return [f"{stat}: {info[stat]}" for stat in stats if stat in info]
def get_opponent_info(self, game):
pass
def name(self):
return self.engine.id["name"]
def report_game_result(self, game, board):
pass
def stop(self):
pass
def quit(self):
self.engine.quit()
class UCIEngine(EngineWrapper):
def __init__(self, commands, options, stderr, draw_or_resign, **popen_args):
super().__init__(options, draw_or_resign)
self.engine = chess.engine.SimpleEngine.popen_uci(commands, stderr=stderr, **popen_args)
self.engine.configure(options)
def stop(self):
self.engine.protocol.send_line("stop")
def get_opponent_info(self, game):
name = game.opponent.name
if name and "UCI_Opponent" in self.engine.protocol.config:
rating = game.opponent.rating if game.opponent.rating is not None else "none"
title = game.opponent.title if game.opponent.title else "none"
player_type = "computer" if title == "BOT" else "human"
self.engine.configure({"UCI_Opponent": f"{title} {rating} {player_type} {name}"})
def report_game_result(self, game, board):
self.engine.protocol._position(board)
class XBoardEngine(EngineWrapper):
def __init__(self, commands, options, stderr, draw_or_resign, **popen_args):
super().__init__(options, draw_or_resign)
self.engine = chess.engine.SimpleEngine.popen_xboard(commands, stderr=stderr, **popen_args)
egt_paths = options.pop("egtpath", {}) or {}
features = self.engine.protocol.features
egt_types_from_engine = features["egt"].split(",") if "egt" in features else []
for egt_type in egt_types_from_engine:
if egt_type in egt_paths:
options[f"egtpath {egt_type}"] = egt_paths[egt_type]
else:
logger.debug(f"No paths found for egt type: {egt_type}.")
self.engine.configure(options)
def report_game_result(self, game, board):
# Send final moves, if any, to engine
self.engine.protocol._new(board, None, {})
winner = game.state.get("winner")
termination = game.state.get("status")
if winner == "white":
game_result = GameEnding.WHITE_WINS
elif winner == "black":
game_result = GameEnding.BLACK_WINS
elif termination == Termination.DRAW:
game_result = GameEnding.DRAW
else:
game_result = GameEnding.INCOMPLETE
endgame_message = translate_termination(termination, board, winner)
if endgame_message:
endgame_message = " {" + endgame_message + "}"
self.engine.protocol.send_line(f"result {game_result}{endgame_message}")
def stop(self):
self.engine.protocol.send_line("?")
def get_opponent_info(self, game):
if game.opponent.name and self.engine.protocol.features.get("name", True):
title = f"{game.opponent.title} " if game.opponent.title else ""
self.engine.protocol.send_line(f"name {title}{game.opponent.name}")
if game.me.rating is not None and game.opponent.rating is not None:
self.engine.protocol.send_line(f"rating {game.me.rating} {game.opponent.rating}")
if game.opponent.title == "BOT":
self.engine.protocol.send_line("computer")
def getHomemadeEngine(name):
import strategies
return eval(f"strategies.{name}")