forked from jar-ben/tamus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
explorer.py
146 lines (129 loc) · 4.92 KB
/
explorer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from z3 import *
class Explorer:
def __init__(self, dimension):
self.dimension = dimension
self.vars = []
for i in range(dimension):
self.vars.append(Bool('x' + str(i)))
self.s = Solver()
self.blockUps = []
self.blockDowns = []
self.shadowBlockUps = []
self.shadowBlockDowns = []
def complement(self, N):
return [i for i in range(self.dimension) if i not in N]
def shadow_block_up(self, N, trace = None):
self.shadowBlockUps.append({"set": N[:], "trace": trace})
def shadow_block_down(self, N):
self.shadowBlockDowns.append(N[:])
def block_up(self, N):
self.blockUps.append(N[:])
block = [Not(self.vars[n]) for n in N ]
self.s.add(Or(block))
def block_down(self, N):
self.blockDowns.append(N[:])
block = [self.vars[n] for n in self.complement(N) ]
self.s.add(Or(block))
#gets a maximal unexplored subset of N
def get_unex_subset(self, N):
assumptions = [Not(self.vars[c]) for c in self.complement(N)]
check = self.s.check(assumptions)
if check == sat:
seed = []
m = self.s.model()
for x in m:
if is_true(m[x]):
seed.append(int(str(x)[1:]))
#maximize
for c in N:
if c in seed: continue
if self.is_unexplored(seed + [c]):
seed.append(c)
return seed
return None
def get_unex(self, minCard = -1, maxCard = -1):
if max(minCard, maxCard) >= 0:
self.s.push()
if minCard >= 0:
self.s.add(PbGe([(x,1) for x in self.vars], minCard))
if maxCard >= 0:
self.s.add(PbLe([(x,1) for x in self.vars], maxCard))
check = self.s.check()
if check == sat:
seed = []
m = self.s.model()
for x in m:
if is_true(m[x]):
seed.append(int(str(x)[1:]))
if max(minCard, maxCard) >= 0:
self.s.pop()
return seed
if max(minCard, maxCard) >= 0:
self.s.pop()
return None
# maximize a given unexplored subset (seed)
def maximize(self, seed, minCard = -1, maxCard = -1):
if max(minCard, maxCard) >= 0:
self.s.push()
if minCard >= 0:
self.s.add(PbGe([(x,1) for x in self.vars], minCard))
if maxCard >= 0:
self.s.add(PbLe([(x,1) for x in self.vars], maxCard))
for c in self.complement(seed):
if self.is_unexplored(seed + [c]):
seed.append(c)
if max(minCard, maxCard) >= 0:
self.s.pop()
return seed
# minimize a given unexplored subset (seed)
def minimize(self, seed, minCard = -1, maxCard = -1):
if max(minCard, maxCard) >= 0:
self.s.push()
if minCard >= 0:
self.s.add(PbGe([(x,1) for x in self.vars], minCard))
if maxCard >= 0:
self.s.add(PbLe([(x,1) for x in self.vars], maxCard))
candidates = seed[:]
while len(candidates) > 0:
c = candidates[-1]
candidates = candidates[:-1]
Nc = seed[:]
Nc.remove(c)
if self.is_unexplored(Nc):
seed.remove(c)
if max(minCard, maxCard) >= 0:
self.s.pop()
return seed
# checks whether c is minable critical for N, i.e., whether N - {c} is unexplored
def is_critical(self, c, N):
assert c in N
Nc = N[:]
Nc.remove(c)
return (not self.is_unexplored(Nc)) or (not self.is_shadow_unexplored(Nc))
# checks whether c is minable conflicting for N, i.e., whether N \cup {c} is unexplored
def is_conflicting(self, c, N):
assert c not in N
Nc = N + [c]
return (not self.is_unexplored(Nc)) or (not self.is_shadow_unexplored(Nc))
def is_shadow_sufficient(self, N):
for B in self.shadowBlockUps:
if len(set(B["set"]) - set(N)) == 0:
return (True, B["trace"])
return (False, None)
def is_shadow_insufficient(self, N):
for B in self.shadowBlockDowns:
if len(set(N) - set(B)) == 0:
return True
return False
def is_shadow_unexplored(self, N):
for B in self.shadowBlockUps:
if len(set(B["set"]) - set(N)) == 0:
return False
for B in self.shadowBlockDowns:
if len(set(N) - set(B)) == 0:
return False
return True
# checks if N is unexplored
def is_unexplored(self, N):
assumptions = [self.vars[c] for c in N] + [Not(self.vars[c]) for c in self.complement(N)]
return (self.s.check(assumptions) == sat)