-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver_logparser.py
More file actions
463 lines (348 loc) · 15.2 KB
/
solver_logparser.py
File metadata and controls
463 lines (348 loc) · 15.2 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import math
import os
import re
import sys
import numpy as np
import pandas as pd
STATUS = ('COMPLETE', 'SOLFOUND', 'UNSATISFIABLE', 'UNKNOWN', 'FAILED', 'INTERMEDIATE')
re_worker_memory = re.compile('^\s+JobID\s+MaxVMSize\s+MaxRSS\s*$\n' +
'^.*$\n^[\w\d.+]+\s+(?P<maxvmsize>[\d.\w]+)\s+(?P<maxrss>[\d.\w]+)\s*$',
re.MULTILINE)
re_log = re.compile(
'(?P<jobid>\d+)-(?P<solver>\w+)-(?P<mzn>[\w.\-_\d]+\.mzn)-(?P<dzn>[\w.\-_\d]+\.dzn)-(?P<taskid>\d+)(-(?P<boundsmode>\w+))?.log')
re_choco_sol = re.compile(
'^% Model\[.*\], (?P<solutions>\d+) Solutions, ' +
'(MINIMIZE|MAXIMIZE) [\w_]+ = (?P<objective>\d+), ' +
'Resolution time (?P<restime>[\d.]+)s, (?P<nodes>\d+) Nodes \([.,\d]+ n/s\), ' +
'(?P<backtracks>\d+) Backtracks, (?P<fails>\d+) Fails, (?P<restarts>\d+) Restarts')
re_choco_final = re.compile('^% Model\[.*\], (?P<vars>\d+) variables, (?P<constr>\d+) constraints, ' +
'building time: (?P<buildtime>[\d.]+)s, w/o user-defined search strategy, ' +
'w/o complementary search strategy')
re_chuffed_stats = re.compile('^-?\d+,\d+,\d+,\d+,\d+,\d+,\d+,\d+,[\d.]+,[\d.]+')
re_gecode_ortools_stats = re.compile('^%%\s+(?P<key>[\w\s]+):\s+(?P<value>[\w\d. \(\)]+)')
re_gecode_obj = re.compile('^(?:objective|t_end) = (?P<objective>\d+);$')
re_solution_time = re.compile('^% time elapsed: (?P<time>\d+) ms')
re_objective = re.compile('^(?:objective|makespan|t_end)\s?=\s?(?P<objective>\d+);')
def process_logfile(f, logdir, instances):
m = re_log.match(f)
if not m:
print('Filename {} does not match regex'.format(f))
solver_log_file = os.path.join(logdir, f)
worker_log_file = os.path.join(logdir, '{}-worker.out'.format(m.group('jobid')))
if not os.path.exists(worker_log_file):
print('Worker log {} does not exist'.format(worker_log_file))
worker_log = open(worker_log_file).read()
if 'completed' not in worker_log:
raise Exception('{}: Job seems to have failed...'.format(worker_log_file))
worker_match = re_worker_memory.search(worker_log)
if not worker_match:
raise Exception('{}: Could not find memory statistics'.format(worker_log_file))
output = open(solver_log_file).read()
if 'aborted' in output or 'failure stack trace' in output:
raise Exception('{}: Solver seems to have failed...'.format(solver_log_file))
solver_lines = output.splitlines()
solver = m.group('solver')
taskid = int(m.group('taskid'))
assert (solver == instances.ix[taskid - 1]['solver'])
lbarg = instances.ix[taskid - 1]['lbarg']
ubarg = instances.ix[taskid - 1]['ubarg']
if isinstance(lbarg, str) and lbarg.startswith('-lb '):
lb = int(lbarg.replace('-lb ', ''))
else:
lb = None
if isinstance(ubarg, str) and ubarg.startswith('-ub '):
ub = int(ubarg.replace('-ub ', ''))
else:
ub = None
boundstype = 'hard'
if 'boundsmode' in m.groupdict():
if m.group('boundsmode') == 'NoBounds':
bounds = 'No'
ub = lb = None
ubarg = lbarg = None
elif m.group('boundsmode') == 'UpperBound':
bounds = 'Upper'
lb = None
lbarg = None
elif m.group('boundsmode') == 'LowerBound':
bounds = 'Lower'
ub = None
ubarg = None
elif m.group('boundsmode') == 'BothBounds':
bounds = 'Both'
assert lb is not None and ub is not None, "Not all bounds set, but marked as BothBounds"
elif m.group('boundsmode') == 'HardBounds':
boundstype = 'hard'
elif m.group('boundsmode') == 'SoftBounds':
boundstype = 'soft'
if lb is None and ub is None:
bounds = 'No'
elif lb is None:
bounds = 'Upper'
elif ub is None:
bounds = 'Lower'
else:
bounds = 'Both'
timeout = instances.ix[taskid - 1]['timeout']
stats = {
'solver': solver,
'mzn': m.group('mzn'),
'dzn': m.group('dzn'),
'lowerbound': -1 if lb is None else lb,
'upperbound': -1 if ub is None else ub,
'lowerboundvalue': -1 if lb is None else lb,
'upperboundvalue': -1 if ub is None else ub,
'timeout': timeout,
'taskid': taskid,
'maxvmsize': convert_memory(worker_match.group('maxvmsize')),
'maxrss': convert_memory(worker_match.group('maxrss')),
'bounds': bounds,
'boundstype': boundstype
}
for l in solver_lines:
if l.startswith('Time_mzn2fzn: '):
stats['time_mzn2fzn'] = math.ceil(float(l.replace('Time_mzn2fzn: ', '')))
elif l.startswith('Time_solver: '):
stats['time_solver'] = math.ceil(float(l.replace('Time_solver: ', '')))
if '###DONE###' not in solver_lines[-4:]:
print(f, 'Solver output not complete, but necessary info might be included')
assert 'time_mzn2fzn' in stats, 'time_mzn2fzn missing' # Preparation time
assert 'time_solver' in stats, 'time_solver missing' # Runtime
stats['time_solver'] = min(stats['time_solver'], stats['timeout'])
solutions = parse_solutions(solver_lines, stats)
try:
if solver == 'choco':
stats = parse_choco(stats, solver_lines)
elif solver == 'chuffed':
stats = parse_chuffed(stats, solver_lines)
elif solver == 'gecode':
stats = parse_gecode(stats, solver_lines)
elif solver == 'ortools':
stats = parse_ortools(stats, solver_lines)
elif solver == 'sunnycp':
stats = parse_sunnycp(stats, solver_lines)
else:
raise NotImplementedError('{}: Solver {} not supported'.format(f, solver))
assert (all('solutions' in s for s in stats))
except Exception as e:
print(solver_log_file, e)
raise
return stats, solutions
def parse_solutions(lines, static_fields):
"""
Iterate through log and parse time when solutions are found and their objective value.
Solver-independent as the time is issued by `solns2out` and the objective is part of the flatzinc output.
"""
solution_times = []
solution_objectives = []
has_optimum = False
for l in lines:
m_time = re_solution_time.match(l)
m_obj = re_objective.match(l)
if m_time:
time_found = int(m_time.group('time')) / 1000 # Convert ms to s
solution_times.append(time_found)
elif m_obj:
obj = int(m_obj.group('objective'))
solution_objectives.append(obj)
elif l == '========':
# Optimal solution found
has_optimum = True
if len(solution_times) != len(solution_objectives):
raise Exception("Different number of times and objectives")
optimal_sol = [False] * len(solution_times)
if len(optimal_sol) > 0:
optimal_sol[-1] = has_optimum
return pd.DataFrame({'time': solution_times, 'objective': solution_objectives, 'optimum': optimal_sol, **static_fields})
def parse_choco(stats, lines):
solutions = []
for l in lines:
m1 = re_choco_sol.match(l)
if m1:
solutions.append(m1.groupdict())
else:
m2 = re_choco_final.match(l)
if m2: # This is the final statistics line and should only appear once
stats.update(m2.groupdict())
m_first_sol = re_solution_time.match(l)
if m_first_sol and 'first_sol_time' not in stats:
stats['first_sol_time'] = int(m_first_sol.group('time')) / 1000
m_first_obj = re_objective.match(l)
if m_first_obj and 'first_objective' not in stats:
stats['first_objective'] = m_first_obj.group('objective')
intermediates = []
for s in solutions[:-1]:
intermediate_stats = dict(stats)
intermediate_stats.update(s)
intermediate_stats['status'] = 'INTERMEDIATE'
intermediates.append(intermediate_stats)
if len(solutions) > 0:
stats.update(solutions[-1])
stats['solutions'] = int(stats['solutions'])
else:
stats['solutions'] = 0
assert ((len(solutions) == 0 and stats['solutions'] == 0) or len(solutions) == stats['solutions'] + 1)
assert (len(intermediates) == stats['solutions'])
if '=====UNKNOWN=====' in lines:
stats['status'] = 'UNKNOWN'
elif '=====UNSATISFIABLE=====' in lines:
stats['status'] = 'UNSATISFIABLE'
elif '==========' in lines:
stats['status'] = 'COMPLETE'
elif stats['solutions'] > 0:
stats['status'] = 'SOLFOUND'
else:
stats['status'] = 'UNKNOWN'
return [stats] + intermediates
def parse_chuffed(stats, lines):
fields = ['objective', 'variables', 'satvariables', 'propagators', 'conflicts', 'satbackjumps', 'propagations',
'solutions', 'inittime', 'search_time']
values = []
for l in lines:
if re_chuffed_stats.fullmatch(l):
values = l.split(',')
m_first_sol = re_solution_time.match(l)
if m_first_sol and 'first_sol_time' not in stats:
stats['first_sol_time'] = int(m_first_sol.group('time')) / 1000
m_first_obj = re_objective.match(l)
if m_first_obj and 'first_objective' not in stats:
stats['first_objective'] = m_first_obj.group('objective')
if len(values) == 0:
stats['status'] = 'FAILED'
return [stats]
stats.update({f: v for f, v in zip(fields, values)})
if stats['objective'] == '-1':
del stats['objective']
if '=====UNKNOWN=====' in lines:
stats['status'] = 'UNKNOWN'
elif '=====UNSATISFIABLE=====' in lines:
stats['status'] = 'UNSATISFIABLE'
elif '==========' in lines:
stats['status'] = 'COMPLETE'
elif int(stats['solutions']) > 0:
stats['status'] = 'SOLFOUND'
else:
stats['status'] = 'UNKNOWN'
return [stats]
def parse_gecode(stats, lines):
stats['solutions'] = lines.count('----------')
for l in lines:
m = re_gecode_ortools_stats.fullmatch(l)
if m:
dictkey = m.group('key').strip().replace(' ', '_')
stats[dictkey] = m.group('value').strip().replace(' ms', '')
continue
m = re_gecode_obj.fullmatch(l)
if m:
stats['objective'] = int(m.group('objective'))
m_first_sol = re_solution_time.match(l)
if m_first_sol and 'first_sol_time' not in stats:
stats['first_sol_time'] = int(m_first_sol.group('time')) / 1000
m_first_obj = re_objective.match(l)
if m_first_obj and 'first_objective' not in stats:
stats['first_objective'] = m_first_obj.group('objective')
if '=====UNKNOWN=====' in lines:
stats['status'] = 'UNKNOWN'
elif '=====UNSATISFIABLE=====' in lines:
stats['status'] = 'UNSATISFIABLE'
elif '==========' in lines:
stats['status'] = 'COMPLETE'
elif int(stats['solutions']) > 0:
stats['status'] = 'SOLFOUND'
else:
stats['status'] = 'UNKNOWN'
return [stats]
def parse_ortools(stats, lines):
stats['solutions'] = lines.count('----------')
for l in lines:
m = re_gecode_ortools_stats.fullmatch(l)
if m:
if m.group('key').strip() == 'max objective' or m.group('key').strip() == 'min objective':
dictkey = 'objective'
stats[dictkey] = int(m.group('value').strip().replace(' (proven)', ''))
else:
dictkey = m.group('key').strip().replace(' ', '_')
stats[dictkey] = m.group('value').strip().replace(' ms', '')
m_first_sol = re_solution_time.match(l)
if m_first_sol and 'first_sol_time' not in stats:
stats['first_sol_time'] = int(m_first_sol.group('time')) / 1000
m_first_obj = re_objective.match(l)
if m_first_obj and 'first_objective' not in stats:
stats['first_objective'] = m_first_obj.group('objective')
if '=====UNKNOWN=====' in lines:
stats['status'] = 'UNKNOWN'
elif '=====UNSATISFIABLE=====' in lines:
stats['status'] = 'UNSATISFIABLE'
elif '==========' in lines:
stats['status'] = 'COMPLETE'
elif int(stats['solutions']) > 0:
stats['status'] = 'SOLFOUND'
else:
stats['status'] = 'UNKNOWN'
return [stats]
def parse_sunnycp(stats, lines):
stats['solutions'] = lines.count('----------')
for l in lines:
if l.startswith('% Current Best Bound: '):
stats['objective'] = int(l.replace('% Current Best Bound: ', ''))
m_first_sol = re_solution_time.match(l)
if m_first_sol and 'first_sol_time' not in stats:
stats['first_sol_time'] = int(m_first_sol.group('time')) / 1000
m_first_obj = re_objective.match(l)
if m_first_obj and 'first_objective' not in stats:
stats['first_objective'] = m_first_obj.group('objective')
if '=====UNKNOWN=====' in lines:
stats['status'] = 'UNKNOWN'
elif '=====UNSATISFIABLE=====' in lines:
stats['status'] = 'UNSATISFIABLE'
elif '==========' in lines:
stats['status'] = 'COMPLETE'
elif int(stats['solutions']) > 0:
stats['status'] = 'SOLFOUND'
else:
stats['status'] = 'UNKNOWN'
return [stats]
def convert_memory(memstring):
if memstring.endswith('K'):
return float(memstring[:-1])
elif memstring.endswith('M'):
return float(memstring[:-1]) * 1000
elif memstring.endswith('G'):
return float(memstring[:-1]) * 1000 * 1000
else:
return float(memstring) / 1000
if __name__ == '__main__':
if len(sys.argv) < 2:
print('Need logdir as parameter')
logdir = sys.argv[1]
logname = logdir + '.csv'
solution_file = logdir + '_solutions.csv'
filelist = sorted(os.listdir(logdir))
instances = pd.read_csv(os.path.join(logdir, 'instances'), sep=';',
names=['solver', 'mzn_path', 'dzn_path', 'lbarg', 'ubarg', 'timeout', 'addargs', 'comment'])
dzn_filter = [os.path.basename(dzn) for dzn in instances.dzn_path.unique()]
records = []
all_solutions = []
failed = []
for f in filelist:
if not f.endswith('.log'):
# Not a log file, ignore
continue
try:
stats, solutions = process_logfile(f, logdir, instances)
records.extend(stats)
all_solutions.append(solutions)
except Exception as e:
print(f, e)
failed.append(f)
continue
solution_df = pd.concat(all_solutions)
solution_df.to_csv(solution_file, index=False)
df = pd.DataFrame.from_records(records)
df.to_csv(logname, index=False)
missing = [str(x) for x in range(1, len(instances) + 1) if x not in df.taskid.unique()]
print('{}\t Results: {}, Intermediate: {}, Failed: {}, Missing: {}'.format(logname,
(df['status'] != 'INTERMEDIATE').sum(),
(df['status'] == 'INTERMEDIATE').sum(),
len(failed),
",".join(missing)))