Created
July 10, 2017 19:40
-
-
Save back-seat-driver/c27fd7e0a230ffeea187f7095aaa5025 to your computer and use it in GitHub Desktop.
Quine-McCluskey Algo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import itertools | |
from GREY_CODE import G_C | |
def sb(size,num): | |
to_return=[] | |
for i in range(num): | |
to_return.append(str_build(list(numpy.random.randint(2,size=(size,))))) | |
return(to_return) | |
def ls(nest_list): | |
to_return=[] | |
for i in range(len(nest_list)): | |
insert='' | |
for x in range(len(nest_list[i])): | |
insert+=str(nest_list[i][x]) | |
to_return.append(insert) | |
return(to_return) | |
def test_vector(n): | |
to_return=[] | |
to_filter=ls(G_C(n)) | |
filter_index=sb(len(G_C(n)),1)[0] | |
for i in range(len(to_filter)): | |
if filter_index[i]=='1': | |
to_return.append(to_filter[i]) | |
return(to_return) | |
def U_V(LIST): | |
to_iter = len(LIST) | |
total_items = [] | |
for i in range(to_iter): | |
insert = LIST[i] | |
total_items.append(insert) | |
unique_items = [] | |
for i in range(len(total_items)): | |
count = 0 | |
for x in range(len(unique_items)): | |
if total_items[i]==unique_items[x]: | |
count+=1 | |
if count==0: | |
unique_items.append(total_items[i]) | |
return(unique_items) | |
def group_set(list_strings): | |
to_return=[] | |
for i in range(0,len(list_strings[0])+1): | |
insert=[] | |
for x in range(len(list_strings)): | |
if list_strings[x].count('1')==i: | |
insert.append(list_strings[x]) | |
to_return.append(insert) | |
return(to_return) | |
def diff_return(str_0,str_1): | |
to_return='' | |
for i in range(len(str_0)): | |
if str_0[i]==str_1[i]: | |
to_return+=str_0[i] | |
else: | |
to_return+='*' | |
return(to_return) | |
def hit_one(str_0,str_1): | |
c=0 | |
for i in range(len(str_0)): | |
if str_0[i]!=str_1[i]: | |
c+=1 | |
if c==1: | |
return(True) | |
return(False) | |
def index(bin_nested_list): | |
to_return=[] | |
for i in range(len(bin_nested_list)): | |
insert=[] | |
for x in range(len(bin_nested_list[i])): | |
insert.append(int(bin_nested_list[i][x],2)) | |
to_return.append(insert) | |
return(to_return) | |
def chain(nest_list): | |
#Fuzzy | |
to_return=[] | |
for i in range(len(nest_list)): | |
try: | |
to_return.append(sorted(list(itertools.chain(*nest_list[i])))) | |
except TypeError: | |
to_return.append(nest_list[i]) | |
return(to_return) | |
def prime_imp(set_0,set_1): | |
c=0 | |
for i in range(len(set_0)): | |
for x in range(len(set_1)): | |
if set_0[i]==set_1[x]: | |
c+=1 | |
if c==len(set_0): | |
return(True) | |
def prime_return(set_main): | |
to_return=[] | |
for i in range(len(set_main)): | |
value=None | |
for x in range(i+1,len(set_main)): | |
if prime_imp(set_main[i],set_main[x])==True: | |
value=True | |
if value==None: | |
to_return.append(set_main[i]) | |
return(to_return) | |
def list_concat(nest_list): | |
to_return=[] | |
for i in range(len(nest_list)): | |
to_return+=nest_list[i] | |
return(to_return) | |
def str_build(a_list): | |
to_return='' | |
for i in range(len(a_list)): | |
to_return+=str(a_list[i]) | |
return(to_return) | |
def bin_int(a_list): | |
to_return=[] | |
for i in range(len(a_list)): | |
to_return.append(int(a_list[i],2)) | |
return(to_return) | |
def sub_filter(a_list): | |
if len(U_V(a_list))==len(a_list): | |
return(True) | |
return(False) | |
def bug_remove_0(nest_list): | |
to_return=[] | |
for i in range(len(nest_list)): | |
insert=[] | |
for x in range(len(nest_list[i])): | |
if sub_filter(nest_list[i][x])==True: | |
insert.append(nest_list[i][x]) | |
if insert!=[]: | |
to_return.append(insert) | |
return(to_return) | |
def bug_remove_1(value_set,nest_list): | |
to_return=[] | |
to_search=list_concat(nest_list) | |
def is_in(value,a_list): | |
for i in range(len(a_list)): | |
if value==a_list[i]: | |
return(True) | |
return(False) | |
for i in range(len(value_set)): | |
if is_in(value_set[i],to_search)==False: | |
to_return.append(value_set[i]) | |
return(to_return) | |
def bug_remove_2(list_int): | |
to_return=[] | |
for i in range(len(list_int)): | |
to_return+=[[list_int[i]]] | |
return(to_return) | |
def prime_set(list_of_strings): | |
list_of_list = group_set(list_of_strings) | |
list_index = index(list_of_list) | |
to_return_0=[] | |
to_return_1=[] | |
#Reduction in 3 iterations!!!! | |
for c in range(3): | |
for i in range(len(list_of_list)-1): | |
insert_0=[] | |
insert_1=[] | |
for x in range(len(list_of_list[i])): | |
for z in range(len(list_of_list[i+1])): | |
if hit_one(list_of_list[i][x],list_of_list[i+1][z])==True: | |
insert_0.append(diff_return(list_of_list[i][x],list_of_list[i+1][z])) | |
insert_1.append([list_index[i][x],list_index[i+1][z]]) | |
if insert_0!=[] and insert_1!=[]: | |
to_return_0.append(U_V(insert_0)) | |
to_return_1.append(U_V(chain(insert_1))) | |
list_of_list=to_return_0 | |
list_index=to_return_1 | |
to_return_0=U_V(to_return_0) | |
to_return_1=bug_remove_0(U_V(to_return_1)) | |
return(to_return_0,to_return_1) | |
def bin_reduce(list_of_strings): | |
if bug_remove_1(bin_int(list_of_strings),list_concat(prime_set(list_of_strings)[1]))==[]: | |
prime_value=prime_return(list_concat(prime_set(list_of_strings)[1])) | |
bin_set=bin_int(list_of_strings) | |
for i in range(1,len(prime_value)+1): | |
to_check=list(itertools.combinations(prime_value,i)) | |
for x in range(len(to_check)): | |
if prime_imp(bin_set,U_V(list_concat(to_check[x])))==True: | |
set_0=list_concat(prime_set(list_of_strings)[0]) | |
set_1=list_concat(prime_set(list_of_strings)[1]) | |
to_return=[] | |
for z in range(len(to_check[x])): | |
to_return.append(set_0[set_1.index(to_check[x][z])]) | |
return(to_return) | |
else: | |
to_iter_0=bug_remove_1(bin_int(list_of_strings),list_concat(prime_set(list_of_strings)[1])) | |
to_iter_1=bin_int(list_of_strings) | |
front_1=bug_remove_2(bug_remove_1(bin_int(list_of_strings),list_concat(prime_set(list_of_strings)[1]))) | |
front_0=[] | |
for i in range(len(to_iter_0)): | |
for x in range(len(to_iter_1)): | |
if to_iter_0[i]==to_iter_1[x]: | |
front_0.append(list_of_strings[x]) | |
prime_value=prime_return(front_1+list_concat(prime_set(list_of_strings)[1])) | |
bin_set=bin_int(list_of_strings) | |
for i in range(1,len(prime_value)+1): | |
to_check=list(itertools.combinations(prime_value,i)) | |
for x in range(len(to_check)): | |
if prime_imp(bin_set,U_V(list_concat(to_check[x])))==True: | |
set_0=front_0+list_concat(prime_set(list_of_strings)[0]) | |
set_1=front_1+list_concat(prime_set(list_of_strings)[1]) | |
to_return=[] | |
for z in range(len(to_check[x])): | |
to_return.append(set_0[set_1.index(to_check[x][z])]) | |
return(to_return) | |
def math_convert(nest_list): | |
to_return=[] | |
set_0=['A','B','C','D','E','F','G','H','I'] | |
set_1=['a','b','c','d','e','f','g','h','i'] | |
for i in range(len(nest_list)): | |
insert='' | |
for x in range(len(nest_list[i])): | |
if nest_list[i][x]!='*': | |
if nest_list[i][x]=='1': | |
insert+=set_0[x] | |
if nest_list[i][x]=='0': | |
insert+=set_1[x] | |
to_return.append(insert) | |
return(to_return) | |
def cross_check(n): | |
value=test_vector(n) | |
print(sorted(bin_int(value))) | |
print(bin_reduce(value)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment