简体   繁体   中英

Z3Py: Generate more counterexamples when using z3.prove

Is it possible to have Z3Py generate more counterexamples within reasonable time ?

I can generate one counterexample using z3.prove as follows:

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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