简体   繁体   English

快速找到最短路径

[英]Find the shortest path fast

I want to make the shortest path between many points.我想在许多点之间做最短的路径。

I generate an 8x8 matrix, with random values like:我生成一个 8x8 矩阵,随机值如下:

[[ 0 31 33  0 43 10  0  0]
 [31  0 30  0  0 13  0  0]
 [33 30  0 11 12  5  6  0]
 [ 0  0 11  0 15  0 38 11]
 [43  0 12 15  0 39  0  0]
 [10 13  5  0 39  0  3 49]
 [ 0  0  6 38  0  3  0 35]
 [ 0  0  0 11  0 49 35  0]]

Now I want to take the first list and see which is the smaller number.现在我想取出第一个列表,看看哪个是较小的数字。 The see where it is in the list and take its position.查看它在列表中的位置并占据它的位置。 Next I clear the first list to forget the first point.接下来我清除第一个列表以忘记第一点。 And put the next position in a new list of path.并将下一个位置放在新的路径列表中。 Then it will do the same for the new point.然后它将对新点执行相同的操作。 And at the final when all points are in my list of path it shows me the shortest way.最后,当所有点都在我的路径列表中时,它会向我显示最短的路径。

indm=0
lenm=[]
prochain=max(matrixF[indm])
chemin=[]
long=len(chemin)

while long != n:
    for i in range (n):
        if matrixF[indm,i] <= prochain and matrixF[indm,i]!=0:
            pluspetit=matrixF[indm,i]   
            
    prochainpoint=np.where(matrixF == pluspetit)
       
    chemin.append(prochainpoint)
    
    indm=prochainpoint
    
    for i in range (n):
        matrixF[indm,i]=0
    long=len(chemin)   
    
print(chemin)
print(matrixF)

But I got this error:但我得到了这个错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

This line computes all the indices where matrixF == pluspetit :此行计算matrixF == pluspetit的所有索引:

prochainpoint=np.where(matrixF == pluspetit)

Problem is, there's more than one, so on your first pass through, prochainpoint ends up as (array([0, 5]), array([5, 0])) .问题是,不止一个,所以在你第一次通过时, prochainpoint最终会变成(array([0, 5]), array([5, 0])) You then set indm to prochainpoint , so on your next pass, instead of getting a single value with matrixF[indm,i] , you retrieve a 2x2 array of (repeated) values, as if you'd done:然后将indm设置为prochainpoint ,因此在下一次通过时,您无需使用matrixF[indm,i]获取单个值,而是检索(重复)值的 2x2 数组,就像您已经完成一样:

np.array([[matrixF[0,1], matrixF[5,1]]
          [matrixF[5,1], matrixF[0,1]])

Comparing this to prochain (still a scalar value) produces a 2x2 array of boolean results, which you then try to test for truthiness, but numpy doesn't want to guess at whether you mean "are they all true?"将此与prochain (仍然是一个标量值)进行比较会产生一个 2x2 布尔结果数组,然后您尝试测试其真实性,但numpy不想猜测您的意思是“它们都是真的吗?” or "are any of them true?", so it dumps that decision back on you with the error message.或“它们中的任何一个都是真的吗?”,因此它会将该决定与错误消息一起转储给您。

I'm assuming the problem is with prochainpoint=np.where(matrixF == pluspetit) , where you get many results when you presumably only want one, but I'm not clear what the real intent of the line is, so you'll have to figure out what you really intended to do there and replace it with something that consistently computes a single value.我假设问题出在prochainpoint=np.where(matrixF == pluspetit)上,当你大概只想要一个时,你会得到很多结果,但我不清楚这条线的真正意图是什么,所以你必须弄清楚你真正打算在那里做什么,并将其替换为始终计算单个值的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM