Reputation: 862
Could someone explain how to access the result values of the equation variables, when using the solve() function of the https://pypi.org/project/z3-solver/.
x, y = BitVecs('x y', 32)
solve(x + y == 2, x > 0, y > 0)
I have tried following to no avail
m = solve(x + y == 2, x > 0, y > 0)
print(m.x)
Mind, in this scenario we do not want to use the Solver
s = Solver()
s.add(And(x + y == 2, x > 0, y > 0))
s.check()
m = s.model()
print(m[x], m[y])
Upvotes: 0
Views: 1496
Reputation: 11322
There seems to be no direct way. Function solve()
prints the found solution but does not return the model.
The definition of solve()
in file z3.py:
def solve(*args, **keywords):
"""Solve the constraints `*args`.
This is a simple function for creating demonstrations. It creates a solver,
configure it using the options in `keywords`, adds the constraints
in `args`, and invokes check.
>>> a = Int('a')
>>> solve(a > 0, a < 2)
[a = 1]
"""
s = Solver()
s.set(**keywords)
s.add(*args)
if keywords.get('show', False):
print(s)
r = s.check()
if r == unsat:
print("no solution")
elif r == unknown:
print("failed to solve")
try:
print(s.model())
except Z3Exception:
return
else:
print(s.model())
So, solve()
is just a wrapper function. It creates a Solver()
and does not expose the resulting model to access the variables.
Upvotes: 2