代码之家  ›  专栏  ›  技术社区  ›  Jordan England

在Z3py中检索匹配的模型?

  •  0
  • Jordan England  · 技术社区  · 13 年前

    在下面的工作示例中,如何检索匹配的模型?

         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)
    

    例如: 作为以下解算器

      s = Solver()
      x1 = Const('x1', S)
      x2 = Const('x2', S)
      s.add(fun(x1,x2)) 
    
      print s.check()
      print s.model()
    
    1 回复  |  直到 13 年前
        1
  •  1
  •   Leonardo de Moura    13 年前

    我假设你想要 x1 x2 在Z3生产的模型中。如果是这种情况,您可以使用以下方法检索它们:

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

    以下是完整的示例(也可在线获取 here ). 顺便说一句,注意我们不需要 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]
    
    推荐文章