使用igraph最短距离时的丢失距离

问题描述 投票:0回答:1

我正在尝试计算网络节点与两个源之间的距离。之后,我将最短距离保存在列表中(称为route_length)。但是我的网络有9693个节点在运行我的代码并计算出我仅有的最短路径之后9602距离。我不明白为什么我的距离比节点少,再加上如果在循环中我将节点保存在列表中并在最后打印其长度,这也会给我9602个节点的结果,这是不正确的。

这是我的代码:

import networkx as nx
import matplotlib.pyplot as plt
import osmnx as ox
import pandas as pd
from shapely.wkt import loads as load_wkt
import numpy as np
import matplotlib.cm as cm
import igraph as ig
import matplotlib as mpl
import random as rd
ox.config(log_console=True, use_cache=True)


city = 'Portugal, Lisbon'
G = ox.graph_from_place(city, network_type='drive')

G_nx = nx.relabel.convert_node_labels_to_integers(G)

ox.speed.add_edge_speeds(G_nx, hwy_speeds=20, fallback=20)

ox.speed.add_edge_travel_times(G_nx)

weight = 'travel_time'

coord_1 = (38.74817825481225, -9.160815118526642)  # Coordenada Hospital Santa Maria
coord_2 = (38.74110711410615, -9.152159572392323)  # Coordenada Hopstial Curry Cabral
coord_3 = (38.7287248180068, -9.139114834357233) # Hospital Dona Estefania
coord_4 = (38.71814053423293, -9.137885476529883) # Hospital Sao Jose 
target_1 = ox.get_nearest_node(G_nx, coord_1)
target_2 = ox.get_nearest_node(G_nx, coord_2)
target_3 = ox.get_nearest_node(G_nx, coord_3)
target_4 = ox.get_nearest_node(G_nx, coord_4)


G_ig = ig.Graph(directed=True)
G_ig.add_vertices(list(G_nx.nodes()))
G_ig.add_edges(list(G_nx.edges()))
G_ig.vs['osmid'] = list(nx.get_node_attributes(G_nx, 'osmid').values())
G_ig.es[weight] = list(nx.get_edge_attributes(G_nx, weight).values())

assert len(G_nx.nodes()) == G_ig.vcount()
assert len(G_nx.edges()) == G_ig.ecount()

route_length=[]
list_nodes=[]

for node in G_nx.nodes:
    length_1 = G_ig.shortest_paths(source=node, target=target_1, weights=weight)[0][0]
    length_2 = G_ig.shortest_paths(source=node, target=target_2, weights=weight)[0][0]

    if length_1<length_2:
       route_length.append(length_1)
       list_nodes.append(node)

   elif length_2 < length_1:
       route_length.append(length_2)
       list_nodes.append(node)

print(len(route_length))
print(len(list_nodes))

如果节点断开连接,则最短路径应为inf。而且route_length列表中没有任何inf值。

谢谢你。

python networkx igraph osmnx
1个回答
0
投票

首先想到的是length_1 == length_2

我相信这是失踪的91个案件得到满足的条件(因此,ifelif并未解决,并且未附加在列表route_length和列表list_nodes中。

© www.soinside.com 2019 - 2024. All rights reserved.