3

私は、ダイクストラのアルゴリズムで最も近い隣人を決定することに行き詰まりました。次のような奇妙な結果が得られます。まず、これはネットワークファイルの内容であり、7つのノード間の距離を表しています。

http://pastebin.com/PUM5qT6D

(最初の列の1〜7の数字は含まれていません)

今私のコードのために:

> infinity = 1000000 invalid_node = -1
> startNode = 0
> 
> #Values to assign to each node class Node:
>      distFromSource = infinity
>      previous = invalid_node
>      visited = False
> 
> #read in all network nodes
> #node = the distance values between nodes def network():
>     f = open ('network.txt', 'r')
>     theNetwork = [[int(node) for node in line.split(',')] for line in
> f.readlines()]
>     #print theNetwork
> 
>     return theNetwork
> 
> #for each node assign default values
> #populate table with default values def populateNodeTable():
>     nodeTable = []
>     index = 0
>     f = open('network.txt', 'r')
>     for line in f:
>       node = map(int, line.split(','))
>       nodeTable.append(Node())
>      
>       #print "The previous node is " ,nodeTable[index].previous
>       #print "The distance from source is " ,nodeTable[index].distFromSource
>       index +=1
>     nodeTable[startNode].distFromSource =
> 0
> 
>     return nodeTable
> 
> #find the nearest neighbour to a particular node def
> nearestNeighbour(nodeTable,
> theNetwork):
>      nearestNeighbour = []
>      nodeIndex = 0
>      for node in nodeTable:
>           if node != 0 and Node.visited == False:
>              nearestNeighbour.append(nodeIndex)
>              nodeIndex +=1
>      print nearestNeighbour
> 
>      return nearestNeighbour
> 
> def tentativeDistance (theNetwork,
> nodeTable, nearestNeighbour):
>     shortestPath = []
>     for nodeIndex in nearestNeighbour:
>          currentDistance = nearestNeighbour[] + startNode
>          print currentDistance
> ##         if currentDistance < Node.distFromSource:
> ##            theNetwork[Node].previous = nodeIndex
> ##            theNetwork[Node].length = nodeIndex
> ##            theNetwork[Node].visited = True;
> ##            shortestPath.append(indexNode)
> ##            nodeIndex +=1
> ##    print shortestPath
> 
> currentNode = startNode
> 
> if __name__ == "__main__":
>     nodeTable = populateNodeTable()
>     theNetwork = network()
>     nearestNeighbour(nodeTable, theNetwork)
>     tentativeDistance(theNetwork, nodeTable, nearestNeighbour)

したがって、ネットワーク関数によって提供される値を確認し、populateNodeTable関数ですべてのノードを「visited = false」に設定してから、前の関数で提供される値を確認してノードの最も近いネイバーを決定しようとしています。このエラーメッセージ:

> Traceback (most recent call last):  
> File "C:\Documents and Settings\Harvey\Desktop\2dArray.py", line 77, in <module>
>     tentativeDistance(theNetwork, nodeTable, nearestNeighbour)   File
> "C:\Documents and Settings\Harvey\Desktop\2dArray.py", line 51, in tentativeDistance
>     for nodeIndex in nearestNeighbour: TypeError: 'function' object is not iterable

ネットワーク関数を実行すると、次の出力が得られます。

[[0, 2, 4, 1, 6, 0, 0], [2, 0, 0, 0, 5, 0, 0], [4, 0, 0, 0, 5, 5, 0], [1, 0, 0, 0, 1, 1, 0], [6, 5, 0, 1, 0, 5, 5], [0, 0, 5, 1, 5, 0, 0], [0, 0, 0, 0, 5, 0, 0]]

これまでのところ、非常に優れています。populateNodeTable関数をネットワーク関数と一緒に実行すると、次の出力が得られます。

> The previous node is  -1 
  The distance from source is  1000000 # happens 7 times#
> 

また、それは良いことです-上記の関数に加えて、最も近いNeighbour関数を実行した後の出力は次のとおりです。

[0, 1, 2, 3, 4, 5, 6]

この出力は間違っており、私の問題が始まるところです

また、tentativeDistanceを含むすべてのコードを実行すると、次のエラーが発生します。

> for nodeIndex in nearestNeighbour:
  TypeError: 'function' object is not iterable

この投稿が長蛇の列になっていることをお詫びします。基本的な機能と思われるものをマスターできないことに不満を感じています。

4

2 に答える 2

2

メソッドの結果ではなく、メソッドnearestNeighbourをに渡します。tentativeDistance

于 2011-03-11T21:10:49.197 に答える
1

これが問題です

tentativeDistance(theNetwork, nodeTable, nearestNeighbour)

する必要があります

 x = nearestNeighbour(nodeTable, theNetwork)
 tentativeDistance(theNetwork, nodeTable, x)

エラーを見ると、コードが反復不可能なオブジェクトを反復しようとしていることがわかります。これは、Pythonfor - in -構文では暗黙的です。

混乱を避けるために、変数名または関数名の名前を変更することも検討してください。これはどちらの方法でも簡単な間違いです。

于 2011-03-11T21:09:04.257 に答える