-
Notifications
You must be signed in to change notification settings - Fork 0
/
mip_query.py
105 lines (96 loc) · 4.82 KB
/
mip_query.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
import sys
import os
import gzip
DIR='mip_matrices'
def getInstances():
return sorted(fileName[:-len('.original.header')] for fileName in os.listdir(DIR) if fileName[-len('.original.header'):] == '.original.header')
def getOriginalData(instance):
firstLine = open(f'{DIR}/{instance}.original.header', 'r').read().split('\n', 1)[0]
data = list(map(int, firstLine.split()))
return { 'original rows': data[0], 'original columns': data[1], 'original nonzeros': data[2] }
def getData(instance, kary):
firstLine = gzip.open(f'{DIR}/{instance}.{kary}.sparse.gz', 'r').read().decode('utf-8').split('\n', 1)[0]
data = list(map(int, firstLine.split()))
isTrivial = (data[0] * data[1] * data[2] == 0)
isCamion = None
seriesParallel = -1
reducedRows = None
reducedColumns = None
reducedNonzeros = None
isTU = None
timeSP = None
timeNoSP = None
if not isTrivial:
try:
for line in open(f'{DIR}/{instance}.{kary}.camion', 'r').read().split('\n'):
if line == 'Matrix IS Camion-signed.':
isCamion = True
elif line == 'Matrix IS NOT Camion-signed.':
isCamion = False
except:
sys.stderr.write(f'WARNING: File {DIR}/{instance}.{kary}.camion is not present.\n')
try:
firstLine = gzip.open(f'{DIR}/{instance}.{kary}.reduced.sparse.gz', 'r').read().decode('utf-8').split('\n', 1)[0]
spData = list(map(int, firstLine.split()))
if spData[0] == data[0] and spData[1] == data[1]:
seriesParallel = 0
reducedRows = data[0]
reducedColumns = data[1]
reducedNonzeros = data[2]
elif spData[0] == 0 and spData[1] == 0:
seriesParallel = 2
reducedRows = 0
reducedColumns = 0
reducedNonzeros = 0
else:
seriesParallel = 1
reducedRows = spData[0]
reducedColumns = spData[1]
reducedNonzeros = spData[2]
except:
sys.stderr.write(f'WARNING: File {DIR}/{instance}.{kary}.reduced.sparse.gz is not present.\n')
if isCamion:
try:
for line in open(f'{DIR}/{instance}.{kary}.sp.tu', 'r').read().split('\n'):
if line == 'Matrix IS regular.':
isTU = True
elif line == 'Matrix IS NOT regular.':
isTU = False
elif line[:8] == ' total:':
timeSP = float(line.split()[3])
except:
sys.stderr.write(f'WARNING: File {DIR}/{instance}.{kary}.sp.tu is not present.\n')
try:
for line in open(f'{DIR}/{instance}.{kary}.no-sp.tu', 'r').read().split('\n'):
if line[:8] == ' total:':
timeNoSP = float(line.split()[3])
except:
sys.stderr.write(f'WARNING: File {DIR}/{instance}.{kary}.no-sp.tu is not present.\n')
return { 'rows': data[0], 'columns': data[1], 'nonzeros': data[2], 'trivial': isTrivial, 'camion': isCamion, 'series-parallel': seriesParallel, 'tu': isTU, 'timeSP': timeSP, 'timeNoSP': timeNoSP, 'SP-reduced rows': reducedRows, 'SP-reduced columns': reducedColumns, 'SP-reduced nonzeros': reducedNonzeros }
def printTex(instance, kary, data):
instance = instance.replace('_', '\\_')
if data['SP-reduced nonzeros'] == 0:
return f"\\texttt{{{instance}}} & \\num{{{data['rows']}}} & \\num{{{data['columns']}}} & \\num{{{data['nonzeros']}}} & \\SI{{{data['timeNoSP']}}}{{\\s}} & \\SI{{{data['timeSP']}}}{{\\s}} \\\\"
else:
return f"\\texttt{{{instance}}} & \\num{{{data['rows']}}} & \\num{{{data['columns']}}} & \\num{{{data['nonzeros']}}} & \\num{{{data['SP-reduced rows']}}} & \\num{{{data['SP-reduced columns']}}} & \\num{{{data['SP-reduced nonzeros']}}} & \\SI{{{data['timeNoSP']}}}{{\\s}} & \\SI{{{data['timeSP']}}}{{\\s}} \\\\"
def dump(instance, tex):
data = getOriginalData(instance)
data['binary'] = getData(instance, 'binary')
data['ternary'] = getData(instance, 'ternary')
print(f"{instance} is {data['original rows']}x{data['original columns']} with {data['original nonzeros']} nonzeros")
s = ' #' + printTex(instance, 'binary', data['binary']) if tex else ''
print(f'{instance} binary: ' + str(data['binary']) + s)
s = ' #' + printTex(instance, 'ternary', data['ternary']) if tex else ''
print(f'{instance} ternary: ' + str(data['ternary']) + s)
if __name__ == '__main__':
args = sys.argv[1:]
tex = False
if args and args[0] == '--tex':
tex = True
args = args[1:]
if args:
for instance in args:
dump(instance, tex)
else:
for instance in getInstances():
dump(instance, tex)