Frage

please the code below determines the length of shortest path between two randomly selected nodes, it then prints the length of the path between them. I need to insert a condition within the loop that tests the tests the length of path between the two nodes and continues until all the output meets this condition (>= 1.5 and <= 2). I am really confused as to how I can nest that within the existing loops. I have included what I tried in triple quotes. That seems to be an infinite loop hence not helpful. Please help. Thanks

import networkx as nx
import random as rand

pos = {1001:(-42503,-3748871),1002:(-42267,-3749806),1003:(-40938,-3750235),1004: (-39452,-3750624),1005:(-39985,-3749564),1006:(-38473,-3749615),1007:(-41714,-3747171),1008:(-42279,-3745275),1009:(-41853,-3744185),1010:(-42000,-3746561),1011:(-42651,-3746188),1012:(-42195,-3747788),1013:(-41498,-3748890),1014:(-40366,-3748684),1015:(-43036,-3750284)}

edge = [(1001, 1003,{'length':0.35}),(1001, 1004,{'length':0.46}),(1001, 1009,{'length':0.49}),(1002, 1007,{'length':0.22}),(1002, 9972,{'length':0.54}),(1002, 1013,{'length':0.59}),(1003, 1014,{'length':0.25}),(1004, 1010,{'length':0.29}),(1004, 1013,{'length':0.57}),(1004, 1003,{'length':0.43}),(1004, 1006,{'length':0.37}),(1005, 1002,{'length':0.23}),(1005, 14566,{'length':0.72}),(1006, 1005,{'length':0.6}),(1007, 1003,{'length':0.39}),(1007, 1010,{'length':0.11}),(1009, 1001,{'length':0.51}),(1010, 1005,{'length':0.2}),(1011, 1004,{'length':0.37}),(1012, 1006,{'length':0.17}),(1013, 1005,{'length':0.19}),(1013, 1007,{'length':0.21}),(1014, 1005,{'length':0.35}),(1014, 1009,{'length':0.51})]


X = nx.MultiDiGraph()
X.add_nodes_from(pos.keys())
X.add_edges_from(edge)


num_routes = 5

for i in range(num_routes):

while True:

    try:
        A = int(rand.choice(pos.keys()))
        B = int(rand.choice(pos.keys()))
        path = nx.dijkstra_path(X,A,B,weight='length')
        pathlength = round(nx.dijkstra_path_length(X,A,B,weight='length'),2)

        ''' 
        while pathlength >= 1.5 and pathlength <= 2: 

            continue

        else: pathlength = round(nx.dijkstra_path_length(X,A,B,weight='length'),2)

        '''

    except:

        continue

    break

print pathlength

output:
pathlength:  2.2
pathlength:  2.94
pathlength:  1.46
pathlength:  1.37
pathlength:  0.77
War es hilfreich?

Lösung

for i in range(num_routes):
    while True:
        try:
            A = int(rand.choice(pos.keys()))
            B = int(rand.choice(pos.keys()))
            path = nx.dijkstra_path(X,A,B,weight='length')
            pathlength = round(nx.dijkstra_path_length(X,A,B,weight='length'),2)
            print path,A,B,pathlength  ,i
            if 1.5 <= pathlength <= 2:
                break
            else:
                pathlength 
        except:pass
    print pathlength,A,B

This should work. If I understand you correctly that is! Edited my code, I was doing extra loops needlessly.

Andere Tipps

You seem a little confused about the functionality of break and continue in loops. break will tell python that it should immediately finish the loop without any more iterations while continue just tells it to skip over the rest current iteration and move on to the next (basically restarting the loop in the case of while True:. The break you have at the end of your while loop immediately exit the loop right after the first time it runs!

for i in range(num_routes):

    while True:

        try:
            A = int(rand.choice(pos.keys()))
            B = int(rand.choice(pos.keys()))
            path = nx.dijkstra_path(X,A,B,weight='length')
            pathlength = round(nx.dijkstra_path_length(X,A,B,weight='length'),2)
            if pathlength >= 1.5 and pathlength <= 2: 
                break
        except:
            continue

    print pathlength
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top