【问题标题】:ValueError: Need more than 2123 valuesValueError:需要超过 2123 个值
【发布时间】:2013-03-21 21:02:25
【问题描述】:

我正在尝试运行一个非常大的 Z3 python 程序,如以下示例:

 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

【问题讨论】:

  • 缩进是怎么回事??
  • 这是一个非常大的程序示例,基于传递的参数
  • 请给出完整的错误信息,堆栈跟踪向我们展示了发生这种情况的确切位置,并更清楚地说明了原因 - 只给我们错误行会使调试变得更加困难。
  • 另外,请告诉我,您插入的 '....' 没有涵盖您要解压到的 2123 多个名称。
  • @Lattyware 我担心这就是他正在做的事情。 :facepalm:

标签: python z3 smt constraint-programming


【解决方案1】:

这不是好的编码习惯:

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

【讨论】:

  • 黄金法则是,如果您花费大量时间一次又一次地做同样的事情,那么计算机应该在做,而不是您。数据结构将使您的生活更轻松。
  • 能否请您根据我的代码通过示例进一步解释
  • @JordanEngland 鉴于你没有给我们一个完整的例子,我想任何人都很难制作一个。简化您的示例,也许答案会效仿。
  • @JordanEngland 唯一的“高级”代码是dict(zip(N,V)),它采用名称 N 的有序列表(或元组)和相应的值列表 V 并构建一个字典,将每个名称映射到它的价值。例如:ages = dict(zip(['John', 'Mary'], [20, 35]))。其他一切都只是基本的 Python。重点是不要手写2123个变量名
  • @JordanEngland 请务必将此答案标记为已接受,除非您需要更多信息。
猜你喜欢
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多