简体   繁体   English

Python Z3 中的约束顺序以获得最佳速度

[英]Order of constraints in Python Z3 for best speed

I'm writing a z3 python program that is running a bit slow.我正在编写一个运行速度有点慢的 z3 python 程序。 The reason why it is running slow is because there is one part of the program where I am adding many harder constraints in nested for loops.它运行缓慢的原因是因为程序的某一部分我在嵌套的 for 循环中添加了许多更难的约束。 My instructor told us that adding equality constraints would make the program run faster in terms of limiting the possibilities the program goes through when the line Solver().check() is reached.我的导师告诉我们,当到达 Solver().check() 行时,在限制程序经历的可能性方面,添加等式约束将使程序运行得更快。

I'm wondering whether I should be adding the equality constraints before the "harder" constraints to make it go faster, or whether the equality constraints should go after the "harder" constraints?我想知道我是否应该在“更难”的约束之前添加等式约束以使其更快,或者等式约束是否应该在“更难”的约束之后?
I would want the equality constraints to be checked first to limit possibilities of the harder constraints, so I would assume s.add(x == y), or something should be added first so that it is checked first?我希望首先检查等式约束以限制更难约束的可能性,所以我会假设 s.add(x == y),或者应该先添加一些东西以便首先检查它?

These sorts of questions come often;这类问题经常出现; and the honest answer is that the performance of the solver on any given question relies on many factors.诚实的回答是,求解器在任何给定问题上的表现取决于许多因素。 Changing the order of constraints should in general have no effect, but in general it might.改变约束的顺序一般应该没有影响,但一般来说可能会。 Even renaming variables (something you'd think that'd have no effect) can change performance.甚至重命名变量(您认为不会产生任何影响的事情)也会改变性能。

If you're having performance problems, it's best to address it as a modeling issue: ie, how can you formulate your problem so it's "easier" to solve;如果您遇到性能问题,最好将其作为建模问题来解决:即,您如何制定问题以便“更容易”解决; instead of how to "reorder" the constraints, which is a never-ending guessing game.而不是如何“重新排序”约束,这是一个永无止境的猜谜游戏。 I suggest you actually post your actual encoding and ask about specific advice regarding that problem.我建议您实际发布您的实际编码并询问有关该问题的具体建议。 Your question is unanswerable in the sense that there's no single strategy that works well in all cases.您的问题是无法回答的,因为没有一种策略可以在所有情况下都有效。

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

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