Get the corresponding python variable name of a Z3 model name

徘徊边缘 提交于 2019-12-11 17:23:15

问题


Is there a way to get the corresponding python variable name of a z3 model name?

Suppose I have the following code:

from z3 import *

s = Solver()

a = [Real('a_%s' % k) for k in range(10)]

for i in range(10):
    s.add(a[i] > 10)

s.check()

m = s.model()
for d in m:
    print(d, m[d])

Here the d in m are a_0, a_1, a_2,..., a_9, and all of their values are 11. I'm trying to set up some constraints which make the variables don't equal to the previous checking result, like the following:

s.add(a[0] != m['a_0']
...
s.add(a[9] != m['a_9']

Therefore my question is that does Z3 has a method to get the corresponding python variable name of a z3 model name? If so, then I don't need to enumerate all the variables names. Because this will be a huge work if we have many variables. What I want to achieve can be the following:

m = s.model()
for d in m:
    s.add(corresponding_python_variabl_name(d) != m[d])

Does Z3 have that corresponding_python_variable_name() function? Thanks in advance!


回答1:


Looks like you're trying to generate all possible models?

If that's the case, use the following template:

from z3 import *

s = Solver()

a = [Real('a_%s' % k) for k in range(10)]

for i in range(10):
    s.add(a[i] > 10)

while s.check() == sat:
   m = s.model()

   if not m:
       break

   print m

   s.add(Not(And([v() == m[v] for v in m])))

Note that this will loop as many different models as there are; so if you have many models (potentially infinite with Reals as in your problem), then this'll go on forever. (Or till you run out of memory/CPU etc.)



来源:https://stackoverflow.com/questions/54115880/get-the-corresponding-python-variable-name-of-a-z3-model-name

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