-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_code 2.py
283 lines (246 loc) · 9 KB
/
all_code 2.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
################################################################################
#
# States are represented by 3-tuples of integers in the range 0, ..., k.
#
# Transitions are 2-tuples of states (start_state, end_state), where start_state
# is the start of the transition and end_state is the end of the transition.
#
# Reachable states should be represented by a 3-tuple (state, length, previous)
# where state is the reachable state, length is the length of the path to get
# there, and previous is the previous state. For the 0 length path to the start,
# that would be (start, 0, start).
#
################################################################################
# start is a state, a 3-tuple (x, y, z) where 0 <= x, y, z <= k
# transitions is a list of 2-tuples of 3-tuples (x, y, z)
# where 0 <= x, y, z <= k.
# Note that the start state is reachable through a path of length 0.
#((0,10, 20), (9, 19, 20))
def reachable_states(start, transitions):
# TODO: Implement part a.
#storing into a hashmap.
# #print("here'")
list_t = {}
q= [] #pop from the back.
result= []
visited={}
c_state= {}
#seen =[]
#building an adjacency list.
for t in transitions:
#storing into hashmap.
visited[t[0]] = None
visited[t[1]] = None
#print("inserting"+str(t[0]))
if t[0] not in list_t:
list_t[t[0]] = []
list_t[t[0]].append(t[1])
else:
list_t[t[0]].append(t[1])
q.append(start)
c_state[start] = (0, start)
visited[start] = True
while len(q) > 0:
cur = q.pop(len(q)-1)
res = (cur, c_state[cur][0], c_state[cur][1])
result.append(res)
if cur in list_t: #if reachable
for n in list_t[cur]:
if visited[n] is None:
q.insert(0, n)
visited[n] = True
c_state[n] = (c_state[cur][0]+1, cur)
#c_state has the information about the parent nodes.
return result
#can i make it so that
def reachable_states_machine(start, transitions):
# TODO: Implement part a.
#storing into a hashmap.
# #print("here'")
list_t = {}
q = [] #pop from the back.
result= []
visited={}
c_state= {}
#seen =[]
#building an adjacency list.
for t in transitions:
#storing into hashmap.
visited[t[0]] = None
visited[t[1]] = None
#print("inserting"+str(t[0]))
if t[0] not in list_t:
list_t[t[0]] = []
list_t[t[0]].append(t[1])
else:
list_t[t[0]].append(t[1])
q.append(start)
c_state[start] = (0, start)
visited[start] = True
while len(q) > 0:
cur = q.pop(len(q)-1)
res = (cur, c_state[cur][0], c_state[cur][1])
result.append(res)
if cur in list_t: #if reachable
for n in list_t[cur]:
if visited[n] is None:
q.insert(0, n)
visited[n] = True
c_state[n] = (c_state[cur][0]+1, cur)
#c_state has the information about the parent nodes.
return result, c_state
# Returns either a path as a list of reachable states if the target is
# reachable or False if the target isn't reachable.
def simple_machine(k, start, target):
# TODO: Implement part b.
#target is reach
#calculate ((a,b,c),(a+1,b+1,c+1)), one transition.
transitions = []
for a in range(0,k+1,1):
for b in range(0,k+1,1):
for c in range(0, k+1):
#a+1, b+1, c+1
if a <= k-1 and b <= k-1 and c <= k-1:
transitions.append(((a,b,c), (a+1, b+1, c+1)))
if a> 0 and b > 0 and c>0:
transitions.append(((a,b,c), (a-1,b-1,c-1)))
if a > 0 :
transitions.append(((a,b,c), (a-1, b,c)))
if a <= k-1:
transitions.append(((a,b,c), (a+1, b,c)))
# #print(transitions)
##print("trans'")
res, c_state= reachable_states_machine(start, transitions)
##print(res)
for item in res:
if target == item[0]:
path=[]
path.append(target)
key =target
while key != start:
key = c_state[key]
#print("KEY")
#print(key)
key = key[1] #find the predecessor
path.append(key)
##print(key)
path.reverse()
##print(path)
return path
##print("return flase")
return False
# Returns either False if the mutual exclusion property is satisfied or
# a minimum-length counterexample as a list of reachable states.
def mutual_exclusion_1():
transitions = []
for a in range(0, 4,1):
for b in range(0,4,1):
for c in range(1, 3, 1):
#a+1, b+1, c+1
if a is 0:
#(0,b,c) -> (1,b,1)
transitions.append(((a,b,c),(a+1, b, 1)))
if b is 0:
#(a,0,c) ->(a,1,2)
transitions.append(((a,b,c),(a, b+1, 2)))
if a is 1 and b is 0:
#(1,0,c) -> (3,0,c)
transitions.append(((a,b,c),(3, b, c)))
if a is 0 and b is 1:
# (0,1,c) -> (0,3,c)
transitions.append(((a,b,c),(0, 3, c)))
if a is 1 and b <3 and c is 2: #1,b, 2 -> 3,b,2
transitions.append(((a,b,c),(3, b, c)))
if a <3 and b is 1 and c is 1: #a,1, 1 -> a,3,1
transitions.append(((a,b,c),(a, 3, c)))
if a is 3: # 3, b, c- > 0,b,c
transitions.append(((a,b,c),(0, b, c)))
if b is 3: #a,3,c -> a,0,c
transitions.append(((a,b,c),(a, 0, c)))
#print(transitions)
res = reachable_states((0,0,1), transitions)
#print(res)
for item in res:
if item[0] == (3,3,1) or item[0] == (3,3,2):
if (3,3,1) in res:
key = (3,3,1)
else:
key= (3,3,2)
#print("Found")
table = {}
#finding shortest path
path=[]
for item in res:
table[item[0]] = item
#print(table)
path.append(key)
while key != (0,0,1):
key = table[key]
key = key[2] #find the predecessor
path.append(key)
#print(key)
path.reverse()
#print(path)
return path
else:
return False
# Returns either False if the mutual exclusion property is satisfied or
# a minimum-length counterexample as a list of reachable states.
def mutual_exclusion_2():
# TODO: Implement part d.
transitions = []
for a in range(0, 4,1):
for b in range(0,4,1):
for c in range(0, 3, 1):
#a+1, b+1, c+1
#print("inserting")
# #print((a,b,c))
if a is 0 and c is 0:
transitions.append(((a,b,c), (1,b,0)))
if b is 0 and c is 0:
transitions.append(((a,b,c), (a,1,0)))
if a is 1 :
transitions.append(((a,b,c), (2,b,1)))
if b is 1 :
transitions.append(((a,b,c), (a,2,2)))
if a is 2 and c != 1:
transitions.append(((a,b,c), (0,b,c)))
if b is 2 and c != 2:
transitions.append(((a,b,c), (a,0,c)))
if b is 2 and c is 2:
transitions.append(((a,b,c), (a, 3,c)))
if a is 2 and c is 1:
transitions.append(((a,b,c), (3,b,c)))
if a is 3:
transitions.append(((a,b,c), (0,b,0)))
if b is 3:
transitions.append(((a,b,c), (a,0,0)))
#print(transitions)
#print("TRANSITIONS")
res = reachable_states((0,0,0), transitions)
#print(res)
for item in res:
if item[0] == (3,3,1) or item[0] == (3,3,2) or item[0] ==(3,3,0):
#print("FOUND FOUND")
if (3,3,1) in res:
key = (3,3,1)
else:
key= (3,3,2)
#print("Found")
table = {}
#finding shortest path
path=[]
for item in res:
table[item[0]] = item
#print(table)
path.append(key)
while key != (0,0,0):
key = table[key]
key = key[2] #find the predecessor
path.append(key)
#print(key)
path.reverse()
#print(path)
return path
else:
return False