Python 给定一个坐标列表,向这些坐标添加值,直到最短路径列表发生更改
所以我有一个问题,我希望你能给我指出正确的方向,完整的上下文问题,我有一个3D numpy数组,我使用Networkx创建图形,用这些图形我找到了最短的路径。我需要的是,给定一组坐标,在这些坐标中添加“1”,直到最短路径列表发生变化(这只是一个示例,坐标列表可以有更多坐标),我的代码如下:Python 给定一个坐标列表,向这些坐标添加值,直到最短路径列表发生更改,python,for-loop,while-loop,networkx,Python,For Loop,While Loop,Networkx,所以我有一个问题,我希望你能给我指出正确的方向,完整的上下文问题,我有一个3D numpy数组,我使用Networkx创建图形,用这些图形我找到了最短的路径。我需要的是,给定一组坐标,在这些坐标中添加“1”,直到最短路径列表发生变化(这只是一个示例,坐标列表可以有更多坐标),我的代码如下: import numpy as np import networkx as nx arr = np.array([[[ 0., 378., 50., 174., 125.],
import numpy as np
import networkx as nx
arr = np.array([[[ 0., 378., 50., 174., 125.],
[ 0., 0., 0., 0., 0.],
[ 0., 154., 0., 20., 0.],
[ 0., 111., 15., 0., 22.],
[ 0., 16., 0., 12., 0.]],
[[ 0., 488., 64., 98., 117.],
[ 0., 0., 0., 0., 0.],
[ 0., 151., 0., 24., 0.],
[ 0., 35., 13., 0., 24.],
[ 0., 71., 0., 10., 0.]],
[[ 0., 374., 110., 187., 189.],
[ 0., 0., 0., 0., 0.],
[ 0., 195., 0., 12., 0.],
[ 0., 42., 14., 0., 21.],
[ 0., 16., 0., 19., 0.]]])
graphs = []
path = []
for i in arr:
graphs.append(nx.from_numpy_array(i, create_using = nx.DiGraph)) #Create graphs from numpy array
for graph in graphs:
path.append(nx.shortest_path(graph, 0, 1, weight = 'weight')) #Find the shortest path
print(path)
#path = [[0, 2, 3, 4, 1], [0, 2, 3, 1], [0, 2, 3, 4, 1]] #Shortest path for each array
coordinates = [[2, 3], [3, 4]] #List of coordinates in the array that I want to add 1
例如,我想迭代我的坐标列表并添加1,直到我的路径列表改变
#My first coordinate is [2, 3] so I add 1 to these coordinates and calculate the shortest path again
#to see if the path changes
arr[:, 2, 3] = arr[:, 2, 3] + 1 #Path = [[0, 2, 3, 4, 1], [0, 2, 3, 1], [0, 2, 3, 4, 1]]
arr[:, 2, 3] = arr[:, 2, 3] + 2 #Path = [[0, 2, 3, 4, 1], [0, 2, 3, 1], [0, 2, 3, 4, 1]]
...
arr[:, 2, 3] = arr[:, 2, 3] + 9 #Path = [[0, 2, 3, 4, 1], [0, 2, 3, 1], [0, 2, 3, 4, 1]]
arr[:, 2, 3] = arr[:, 2, 3] + 10 #Path = [[0, 2, 3, 4, 1], [0, 3, 1], [0, 2, 3, 4, 1]] #Here the path changes so I keep the 9
完成第一个坐标后,我转到第二个坐标
#The second coordinate is [3, 4] so...
arr[:, 3, 4] = arr[:, 3, 4] + 1 #Path = [[0, 2, 3, 4, 1], [0, 2, 3, 1], [0, 2, 3, 4, 1]]
...
arr[:, 3, 4] = arr[:, 3, 4] + 4 #Path = [[0, 2, 3, 4, 1], [0, 2, 3, 1], [0, 2, 3, 4, 1]]
arr[:, 3, 4] = arr[:, 3, 4] + 5 #Path = [[0, 2, 3, 4, 1], [0, 2, 3, 1], [0, 2, 3, 1]] #Here the path changes so I keep the 4
我正在考虑使用一个while循环,比如
while(path==newpath)
然后继续添加一个,但是我不知道如何遍历坐标列表,以及在找到改变路径的值后如何停止,因此我们将感谢您的帮助,谢谢 您假设循环的结构是正确的。只要确保正确复制阵列即可。代码如下:
from copy import deepcopy
for x,y in coordinates:
# Make deepcopies of path and arr
# For the first iteration, set newpath = path
new_path = deepcopy(path)
temp_arr = deepcopy(arr)
# Set counter for each coordinate to zero
cnt = 0
# Iterate till a change in path is observed
while path == new_path:
# Add 1 to x,y
temp_arr[:, x, y] = temp_arr[:, x, y] + 1
# Increment the counter
cnt += 1
# Reconstruct the graph and shortest path
temp_graph = []
new_path = []
for i in temp_arr:
temp_graph.append(nx.from_numpy_array(i, create_using = nx.DiGraph))
for graph in temp_graph:
new_path.append(nx.shortest_path(graph, 0, 1, weight = 'weight'))
# If we are out of the loop, this means that
# the shortest path has changed. Print the details.
print("For coordinates X={} and Y={} the change is at {}".format(x, y, cnt))
对于工作版本,您可以签出
另外,我不确定您是否总是想找出source=0
和target=1
之间的最短路径,但您可以根据需要更改这些值。您可以查看文档
参考资料: