简体   繁体   English

如何循环 Z3 变量以在 python 中添加约束

[英]How to loop a Z3 variable for adding constraints in python

I want to loop a Z3 variable for adding constraints in python.我想循环一个 Z3 变量以在 python 中添加约束。 Following code snippet might be helpful to clarify the problem I am facing.以下代码片段可能有助于澄清我面临的问题。

from z3 import *

a = Int('a')
b = [Int( 'b_' + str(i)) for i in range(10)]

s = Solver()
s.add(a <= 4)

for i in range(a): ################## How to do this? #####################
    s.add(b[i] == 1)

s.check()
s.model()

You can't.你不能。 That's the whole point of a symbolic variable.这就是符号变量的全部意义所在。 It's value can be anything (subject to constraints you put in, of course), and thus it cannot be passed as an argument to range .它的值可以是任何东西(当然,受您输入的约束),因此它不能作为参数传递给range

Since your b 's come from a list, you should instead iterate over that range.由于您的b来自列表,因此您应该迭代该范围。 Something like:就像是:

for i in range(10):
  s.add(Implies(i < a,  b[i] == 1))

Of course it depends on exactly what you are trying to achieve.当然,这取决于您要达到的目标。 Your question sounds like an instance of the XY problem .您的问题听起来像是XY 问题的一个实例。 You might get a better answer if you ask about what you're trying to achieve, as opposed to how you can simulate range for a symbolic variable.如果您询问您要实现的目标,而不是如何模拟符号变量的range ,您可能会得到更好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM