我认为混淆可能源于 Store 和 Select 作为具有副作用的方法与表达式之间的差异。
当您这样做时:Store(a, Int(i), Int(i*2))
,您正在构建一个表示执行存储结果的表达式。因此,正如@alias 建议的那样,您需要继续构建相同的表达式。
我假设您可能会遇到类似的问题Select
。如果你这样做s = Select(a, Int(0))
,你正在构建一个表达式,而不是一个值。如果a
是 a 有一个定义索引 0 的值,您应该能够做到s.simplify()
并获得该值。
在上面的示例中,您应该能够将步骤 3) 简单地替换为:
simplify(Select(k, Int(0))) # Int(5)
编辑:下面讨论的完整示例
from pysmt.shortcuts import Symbol, Store, Select, Int, get_model
from pysmt.typing import ArrayType, INT
a = Symbol("a", ArrayType(INT, INT))
for i in range(10):
a = Store(a, Int(i), Int(i*2))
print(a.serialize())
# >>> a[0 := 0][1 := 2][2 := 4][3 := 6][4 := 8][5 := 10][6 := 12][7 := 14][8 := 16][9 := 18]
# x is the expression resulting from reading element 5
# Simplify does not manage to infer that the value should be 10
x = Select(a, Int(5))
print(x.simplify().serialize())
# >>> a[0 := 0][1 := 2][2 := 4][3 := 6][4 := 8][5 := 10][6 := 12][7 := 14][8 := 16][9 := 18][5]
# Ask the solver for a value:
# Note that we can only assert Boolean expressions, and not theory expressions, so we write a trivial expression a = a
m = get_model(a.Equals(a))
# Evaluate the expression x within the model
print(m[x])
# >>> 10