-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindpath.py
More file actions
242 lines (182 loc) · 6.52 KB
/
findpath.py
File metadata and controls
242 lines (182 loc) · 6.52 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
import numpy as np
from module import sinequal as si
import argparse
# Initialize parser
parser = argparse.ArgumentParser()
# Adding optional argument
parser.add_argument("-t", "--vectors")
parser.add_argument("-v",'--version', action='version', version='%(prog)s 2.0',help = 'show version')
parser.add_argument("-c",'--circuit',default= 'p', help='Circuit (c) or path (p)?')
# Read arguments from command line
args = parser.parse_args()
def FindPoints(list_vec): #Find point in graph
points = []
tem_point = [points.extend(list(vector)) for vector in list_vec]
points = list(set(points))
#print(points)
return sorted(points)
def Cre_Matrix(vectors, points): # Creating matrixes for vectors
# print(vectors)
# print(sorted(points))
matrixes = []
for vector in vectors:
pos_row = [0]*len(points)
neg_row = [0]*len(points)
pos_index = points.index(vector[1])
neg_index = points.index(vector[0])
pos_row[pos_index] += 1
neg_row[neg_index] += 1
matrixes.append(np.array([pos_row,neg_row]))
# print(matrixes)
return matrixes
def PrepareCo(matrixes): # Prepare coeffcient condition matrix
# print(matrixes)
flatten_matrix = []
for array in matrixes:
lis = array.tolist()
tem = lis[0]+lis[1]
flatten_matrix.append(tem)
# print(flatten_matrix)
# print(len(flatten_matrix))
final_matrix = []
for k in range(len(flatten_matrix[0])):
# print(k)
tem_row = []
for i in range(len(flatten_matrix)):
# print(-flatten_matrix[i][k])
tem_row.append(-flatten_matrix[i][k])
#print(tem_row)
tem_row.append(1)
final_matrix.append(tem_row)
# print(final_matrix)
return np.array(final_matrix)
def SolveMatrix(list_vec, points, matrixes, c_value): # Intializing matrix for solving
sys_matrix = np.empty((0,len(list_vec)+1), int)
#print(sys_matrix)
for i in range(len(list_vec)): # Add postitive condition and lesser than 1
tem_row = [0]*(len(list_vec)+1)
tem_row[i] = 1
#print(tem_row)
less_row = [0]*(len(list_vec)+1)
less_row[i] = -1
less_row[-1] = 1
#print(less_row)
sys_matrix = np.append(sys_matrix,np.array([tem_row,less_row]),axis=0)
#print(sys_matrix)
sum_row = [1]*(len(list_vec)+1) # Add sum condition equal number of points
sum_row[-1] = -len(points)+c_value
neg_sum_row = [-1]*(len(list_vec)+1)
neg_sum_row[-1] = len(points)-c_value
sys_matrix = np.append(sys_matrix,np.array([sum_row,neg_sum_row]),axis=0)
#print(sys_matrix)
#print(matrixes)
co_matrix = PrepareCo(matrixes)
# print(co_matrix)
total_matrix = np.append(sys_matrix,co_matrix, axis = 0)
#print(total_matrix)
return total_matrix
def CreatNguon(total_matrix,list_vec):
init_nguon = [0]*len(total_matrix)
init_nguon[len(list_vec)*2] = 1
init_nguon[len(list_vec)*2+1] = 1
#print(init_nguon)
return init_nguon
def Solve_Coeff(list_vec, points, matrixes, c_value): # Solve vector coefficients
het_matrix = SolveMatrix(list_vec, points, matrixes, c_value)
# print(het_matrix)
nguon_goc = CreatNguon(het_matrix,list_vec)
# print(nguon_goc)
order = list(range(len(list_vec)))
#print(order)
cuctri = [0]*len(list_vec)
#print(cuctri)
return si.SolveByMatrix(het_matrix, order, nguon_goc, cuctri)
def PhiMatrix(coeffs,points,matrixes):
rs_matrix = []
for coeff in coeffs:
matrix_i = np.array([[0]*len(points),[0]*len(points)])
for matrix, co in zip(matrixes,coeff):
matrix_i += matrix*co
rs_matrix.append(matrix_i)
return rs_matrix
def ChangeVec(took_vectors,start_char,path):
for vector in took_vectors:
if vector[0] == start_char:
path += vector[-1]
took_vectors.remove(vector)
# print(path)
# print(took_vectors)
return path, took_vectors
def FindPath(coe, ma, points):
took_vectors = [list_vec[i] for i in range(len(list_vec)) if coe[i] == 1]
# print(took_vectors)
start_index = list(ma[0]).index(0)
start_char = points[start_index]
# print(start_char)
end_index = list(ma[1]).index(0)
end_char = points[end_index]
# print(end_char)
path = start_char
path, took_vectors = ChangeVec(took_vectors,start_char,path)
# print(path)
# print(took_vectors)
while path[-1] != end_char:
path, took_vectors = ChangeVec(took_vectors,path[-1],path)
# print(path)
return path
def ColectPath(coeffs,rs_matrix,points):
final_paths = []
lene = len(points)
for coe, ma in zip(coeffs,rs_matrix):
path = FindPath(coe, ma, points)
# print(path)
if len(path) == lene:
final_paths.append(path)
return final_paths
def FindPathC(coe, points):
took_vectors = [list_vec[i] for i in range(len(list_vec)) if coe[i] == 1]
# print(took_vectors)
start_char = points[0]
# print(start_char)
end_char = points[0]
# print(end_char)
path = start_char
path, took_vectors = ChangeVec(took_vectors,start_char,path)
# print(path)
# print(took_vectors)
while path[-1] != end_char:
path, took_vectors = ChangeVec(took_vectors,path[-1],path)
# print(path)
return path
def ColectPathC(coeffs,points):
final_paths = []
lene = len(points)+1
for coe in coeffs:
path = FindPathC(coe, points)
if len(path) == lene:
final_paths.append(path)
return final_paths
def PathThrough(list_vec,c_value):
points = FindPoints(list_vec)
# print(points)
matrixes = Cre_Matrix(list_vec, points)
# print(matrixes)
coeffs = Solve_Coeff(list_vec,points,matrixes,c_value)
# print(coeffs)
if c_value == 1:
rs_matrix = PhiMatrix(coeffs,points,matrixes)
# print(rs_matrix)
path = ColectPath(coeffs,rs_matrix,points)
# print(path)
elif c_value == 0:
path = ColectPathC(coeffs,points)
return path
# list_vec = ['BA', 'AC', 'CB', 'BD', 'DC', 'CE', 'DE', 'DF', 'EF']
# list_vec = ['AC','CB','AB','BD','DC']
list_vec = args.vectors.split(',')
if args.circuit == 'p':
c_value = 1
elif args.circuit == 'c':
c_value = 0
path = PathThrough(list_vec, c_value)
print(path)