-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.py
executable file
·110 lines (89 loc) · 3.32 KB
/
generate.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
#!/usr/bin/python3
import math
import random
import subprocess
import sys
import chess
# Configure here
Chess960 = True
Engine = '../Stockfish/master'
Options = {'Hash': 64, 'Threads': 7}
Pruning = {'score': 50, 'random': 0.5}
TimeControl = {'depth': 14, 'nodes': None, 'movetime': 1000}
class UCIEngine():
def __init__(self, engine):
self.process = subprocess.Popen(engine, stdout=subprocess.PIPE,
stdin=subprocess.PIPE, universal_newlines=True, bufsize=1)
self.writeline('uci')
self.options = []
while True:
line = self.readline()
if line.startswith('option name '):
tokens = line.split()
name = tokens[2:tokens.index('type')]
self.options.append(' '.join(name))
elif line == 'uciok':
break
def readline(self):
return self.process.stdout.readline().rstrip()
def writeline(self, string):
self.process.stdin.write(string + '\n')
def setoptions(self, options):
for name in options:
value = options[name]
if type(value) is bool:
value = str(value).lower()
self.writeline('setoption name {} value {}'.format(name, value))
def isready(self):
self.writeline('isready')
while self.readline() != 'readyok':
pass
def newgame(self):
self.writeline('ucinewgame')
def go(self, args):
tokens = ['go']
for name in args:
if args[name] is not None:
tokens += [name, str(args[name])]
self.writeline(' '.join(tokens))
score = None
while True:
line = self.readline()
if line.startswith('info'):
i = line.find('score ')
if i != -1:
tokens = line[(i + len('score ')):].split()
assert len(tokens) >= 2
if tokens[0] == 'cp':
if len(tokens) == 2 or not tokens[2].endswith('bound'):
score = int(tokens[1])
elif tokens[0] == 'mate':
score = math.copysign(Pruning['score'], int(tokens[1]))
elif line.startswith('bestmove'):
return line.split()[1], score
def quit(self):
self.writeline('quit')
self.process.wait()
if __name__ == '__main__':
uciEngine = UCIEngine(Engine)
uciEngine.setoptions(Options)
uciEngine.setoptions({'UCI_Chess960': Chess960})
with open(sys.argv[1], 'r') as inFile, open(sys.argv[2], 'w') as outFile:
for line in inFile:
fen = line.rstrip().split(';')[0]
board = chess.Board(fen, Chess960)
uciEngine.newgame()
print(fen)
for move in board.legal_moves:
if random.random() < Pruning['random']:
continue
print(' ->', move)
board.push(move)
childFen = board.shredder_fen() if Chess960 else board.fen()
board.pop()
uciEngine.writeline('position fen ' + childFen)
uciEngine.isready()
bestmove, score = uciEngine.go(TimeControl)
if abs(score) < Pruning['score']:
print(childFen, file=outFile)
uciEngine.quit()