-
Notifications
You must be signed in to change notification settings - Fork 0
/
range_engine.py
343 lines (300 loc) · 11.4 KB
/
range_engine.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
import random
import chess,time
import chess.syzygy as syzygy
from NetworkTableBase import TableBase
from types import GeneratorType
# For making recursion iterative
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
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 Dummy:
def __init__(self):
pass
def stop(self):
pass
def ponderhit(self):
pass
material = {chess.PAWN : 1,
chess.QUEEN : 9,
chess.KNIGHT : 3.1,
chess.BISHOP : 3.25,
chess.ROOK : 5,
chess.KING : 100000}
class RangeEngine(EngineWrapper):
def __init__(self, board, commands, options, silence_stderr=False):
self.engine = Dummy()
self.board = board
self.inf = pow(10,8)
self.depth = 4
self.count = 0
self.piece_values = material
self.tb = TableBase()
self.table = {}
self.square_table = square_table = {
1: [
0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, -5, -10, 0, 0, -10, -5, 5,
5, 10, 10, -20, -20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
],
2: [
-50, -40, -30, -30, -30, -30, -40, -50,
-40, -20, 0, 0, 0, 0, -20, -40,
-30, 0, 10, 15, 15, 10, 0, -30,
-30, 5, 15, 20, 20, 15, 5, -30,
-30, 0, 15, 20, 20, 15, 0, -30,
-30, 5, 10, 15, 15, 10, 5, -30,
-40, -20, 0, 5, 5, 0, -20, -40,
-50, -40, -30, -30, -30, -30, -40, -50,
],
3: [
-20, -10, -10, -10, -10, -10, -10, -20,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 5, 5, 10, 10, 5, 5, -10,
-10, 0, 10, 10, 10, 10, 0, -10,
-10, 10, 10, 10, 10, 10, 10, -10,
-10, 5, 0, 0, 0, 0, 5, -10,
-20, -10, -10, -10, -10, -10, -10, -20,
],
4: [
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, 10, 10, 10, 10, 5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
0, 0, 0, 5, 5, 0, 0, 0
],
5: [
-20, -10, -10, -5, -5, -10, -10, -20,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0, -10,
-10, 0, 5, 0, 0, 0, 0, -10,
-20, -10, -10, -5, -5, -10, -10, -20
],
6: [
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-20, -30, -30, -40, -40, -30, -30, -20,
-10, -20, -20, -20, -20, -20, -20, -10,
20, 20, 0, 0, 0, 0, 20, 20,
20, 30, 10, 0, 0, 10, 30, 20
]
}
def first_search(self, board, movetime):
# return random.choice(list(board.legal_moves))
self.board = board.copy()
self.count = 0
self.depth = 4
result = self.alphabeta(-self.inf-1,self.inf+1,self.board.turn,self.depth)[1]
# print('Nodes searched : {}, Move : {}',self.count,result)
return result
def search_with_ponder(self, board, wtime, btime, winc, binc, ponder=False):
# return random.choice(list(board.legal_moves)),chess.Move.from_uci('e2e4')
self.board = board.copy()
self.count = 0
# if pieces>15:
# self.depth = 4
# elif pieces<=15 and pieces>=8:
# self.depth = 6
self.depth = 4
if len(board.piece_map()) <= 10:
self.depth=6
print('searching for depth = {}'.format(self.depth))
# result = self.alphabeta(-self.inf-1,self.inf+1,self.board.turn,self.depth)[1],None
while True:
try:
result = self.alphabeta(-self.inf-1,self.inf+1,self.board.turn,self.depth)[1],None
break
except:
pass
return result
def search(self, board, wtime, btime, winc, binc):
# return random.choice(list(board.legal_moves))
self.board = board.copy()
self.count = 0
self.depth = 4
if len(board.piece_map()) <= 10:
self.depth=6
result = self.alphabeta(-self.inf-1,self.inf+1,self.board.turn,self.depth)[1]
# result = self.optimized_alphabeta(self.board,-self.inf-1,self.inf+1,self.board.turn,self.depth).compute()[1]
# print('Nodes searched : {}, Move : {}',self.count,result)
return result
def quit(self):
pass
def stop(self):
pass
def name(self):
return 'Rangefish'
# self.engine.stop()
def print_stats(self):
# print('Depth searched = {}'.format(self.depth+10))
print('Nodes searched = {}'.format(self.count))
def get_stats(self):
# return self.get_handler_stats(self.engine.info_handlers[0].info, ["depth", "nps", "nodes", "score"])
return 'get_stats'
def eval(self,board):
# return self.material_eval(board)
return self.material_eval(board)+self.position_eval(board)/100
def position_eval(self,board):
score = 0
# iterate through the pieces
for i in range(1, 7):
# eval white pieces
w_squares = board.pieces(i, chess.WHITE)
# score += len(w_squares) * self.piece_values[i]
for square in w_squares:
score += self.square_table[i][-square]
b_squares = board.pieces(i, chess.BLACK)
# score -= len(b_squares) * self.piece_values[i]
for square in b_squares:
score -= self.square_table[i][square]
return score
def material_eval(self,board):
# TODO need to give higher eval for less pieces
# board = self.board
b,w = 0,0
for i in range(1,6):
w+=len(board.pieces(i,chess.WHITE))*material[i]
b-=len(board.pieces(i,chess.BLACK))*material[i]
return (w+b)*(32/len(board.piece_map()))
def order_moves(self,board):
# board = board.copy()
def key_func(move):
board.push(move)
val,depth = self.table.get(hash(str(board)),(self.eval(board),0))
board.pop()
return val
moves = list(board.legal_moves)
moves.sort(key=key_func,reverse=board.turn)
return moves
def addtotable(self,board,val,dep):
h = hash(str(board))
if (h in self.table and self.table[h][1]<dep) or (h not in self.table):
self.table[h] = (val,dep)
@bootstrap
def alphabeta(self,alpha,beta,ismax,depth):
board = self.board
self.count+=1
moves = self.order_moves(board)
if board.is_checkmate():
if board.result() == "1-0":
yield 1000000,None
elif board.result() == "0-1":
yield -1000000,None
elif board.can_claim_draw() or len(moves) == 0:
yield 0,None
else:
pieces = len(board.piece_map())
if pieces<=7 and depth == 4:
yield self.tb.get_eval_and_move(board.fen())
if depth <= 0:
self.depth = depth
# return self.eval(board),None
yield self.eval(board),None
else:
if ismax:
best = -self.inf
bmove = None
for move in moves:
board.push(move)
value,_= yield self.alphabeta(alpha,beta,False,depth-1)
board.pop()
if value>best:
best = value
bmove=move
alpha = max(alpha,best)
if beta<=alpha:
break
self.addtotable(board,best,self.depth-depth)
yield best,bmove
else:
best = self.inf
bmove = None
for move in moves:
board.push(move)
value,_= yield self.alphabeta(alpha,beta,True,depth-1)
board.pop()
if value<best:
best = value
bmove = move
beta = min(beta,best)
if beta<=alpha:
break
self.addtotable(board,best,self.depth-depth)
yield best,bmove
def probe(self,board1):
board = board1.copy()
with chess.syzygy.open_tablebase("tablebases/") as tablebase:
# return tablebase.probe_wdl(board)
return tablebase.probe_wdl(board),tablebase.probe_dtz(board)
def main():
# fen = '8/2p1P1k1/8/1p3QNP/1P1P1B2/KP3P2/7P/5R2 w - - 1 51'
# fen = 'r1bqr1k1/pp3pbp/5np1/2np4/5B2/2N1PN2/PP2BPPP/2RQK2R w K - 4 15'
# fen = 'N2k1b1r/3b1ppp/5n2/4pq2/1Q6/2N5/PP2PPPP/n1BK1B1R w - - 11 21'
# fen = 'r1b1kb1r/p3pppp/2p5/3pB3/8/2q1PN2/P1P2PPP/R2Q1RK1 b kq - 1 11'
# fen = '6kr/8/p6p/8/8/3K4/8/8 b - - 17 70'
board = chess.Board()
board.push(chess.Move.from_uci('e2e4'))
board.push(chess.Move.from_uci('e7e5'))
print(board.move_stack)
# print(board)
# tb = TableBase()
# print(tb.get_eval_and_move(fen))
# with chess.syzygy.open_tablebase("tablebases/") as tablebase:
# print(tablebase.probe_wdl(board))
# print(tablebase.probe_dtz(board))
# engine = RangeEngine(board,None,None)
# move = engine.search_with_ponder(board,1,1,1,1)[0]
if __name__ == '__main__':
main()