Is there a way to convert z3.z3.ArithRef Real to Numpy Float64?

Question:

I recently started using Microsoft Z3 for Theorem Proving and I was wondering if there was a way to convert the Z3 Real Numbers to Python Floating Point Numbers so that I can pass them to other complex functions.

This is what I would like to do,

from z3 import *

def prediction(x):
   if x > 10: # This is where Z3 gives me an error as 'x' is a z3 object
      return 10 
   else:
      return x

x = Real('x')
z = prediction(x)
s = Solver()
s.add(2 <= x, x < 5)
s.add(z > 4)
res = s.check()
print(res)
if res == sat:
    print(s.model()) 

ERROR: Z3Exception: Symbolic expressions cannot be cast to concrete Boolean values.

I wish to convert them to NumPy format as the other libraries accept inputs as NumPy arrays and not as Arithref Objects.

Any help would be appreciated. Thanks!

Asked By: valency007

||

Answers:

You shouldn’t try to convert a symbolic Real to a Python float. Not only there is no way to do so, these two types are incompatible with each other. Instead, you should directly program with the symbolic values using z3py control idioms. For if-then-else, use the If function. So, your example would be coded as:

from z3 import *

def prediction(x):
   return If(x > 10, 10, x)

x = Real('x')
z = prediction(x)
s = Solver()
s.add(2 <= x, x < 5)
s.add(z > 4)
res = s.check()
print(res)
if res == sat:
    print(s.model())

which prints:

sat
[x = 9/2]
Answered By: alias