-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.py
executable file
·71 lines (52 loc) · 1.65 KB
/
bench.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
#!/usr/bin/python3
import subprocess
import sys
import re
import time
from string import Template
from concurrent.futures import ProcessPoolExecutor
def get_output(cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
out, err = p.communicate()
if p.returncode != 0:
print(out)
print(err)
sys.exit(p.returncode)
return out.rstrip() # remove '\n' in the end
def wins(s):
return [int(w) for w in re.match(r'.*\((.*)\).*', s).group(1).split(',')]
def score(w):
return w[0] + 2*w[1] + 4*w[2]
def ratio(w):
return [('%s%%' % int(round(f*100))) for f in [w[0]/10000.0, w[1]/5000.0, w[2]/2500.0]]
def config_file(s, g):
return f'{"-".join(s)}-{g}.toml'
def bench(args):
s, g = args
with open('template.toml') as f:
t = Template(f.read())
c = config_file(s, g)
with open(c, 'w') as f:
f.write(t.substitute(strategies=s, guess=g))
t = time.time()
out = get_output(f'./winminer -c {c} | grep win:')
ct = time.time() - t
ws = wins(out.decode('utf-8'))
si = score(ws)
print('strategies = %s, guess = %s' % (s, g))
print('score=%s %s %s, cost %.2f seconds' % (si, ws, ratio(ws), ct))
if len(args) < 1:
return
if si < int(args[0]):
sys.exit(1)
def bench_combinations():
strategies = ["diff", "reduce", "isle"]
gs = ["first", "random", "corner", "min"]
args = []
for i in range(len(strategies)):
for j in range(len(gs)):
args.append((strategies[:i+1], gs[j]))
with ProcessPoolExecutor() as executor:
executor.map(bench, args)
if __name__ == '__main__':
bench_combinations()