-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic_function_solver.py
More file actions
333 lines (280 loc) · 10.6 KB
/
logic_function_solver.py
File metadata and controls
333 lines (280 loc) · 10.6 KB
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
328
329
330
331
332
333
# coding: utf-8
"""
@Author: JC
"""
from texttable import Texttable
def minterm_to_binary(minterm, num_vars):
"""
Convert a minterm to its binary representation.
"""
binary = bin(minterm)[2:].zfill(num_vars)
return binary
def get_ones_count(binary):
"""
Return the number of '1' in a binary term.
"""
return binary.count('1')
def get_dashes_count(binary):
"""
Return the number of '-' in a binary term.
"""
return binary.count('-')
def combine_terms(terms, one_counter, minterms_set):
"""
Combine and group terms with one difference
"""
combined_terms = []
used_terms = []
terms_set = []
used_terms_set = []
for i in range(len(terms)):
for j in range(i + 1, len(terms)):
term1 = terms[i]
term2 = terms[j]
term1_set = minterms_set[i]
term2_set = minterms_set[j]
if get_ones_count(term1) != get_ones_count(term2) - 1: # Combines terms with only one '1' difference
continue
temp = 0
for k in range(len(term1)):
if term1[k] == "-" and term2[k] == "-": # only terms with same dash position and quantity will be considered
temp += 1
if temp != one_counter:
continue
combined_term = ''
for k in range(len(term1)):
if term1[k] == term2[k]:
combined_term += term1[k]
else:
combined_term += '-'
if get_dashes_count(combined_term) == (one_counter + 1): # the number of '-' must be equal to the generation of iterations
combined_terms.append(combined_term)
used_terms.append(term1)
used_terms.append(term2)
used_terms_set.append(tuple(term1_set))
used_terms_set.append(tuple(term2_set))
temp = list(tuple(term1_set) + tuple(term2_set))
temp.sort()
terms_set.append(tuple(temp))
if used_terms:
for term in terms:
if term not in used_terms:
combined_terms.append(term) # the term can't be combined with all terms will become PI
if used_terms_set:
for term in minterms_set:
if term not in used_terms_set:
terms_set.append(term)
if not combined_terms: # end of iteration and return a list of PI
return True, terms, minterms_set
return False, combined_terms, terms_set
def check_only(minterm, terms_set, current_set):
"""
Determine if a minterm is in other minterm groups.
"""
for term_set in terms_set:
if term_set == current_set:
continue
if minterm in term_set:
return False
return True
def remove_covered_by_epi(pi_terms_set, epi_terms_set, pi, dc):
"""
If a minterm is covered by EPI, remove it from its own PI group
"""
pi_terms = [i for i in pi_terms_set if i not in epi_terms_set]
epi_temp = []
epi_set_temp = []
perfect_set = []
for term_set in pi_terms:
set_temp = ()
for j in term_set:
if check_only(j, epi_terms_set, term_set):
if j not in dc:
set_temp += (j,)
if not set_temp == ():
perfect_set.append(term_set)
epi_temp.append(pi[pi_terms_set.index(term_set)])
epi_set_temp.append(set_temp)
return epi_temp, epi_set_temp, perfect_set
def find_prime_implicants(num_vars, minterms, minterms_set):
"""
Find the prime implicants using the tabulation method.
"""
terms = [minterm_to_binary(minterm, num_vars) for minterm in minterms]
# print(terms)
for i in range(num_vars):
done, terms, minterms_set = combine_terms(terms, i, minterms_set)
# print(terms)
# print(minterms_set)
if done:
break
return terms, minterms_set
def find_epi(terms_set, pi, dc):
"""
Find EPI
"""
epi_terms_set = []
epi_terms = []
for term_set in terms_set:
for j in term_set:
if check_only(j, terms_set, term_set):
if j not in dc: # exclude the don't care
epi_terms_set.append(term_set)
epi_terms.append(pi[terms_set.index(term_set)])
break
pi_terms_set = [i for i in terms_set if i not in epi_terms_set]
pi_terms = [i for i in pi if i not in epi_terms]
while True:
perfect_term, removed_epi_set, perfect_set = remove_covered_by_epi(pi_terms_set, epi_terms_set, pi_terms, dc)
max_len = -1
epi_term = ""
epi_set = ()
# most covered PI is an EPI
for term in removed_epi_set:
if len(term) > max_len:
max_len = len(term)
epi_set = perfect_set[removed_epi_set.index(term)]
epi_term = perfect_term[removed_epi_set.index(term)]
if not epi_term:
break
else:
epi_terms.append(epi_term)
epi_terms_set.append(epi_set)
# remove redundant term
for term_set in epi_terms_set:
count = 0
for j in term_set:
if not check_only(j, epi_terms_set, term_set):
count += 1
else:
if j in dc:
count += 1
if count == len(term_set):
epi_terms.pop(epi_terms_set.index(term_set))
epi_terms_set.remove(term_set)
return epi_terms, epi_terms_set
def print_table(num_vars, prime_implicants, pi_set, epi_terms, epi_set, MINTERM):
is_epi = ["^_^" if prime_implicants[i] in epi_terms else "" for i in range(len(prime_implicants))]
symbol_groups = []
plus = False
for term in prime_implicants:
symbol = ""
for j in range(num_vars):
if term[j] != '-':
if not MINTERM:
if plus:
symbol += " + "
else:
plus = True
symbol += chr(ord('A') + j)
if term[j] == '0':
if MINTERM:
symbol += "'"
if term[j] == '1':
if not MINTERM:
symbol += "'"
plus = False
symbol_groups.append(symbol)
table = Texttable()
table.set_cols_align(["c", "c", "c", "c"])
table.set_header_align(["c", "c", "c", "c"])
header = ["Prime Implicant", "Prime Implicant Group", "Is EPI", "Symbol(s)"]
table.add_row(header)
combined = list([prime_implicants[i], pi_set[i], is_epi[i], symbol_groups[i]] for i in range(len(prime_implicants)))
for i in combined:
table.add_row(i)
print(table.draw())
print()
symbol_groups = []
for term in epi_terms:
symbol = ""
for j in range(num_vars):
if term[j] != '-':
if not MINTERM:
if plus:
symbol += " + "
else:
plus = True
symbol += chr(ord('A') + j)
if term[j] == '0':
if MINTERM:
symbol += "'"
if term[j] == '1':
if not MINTERM:
symbol += "'"
plus = False
symbol_groups.append(symbol)
table = Texttable()
table.set_cols_align(["c", "c", "c"])
table.set_header_align(["c", "c", "c"])
table.header(["Essential Prime Implicant", "Essential Prime Implicant Group", "Symbol(s)"])
combined = list([epi_terms[i], epi_set[i], symbol_groups[i]] for i in range(len(epi_terms)))
for i in combined:
table.add_row(i)
print(table.draw())
print("F(", end="")
for i in range(num_vars):
print(chr(ord('A') + i), end="")
if i != num_vars - 1:
print(",", end="")
print("): ", end="")
plus = False
for i in symbol_groups:
if MINTERM:
if plus:
print(" + ", end="")
else:
plus = True
print(i, end="")
else:
print(f"({i})", end="")
print()
def main():
num_vars = int(input("Enter the number of variables: "))
minterms = list(map(int, input("Enter the minterms separated by spaces: ").split()))
dc = list(map(int, input("Enter the don't cares(otherwise leave it blank): ").split()))
"""
For testing purpose
"""
# num_vars = 5
# minterms = list(map(int, "0 4 8 9 10 11 12 13 15".split()))
# minterms = list(map(int, "0 1 2 3 4 6 7 11 12 15".split()))
# minterms = list(map(int, "0 1 2 4 5 6 8 9 10 14 15".split()))
# minterms = list(map(int, "0 1 2 4 5 9 11 12 15".split()))
# minterms = list(map(int, "0 1 2 4 5 9 10 11 12 15".split()))
# minterms = list(map(int, "0 1 2 4 5 7 8 9 11 12 15 19 20 22 26 27 28 30 34".split()))
# minterms = list(map(int, "1 3 6 8 9 29 30 31 33 37 38 40 41 43 44 47 48 51 55 57 59 60 61 62 63".split()))
# dc = [3, 14]
minterms_set = []
maxterms = []
maxterms_set = []
if dc:
for i in dc:
minterms.append(i)
maxterms.append(i)
minterms.sort()
minterms_set.extend(list((i,) for i in minterms))
maxterms.extend(list(i for i in range(2 ** num_vars) if i not in minterms)) # find maxterms from given minterms
maxterms.sort()
maxterms_set.extend(list((i,) for i in maxterms))
prime_implicants, pi_set = find_prime_implicants(num_vars, minterms, minterms_set)
prime_implicants = list(dict.fromkeys(prime_implicants)) # remove minterms duplicates
pi_set = list(dict.fromkeys(pi_set)) # remove minterms set duplicates
epi_terms, epi_set = find_epi(pi_set, prime_implicants, dc)
epi_terms = list(dict.fromkeys(epi_terms)) # remove epi terms duplicates
epi_set = list(dict.fromkeys(epi_set)) # remove epi set terms duplicates
print("\nThe SOP expression of the logic function:")
print_table(num_vars, prime_implicants, pi_set, epi_terms, epi_set, True)
print()
print("_"*76)
print()
print("The POS expression of the logic function:")
prime_implicants, pi_set = find_prime_implicants(num_vars, maxterms, maxterms_set)
prime_implicants = list(dict.fromkeys(prime_implicants)) # remove maxterms duplicates
pi_set = list(dict.fromkeys(pi_set)) # remove maxterms_set duplicates
epi_terms, epi_set = find_epi(pi_set, prime_implicants, dc)
epi_terms = list(dict.fromkeys(epi_terms)) # remove epi terms duplicates
epi_set = list(dict.fromkeys(epi_set)) # remove epi set terms duplicates
print_table(num_vars, prime_implicants, pi_set, epi_terms, epi_set, False)
if __name__ == "__main__":
main()