-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchMethods.py
More file actions
434 lines (334 loc) · 21.1 KB
/
searchMethods.py
File metadata and controls
434 lines (334 loc) · 21.1 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from graphBuilder import *
from graphDiagnostics import *
from graphml2nwx import *
from smallWorldMeasures import *
from physicsPositions import *
from allPaths import *
''' searchMethods--------------------------------------------------------------------------------------------------
****************************** Last Updated: 10 April 2024 ******************************
Methods:
1) breadth_first_child: inputs adjacency matrix, names of nodes, start node --> outputs tree, adjacency matrix,
dictionary of nodes and generations, list of 'effect' nodes, list of 'mode' nodes
2) breadth_first_parent: inputs adjacency matrix, names of nodes, start node --> outputs tree, adjacency matrix,
dictionary of nodes and generations, list of 'effect' nodes, list of 'mode' nodes
3) draw_bfs_multipartite: nputs adjacency matrix, list of node names, start node --> plots breadth first tree.
Nothing is returned.
4) breadth_first_multi: adjacency matrix, list of node names, starting node, type of breadth first search -->
outputs tree, adjacency matrix, dictionary of nodes, arrays for effects and modes, and array of nodes
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
breadth_first_child Documentation
-----------------------------------
This method takes in an adjacency matrix, list of node names, and an integer indicating te starting node. We
traverse through the graph. For each generation, starting from the source node, we find and add all the children
of the nodes in this generation, unless the node has aldready been placed in the tree. As such, we create a tree.
This method returns this tree, the tree's adjacency matrix, a dictionary of nodes, and arrays for effects and modes.
--> Note: a tree is a graph with no cycles or loops. It does not have to be directed, but it is acyclic.'''
def breadth_first_child(arr, nodeNames, source):
# Initialize a new digraph for the tree we are going to make and add all the nodes (we will determine edges later)
G = nx.DiGraph()
# Initialize effects and modes arrays for plotting purposes
effects = []
modes = []
# Add the source node to the graph
G.add_node(nodeNames[source - 1])
# Determine if the source node is an effect or a mode, and add it to the correct array
if source < 27: effects.append(nodeNames[source - 1])
elif source <= 47: modes.append(nodeNames[source - 1])
'''if source < 49: modes.append(nodeNames[source - 1])
else: effects.append(nodeNames[source - 1])'''
# Note that we are changing the numerical names of the nodes so that the values of nodes are from 1 to 47 rather
# than from 0 to 46. This is so we can find all the children of the nodes. Using the 0-46 range poses a problem
# since we do not know if a zero indicates no relation or that the 0 node is a child of the node we are looking at.
# We binarize the adjacency matrix so that relationships either exist or not (rather than having an intensity)
adj = make_binary(arr, 0.5).astype(int)
# Create an array of booleans such that the ith entry indicates if the node has already been visited. Nodes
# that have been visited are set to TRUE.
nodeList = np.reshape(np.repeat(False, arr.shape[0]), (arr.shape[0], 1))
# Create a diagonal matrix such that the value of the i,i entry is 1+1, referencing the node with name i+1
nodes = diagonal_nodes(adj)
# print("nodes", nodes.shape)
# Visit the source node
nodeList[source-1] = True
# Add the source node to the queue. We use a queue to iterate through the nodes. This allows us to search through
# the graph generation by generation rather than following one specific path at a time.
queue = [[source, 0]]
# Initialize a dictionary that will tell us what generation each node is in. Label the generation of the source node
# as zero.
gens = {nodeNames[source-1]: {"layer": 0}}
# Continue while there are still nodes in the queue (reference algorithms for a breadth first search for more info)
while len(queue) > 0:
# Set the node we are looking at equal to the node next in line in the queue
current = queue[0]
# Determine the children of the current node.
# Proof/Reason Why This Works --> Using the unweighted adjacency matrix, we can find the children
# by multiplying the row of the adjacency matrix corresponding to the current node by the diagonal matrix of
# node names. If the jth node is a child of the current node, then there is a 1 in the j-1 column of the
# current row. When multiplied by the diagonal matrix, this 1 will be multiplied by the j-1 column of the.
# since the only non zero entry in the j-1 column of the diagonal matrix is j (located on the diagonal), then
# the entry in the j-1 column of the returned vector will be j. However, if the jth node is not a child of
# the current node, then there is a zero in the j-1 column of the current row of the adjacency matrix. When
# multiplied by the diagonal matrix, this zero is multiplied by every entry in the j-1 column of the diagonal
# matrix. So, the j-1 column of the returned vector is zero.
children_bool = adj[current[0]-1] @ nodes # vector of zeros and child names (numerical names)
children = children_bool[np.nonzero(children_bool)] #list of just the child names (numerical names)
for child in children: # For every child of the current node that was found above...
# Check if the child has been visited or if it is in the same generation as child. If either not visited or
# in the same generation, continue with the following:
if nodeList[child - 1] == False or gens[nodeNames[child - 1]]['layer'] > current[1]:
# Add the child to the graph
G.add_node(nodeNames[child-1])
# Determine if the child is an effect or mode and add it to the correct array
if child < 27: effects.append(nodeNames[child - 1])
elif child <= 47: modes.append(nodeNames[child - 1])
'''if child < 49: modes.append(nodeNames[child - 1])
else: effects.append(nodeNames[child - 1])'''
# Add an edge between the current node and child in the tree we are building
G.add_edge(nodeNames[current[0]-1], nodeNames[child-1])
queue.append([child, current[1]+1]) # Append the child to the queue
nodeList[child - 1] = True # Change the status of the child to say we have visited it
gens.update({nodeNames[child-1]: {"layer": current[1]+1}}) # Add child to dictionary of nodes
# Remove the current node from the queue
queue = queue[1:]
# Return the tree we made, its adjacency matrix, the dictionary of nodes, the effects array, and the modes arrat
return G, nx.to_numpy_array(G), gens, effects, modes
''' breadth_first_parent Documentation
---------------------------------------
This method takes in an adjacency matrix, list of node names, and an integer indicating te starting node. We
traverse through the graph. For each generation, starting from the source node, we find and add all the parents
of the nodes in this generation, unless the node has aldready been placed in the tree. As such, we create a tree.
This method returns this tree, the tree's adjacency matrix, a dictionary of nodes, and arrays for effects and modes.'''
def breadth_first_parent(arr, nodeNames, source):
# Initialize a new digraph for the tree we are going to make and add all the nodes (we will determine edges later)
# Initialize a graph in Networkx
G = nx.DiGraph()
# Initialize effects and modes arrays for plotting purposes
effects = []
modes = []
names_of_nodes = []
# Add the source node to the graph
G.add_node(nodeNames[source - 1])
names_of_nodes.append(nodeNames[source - 1])
# Determine if the source node is an effect or a mode, and add it to the correct array
'''if source < 27: effects.append(nodeNames[source - 1])
else: modes.append(nodeNames[source - 1])'''
if source < 49: modes.append(nodeNames[source - 1])
else: effects.append(nodeNames[source - 1])
# Binarize the adjacency matrix
arr2 = arr.copy()
adj = make_binary(arr2).astype(int)
# Create an array of booleans such that the ith entry indicates if the node has already been visited. Nodes
# that have been visited are set to TRUE.
nodeList = np.reshape(np.repeat(False, arr.shape[0]), (arr.shape[0], 1))
# Create a diagonal matrix such that the value of the i,i entry is 1+1, referencing the node with name i+1
nodes = diagonal_nodes(adj)
# Visit the source node
nodeList[source-1] = True
# Add the source node to the queue. We use a queue to iterate through the nodes. This allows us to search through
# the graph generation by generation rather than following one specific path at a time.
queue = [[source, 100]]
# Initialize a dictionary that will tell us what generation each node is in. Label the generation of the source node
# as zero.
gens = {nodeNames[source-1]: {"layer": 100}}
# Continue while there are still nodes in the queue (reference algorithms for a breadth first search for more info)
while len(queue) > 0:
# Set the node we are looking at equal to the node next in line in the queue
current = queue[0]
# Determine the parents of the current node.
parents_bool = nodes @ adj[:, current[0]-1] # vector of zeros and parent names (numerical names)
parents = parents_bool[np.nonzero(parents_bool)] #list of just the parent names (numerical names)
for parent in parents: # For every child of the current node that was found above...
# Check if the parent has been visited or if it is in the same generation as parent. If either not visited or
# in the same generation, continue with the following:
if nodeList[parent - 1] == False or gens[nodeNames[parent - 1]]['layer'] < current[1]:
# Add the parent to the graph
G.add_node(nodeNames[parent-1])
# Determine if the parent is an effect or mode and add it to the correct array
if parent < 27: effects.append(nodeNames[parent- 1])
else: modes.append(nodeNames[parent - 1])
'''if parent < 49: modes.append(nodeNames[parent- 1])
else: effects.append(nodeNames[parent - 1])'''
# Add an edge between the current node and parent in the tree we are building
G.add_edge(nodeNames[parent-1], nodeNames[current[0]-1])
queue.append([parent, current[1]-1]) # Append the parent to the queue
nodeList[parent - 1] = True # Change the status of the parent to say we have visited it
gens.update({nodeNames[parent-1]: {"layer": current[1]-1}}) # Add parent to dictionary of nodes
# Remove the current node from the queue
queue = queue[1:]
# Return the tree we made, its adjacency matrix, the dictionary of nodes, the effects array, and the modes arrat
return G, nx.to_numpy_array(G), gens, effects, modes
''' draw_bfs_multipartite Documentation
---------------------------------------
This method inputs an adjacency matrix, list of node names, and a start node. We use the breadth first searh to
find generations of nodes starting from the start node. This method plots the resulting graph from the breadth
first search. Nothing is returned.'''
def draw_bfs_multipartite(arr, nodeNames, start, type = "child", multi_turbine = False):
# Obtain the graph, adjacency matrix, dictionary, effects, and modes from breadth_first_tree
if type == "child":
G, arr, gens, effects, modes = breadth_first_child(arr, nodeNames, start)
elif type == "parent":
G, arr, gens, effects, modes = breadth_first_parent(arr, nodeNames, start)
elif type == "multi-child":
G, arr, gens, effects, modes, non = breadth_first_multi(arr, nodeNames, start, "child")
else:
G, arr, gens, effects, modes, non = breadth_first_multi(arr, nodeNames, start, "parent")
# Give each node the attribute of their generation
nx.set_node_attributes(G, gens)
# print(effects)
if multi_turbine:
effect_colors = ["#ffd6ed", "#ffb3ba", "#ffdfba", "#ffffba", "#baffc9", "#bae1ff", "#b1adff", "#e4adff", "#e5e5e5", "#e8d9c5"]
mode_colors = ["#e5c0d5", "#e5a1a7", "#e5c8a7", "#e5e5a7", "#a7e5b4", "#a7cae5", "#9f9be5", "#cd9be5", "#cecece", "#d0c3b1"]
pos = nx.multipartite_layout(G, subset_key='layer')
for node in G.nodes:
# print(int(str(node)[0]))
if str(node) in effects:
nx.draw_networkx_nodes(G, pos, nodelist=[node], node_color = effect_colors[int(str(node)[0])], node_size=750, node_shape="s")
else:
nx.draw_networkx_nodes(G, pos, nodelist=[node], node_color = effect_colors[int(str(node)[0])], node_size=750)
nx.draw_networkx_labels(G, pos, font_size=5, verticalalignment='center_baseline')
nx.draw_networkx_edges(G, pos, arrowsize=20)
plt.box(False)
plt.show()
else:
# Plot the graph
pos = nx.multipartite_layout(G, subset_key='layer')
nx.draw_networkx_nodes(G, pos, nodelist=effects, node_color="#98c5ed", node_size=2700, edgecolors="#799dbd", node_shape="s")
nx.draw_networkx_nodes(G, pos, nodelist=modes, node_color="#fabc98", node_size=2700, edgecolors="#c89679", node_shape="s")
nx.draw_networkx_labels(G, pos, font_size=10, verticalalignment='center_baseline')
nx.draw_networkx_edges(G, pos, arrowsize=60)
plt.box(False)
plt.show()
# Nothing is returned
return
'''breadth_first_multi Documentation
-----------------------------------
This method takes in an adjacency matrix, list of node names, an integer indicating te starting node, and a string
that indicates which type of breadth first search we are conducting (child or parent). We traverse through the graph.
For each generation, starting from the source nodes (handles multiple start nodes), we find and add all the children/parents
of the nodes in this generation, unless the node has aldready been placed in the tree. As such, we create a tree.
This method returns this tree, the tree's adjacency matrix, a dictionary of nodes, arrays for effects and modes, and array
of nodes (in the order they are added).'''
def breadth_first_multi(arr, nodeNames, sources, type_poc):
# Function that determines how value of the layer changes, depending on the type of calculation
def layer_fcn(layer, type_poc):
if type_poc == "child":
return layer + 1
elif type_poc == "parent":
return layer - 1
# Determining if the current generation is after the former node's generation, depending on the type of calculation
def same_layer(layer1, layer2, type_poc):
if type_poc == "child":
if layer1 > layer2:
return True
else:
return False
elif type_poc == "parent":
if layer1 < layer2:
return True
else:
return False
# Initialize a new digraph for the tree we are going to make and add all the nodes (we will determine edges later)
G = nx.DiGraph()
# Initialize arrays for tracking nodes
effects = []
modes = []
names_of_nodes = []
adj = make_binary(arr).astype(int)
nodeList = np.reshape(np.repeat(False, arr.shape[0]), (arr.shape[0], 1))
nodes = diagonal_nodes(adj)
queue = []
gens = {}
# Initialize value for the first layer created
if type_poc == "child":
layer_val = 0
else:
layer_val = 100
# Add the source node to the graph
for start in sources:
G.add_node(nodeNames[start - 1])
# Determine if the source node is an effect or a mode, and add it to the correct array
if start < 27: effects.append(nodeNames[start - 1])
else: modes.append(nodeNames[start - 1])
'''if start < 49: modes.append(nodeNames[start - 1])
else: effects.append(nodeNames[start - 1])'''
# Visit the source node
nodeList[start-1] = True
names_of_nodes.append(nodeNames[start - 1])
# Add the source node to the queue. We use a queue to iterate through the nodes. This allows us to search through
# the graph generation by generation rather than following one specific path at a time.
queue.append([start, 0])
# Initialize a dictionary that will tell us what generation each node is in. Label the generation of the source node
# as zero.
gens.update({nodeNames[start-1]: {"layer": layer_val}})
# print("non", names_of_nodes)
# Continue while there are still nodes in the queue (reference algorithms for a breadth first search for more info)
while len(queue) > 0:
# Set the node we are looking at equal to the node next in line in the queue
current = queue[0]
if type_poc == "child":
# Determine the children of the current node.
children_bool = adj[current[0]-1] @ nodes # vector of zeros and child names (numerical names)
children = children_bool[np.nonzero(children_bool)] #list of just the child names (numerical names)
else:
children_bool = nodes @ adj[:, current[0]-1] # vector of zeros and parent names (numerical names)
children = children_bool[np.nonzero(children_bool)] #list of just the parent names (numerical names)
for child in children: # For every child of the current node that was found above...
# Check if the child has been visited or if it is in the same generation as child. If either not visited or
# in the same generation, continue with the following:
# print("child", child)
if nodeList[child - 1] == False or same_layer(gens[nodeNames[child - 1]]['layer'], current[1], type_poc):
# Add the child to the graph, and to the array of node names
G.add_node(nodeNames[child-1])
if nodeNames[child - 1] in names_of_nodes:
x = 14
else:
names_of_nodes.append(nodeNames[child - 1])
# Determine if the child is an effect or mode and add it to the correct array
'''if child < 27: effects.append(nodeNames[child - 1])
else: modes.append(nodeNames[child - 1])'''
if child < 49: modes.append(nodeNames[child - 1])
else: effects.append(nodeNames[child - 1])
# Add an edge between the current node and child in the tree we are building
if type_poc == "child":
G.add_edge(nodeNames[current[0]-1], nodeNames[child-1])
else:
G.add_edge(nodeNames[child-1], nodeNames[current[0]-1])
queue.append([child, layer_fcn(current[1], type_poc)]) # Append the child to the queue
nodeList[child - 1] = True # Change the status of the child to say we have visited it
gens.update({nodeNames[child-1]: {"layer": layer_fcn(current[1], type_poc)}}) # Add child to dictionary of nodes
# Remove the current node from the queue
queue = queue[1:]
# Return the tree we made, its adjacency matrix, the dictionary of nodes, the effects array, and the modes arrat
return G, nx.to_numpy_array(G), gens, effects, modes, names_of_nodes
'''# **** Below code is still being worked on *****
arr, nodeNames = excel2Matrix("failureData.xlsx", "bigMatrix")
source = 11
G = nx.DiGraph()
effects = []
modes = []
G.add_node(nodeNames[source - 1])
if source < 49: modes.append(nodeNames[source - 1])
else: effects.append(nodeNames[source - 1])
adj = make_binary(arr).astype(int)
nodeList = np.reshape(np.repeat(False, arr.shape[0]), (arr.shape[0], 1))
nodes = diagonal_nodes(adj)
nodeList[source-1] = True
queue = [[source, 0]]
gens = {nodeNames[source-1]: {"layer": 0}}
while len(queue) > 0:
current = queue[0]
children_bool = adj[current[0]-1] @ nodes # vector of zeros and child names (numerical names)
children = children_bool[np.nonzero(children_bool)] #list of just the child names (numerical names)
for child in children:
if nodeList[child - 1] == False or gens[nodeNames[child - 1]]['layer'] > current[1]:
G.add_node(nodeNames[child-1])
if child < 27: effects.append(nodeNames[child - 1])
else: modes.append(nodeNames[child - 1])
G.add_edge(nodeNames[current[0]-1], nodeNames[child-1])
queue.append([child, current[1]+1]) # Append the child to the queue
nodeList[child - 1] = True # Change the status of the child to say we have visited it
gens.update({nodeNames[child-1]: {"layer": current[1]+1}}) # Add child to dictionary of nodes
queue = queue[1:]'''