问题
How can I get real python values from a Z3 model?
E.g.
p = Bool('p')
x = Real('x')
s = Solver()
s.add(Or(x < 5, x > 10), Or(p, x**2 == 2), Not(p))
s.check()
print s.model()[x]
print s.model()[p]
prints
-1.4142135623?
False
but those are Z3 objects and not python float/bool objects.
I know that I can check boolean values using is_true
/is_false
, but how can I elegantly convert ints/reals/... back to usable values (without going through strings and cutting away this extra ?
symbol, for example).
回答1:
For Boolean values, you can use the functions is_true
and is_false
. Numerical values can be integer, rational or algebraic. We can use the functions is_int_value
, is_rational_value
and is_algebraic_value
to test each case. The integer case is the simplest, we can use the method as_long()
to convert the Z3 integer value into a Python long. For rational values, we can use the methods numerator()
and denominator()
to obtain the Z3 integers representing the numerator and denominator. The methods numerator_as_long()
and denominator_as_long()
are shortcuts for self.numerator().as_long()
and self.denominator().as_long()
. Finally, algebraic numbers are used to represent irrational numbers. The AlgebraicNumRef
class has a method called approx(self, precision)
. It returns a Z3 rational number that approximates the algebraic number with precision 1/10^precision
. Here is an example on how to use this methods. It is also available online at: http://rise4fun.com/Z3Py/Mkw
p = Bool('p')
x = Real('x')
s = Solver()
s.add(Or(x < 5, x > 10), Or(p, x**2 == 2), Not(p))
s.check()
m = s.model()
print m[p], m[x]
print "is_true(m[p]):", is_true(m[p])
print "is_false(m[p]):", is_false(m[p])
print "is_int_value(m[x]):", is_int_value(m[x])
print "is_rational_value(m[x]):", is_rational_value(m[x])
print "is_algebraic_value(m[x]):", is_algebraic_value(m[x])
r = m[x].approx(20) # r is an approximation of m[x] with precision 1/10^20
print "is_rational_value(r):", is_rational_value(r)
print r.numerator_as_long()
print r.denominator_as_long()
print float(r.numerator_as_long())/float(r.denominator_as_long())
来源:https://stackoverflow.com/questions/12598408/z3-python-getting-python-values-from-model