Python 用Dijkstra算法求三维模型中两节点间的最短路径

Python 用Dijkstra算法求三维模型中两节点间的最短路径,python,shortest-path,dijkstra,Python,Shortest Path,Dijkstra,我想实现Dijkstra算法来获得这个3D界面中两个节点之间的最短路径。以前我在Dijkstra算法中使用图形处理2D曲面。但这一次我坚持住了。寻找最终的解决方案。 星形节点是我的目标节点,其余任何节点都可以是源节点 这些节点的位置由以下代码估计 import numpy as np import matplotlib.pyplot as plt n = 50 x1, x2 = 0.1, 0.5 y1, y2 = 0.1, 0.5 z1, z2 = 0.1, 0.5 xs = (x2 -

我想实现Dijkstra算法来获得这个3D界面中两个节点之间的最短路径。以前我在Dijkstra算法中使用图形处理2D曲面。但这一次我坚持住了。寻找最终的解决方案。 星形节点是我的目标节点,其余任何节点都可以是源节点

这些节点的位置由以下代码估计

import numpy as np
import matplotlib.pyplot as plt

n = 50

x1, x2 = 0.1, 0.5
y1, y2 = 0.1, 0.5
z1, z2 = 0.1, 0.5

xs = (x2 - x1)*np.random.rand(n) + x1
ys = (y2 - y1)*np.random.rand(n) + y1
zs = (z2 - z1)*np.random.rand(n) + z1

xs1 = (x2 - x1)*np.random.rand(n) + x1
ys1 = (y2 - y1)*np.random.rand(n) + y1
zs1 = (z2 - z1)*np.random.rand(n) + z1

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')

for i in range(n):
    if zs[i] == max(zs):
        ax.plot(xs[i], ys[i], zs[i], "k*")
    else:
        ax.plot(xs[i], ys[i], zs[i], "go")

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()
要估计节点之间的距离,请执行以下操作:

def distance(p1,p2):
    squared_dist = np.sum((p1-p2)**2, axis=0)
    dist = np.sqrt(squared_dist)
    return dist

graph = []

for i in range(len(xs)):
    graph.append([])
    for j in range(len(xs)):
        p1 = np.array([xs[i], ys[i], zs[i]])
        p2 = np.array([xs[j], ys[j], zs[j]])
        graph[i].append(distance(p1,p2))
graph = np.array(graph)
print(graph)
求最短路径的Dijkstra算法

M = []
class DijkstraAlgoWithPath:
    global M
    
    def minDistance(self, dist, queue):
        minimum = float("Inf")
        min_index = -1
        
        for i in range(len(dist)):
            if dist[i] < minimum and i in queue:
                minimum = dist[i] 
                min_index = i
        return min_index
    
    def printPath(self, parent, j):
        if parent[j] == -1:                 # If 'j' is the source
            print (j+1, end="  ")
            M.append(j+1)
            return 0
        self.printPath(parent, parent[j])   #If 'j' is not the source, call the recursive function
        M.append(j+1)
        print (j+1, end="  ")
        


    def dijkstraWithPath(self, graph, src, des):
        s = src - 1
        row = len(graph)
        col = len(graph[0])
        
        dist = [float('Infinity')] * row    # initializing all distances are inifinity
        parent = [-1] * row                 # The parent array where to store the shortest path tree
        
        dist[s] = 0                         # Distance of source from itself is zero
        
        q = []                              # An empty list to store all vertices in queue
        for i in range(row):
            q.append(i)
        
        # Find the shortest path for all vertices
        while q:
            # Select the minimum distance vertex 
            # from the set of vertices 
            # which are still in the queue
            u = self.minDistance(dist, q)
            q.remove(u)     # Now remove the minimum distance element which already got
            
            # Consider the vertices which are still in the queue,
            # update the distance and parent index of the adjacent vertices
            # which are selected 
            for i in range(col):
                if graph[u][i] and i in q:  # If dist[i] in the queue
                    if dist[u] + graph[u][i] < dist[i]: # and if the total weight of path from source to destination is less than the current value of dist[i]
                        dist[i] = dist[u] + graph[u][i]
                        parent[i] = u
        self.printPath(parent, des-1)
        

def main():
    global graph
    
    x = DijkstraAlgoWithPath()
    source = 5   # Take input of the source value
    des = 41
    x.dijkstraWithPath(graph, source, des)
    
if __name__ == '__main__':
    main()

非常有趣的问题

为了使用Dijkstra算法,可能需要在图中定义一些边。否则,每对音符都有一条欧氏距离的边,那么直线应该是最短路径

这个问题与运动规划有关。它是NP难的


J.Canny和J.H.Reif,机器人运动规划问题的新下限技术,Proc。第28年。IEEE研讨会。建立计算机。《科学》,1987年,第49-60页。

非常有趣的问题

为了使用Dijkstra算法,可能需要在图中定义一些边。否则,每对音符都有一条欧氏距离的边,那么直线应该是最短路径

这个问题与运动规划有关。它是NP难的


J.Canny和J.H.Reif,机器人运动规划问题的新下限技术,Proc。第28年。IEEE研讨会。建立计算机。科学杂志,1987年,第49-60页。

使用代码时会发生什么?有错误吗?它没有显示源和目标之间的其他节点。例如:当我将源节点输入为1时,输出为:1 44,但这不是正确的路径。它不包括它们之间的其他节点。算法只是输出起始节点和结束节点吗?从起点到终点的直线显然是最短的路线,除非有一些我看不到的限制。当你使用你的代码时会发生什么?有错误吗?它没有显示源和目标之间的其他节点。例如:当我将源节点输入为1时,输出为:1 44,但这不是正确的路径。它不包括它们之间的其他节点。算法只是输出起始节点和结束节点吗?从起点到终点的直线显然是最短的路线,除非有一些我看不到的限制。非常感谢您宝贵的回复。我已经解决了我的问题。我构建了一个python包来解决这个问题。您可以访问查看工作。非常感谢您宝贵的回复。我已经解决了我的问题。我构建了一个python包来解决这个问题。您可以访问以检查工作。