-
Notifications
You must be signed in to change notification settings - Fork 3
/
bayesopt_cli.py
131 lines (114 loc) · 3.44 KB
/
bayesopt_cli.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
import random
import math
import numpy as np
from scipy.stats import norm
from bayes_opt import BayesianOptimization
from cutechess_batch import main
ParametersFile = 'nullmove.txt'
# 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)
Pars = get_pars()
class DifferentialEvolution():
def __init__(self):
self.nameArray = [str(par[0]) for par in Pars]
self.bounds = {str(p[0]):(p[2], p[3]) for p in Pars}
self.current = [par[1] for par in Pars]
self.trial = [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):
for name in variables:
variables[name] = round(variables[name],0)
trial = dict(zip(self.nameArray, self.trial))
result = []
result = main(variables, trial,)
pentares_result = self.translate_result(result)
# print(pentares_result)
# alpha_hat = self.calc_ridits(pentares_result)
los = self.calc_los(pentares_result)
return los * 100
def translate_result(self, result):
pentares = []
for i in range(0,len(result) - 1,2):
current, next = result[i], result[i + 1]
pentares.append(str(current)+str(next))
category = [0,0,0,0,0]
for score in pentares:
if score == 'll':
category[0] = category[0]+1
if score == 'ld' or score == 'dl':
category[1] = category[1]+1
if score == 'dd' or score == 'wl' or score == 'lw':
category[2] = category[2]+1
if score == 'wd' or score == 'dw':
category[3] = category[3]+1
if score == 'ww':
category[4] = category[4]+1
return category
def pentanomial(self, result):
pentares = []
for i in range(0,5):
pentares.append(result.count(i))
print(pentares)
return pentares
def calc_ridits(self, pentares):
sump=sum(pentares)
pentares1=np.array(pentares)/sump
pentares2=np.array(pentares[::-1])/sump
marginal=(pentares1+pentares2)/2
ridits =[]
ridits.append(0.5*marginal[0])
for i in range(1,5):
ridits.append(sum(marginal[:i])+0.5*marginal[i])
ridits1=ridits*pentares1
ridits2=ridits*pentares2
A1bar=sum(ridits1)
A2bar=sum(ridits2)
alpha_hat=A1bar-A2bar+0.5
return alpha_hat
def calc_los(self, pentares):
sumi, sumi2 = 0, 0
for i,score in enumerate([0,0.5,1,1.5,2]):
N = sum(pentares)
sumi += pentares[i] * score / N
sumi2 += pentares[i] * score * score / N
sigma = math.sqrt(sumi2 - sumi * sumi)
try:
t0 = (sumi - 1) / sigma
except ZeroDivisionError:
t0 = (sumi - 1)
los = norm.cdf(t0)
return los
if __name__ == '__main__':
de = DifferentialEvolution()
print(de.bounds)
variables = de.variables
def black_box_function(**variables):
for name in variables:
variables[name] = int(variables[name])
f = de.evaluate(variables)
return f
optimizer = BayesianOptimization(
f=black_box_function,
pbounds=de.bounds,
verbose=2, # verbose = 1 prints only when a maximum is observed, verbose = 0 is silent
# random_state=0,
)
optimizer.maximize(
n_iter = 100,
acq='ei',
alpha = 0.05,
# n_restarts_optimizer=4,
# kappa=2,
xi = 1e-4,
)
print(optimizer.max)