我是使用Z3的新手。我一直试图解决Z3的问题,但我不能。我有一个像这样的c / c ++代码:
n5
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()
变量必须大于零。我编写的Z3py代码如下代码所示:
TypeError: unsupported operand type(s) for ** or pow(): 'instance' and 'int'
运行脚本后我收到了这个错误:
SELECT (CASE WHEN (
SELECT COUNT(*)
FROM mlm_register r2
WHERE r2.dsponserid = 10001 AND
r2.joindate BETWEEN '2018-02-13' AND DATE_ADD('2018-02-13', INTERVAL 7 DAY)
) >= 5
THEN 200
WHEN r.dsponserid = 10001 AND
r.joindate BETWEEN '2018-02-13' AND DATE_ADD('2018-02-13', INTERVAL 7 DAY)
THEN 0
ELSE 0
END) AS MONEY
FROM mlm_register r
提前致谢。
答案 0 :(得分:0)
我不确定你的实际问题是什么,但我想我会按照我理解的方式翻译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