Z3 Solver TypeError: ‘ArithRef’ object cannot be interpreted as an integer

I am trying to run the code below with Z3py and the main problem is that I want to find the arguments for my function using a solver. And I want to use the arguments as integer and real inside my function.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
x = Real('x') #declare a real symbolic variable
k = Int('k') #declare an integer symbolic variable
s = Solver() #initialize the solver
#this is the function that takes x and k and does some calculations with them
def f(x, k):
for i in range(k):
x = x*1.099-1
return x
x0 = 10
s.add(Exists((x,k), And(f(x0,k)<0))) #here i want to find if there exists such x and k as my arguments to function k that makes the output negative
s.check()
</code>
<code> x = Real('x') #declare a real symbolic variable k = Int('k') #declare an integer symbolic variable s = Solver() #initialize the solver #this is the function that takes x and k and does some calculations with them def f(x, k): for i in range(k): x = x*1.099-1 return x x0 = 10 s.add(Exists((x,k), And(f(x0,k)<0))) #here i want to find if there exists such x and k as my arguments to function k that makes the output negative s.check() </code>

x = Real('x') #declare a real symbolic variable
k = Int('k') #declare an integer symbolic variable


s = Solver() #initialize the solver

#this is the function that takes x and k and does some calculations with them
def f(x, k):
    for i in range(k):
        x = x*1.099-1
    return x

x0 = 10

s.add(Exists((x,k),  And(f(x0,k)<0))) #here i want to find if there exists such x and k as my arguments to function k that makes the output negative

s.check()

However, this code results in a TypeError:’ArithRef’ object cannot be interpreted as an integer. I know I cannot use k as it is but I couldn’t find a way to make it work.

Is this possible? Thank you in advance!

I tried to write this as well:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>x = Real('x') #declare a real symbolic variable
k = Int('k') #declare an integer symbolic variable
s = Solver() #initialize the solver
#this is the function that takes x and k and does some calculations with them
def f(x, k):
for i in range(k):
x = x*1.099-1
return x
x0 = 10
s.add(Exists((x,k), And(f(x0,k)<0))) #here i want to find if there exists such x and k as my arguments to function k that makes the output negative
s.check()
</code>
<code>x = Real('x') #declare a real symbolic variable k = Int('k') #declare an integer symbolic variable s = Solver() #initialize the solver #this is the function that takes x and k and does some calculations with them def f(x, k): for i in range(k): x = x*1.099-1 return x x0 = 10 s.add(Exists((x,k), And(f(x0,k)<0))) #here i want to find if there exists such x and k as my arguments to function k that makes the output negative s.check() </code>
x = Real('x') #declare a real symbolic variable
k = Int('k') #declare an integer symbolic variable


s = Solver() #initialize the solver

#this is the function that takes x and k and does some calculations with them
def f(x, k):
    for i in range(k):
        x = x*1.099-1
    return x

x0 = 10

s.add(Exists((x,k),  And(f(x0,k)<0))) #here i want to find if there exists such x and k as my arguments to function k that makes the output negative

s.check()

This results in a ArgumentError: argument 2: <class ‘TypeError’>: wrong type

While z3py allows mixing/matching Python and z3 constructs, it does not allow for arbitrary python expressions to be used symbolically. (This would be an extremely hard thing to do.)

In particular, the call to range is not supported with a symbolic-variable.

Note that your loop over k creates a non-linear problem (multiplying variables together), which is already difficult for SMT solvers.

Your best bet would be to do an incremental search over k. Something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from z3 import *
x = Real('x')
def f(x, k):
for i in range(k):
x = x*1.099-1
return x
for k in range(10):
print(f"Trying k: {k}")
s = Solver()
s.add(x > 0)
s.add(And(f(x, k) < 0))
r = s.check()
if(r == sat):
print(s.model())
break
else:
print(f"Solver said: {r}")
</code>
<code>from z3 import * x = Real('x') def f(x, k): for i in range(k): x = x*1.099-1 return x for k in range(10): print(f"Trying k: {k}") s = Solver() s.add(x > 0) s.add(And(f(x, k) < 0)) r = s.check() if(r == sat): print(s.model()) break else: print(f"Solver said: {r}") </code>
from z3 import *

x = Real('x')

def f(x, k):
    for i in range(k):
        x = x*1.099-1
    return x

for k in range(10):
    print(f"Trying k: {k}")

    s = Solver()
    s.add(x > 0)
    s.add(And(f(x, k) < 0))

    r = s.check()
    if(r == sat):
        print(s.model())
        break
    else:
        print(f"Solver said: {r}")

This prints:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Trying k: 0
Solver said: unsat
Trying k: 1
[x = 500/1099]
</code>
<code>Trying k: 0 Solver said: unsat Trying k: 1 [x = 500/1099] </code>
Trying k: 0
Solver said: unsat
Trying k: 1
[x = 500/1099]

You probably wanted to assert some other constraints to make this more interesting. (For instance, you have an Exists(x), which goes unused; but you also have x0 = 10. Not quite clear what you’re trying to express.)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật