-
Notifications
You must be signed in to change notification settings - Fork 3
/
bayesopt.py
266 lines (233 loc) · 7.22 KB
/
bayesopt.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
import timeit
import subprocess
import random
import numpy as np
import scipy as sp
import math
import re
import chess
from bayes_opt import BayesianOptimization
from operator import itemgetter
from chess import uci
from chess import Board
from chess import Move
from chess import syzygy
from numpy import sqrt
from scipy.stats import chi2
from scipy.stats import norm
from statistics import median
Engines = [
{'file': 'C:\\msys2\\home\\lanto\\safechecks\\tune.exe', 'name': 'test'},
{'file': 'C:\\msys2\\home\\lanto\\safechecks\\tune.exe', 'name': 'base'}
]
Draw = {'movenumber': 40, 'movecount': 8, 'score': 20}
Resign = {'movecount': 3, 'score': 400}
population_size=40
iterations=200
dynamic_rate=5
Openings = 'C:\\Cutechess\\2moves.epd'
Games = 50
UseEngine = False
Syzygy = 'C:\\Winboard\\Syzygy'
ParametersFile = 'C:\\Rockstar\\safechecks.txt'
LogFile = 'tuning.txt'
DynamicConstraints = True
Options = {'Clear Hash': True, 'Hash': 16, 'SyzygyPath': Syzygy, \
'SyzygyProbeDepth': 10, 'Syzygy50MoveRule': True, 'SyzygyProbeLimit': 5}
## Preparatory phase
# takes parameters from the engine
def getPars():
sf = subprocess.Popen(Engines[0]['file'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, bufsize=1)
sf.stdin.write('isready' + '\n')
pars = []
outline = []
while outline is not '':
outline = sf.stdout.readline().rstrip()
if not (outline.startswith('Stockfish ') or outline.startswith('Unknown ') or outline == ''):
pars.append(outline.split(','))
sf.terminate()
sf.wait()
return pars
# takes parameters from file that is copied from engine output
def get_pars():
params = []
f = open(ParametersFile)
lines = f.read().split('\n')
if lines[-1] == '':
lines.remove('')
for p in lines:
params.append(p.split(','))
return sorted(params)
if UseEngine:
Pars = getPars()
else:
Pars = get_pars()
# openings
def get_fens():
fens = []
lines = open(Openings).read().splitlines()
for i in range(0, Games, 1):
fen =random.choice(lines)
fens.append(fen)
# print(fens)
return fens
def shuffled(x):
y = x[:]
random.shuffle(y)
return y
def init_engines(pars):
info_handlers = []
uciEngines = []
for e in Engines:
uciEngines.append(uci.popen_engine(e['file']))
for e,u in enumerate(uciEngines):
u.uci()
u.setoption(Options)
u.setoption(pars[e])
u.isready()
return uciEngines
class DifferentialEvolution():
def __init__(self):
self.nameArray = [str(par[0]) for par in Pars]
self.parsArray = [int(par[1]) for par in Pars]
self.bounds = [(int(p[2]), int(p[3])) for p in Pars]
self.n_parameters = len(self.nameArray)
self.current = [int(par[1]) for par in Pars]
self.trial = [int(par[1]) for par in Pars]
self.variables = dict(zip(self.nameArray, self.current))
self.pbounds = dict(zip(self.nameArray, self.bounds))
### Evaluation
def evaluate(self, variables):
num = 0
fens = get_fens()
# current = dict(zip(self.nameArray, variables))
trial = dict(zip(self.nameArray, self.trial))
result = []
with syzygy.open_tablebases(Syzygy) as tablebases:
for fen in fens:
result1 = self.trans_result(self.launchSf([variables, trial], fen, tablebases,))
result2 = self.trans_result(self.launchSf([trial, variables], fen, tablebases,))
result.append(result1 + result2)
pentares = self.pentanomial(result)
curr = float(self.calc_los(pentares))
return curr
def trans_result(self, score):
return {'1-0': 2, '1/2-1/2': 1, '0-1': 0}[score]
def pentanomial(self, result):
pentares = []
for i in range(0,5):
pentares.append(result.count(i))
return pentares
def calc_los(self, pentares):
sumi, sumi2 = 0, 0
for i in range(0,5):
res = 0.5 * i
N = sum(pentares)
sumi += pentares[i] * res / N
sumi2 += pentares[i] * res * res / N
sigma = math.sqrt(sumi2 - sumi * sumi)
try:
t = math.sqrt(N) * (sumi - 1) / sigma * 100
except ZeroDivisionError:
t = 0.0
# los = norm.cdf(t) * 100
# return '{0:.2f}'.format(round(t, 2))
return t
### Game playing
def launchSf(self, pars, fen, tablebases,):
try:
board = Board(fen,chess960=False)
except BaseException:
try:
board.set_epd(fen)
except BaseException:
board = Board(chess960=False)
wdl = None
drawPlyCnt, resignPlyCnt = 0, 0
whiteIdx = 1
turnIdx = whiteIdx ^ (board.turn == chess.BLACK)
uciEngines = init_engines(pars)
info_handler = uci.InfoHandler()
for u in uciEngines:
u.info_handlers.append(info_handler)
u.ucinewgame()
while (not board.is_game_over(claim_draw=True)):
if board.castling_rights == 0:
# if len(re.findall(r"[rnbqkpRNBQKP]", board.board_fen())) < 6:
# wdl = tablebases.probe_wdl(board)
# if wdl is not None:
# break # ~ 1.5 ms
try:
wdl = tablebases.probe_wdl(board)
if wdl is not None:
break
except KeyError:
pass # < 1 ms
uciEngines[turnIdx].position(board)
bestmove, score = uciEngines[turnIdx].go(depth=9)
score = info_handler.info["score"][1].cp
# print(score)
if score is not None:
# Resign adjudication
if abs(score) >= Resign['score']:
resignPlyCnt += 1
if resignPlyCnt >= 2 * Resign['movecount']:
break
else:
resignPlyCnt = 0
# Draw adjudication
if abs(score) <= Draw['score'] and board.halfmove_clock > 0:
drawPlyCnt += 1
if drawPlyCnt >= 2 * Draw['movecount'] \
and board.fullmove_number >= Draw['movenumber']:
break
else:
drawPlyCnt = 0
else:
# Disable adjudication over mate scores
drawPlyCnt, resignPlyCnt = 0, 0
board.push(bestmove)
turnIdx ^= 1
result = board.result(True)
if result == '*':
if resignPlyCnt >= 2 * Resign['movecount']:
if score > 0:
result = '1-0' if board.turn == chess.WHITE else '0-1'
else:
result = '0-1' if board.turn == chess.WHITE else '1-0'
elif wdl is not None:
if wdl <= -1:
result = '1-0' if board.turn == chess.WHITE else '0-1'
elif wdl >= 1:
result = '0-1' if board.turn == chess.WHITE else '1-0'
else:
result = '1/2-1/2'
# print('tb draw')
else:
result = '1/2-1/2'
# print('draw')
# print(board.fen())
# print(re.findall(r"[rnbqkpRNBQKP]", board.board_fen()))
for u in uciEngines:
u.quit(0)
# print(result)
return result
exit(0)
if __name__ == '__main__':
de = DifferentialEvolution()
variables = dict(zip(de.nameArray, de.current))
def black_box_function(**variables):
f = de.evaluate(variables)
return f
optimizer = BayesianOptimization(
f=black_box_function,
pbounds=de.pbounds,
verbose=2, # verbose = 1 prints only when a maximum is observed, verbose = 0 is silent
random_state=0,
)
optimizer.maximize(
init_points=2,
n_iter=30,
acq='poi',
)
print(optimizer.max)