2013-03-31 53 views
0

我想運行一個非常大的Z3 Python程序,如下面的示例:ValueError異常:需要超過2123個值

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', …………..]) 
######################################### 
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) 

####################################### 
def fun2(m1 , m2): 
    conds = [ 
    (cl_3, me_32), 
    (cl_39, me_59), 
    (cl_11, me_81), 
    ... 
    ] 
    and_conds = (And(m1==a, m2==b) for a,b in conds) 
    return Or(*and_conds) 
####################################### 
def fun3(y1 , y2): 
    conds = [ 
    (cl_3, me_32), 
    (cl_39, me_59), 
    (cl_11, me_81), 
    ... 
    ] 
    and_conds = (And(y1==a, y2==b) for a,b in conds) 
    return Or(*and_conds) 

我已經使用了一組約束來檢索匹配的模型;匹配車型將基於函數的參數進行檢索,如下:

s = Solver() 
x1 = Const('x1', S) 
x2 = Const('x2', S) 
x3 = Const('x3', S) 

s.add(fun(x1,x2)) 
s.add(fun2(x2,x3) 
. 
. 
. 
s.add(fun3(x3,x1) 

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

不過,我收到以下錯誤

ValueError: need more than 2123 values to unpack 
+0

什麼是縮進? – pradyunsg

+0

這是一個非常大的程序示例,基於傳遞的參數 –

+0

請給出完整的錯誤信息,堆棧跟蹤顯示了發生這種情況的具體位置並使其更加清晰爲什麼 - 給我們提供的只有錯誤行使調試更加困難。 –

回答

3

這是不好的編碼習慣:

S, (cl_3, cl_39, cl_11, me_32, m_59, m_81...) = EnumSort(...) 

而不是像這樣定義數百個命名變量,你應該使用一個名稱列表,一個值列表,並建立一個字典來映射它們:

names = ['cl_3', 'cl_39'...] # don't write this list by hand, if you can avoid it 
# eg.: ['cl_{}'.format(i) for i in range(50)] + ['m_{}'.format(i) for i...] 

S, values = EnumSort('S', names) 

if len(names) != len(values): 
    raise Exception('...') 

name_to_value = dict(zip(names, values)) 

# then you can use name_to_value['cl_3'] and so on 
+0

黃金法則是,如果你一次又一次地做同樣的事情,計算機應該這樣做,而不是你。數據結構將使您的生活更輕鬆。 –

+0

能否請你根據我的代碼進一步說明示例 –

+0

@JordanEngland鑑於你沒有給我們一個完整的例子,我想任何人都很難做出一個例子。簡化你的例子,也許一個答案會隨之而來。 –

相關問題