-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab 03.py
executable file
·327 lines (247 loc) · 7.16 KB
/
Lab 03.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# -*- coding: utf-8 -*-
import sys
from types import *
from itertools import *
map_path = [ 'X1','X2','X3','X4','X5','X6','X7','F1', 'F2', 'F3', 'F4','F5' ]
## Logic functions
class Logic:
def AND(self,*args):
ret = 1
for arg in args:
ret = ret & arg
return bool(ret)
def OR(self,*args):
ret = 0
for arg in args:
ret = ret | arg
return bool(ret)
def NOT(self,arg):
return not arg
def XOR(self,arg1,arg2):
return bool(arg1 ^ arg2)
def NOR(self,*args):
return not self.OR(*args)
def NAND(self,*args):
return not self.AND(*args)
def Call(self,name,*args):
return getattr(self,name)(*args)
def Func(self,name):
return getattr(self,name)
## Blocks
class Block:
def __init__(self,name,vars,func,*inp):
self._name = name
self._vars = vars
self._logic = Logic()
self.func_name = func
self.func = self._logic.Func(func.upper())
if (len(inp) == 1):
self.inp = inp[0]
else:
self.inp = inp
def __str__(self):
ret = self.func_name +" ( "
if type(self.inp) is tuple:
for num,item in enumerate(self.inp):
ret += str(item)
if num != len(self.inp) - 1:
ret += ", "
else:
ret += str(self.inp)
ret += " )"
return ret
def __getattr__(self,name):
if (name == "value"):
if type(self.inp) is tuple:
ret = []
for item in self.inp:
if type(item) is Block:
ret.append(item.value)
elif type(item) is str:
ret.append(self._vars[item])
else:
ret.append(item)
return self.func(*tuple(ret))
elif type(self.inp) is Block:
return self.func(self.inp.value)
elif type(self.inp) is str:
return self.func(self._vars[self.inp])
else:
return self.func(self.inp)
elif (name == "type"):
return self.func_name
elif (name == "name"):
return self._name
else:
super().__getattr__(name)
##
def FuncInit(inputX,BadPin,BadStuckAt):
# Formula description
Field = {}
Field["F1"] = BadStuckAt if BadPin == "F1" else Block("F1",inputX,"xor",'x1','x2')
Field["F2"] = BadStuckAt if BadPin == "F2" else Block("F2",inputX,"not",'x3')
Field["F3"] = BadStuckAt if BadPin == "F3" else Block("F3",inputX,"nor",'x5','x6')
Field["F4"] = BadStuckAt if BadPin == "F4" else Block("F4",inputX,"nand",'x4','x7',Field["F3"])
Field["F5"] = BadStuckAt if BadPin == "F5" else Block("F5",inputX,"or",Field["F2"],Field["F4"])
Field["F6"] = BadStuckAt if BadPin == "F6" else Block("F6",inputX,"nor",Field["F1"],Field["F5"])
Out = Field["F6"]
#print ("Formula: Y = "+str(Out))
return Out,Field
def FuncCalc(inputX,BadPin="",BadStuckAt=-1):
LastCall,Field = FuncInit(inputX,BadPin,BadStuckAt)
return LastCall.value
print("Error: {0} ({1}) stuck at {2}".format(test_field,str(Field[test_field]),error_bind_to))
def FuncCallWithCount(inputX,BadPin="",BadStuckAt=-1):
LastCall,Field = FuncInit(inputX,BadPin,BadStuckAt)
vals = [ int(Field[inp].value) for inp in sorted(Field.keys()) ]
inps = [ inputX[inp] for inp in sorted(inputX.keys()) ]
ret = inps + vals
return ret
def main():
# Generate all input variants
X_variants = []
perm = list(range(128))
i =0
for variant in perm:
var_X = {}
# Gen inputs
inp = '{0:07b}'.format(variant)
var_pos = 1
for pos in inp:
if pos == "0":
val = 0
else:
val = 1
var_X["x"+str(var_pos)] = val
var_pos += 1
X_variants.append(var_X)
## Test them out!
# Generate Test Cases
Test_Cases = {}
for block in map_path:
BadPin = block
for stuck in [0,1]:
BadStuckAt = stuck
normal_Y = []
bad_Y = []
items = []
for var_X in X_variants:
Y = FuncCalc(var_X)
normal_Y.append(Y)
# Filtering xN for BadPins
filtered_var_X = {}
filtered_var_X["x1"] = BadStuckAt if BadPin == "X1" else var_X["x1"]
filtered_var_X["x2"] = BadStuckAt if BadPin == "X2" else var_X["x2"]
filtered_var_X["x3"] = BadStuckAt if BadPin == "X3" else var_X["x3"]
filtered_var_X["x4"] = BadStuckAt if BadPin == "X4" else var_X["x4"]
filtered_var_X["x5"] = BadStuckAt if BadPin == "X5" else var_X["x5"]
filtered_var_X["x6"] = BadStuckAt if BadPin == "X6" else var_X["x6"]
filtered_var_X["x7"] = BadStuckAt if BadPin == "X7" else var_X["x7"]
bY = FuncCalc(filtered_var_X,BadPin,BadStuckAt)
bad_Y.append(bY)
for pos in range(len(normal_Y)):
val1 = normal_Y[pos]
val2 = bad_Y[pos]
if val1 != val2:
items.append(X_variants[pos])
for i in items:
test = ""
for var in ["x1","x2","x3","x4","x5","x6","x7"]:
test += str(i[var])
if var != "x7": test+=" "
if test in Test_Cases.keys():
if BadStuckAt == 0:
if BadPin not in Test_Cases[test][1]:
Test_Cases[test][1].append(BadPin)
else:
if BadPin not in Test_Cases[test][0]:
Test_Cases[test][0].append(BadPin)
else:
S0 = []
S1 = []
if BadStuckAt == 0:
S0.append(BadPin)
else:
S1.append(BadPin)
Test_Cases[test] = [S1,S0]
# Find minimum test cases
# Sort Lists by count of switches
MinMaxList = {}
for case in Test_Cases:
num = len(Test_Cases[case][0]) + len(Test_Cases[case][1])
if num in MinMaxList.keys():
MinMaxList[num].append(case)
else:
MinMaxList[num] = [case]
UncoveredFailures0 = map_path[:]
UncoveredFailures1 = map_path[:]
ReversedOrder = list(reversed(sorted(MinMaxList.keys())))
FinalTests = []
flag = False
for n in ReversedOrder:
cases = MinMaxList[n]
if flag == True :
break
for case in cases:
S1 = Test_Cases[case][0]
S0 = Test_Cases[case][1]
covered_pins = 0
for cs1 in S1:
if cs1 in UncoveredFailures1:
covered_pins += 1
UncoveredFailures1.remove(cs1)
for cs0 in S0:
if cs0 in UncoveredFailures0:
covered_pins += 1
UncoveredFailures0.remove(cs0)
if covered_pins != 0 :
FinalTests.append(case)
if len(UncoveredFailures0) == 0 and len(UncoveredFailures0) == 1:
flag = True
break
print ("Covering tests: ")
for test in FinalTests:
print ( test, Test_Cases[test] )
print(" ")
# Calc function switching
calls = []
for test in FinalTests:
# Generate test input
vals = test.split(" ")
var_x = {}
n = 1
for val in vals:
var_x["x"+str(n)] = int(val)
n += 1
calls.append(FuncCallWithCount(var_x))
# Calculate costs
perm_list = {}
n = 0
for perm in permutations(calls):
cost = 0
for i in range(0,len(perm)-1):
st1 = perm[i]
st2 = perm[i+1]
for j in range(0,len(st1)):
if st1[j] != st2[j]:
cost += 1
if cost in perm_list:
perm_list[cost].append(perm)
else:
perm_list[cost] = [perm]
#
optimal_list = list(sorted(perm_list.keys()))
best_sw = optimal_list[0]
print("Switches : ",best_sw)
print("First three alternatives: ")
#print (perm_list[best_sw])
n = 0
for perm in perm_list[best_sw]:
n += 1
if n==4: break
for item in perm:
print (item[:7])
print ("=========================")
##################################
if __name__=="__main__":
main()