retrieve the matched model in Z3py?

匆匆过客 提交于 2019-12-06 15:07:40

问题


In the following working example , How to retrieve the matched model?

     S,   (cl_3,cl_39,cl_11, me_32,m_59,m_81) = 
     EnumSort('S', ['cl_3','cl_39','cl_11','me_32','me_59','me_81'])

       h1, h2 = Consts('h1 h2', S)
       def fun(h1 , h2):

        conds = [
        (cl_3, me_32),
        (cl_39, me_59),
        (cl_11, me_81),
         # ...
             ]

    and_conds = (And(h1==a, h2==b) for a,b in conds)
     return Or(*and_conds)

For Example: as the following solver

  s = Solver()
  x1 = Const('x1', S)
  x2 = Const('x2', S)
  s.add(fun(x1,x2)) 

  print s.check()
  print s.model()

回答1:


I'm assuming that you want the value of x1 and x2 in the model produced by Z3. If that is the case, you can retrieve them using:

   m = s.model()
   print m[x1]
   print m[x2]

Here is the complete example (also available online here). BTW, note that we don't need h1, h2 = Consts('h1 h2', S).

S, (cl_3, cl_39, cl_11, me_32, me_59, me_81) = 
      EnumSort('S', ['cl_3','cl_39','cl_11','me_32','me_59','me_81'])
def fun(h1 , h2):
   conds = [
     (cl_3, me_32),
     (cl_39, me_59),
     (cl_11, me_81),
   ]
   and_conds = (And(h1==a, h2==b) for a,b in conds)
   return Or(*and_conds)

s = Solver()
x1 = Const('x1', S)
x2 = Const('x2', S)
s.add(fun(x1,x2)) 
print s.check()
m = s.model()
print m
print m[x1]
print m[x2]


来源:https://stackoverflow.com/questions/15665235/retrieve-the-matched-model-in-z3py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!