-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck
executable file
·68 lines (51 loc) · 1.6 KB
/
check
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
#!/usr/bin/env -S python3 -B
import csv
import sys
import logging
import networkx as nx
import utils
def main():
logging.getLogger().setLevel(logging.ERROR)
for f in sys.argv[1:]:
with open(f) as csvfile:
for i, row in enumerate(csv.DictReader(csvfile)):
instance = row['instance']
d = row['d']
obj = float(row['obj'])
sol = list(map(parse_edge, row['solution'].split()))
violations = check(instance, d, obj, sol)
if violations != []:
print(i + 1, violations)
def check(instance, d, obj, edges):
w, dd, s = utils.read(instance)
n = len(w)
utils.scale(w, s)
if d != 'file':
dd = [int(d)] * n
exp_obj = 0
degree = [0] * n
for u, v in edges:
degree[u] += 1
degree[v] += 1
exp_obj += w[u][v]
violations = []
for v in range(n):
if degree[v] > dd[v]:
violations.append(
'{} has degree {} > {}'.format(v, degree[v], dd[v]))
G = nx.Graph()
G.add_edges_from(edges)
if edges == []:
violations.append('empty tree, maybe the solver did not print the solution')
return violations
elif G.number_of_nodes() != n or G.number_of_edges() + 1 != n or not nx.algorithms.is_tree(G):
violations.append('not a tree')
exp_obj /= s
if obj != exp_obj:
violations.append('obj {} is not {}'.format(obj, exp_obj))
return violations
def parse_edge(s):
u, v = s.split('-')
return int(u), int(v)
if __name__ == "__main__":
main()