Do you know how to set “weak” initial values to each of z3 Array element?

我与影子孤独终老i 提交于 2021-02-11 17:01:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!