Z3py return 不支持使用 pow() 函数的操作数
Z3py return unsupported operand using pow() function
我是 Z3 的新手。我一直在尝试用 Z3 解决问题,但我做不到。我有一个像这样的 c/c++ 代码:
n4 = 0
if ( j1 != j6 )
++n4;
if ( j1 + 1 != j2 )
++n4;
if ( j4 + 1 != j1 )
++n4;
if ( j3 + 4 != j6 )
++n4;
if ( j5 + 2 != j3 )
++n4;
n5 = n4 + (j4 ^ 102) + j7;
n5
变量必须大于零。我编写的 Z3py 代码如下所示:
for i in range(1,8):
globals()['j%i' % i] = BitVec ('j%i' % i, 8)
s = Solver()
a = IntVector('a', 5)
a__1 = If(eq(j1, j6), 0, 1)
a__2 = If(j1+1 != j2, 1, 0)
a__3 = If(j4+1 != j1,1, 0)
a__4 = If(j3+4 != j6,1, 0)
a__5 = If(j5+2 != j3, 1,0)
su = IntVector('su',1)
su = Sum(a)
z= Real('z')
s.add(z == j4**102)
s.add(su + z + j7 > 0)
while s.check() == sat:
print s.model()
在 运行 脚本之后我得到了这个错误:
TypeError: unsupported operand type(s) for ** or pow(): 'instance' and 'int'
提前致谢。
我不确定您的实际问题是什么,但我想我会按照我理解的方式翻译 C(++) 代码。
我避免使用 IntVector,而是使用 Int 来创建 Sum。
我想出了下面的代码。
可以打印模型,希望对你有帮助。
# import all of z3, because why not; define solver
from z3 import *
s = Solver()
# define j
for i in range(1,8):
globals()['j%i' % i] = BitVec ('j%i' % i, 8)
# define 8-bit BitVector (BV) (not Integer in the mathematical sense)
n4 = BitVec('n4', 8)
# create an Int so we can use Sum (we could also create BV for each if and then sum directly)
su = Int("sum")
s.add(su == Sum(
If(eq(j1, j6), 0, 1),
If(j1+1 != j2, 1, 0),
If(j4+1 != j1, 1, 0),
If(j3+4 != j6, 1, 0),
If(j5+2 != j3, 1, 0)
)
)
# we used an int, so we need to convert to a BV
s.add(n4 == Int2BV(su, 8))
# create n5 as an 8-bit BV and make sure it's value matches the formula
n5 = BitVec('n5', 8)
s.add(n5 == (n4 + (j4 ^ 102) + j7)) # I assume you meant ^ and not **
# requirement, n5 > 0 (could be merged with the lines above, avoiding the creation of n5 BV)
s.add(n5 > 0)
# iterate over solutions
while s.check() == sat:
print(s.model()) # Python3 uses brackets
我是 Z3 的新手。我一直在尝试用 Z3 解决问题,但我做不到。我有一个像这样的 c/c++ 代码:
n4 = 0
if ( j1 != j6 )
++n4;
if ( j1 + 1 != j2 )
++n4;
if ( j4 + 1 != j1 )
++n4;
if ( j3 + 4 != j6 )
++n4;
if ( j5 + 2 != j3 )
++n4;
n5 = n4 + (j4 ^ 102) + j7;
n5
变量必须大于零。我编写的 Z3py 代码如下所示:
for i in range(1,8):
globals()['j%i' % i] = BitVec ('j%i' % i, 8)
s = Solver()
a = IntVector('a', 5)
a__1 = If(eq(j1, j6), 0, 1)
a__2 = If(j1+1 != j2, 1, 0)
a__3 = If(j4+1 != j1,1, 0)
a__4 = If(j3+4 != j6,1, 0)
a__5 = If(j5+2 != j3, 1,0)
su = IntVector('su',1)
su = Sum(a)
z= Real('z')
s.add(z == j4**102)
s.add(su + z + j7 > 0)
while s.check() == sat:
print s.model()
在 运行 脚本之后我得到了这个错误:
TypeError: unsupported operand type(s) for ** or pow(): 'instance' and 'int'
提前致谢。
我不确定您的实际问题是什么,但我想我会按照我理解的方式翻译 C(++) 代码。 我避免使用 IntVector,而是使用 Int 来创建 Sum。
我想出了下面的代码。
可以打印模型,希望对你有帮助。
# import all of z3, because why not; define solver
from z3 import *
s = Solver()
# define j
for i in range(1,8):
globals()['j%i' % i] = BitVec ('j%i' % i, 8)
# define 8-bit BitVector (BV) (not Integer in the mathematical sense)
n4 = BitVec('n4', 8)
# create an Int so we can use Sum (we could also create BV for each if and then sum directly)
su = Int("sum")
s.add(su == Sum(
If(eq(j1, j6), 0, 1),
If(j1+1 != j2, 1, 0),
If(j4+1 != j1, 1, 0),
If(j3+4 != j6, 1, 0),
If(j5+2 != j3, 1, 0)
)
)
# we used an int, so we need to convert to a BV
s.add(n4 == Int2BV(su, 8))
# create n5 as an 8-bit BV and make sure it's value matches the formula
n5 = BitVec('n5', 8)
s.add(n5 == (n4 + (j4 ^ 102) + j7)) # I assume you meant ^ and not **
# requirement, n5 > 0 (could be merged with the lines above, avoiding the creation of n5 BV)
s.add(n5 > 0)
# iterate over solutions
while s.check() == sat:
print(s.model()) # Python3 uses brackets