对于最多 n 个 x_i 变量(随机选择),如何将 Kyle 的答案与不同的答案结合起来?
这将给出一个模型(对于 N = 50 和 100 x_i 变量):
x = [0 -> 1,
1 -> 11,
2 -> 50,
3 -> 1,
4 -> 2,
5 -> 1,
6 -> 36,
7 -> 1,
8 -> 34,
9 -> 1,
10 -> 13,
11 -> 5,
12 -> 7,
13 -> 23,
14 -> 1,
15 -> 40,
16 -> 42,
17 -> 1,
18 -> 1,
19 -> 1,
20 -> 16,
21 -> 33,
22 -> 1,
23 -> 17,
24 -> 20,
25 -> 1,
26 -> 9,
27 -> 44,
28 -> 1,
29 -> 49,
30 -> 26,
31 -> 1,
32 -> 29,
33 -> 46,
34 -> 8,
35 -> 1,
36 -> 27,
37 -> 1,
38 -> 1,
39 -> 32,
40 -> 1,
41 -> 31,
42 -> 1,
43 -> 1,
44 -> 14,
45 -> 1,
46 -> 1,
47 -> 1,
48 -> 1,
49 -> 1,
50 -> 35,
51 -> 19,
52 -> 43,
53 -> 22,
54 -> 1,
55 -> 1,
56 -> 1,
57 -> 1,
58 -> 21,
59 -> 1,
60 -> 1,
61 -> 39,
62 -> 28,
63 -> 12,
64 -> 1,
65 -> 1,
66 -> 1,
67 -> 1,
68 -> 1,
69 -> 41,
70 -> 1,
71 -> 25,
72 -> 1,
73 -> 6,
74 -> 1,
75 -> 1,
76 -> 1,
77 -> 1,
78 -> 1,
79 -> 24,
80 -> 1,
81 -> 30,
82 -> 38,
83 -> 3,
84 -> 4,
85 -> 1,
86 -> 1,
87 -> 1,
88 -> 1,
89 -> 1,
90 -> 18,
91 -> 1,
92 -> 47,
93 -> 37,
94 -> 1,
95 -> 45,
96 -> 1,
97 -> 15,
98 -> 48,
99 -> 10,
else -> 1],
这是一个完成此操作的 Z3Py 脚本,假设可以限制前 N 个索引,而不是随机索引(并且使用 x 函数而不是常量,因此编写速度更快):http ://rise4fun.com/Z3Py/M3TG
接下来是为一组随机索引执行此操作的代码,但您不能在 Z3Py@Rise 上运行它,因为它不允许使用导入,因此您必须在本地运行它。
from random import *
from z3 import *
x = Function('x', IntSort(), IntSort())
M = 100
N = 50
s = Solver()
idxs = sample(xrange(M),N) # get N random ids from sequence {1,...M}
print idxs
distinctlist = []
for i in range(M):
s.add(And(x(i) >= 1, x(i) <= N))
if i in idxs:
distinctlist.append(x(i))
print distinctlist
s.add(Distinct(distinctlist))
print "checking..."
r = s.check()
print r
if r == sat:
print s.model()
(请注意,如果您不满足此查询,它可能会超时。)