问题
For example, is there a something like below weak_Store function in Z3?
from z3 import *
a = Array('a', IntSort(), IntSort())
a = weak_Store(a, 0, 0)
a = weak_Store(a, 1, 1)
s = Solver()
s.add(a[0] == 100)
print(s.check()) # should print "sat"
print(s.model().eval(a[0])) # should print "100"
print(s.model().eval(a[1])) # should print "1" which is stored as weak_Store.
Since a[1] is not involved in the above constraint solving, this should not be computed and changed even after s.check().
I think this is related to model_completion
variable in z3_model_eval
,
but z3_model_eval
does not work for z3 Array
element.
Although the example is written in Python, I would like to do it with z3 C api.
Can anybody help me?
Thank you in advance.
回答1:
This sort of constraints are called "soft" constraints, i.e., those that can be violated if necessary, but the solver will try to satisfy them otherwise. Note that you have to use the Optimize
object, (not Solver
), which has diminished capacity in general. (i.e., slower, more likely to say unknown
etc.)
To add a soft constraint, use add_soft
instead of add
in z3py. You can code your example in Python like this:
from z3 import *
a = Array('a', IntSort(), IntSort())
s = Optimize()
s.add_soft(a[0] == 0)
s.add_soft(a[1] == 1)
s.add(a[0] == 100)
print(s.check())
print(s.model().eval(a[0]))
print(s.model().eval(a[1]))
This prints:
sat
100
1
as you requested.
In the C-API, the corresponding call is Z3_optimize_assert_soft.
来源:https://stackoverflow.com/questions/60666626/do-you-know-how-to-set-weak-initial-values-to-each-of-z3-array-element