-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearcher.py
368 lines (315 loc) · 12 KB
/
searcher.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import time
import subprocess
from subprocess import PIPE
import re
from chess import Move
import chess
import numpy as np
nodes = 0
TIME_LIMIT = 1000
NODE_LIMIT = 30000
PRINT_LIMIT = 5000
EPSILON = 0.01
class TimeOutException(Exception):
pass
class Searcher:
def __init__(self, evaluator, name="Tensorflow"):
self.evaluator = evaluator
self.name = name
def pos_key(self, b):
fen = b.fen()
parts = fen.split(" ")
return "{} {}".format(parts[0], parts[1])
def history_index(self, m):
return (m.from_square << 6) + m.to_square
def eval_cached(self, b):
key = self.pos_key(b)
if key in self.eval_cache:
return self.eval_cache[key]
else:
eval = self.evaluator(b)
self.eval_cache[key] = eval
return eval
def next_capture(self, board):
victims = [
board.pieces_mask(chess.QUEEN, not board.turn),
board.pieces_mask(chess.ROOK, not board.turn),
board.pieces_mask(chess.KNIGHT, not board.turn)
| board.pieces_mask(chess.BISHOP, not board.turn),
board.pieces_mask(chess.PAWN, not board.turn),
]
attackers = [
board.pieces_mask(chess.PAWN, board.turn),
board.pieces_mask(chess.KNIGHT, board.turn)
| board.pieces_mask(chess.BISHOP, board.turn),
board.pieces_mask(chess.ROOK, board.turn),
board.pieces_mask(chess.QUEEN, board.turn),
board.pieces_mask(chess.KING, board.turn),
]
for victim in range(0, 4):
victims_mask = victims[victim]
for attacker in range(0, 4 - victim):
attackers_mask = attackers[attacker]
captures = board.generate_pseudo_legal_captures(
attackers_mask, victims_mask
)
for move in captures:
if board.is_legal(move):
yield move
for victim in range(0, 4):
victims_mask = victims[victim]
for attacker in range(4 - victim, len(attackers)):
attackers_mask = attackers[attacker]
captures = board.generate_pseudo_legal_captures(
attackers_mask, victims_mask
)
for move in captures:
if board.is_legal(move):
yield move
def qsearch(self, b, alpha, beta, ply=0):
self.nodes += 1
if b.is_insufficient_material():
return 0
# print("{} qsearch({}, {})".format(" " * ply, alpha, beta))
score = self.eval_cached(b)
if score >= beta:
return score
if score > alpha:
alpha = score
for move in self.next_capture(b):
b.push(move)
t = -self.qsearch(b, -beta, -alpha, ply + 1)
b.pop()
if t > score:
score = t
if score >= beta:
return score
if score > alpha:
alpha = score
return score
def move_score(self, move, board):
board.push(move)
score = self.eval_cached(board)
board.pop()
return score
def next_move_old(self, board):
key = self.pos_key(board)
hash_move = None
if key in self.move_cache:
hash_move = self.move_cache[key]
yield hash_move
l = list(board.generate_legal_moves())
l.sort(key=lambda m: self.move_score(m, board))
for move in l:
if move != hash_move:
yield move
def next_move(self, board):
key = self.pos_key(board)
hash_move = None
if key in self.move_cache:
hash_move = self.move_cache[key]
yield hash_move
captures_searched = set()
victims = [
board.pieces_mask(chess.QUEEN, not board.turn),
board.pieces_mask(chess.ROOK, not board.turn),
board.pieces_mask(chess.KNIGHT, not board.turn)
| board.pieces_mask(chess.BISHOP, not board.turn),
board.pieces_mask(chess.PAWN, not board.turn),
]
attackers = [
board.pieces_mask(chess.PAWN, board.turn),
board.pieces_mask(chess.KNIGHT, board.turn)
| board.pieces_mask(chess.BISHOP, board.turn),
board.pieces_mask(chess.ROOK, board.turn),
board.pieces_mask(chess.QUEEN, board.turn),
board.pieces_mask(chess.KING, board.turn),
]
for victim in range(0, 4):
victims_mask = victims[victim]
for attacker in range(0, 4 - victim):
attackers_mask = attackers[attacker]
captures = board.generate_pseudo_legal_captures(
attackers_mask, victims_mask
)
for move in captures:
if move != hash_move and board.is_legal(move):
captures_searched.add(move)
yield move
# for victim in range(0, 4):
# victims_mask = victims[victim]
# for attacker in range(4-victim, len(attackers)):
# attackers_mask = attackers[attacker]
# captures = board.generate_pseudo_legal_captures(attackers_mask, victims_mask)
# for move in captures:
# if move != hash_move and board.is_legal(move):
# captures_searched.add(move)
# yield move
l = list(board.generate_pseudo_legal_moves())
l = sorted(
l, key=lambda m: self.history_table[self.history_index(m)], reverse=True
)
for move in l:
if move != hash_move and not move in captures_searched:
if board.is_legal(move):
yield move
def search(self, b, alpha, beta, depth):
if depth == 0:
return self.qsearch(b, alpha, beta)
self.nodes += 1
if self.nodes > self.next_time_check:
self.elapsed = time.perf_counter() - self.start_time
if self.elapsed > self.time_limit or self.nodes > NODE_LIMIT:
raise TimeOutException()
self.next_time_check = self.nodes + 100
if b.is_insufficient_material():
return 0
if b.can_claim_draw() and 0 >= beta:
return 0
if b.is_check():
depth += 1
max_score = -1000
best_move = None
# if ply > 2:
# b.push(Move.null())
# try:
# score = -self.search(b, -beta, -alpha, ply-2)
# finally:
# b.pop()
# if score >= beta:
# return score
for move in self.next_move(b):
b.push(move)
try:
if b.is_fivefold_repetition():
score = 0
else:
score = -self.search(b, -beta, -alpha, depth - 1)
finally:
b.pop()
if score > max_score:
max_score = score
best_move = move
if max_score >= beta:
key = self.pos_key(b)
self.move_cache[key] = move
self.history_table[self.history_index(move)] += 1 << depth
return max_score
if max_score > alpha:
alpha = max_score
if best_move is None:
if b.is_stalemate():
return 0
if b.is_checkmate():
return -999
else:
key = self.pos_key(b)
self.move_cache[key] = best_move
return max_score
def pv(self, b, move, depth=0):
line = b.san(move)
if depth < 10:
b.push(move)
key = self.pos_key(b)
hash_move = None
if key in self.move_cache:
hash_move = self.move_cache[key]
line += " " + self.pv(b, hash_move, depth + 1)
b.pop()
return line
def select_move(self, b):
self.nodes = 0
self.eval_cache = {}
self.move_cache = {}
self.history_table = np.zeros((4096), np.int32)
l = list(b.generate_legal_moves())
if len(l) == 1:
return l[0]
l.sort(key=lambda m: self.move_score(m, b))
self.start_time = time.perf_counter()
self.next_time_check = self.nodes + 100
self.time_limit = TIME_LIMIT
self.elapsed = 0
best_move = None
max_score = 0
try:
for depth in range(1, 10):
is_pv = True
for move in l:
san = b.san(move)
print(
"{:2d} {:5.1f} {} ".format(
depth, self.elapsed, san
),
end="\r",
)
b.push(move)
try:
alpha = max_score
if is_pv:
alpha = max_score - EPSILON
beta = max_score + EPSILON
if b.is_fivefold_repetition():
score = 0
else:
score = -self.search(b, -beta, -alpha, depth - 1)
if is_pv and score <= alpha:
if self.nodes > PRINT_LIMIT:
print(
"{:2d}- {:5.1f} {:+.3f} {}".format(
depth, self.elapsed, score, san
)
)
score = -self.search(b, -score, 1000, depth - 1)
if score >= beta:
best_move = move
if self.nodes > PRINT_LIMIT:
print(
"{:2d}+ {:5.1f} {:+.3f} {}".format(
depth, self.elapsed, score, san
)
)
score = -self.search(b, -1000, -score, depth - 1)
finally:
b.pop()
self.elapsed = time.perf_counter() - self.start_time
if is_pv or score > max_score:
max_score = score
best_move = move
l.remove(move)
l.insert(0, move)
if self.nodes > PRINT_LIMIT:
print(
"{:2d}+ {:5.1f} {:+.3f} {}".format(
depth, self.elapsed, score, self.pv(b, best_move)
)
)
is_pv = False
print(
"{:2d} {:5.1f} {:+.3f} {}, {} nodes/sec ".format(
depth,
self.elapsed,
max_score,
self.pv(b, best_move),
int(self.nodes / self.elapsed),
)
)
except TimeOutException:
pass
print(
"==> {} with score {:.3f}, {} nodes/sec ".format(
b.san(best_move), max_score, int(self.nodes / self.elapsed)
)
)
return best_move
class AmySearcher:
def __init__(self):
self.name = "Amy"
def select_move(self, b):
p = subprocess.Popen("Amy", stdin=PIPE, stdout=PIPE)
fen = b.fen()
commands = "easy\nlevel fixed/2\nepd {}\nxboard\ngo\n".format(fen)
out, err = p.communicate(bytes(commands, "ASCII"))
reply = out.decode("ASCII")
m = re.search("move ([a-h][1-8][a-h][1-8][QRNB]?)", out.decode("ASCII"))
return b.parse_uci(m[1].lower())