Search code examples
pythonsympyz3inequalitysatisfiability

z3 solver using Sympy symbols


I'm trying to use Z3 to determine if an expression is satisfiable.I have created all equations that I want to use it as constraints using SymPy and the variables are Symbols. the equations for example

"f1==(x -y >= 0 )"
"f2 == (x_y >= 1)"
"f3 == f1 -f2> =0"

I want the solver to return correct values for x,y,f1,f2,f3 that makes the equations satisfiable..


    s = z3.Solver()
    for m,n in zip((allvariables[3:len(allvariables)-1]),allequations):
        print('variable',m)
        print('equations',n)
        s.add(m==(n))
    
    print('solver check', s.check())
    while s.check() == sat:
        print(s)


Solution

  • In general, you cannot mix and match z3 and Sympy symbols. They live in different worlds, they cannot be readily interchanged.

    In essence, you'll have to parse the SymPy expression, and reconstruct it in terms of z3. This can be achieved in a variety of ways, but it's not a cheap/easy thing to do since you'd need to analyze large swaths of SymPy code. However, if your expressions are "simple" enough, you can get away with a simple translator, perhaps. Start by looking at how SymPy expressions can be turned into a parse-tree. This is a good point to start: https://docs.sympy.org/latest/tutorials/intro-tutorial/manipulation.html