-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmt_solver.py
70 lines (57 loc) · 1.48 KB
/
smt_solver.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
#!/bin/python3
''''exec python3 -- "$0" ${1+"$@"} # '''
from z3 import *
import json
import sys
import ast
import traceback
def check_feasibility(matrix, vector):
Lambda = []
n_lambda = len(matrix[0])
s = Solver()
for i in range(n_lambda):
Lambda.append(Int('Lambda' + str(i+1)) )
s.add(Lambda[i] >= 0)
for i in range(len(vector)):
expression = 0
for j in range(n_lambda):
expression += matrix[i][j] * Lambda[j]
s.add(expression == vector[i])
return s.check() == sat
def _parseJSON(obj):
"""Convert unicode strings to standard strings"""
if isinstance(obj, dict):
newobj = {}
for key, value in obj.iteritems():
key = str(key)
newobj[key] = _parseJSON(value)
elif isinstance(obj, list):
newobj = []
for value in obj:
newobj.append(_parseJSON(value))
elif isinstance(obj, unicode):
newobj = str(obj)
else:
newobj = obj
return newobj
def main():
try:
while True:
inp = sys.stdin.readline()
if inp == '':
break
cmd = ast.literal_eval(inp)
M = cmd["matrix"]
v = cmd["vector"]
if len(M) == 0:
result = v == []
elif len(M[0]) == 0:
result = not any(v)
else:
result = check_feasibility(M, v)
print(json.dumps(result))
sys.stdout.flush()
except Exception:
traceback.print_exc()
if __name__ == "__main__":
main()