Z3 python - Return 约束成立
Z3 python - Return constraint that holds True
我创建了 bool
xs 列表,其中之一是 True
# b is a random number
xs= [Bool("xs{i}".format(i)) for i in range(0,b)]
# only one is True
s.add(PbEq([(x,1) for x in xs], 1))
if s.check() == sat:
m = s.model()
我怎样才能 return 只包含 True
值的约束?我将其处理为字符串列表。但是,我认为转换为不合逻辑的字符串列表。
您可以遍历 xs
并计算:
from z3 import *
b = 10
xs = [Bool("xs_%d" % i) for i in range(0,b)]
s = Solver()
s.add(PbEq([(x,1) for x in xs], 1))
if s.check() == sat:
m = s.model()
for x in xs:
if(m.eval(x)):
print("%s is true" % x)
else:
print("%s is false" % x)
当我运行这个时,它打印:
xs_0 is true
xs_1 is false
xs_2 is false
xs_3 is false
xs_4 is false
xs_5 is false
xs_6 is false
xs_7 is false
xs_8 is false
xs_9 is false
我创建了 bool
xs 列表,其中之一是 True
# b is a random number
xs= [Bool("xs{i}".format(i)) for i in range(0,b)]
# only one is True
s.add(PbEq([(x,1) for x in xs], 1))
if s.check() == sat:
m = s.model()
我怎样才能 return 只包含 True
值的约束?我将其处理为字符串列表。但是,我认为转换为不合逻辑的字符串列表。
您可以遍历 xs
并计算:
from z3 import *
b = 10
xs = [Bool("xs_%d" % i) for i in range(0,b)]
s = Solver()
s.add(PbEq([(x,1) for x in xs], 1))
if s.check() == sat:
m = s.model()
for x in xs:
if(m.eval(x)):
print("%s is true" % x)
else:
print("%s is false" % x)
当我运行这个时,它打印:
xs_0 is true
xs_1 is false
xs_2 is false
xs_3 is false
xs_4 is false
xs_5 is false
xs_6 is false
xs_7 is false
xs_8 is false
xs_9 is false