-
Notifications
You must be signed in to change notification settings - Fork 2
/
KritiCXLogicDeducer.py
169 lines (132 loc) · 6.85 KB
/
KritiCXLogicDeducer.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
import numpy as np
def input_locations(matrix2x2):
input_matrix = matrix2x2[:,:2]
types_matrix = (input_matrix/10).astype(int)
return (types_matrix == 8) | (types_matrix == 9) | (types_matrix == 10)
def unique_init_inputs(matrix2x2):
input_loc = input_locations(matrix2x2)
total_unique_inputs = np.any((matrix2x2/10).astype(int) == 8).astype(int) + np.any((matrix2x2/10).astype(int) == 9).astype(int) + np.any((matrix2x2/10).astype(int) == 10).astype(int)
mask_red = (matrix2x2/10).astype(int) == 8
mask_green = (matrix2x2/10).astype(int) == 9
mask_blue = (matrix2x2/10).astype(int) == 10
return total_unique_inputs, [mask_red,mask_green,mask_blue], [np.any(mask_red),np.any(mask_green),np.any(mask_blue)]
def all_possible_inp_maker(matrix2x2):
total_inps,_,_ = unique_init_inputs(matrix2x2) #This was to find the no. of all the inputs which WAS WRONG, Now it's correct...
arr = np.zeros(total_inps)
all_bin_list = []
for i in range(np.power(2,total_inps)):
bin_string = bin(i)[2:].rjust(total_inps,'0')
all_bin_list.append([i for i in bin_string])
return np.array(all_bin_list).astype(int)
def input_value_putter(matrix2x2,vector_to_put):
unique_inputs, masks, inp_types = unique_init_inputs(matrix2x2)
new_matrix = np.zeros_like(matrix2x2)
for i in range(unique_inputs):
new_matrix[masks[i]] = vector_to_put[i]
return new_matrix,inp_types
def output_generator_4_inputs(matrix2x2, binary_vector):
#Where's binary_vector???? Tick
#Initial Evaluation directly from inputs
matrix_4_op,inp_types = input_value_putter(matrix2x2,binary_vector)
out_mat = matrix_4_op
input_loc = input_locations(matrix2x2)
cache = np.zeros_like(input_loc).astype(bool)
#INITIALIZE THE INPUT FOR THE NOT GATES
for i,val in np.ndenumerate(matrix2x2[:,2]):
if int(val/10) == 7:
if input_loc[i,0] == True:
input_loc[i,1] = True
for i in range(matrix_4_op.shape[0]):
if input_loc[i,0]:
if int(matrix2x2[i,2]/10) == 1:
out_mat[i,2] = out_mat[i,0] and out_mat[i,1]
elif int(matrix2x2[i,2]/10) == 2:
out_mat[i,2] = out_mat[i,0] or out_mat[i,1]
elif int(matrix2x2[i,2]/10) == 3:
out_mat[i,2] = not(out_mat[i,0] and out_mat[i,1])
elif int(matrix2x2[i,2]/10) == 4:
out_mat[i,2] = not(out_mat[i,0] or out_mat[i,1])
elif int(matrix2x2[i,2]/10) == 5:
out_mat[i,2] = out_mat[i,0] ^ out_mat[i,1]
elif int(matrix2x2[i,2]/10) == 6:
out_mat[i,2] = not(out_mat[i,0] ^ out_mat[i,1])
elif int(matrix2x2[i,2]/10) == 7:
out_mat[i,2] = not(out_mat[i,0])
#checking 111, i.e output
#elif int(matrix_4_op[i,2]) == 111:
# output = matrix_4_op[i,2]
count = 0
#print(input_loc,out_mat)
while True:
count+=1
#PUT OUTPUT TO NEXT INPUTS
#inserting the values just calculated, in out_mat to the specefic locations in the two input columns::--
for i,outs in np.ndenumerate(matrix2x2[:,2]):
if input_loc[i,0] & input_loc[i,1]:
out_mat[:,:2][matrix2x2[:,:2] == outs] = out_mat[:,2][matrix2x2[:,2] == outs]
#Check the fucking pairs and initialize the input_loc again
#SIMILARILY, UPDATE THE input_loc BOOL TABLE
pairs_loc = input_loc[:,0] & input_loc[:,1]
#Long Term Memory
input_loc = np.zeros_like(input_loc).astype(bool)
for i in range(cache.shape[0]):
if (((cache[i,0] ==True) & (cache[i,1]==False))) :#| ((cache[i,0] ==True) & (cache[i,1]==False))):
input_loc[i,0] = True
elif (((cache[i,0] ==False) & (cache[i,1]==True))):
input_loc[i,1] = True
for i,val in np.ndenumerate(pairs_loc):
if val:
input_loc[matrix2x2[:,:2] == matrix2x2[i,2]] = True
#Checks whether the next input is for NOT gate, AND IF IT IS, PUT A TRUE ON IT'S NEXT ROW TOO (I know it's an inefficient implementation!!)
for i,val in np.ndenumerate(matrix2x2[:,2]):
if int(val/10) == 7:
if input_loc[i,0] == True:
input_loc[i,1] = True
#LOOP BREAKING CONDITION
#Below Condition is for the breaking from the loop when the corresponding row in which the flow is have output as '111'
if np.any(matrix2x2[input_loc[:,0] == True][:,2] == 111):
output = out_mat[:,0][matrix2x2[:,2] == 111]
break
#CALCULATE NEXT OUTPUTS
#Now, the new pairs are made (if there's any) and now we need to calculate the new outputs to put in out_mat
for i in range(matrix_4_op.shape[0]):
if input_loc[i,0] & input_loc[i,1]:
if int(matrix2x2[i,2]/10) == 1:
out_mat[i,2] = out_mat[i,0] and out_mat[i,1]
elif int(matrix2x2[i,2]/10) == 2:
out_mat[i,2] = out_mat[i,0] or out_mat[i,1]
elif int(matrix2x2[i,2]/10) == 3:
out_mat[i,2] = not(out_mat[i,0] and out_mat[i,1])
elif int(matrix2x2[i,2]/10) == 4:
out_mat[i,2] = not(out_mat[i,0] or out_mat[i,1])
elif int(matrix2x2[i,2]/10) == 5:
out_mat[i,2] = out_mat[i,0] ^ out_mat[i,1]
elif int(matrix2x2[i,2]/10) == 6:
out_mat[i,2] = not(out_mat[i,0] ^ out_mat[i,1])
elif int(matrix2x2[i,2]/10) == 7:
out_mat[i,2] = not(out_mat[i,0])
#verb_count+=1
#print('While Loop number {}'.format(verb_count))
#print(input_loc,out_mat)
if count==1:
cache = input_loc
return output,out_mat,input_loc,inp_types
#After this the input_pair_matrix[:,:2] will be filled by 1's where the pairs are there.
#After this the input_pair_matrix[:,:2] will be filled by 1's where the pairs are there.
def truth_table_generator(matrix2x2):
binary_vectorS = all_possible_inp_maker(matrix2x2)
TT = []
for i in range(binary_vectorS.shape[0]):
output, _ , _ , inp_types= output_generator_4_inputs(matrix2x2,binary_vectorS[i])
TT.append([binary_vectorS[i],output])
print("Outputs:")
l = []
for i in range(3):
if ((inp_types[i] == True) and (i==0)):
l.append("Red")
elif ((inp_types[i] == True) and (i==1)):
l.append("Green")
elif ((inp_types[i] == True) and (i==2)):
l.append("Blue")
print(l)
return np.array(TT)