簡體   English   中英

在 Z3-Python 中,執行 model 搜索時,我得到“builtin_function_or_method' object is not iterable”

[英]In Z3-Python, I get "builtin_function_or_method' object is not iterable" when performing model search

我正在探索在 Z3 (Python) 中執行 SAT 求解的快速方法。 為此,我試圖模仿https://theory.stanford.edu/~nikolaj/programmingz3.html#sec-blocking-evaluations的第 5.1 章的結果。

我正在使用的代碼如下:

def block_modelT(s, terms): #I used the name 'block_modelT' so as not to use 'block_model'
    m = s.model()
    s.add(Or([t != m.eval(t) for t in terms]))

def all_smt(s, terms):
    while sat == s.check():
       print(s.model())
       block_modelT(s, terms)

因此,我執行它:

pip install z3-solver
from z3 import *

x, y, z = Bools('x y z')
vars = [x,y,z]

phi_0 = x #ignore that I call them phi: it is because I am used to do it like that for predicates (i.e. literals) of other theories: e.g., if x is Int, then phi_0 = (x>0)
phi_1 = y
phi_2 = z

phi = And(phi_0, Or(phi_1, phi_2))

all_smt(s, vars)

並得到以下錯誤:

      1 def block_modelT(s, terms):
      2     m = s.model()
----> 3     s.add(Or([t != m.eval(t) for t in terms]))

TypeError: 'builtin_function_or_method' object is not iterable

有什么幫助嗎?

編輯:

問題已經解決,所以我終於嘗試下一段代碼:

def all_smt(s, initial_terms):
    def block_term(s, m, t):
        s.add(t != m.eval(t))
    def fix_term(s, m, t):
        s.add(t == m.eval(t))
    def all_smt_rec(terms):
        if sat == s.check():
           m = s.model()
           yield m
           for i in range(len(terms)):
               s.push()
               block_term(s, m, terms[i])
               for j in range(i):
                   fix_term(s, m, terms[j])
               for m in all_smt_rec(terms[i:]):
                   yield m
               s.pop()   
    for m in all_smt_rec(list(initial_terms)):
        yield m   

我執行它: all_smt(s, varss) #has the same name <generator object all_smt at 0x7...而不是有效分配(即模型)。 怎樣才能得到正確答案[z = False, y = True, x = True], [z = True, x = True] 我嘗試了printreturn

您的程序沒有定義s 而且您還沒有向其中添加公式。 以下對我有用:

from z3 import *

def block_modelT(s, terms): #I used the name 'block_modelT' so as not to use 'block_model'
    m = s.model()
    s.add(Or([t != m.eval(t) for t in terms]))

def all_smt(s, terms):
    while sat == s.check():
       print(s.model())
       block_modelT(s, terms)

x, y, z = Bools('x y z')
vars = [x,y,z]

phi_0 = x #ignore that I call them phi: it is because I am used to do it like that for predicates (i.e. literals) of other theories: e.g., if x is Int, then phi_0 = (x>0)
phi_1 = y
phi_2 = z

phi = And(phi_0, Or(phi_1, phi_2))

s = Solver()
s.add(phi)

all_smt(s, vars)

這打印:

[z = False, y = True, x = True]
[z = True, x = True]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM