简体   繁体   English

Z3Py:使用z3.prove时生成更多反例

[英]Z3Py: Generate more counterexamples when using z3.prove

Is it possible to have Z3Py generate more counterexamples within reasonable time ? Z3Py是否有可能在合理的时间内生成更多反例?

I can generate one counterexample using z3.prove as follows: 我可以使用z3.prove生成一个反例,如下所示:

import z3

x = z3.Real("x")

rule = x > 0
goal = x < 0
z3.prove(z3.Implies(rule, goal))

which gives the following output, 它给出以下输出,

counterexample
[x = 1]

But what if I want to generate more counterexamples ? 但是,如果我想产生更多的反例呢?

Is the only way to do this by incrementally adding rules based on the counterexamples ? 通过基于反例逐步添加规则来做到这一点的唯一方法吗?

For the above case, I can get a different counterexample by doing this, 对于上述情况,我可以通过执行以下操作获得另一个反例,

z3.prove(z3.Implies(z3.And(rule, x!=1), goal))

but I believe this is slow when many rules are involved so I am hoping for a faster approach. 但是我认为,当涉及到许多规则时,这很慢,因此我希望有一个更快的方法。

Thanks! 谢谢!

What you suggest is the "usual" recommendation for getting more than one counter-example. 您建议的是获得多个反例的“常规”建议。 Z3 does not have methods for producing more than one counter-example because this is easy to do from the outside. Z3没有产生多个反例的方法,因为这很容易从外部进行。 However, it does have support for optimization, so if your goal is to find exactly one "best" or "at least as good" solution, then this may be a solution. 但是,它确实支持优化,因此,如果您的目标是找到一个“最佳”或“至少一样好”的解决方案,那么这可能是一个解决方案。

A simple optimization to the trivial counter-example addition is to check whether all the assignments in the counter-example are actually required. 对平凡的反例添加进行简单的优化是检查反例中的所有分配是否实际上是必需的。 For instance, it may be the case that the counter-example is (x = 5, y = 0), but nothing depends on y = 0, so we can remove it, which makes the counter-example smaller (and thus there will be fewer of them). 例如,反例可能是(x = 5,y = 0),但没有任何关系取决于y = 0,因此我们可以将其删除,这会使反例变小(因此,少一些)。

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

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