-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.py
165 lines (141 loc) · 5.48 KB
/
eval.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
import csv, os, re, subprocess, sys, time
from collections import defaultdict
from os import path
timeout = default_timeout = 5
timeout_suffix = ''
eval_all = False
only_theorems = False
i = 1
while i < len(sys.argv):
arg = sys.argv[i]
if arg == '-a':
eval_all = True
i += 1
elif arg == '-h':
only_theorems = True
i += 1
elif arg.startswith('-t'):
timeout = int(arg[2:])
timeout_suffix = f'_{timeout}'
i += 1
else:
break
if i != len(sys.argv) - 1:
print(f'usage: {sys.argv[0]} [-a] [-t<num>] <dir>')
print( ' -a: evaluate all provers')
print(f' -t<num>: timeout (default is {default_timeout} seconds)')
sys.exit(1)
dir = sys.argv[i]
all_provers = [
('Natty', f'./natty -t{timeout}'),
('E', f'eprover-ho --auto -s --cpu-limit={timeout}'),
('Vampire', f'vampire -t {timeout}'),
('Zipperposition', f'zipperposition --mode best --input tptp --timeout {timeout}'),
]
provers = all_provers if eval_all else [all_provers[0]]
prover_names = [p[0] for p in provers]
files = [name.removesuffix('.thf') for name in os.listdir(dir) if name.endswith('.thf')]
files.sort(key = lambda s: [int(n) for n in s.replace('s', '').split('_')])
class Group:
def __init__(self, name):
self.results = {}
self.results_file = f'{dir}_{name}{timeout_suffix}.csv'
def read(self):
if path.exists(self.results_file):
with open(self.results_file) as f:
reader = csv.DictReader(f)
for row in reader:
name, conjecture = row[''], row['conjecture']
row_ids = [k for k, v in self.results.items()
if (v[''], v['conjecture']) == (name, conjecture)]
if row_ids != []:
self.results[row_ids[0]] = row
def write(self):
proved : defaultdict = defaultdict(int)
total_time = defaultdict(float)
total_score = defaultdict(float)
for result in self.results.values():
for prover in prover_names:
r = result.get(prover)
if r != None and r != '':
try:
time = float(r)
proved[prover] += 1
total_time[prover] += time
total_score[prover] += time
except ValueError:
total_score[prover] += 2 * timeout
with open(self.results_file, 'w') as out:
fieldnames = ['', 'conjecture'] + prover_names
writer = csv.DictWriter(out, fieldnames = fieldnames, extrasaction = 'ignore')
writer.writeheader()
writer.writerows(self.results.values())
out.write('\n')
n = len(self.results)
proved[''] = f'proved (of {n})'
writer.writerow(proved)
avg_time = { prover : f'{t / proved[prover]:.2f}'
for prover, t in total_time.items() }
avg_time[''] = 'average time'
writer.writerow(avg_time)
score = { prover : f'{t / n:.2f}' for prover, t in total_score.items() }
score[''] = 'PAR-2 score'
writer.writerow(score)
plural = '' if timeout == 1 else 's'
out.write(f'\nlimit = {timeout} second{plural}\n')
thm_group = Group('theorems')
step_group = Group('steps')
groups = [thm_group] if only_theorems else [thm_group, step_group]
results = {}
for file in files:
with open(path.join(dir, file + '.thf')) as f:
conjecture = f.readline().strip().removeprefix('% Problem: '.strip())
group = step_group if '_s' in file else thm_group
name = file.replace('_s','_').replace('_', '.')
group.results[file] = { '' : f'thm {name}', 'conjecture' : conjecture }
for g in groups:
g.read()
for prover, command in provers:
for group in groups:
changed = False
for file, result in group.results.items():
r = result.get(prover)
if r != None and r != '':
continue
changed = True
filename = path.join(dir, file + '.thf')
cmd = command + " " + filename
print(cmd)
start = time.time()
completed = subprocess.run(cmd, shell = True, capture_output = True)
elapsed = time.time() - start
text = completed.stdout.decode('utf-8') + completed.stderr.decode('utf-8')
for line in text.splitlines():
if m := re.search(r'SZS status (\w+)', line):
status = m[1]
break
if line == 'Aborted':
status = 'Error'
break
else:
if prover.startswith('Vampire') or prover.startswith('Zipperposition'):
status = 'Timeout'
else:
status = 'Error'
print(status)
match status:
case 'Theorem':
res = f'{elapsed:.2f}'
case 'GaveUp':
res = 'timeout' if prover.startswith('E ') else 'gave up'
case 'ResourceOut' | 'Timeout':
res = 'timeout'
case 'Error':
res = 'error'
case _:
print(f'unknown status: {text}')
assert False
result[prover] = res
# output continuously
if changed:
group.write()