-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve
executable file
·72 lines (57 loc) · 1.88 KB
/
solve
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
#!/usr/bin/env -S python3 -B
from __future__ import print_function
import os
import subprocess
import sys
import utils
def main():
if len(sys.argv) < 4:
usage()
sys.exit(1)
solver = sys.argv[1]
d = sys.argv[2]
instances = sys.argv[3:]
if solver not in get_solvers():
print('Invalid solver:', solver)
usage()
sys.exit(1)
if d != 'file':
try:
d = int(d)
if d < 2:
raise Exception()
except:
print('d must be file or a number >= 2')
sys.exit(1)
run(solver, d, instances)
def run(solver, d, instances):
print('instance,d,solver,time,obj,solution')
for instance in instances:
w, dd, s = utils.read(instance)
if d == 'file':
if dd == None:
print(instance, 'no degree constrained provided')
continue
else:
dd = [d] * len(w)
utils.scale(w, s)
# TODO: avoid writing the instance if it is already in the correct format
# TODO: allow run in parallel
with utils.write_andinst(w, dd) as tmp:
time, obj, sol = utils.run_solver(solver, instance, tmp.name)
obj /= s
print('{},{},{},{:g},{:g},{}'.format(
instance, d, solver, time, obj, sol))
def usage():
solvers = get_solvers()
print(
'Usage: {} solver (file | number) instance [instance ...]'.format(sys.argv[0]))
print(' solver - one of', ', '.join(solvers))
print(' file - the degree constrain is read from the instance file')
print(' number - use number as degree constraint (>= 2)')
def get_solvers():
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
return sorted(list(s for s in os.listdir('solvers/') if is_exe('solvers/' + s)))
if __name__ == "__main__":
main()