我正在探索在Z3(Python)中执行SAT解算的快速方法。为此,我尝试模仿https://theory.stanford.edu/~nikolaj/programmingz3.html#sec-blocking-evaluations第5.1章的结果。
我使用的代码如下:
def block_modelT(s, terms): #I used the name 'block_modelT' so as not to use 'block_model'
m = s.model()
s.add(Or([t != m.eval(t) for t in terms]))
def all_smt(s, terms):
while sat == s.check():
print(s.model())
block_modelT(s, terms)
因此,我执行它:
pip install z3-solver
from z3 import *
x, y, z = Bools('x y z')
vars = [x,y,z]
phi_0 = x #ignore that I call them phi: it is because I am used to do it like that for predicates (i.e. literals) of other theories: e.g., if x is Int, then phi_0 = (x>0)
phi_1 = y
phi_2 = z
phi = And(phi_0, Or(phi_1, phi_2))
all_smt(s, vars)
并出现以下错误:
1 def block_modelT(s, terms):
2 m = s.model()
----> 3 s.add(Or([t != m.eval(t) for t in terms]))
TypeError: 'builtin_function_or_method' object is not iterable
有帮助吗?
编辑:
问题已解决,因此我最终尝试了下一段代码:
def all_smt(s, initial_terms):
def block_term(s, m, t):
s.add(t != m.eval(t))
def fix_term(s, m, t):
s.add(t == m.eval(t))
def all_smt_rec(terms):
if sat == s.check():
m = s.model()
yield m
for i in range(len(terms)):
s.push()
block_term(s, m, terms[i])
for j in range(i):
fix_term(s, m, terms[j])
for m in all_smt_rec(terms[i:]):
yield m
s.pop()
for m in all_smt_rec(list(initial_terms)):
yield m
我执行它:all_smt(s, varss) #has the same name
。并获取生成器<generator object all_smt at 0x7...
,而不是有效的赋值(即模型)。如何才能得到正确答案[z = False, y = True, x = True], [z = True, x = True]
?我同时尝试了print
和return
。
您的程序没有定义s
;您也没有向它添加公式。以下内容适用于我:
from z3 import *
def block_modelT(s, terms): #I used the name 'block_modelT' so as not to use 'block_model'
m = s.model()
s.add(Or([t != m.eval(t) for t in terms]))
def all_smt(s, terms):
while sat == s.check():
print(s.model())
block_modelT(s, terms)
x, y, z = Bools('x y z')
vars = [x,y,z]
phi_0 = x #ignore that I call them phi: it is because I am used to do it like that for predicates (i.e. literals) of other theories: e.g., if x is Int, then phi_0 = (x>0)
phi_1 = y
phi_2 = z
phi = And(phi_0, Or(phi_1, phi_2))
s = Solver()
s.add(phi)
all_smt(s, vars)
此打印:
[z = False, y = True, x = True]
[z = True, x = True]
上一篇:如何发挥非缓冲WAV与MediaStreamSource的实施在Silverlight 4?WAV、MediaStreamSource、Silverlight
下一篇:我需要做什么来安装以获得Microsoft.TeamFoundation.WorkItemTracking.Client.dll?做什么、Microsoft、TeamFoundation、dll