forked from thomasahle/sunfish
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
289 lines (256 loc) · 9.6 KB
/
Copy pathtest.py
File metadata and controls
289 lines (256 loc) · 9.6 KB
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
#!/usr/bin/env pypy
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import re
import time
import subprocess
import functools
import os
import signal
import sunfish
import xboard
###############################################################################
# Playing test
###############################################################################
def selfplay():
""" Start a game sunfish vs. sunfish """
pos = xboard.parseFEN('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
for d in range(200):
# Always print the board from the same direction
board = pos.board if d % 2 == 0 else pos.rotate().board
print(' '.join(board))
m, _ = sunfish.search(pos, maxn=200)
if m is None:
print("Game over")
break
print("\nmove", xboard.mrender(d%2, pos, m))
pos = pos.move(m)
###############################################################################
# Test Xboard
###############################################################################
class timeout:
def __init__(self, seconds=1, error_message='Timeout'):
self.seconds = seconds
self.error_message = error_message
def handle_timeout(self, signum, frame):
raise TimeoutError(self.error_message)
def __enter__(self):
signal.signal(signal.SIGALRM, self.handle_timeout)
signal.alarm(self.seconds)
def __exit__(self, type, value, traceback):
signal.alarm(0)
def testxboard(python='python3'):
print('Xboard test \'%s\'' % python)
fish = subprocess.Popen([python, '-u', 'xboard.py'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
universal_newlines=True)
def waitFor(regex):
with timeout(20, '%s was never encountered'%regex):
while True:
line = fish.stdout.readline()
# print("Saw lines", line)
if re.search(regex, line):
return
try:
print('xboard', file=fish.stdin)
print('protover 2', file=fish.stdin)
waitFor('done\s*=\s*1')
print('usermove e2e4', file=fish.stdin)
waitFor('move ')
print('setboard rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1', file=fish.stdin)
print('usermove e7e5', file=fish.stdin)
waitFor('move ')
print('quit', file=fish.stdin)
with timeout(5, 'quit did not terminate sunfish'):
fish.wait()
finally:
if fish.poll() is None:
fish.kill()
###############################################################################
# Perft test
###############################################################################
def allperft(path, depth=4):
for d in range(1, depth+1):
print("Going to depth %d" % d)
with open(path) as f:
for line in f:
parts = line.split(';')
print(parts[0])
pos, score = xboard.parseFEN(parts[0]), int(parts[d])
res = perft(pos, d)
if res != score:
print('=========================================')
print("ERROR at depth %d. Gave %d rather than %d" % (d, res, score))
print('=========================================')
if d == 1:
print(pos)
perft(pos, d, divide=True)
return
print('')
def perft(pos, depth, divide=False):
if depth == 0:
return 1
res = 0
for m in pos.genMoves():
pos1 = pos.move(m)
# Make sure the move was legal
if not any(pos1.value(m) >= sunfish.MATE_VALUE for m in pos1.genMoves()):
sub = perft(pos1, depth-1, False)
if divide:
print(" "*depth+xboard.mrender(m), sub)
res += sub
return res
###############################################################################
# Find mate test
###############################################################################
def allmate(path):
with open(path) as f:
for line in f:
line = line.strip()
print(line)
pos = xboard.parseFEN(line)
_, score = sunfish.search(pos, maxn=1e9)
if score < sunfish.MATE_VALUE:
print("Unable to find mate. Only got score = %d" % score)
break
def quickdraw(path, depth):
with open(path) as f:
for line in f:
line = line.strip()
print(line)
pos = xboard.parseFEN(line)
for d in range(depth, 99):
s0 = sunfish.bound(pos, 0, d)
s1 = sunfish.bound(pos, 1, d)
if s0 >= 0 and s1 < 1:
break
print(d, s0, s1, xboard.pv(0, pos))
else:
print("Fail: Unable to find draw!")
return
def quickmate(path, depth):
""" Similar to allmate, but uses the `bound` function directly to only
search for moves that will win us the game """
with open(path) as f:
for line in f:
line = line.strip()
print(line)
pos = xboard.parseFEN(line)
for d in range(depth, 99):
score = sunfish.bound(pos, sunfish.MATE_VALUE, d)
if score >= sunfish.MATE_VALUE:
break
print(d, score)
else:
print("Unable to find mate. Only got score = %d" % score)
return
###############################################################################
# Best move test
###############################################################################
def renderSAN(pos, move):
# TODO: How do we simply make this work for black as well?
i, j = move
csrc, cdst = sunfish.render(i), sunfish.render(j)
# Check
pos1 = pos.move(move)
cankill = lambda p: any(p.board[b]=='k' for a,b in p.genMoves())
check = ''
if cankill(pos1.rotate()):
check = '+'
if all(cankill(pos1.move(move1)) for move1 in pos1.genMoves()):
check = '#'
# Castling
if pos.board[i] == 'K' and csrc == 'e1' and cdst in ('c1','g1'):
if cdst == 'c1':
return 'O-O-O' + check
return 'O-O' + check
# Pawn moves
if pos.board[i] == 'P':
pro = '=Q' if cdst[1] == '8' else ''
cap = csrc[0] + 'x' if pos.board[j] != '.' or j == pos.ep else ''
return cap + cdst + pro + check
# Normal moves
p = pos.board[i]
srcs = [a for a,b in pos.genMoves() if pos.board[a] == p and b == j]
# TODO: We can often get away with just sending the rank or file here.
src = csrc if len(srcs) > 1 else ''
cap = 'x' if pos.board[j] != '.' else ''
return p + src + cap + cdst + check
def parseSAN(pos, color, msan):
# Normal moves
normal = re.match('([KQRBN])([a-h])?([1-8])?x?([a-h][1-8])', msan)
if normal:
p, fil, rank, dst = normal.groups()
src = (fil or '[a-h]')+(rank or '[1-8]')
# Pawn moves
pawn = re.match('([a-h])?x?([a-h][1-8])', msan)
if pawn:
p, (fil, dst) = 'P', pawn.groups()
src = (fil or '[a-h]')+'[1-8]'
# Castling
if msan == "O-O-O":
p, src, dst = 'K', 'e1|d1', 'c1|f1'
if msan == "O-O":
p, src, dst = 'K', 'e1|d1', 'g1|b1'
# Find possible match
for i, j in pos.genMoves():
# TODO: Maybe check for check here?
csrc, cdst = sunfish.render(i), sunfish.render(j)
if pos.board[i] == p and re.match(dst, cdst) and re.match(src, csrc):
return (i, j)
def parseEPD(epd):
parts = epd.strip('\n ;').replace('"','').split(maxsplit=6)
fen = ' '.join(parts[:6])
opts = dict(p.split(maxsplit=1) for p in parts[6].split(';'))
return fen, opts
def findbest(path, times):
print('Calibrating search speed...')
pos = xboard.parseFEN('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
CAL_NODES = 10000
start = time.time()
_ = sunfish.search(pos, CAL_NODES)
factor = CAL_NODES/(time.time()-start)
print('Running benchmark with %.1f nodes per second...' % factor)
print('-'*60)
totalpoints = 0
totaltests = 0
with open(path) as f:
for k, line in enumerate(f):
fen, opts = parseEPD(line)
pos = xboard.parseFEN(fen)
color = 0 if fen.split()[1] == 'w' else 1
# am -> avoid move; bm -> best move
am = parseSAN(pos,color,opts['am']) if 'am' in opts else None
bm = parseSAN(pos,color,opts['bm']) if 'bm' in opts else None
points = 0
print(opts['id'], end=' ', flush=True)
for t in times:
move, _ = sunfish.search(pos, factor*t)
mark = renderSAN(pos, move)
if am and move != am or bm and move == bm:
mark += '(1)'
points += 1
totalpoints += 1
else:
mark += '(0)'
print(mark, end=' ', flush=True)
totaltests + 1
print(points)
print('-'*60)
print('Total Points: %d/%d', totalpoints, totaltests)
# Python 2 compatability
if sys.version_info[0] == 2:
input = raw_input
if __name__ == '__main__':
allperft('tests/queen.fen', depth=3)
quickmate('tests/mate1.fen', 3)
quickmate('tests/mate2.fen', 5)
quickmate('tests/mate3.fen', 7)
testxboard('python')
testxboard('python3')
testxboard('pypy')
# findbest('tests/ccr_one_hour_test.epd', [15, 30, 60, 120])
# findbest('tests/bratko_kopec_test.epd', [15, 30, 60, 120])
# quickdraw('tests/stalemate2.fen', 3)
# selfplay()