Skip to content

Commit

Permalink
Merge pull request #132 from toruseo/develop
Browse files Browse the repository at this point in the history
add `get_shortest_path_distance_between_all_nodes`
  • Loading branch information
toruseo authored Sep 16, 2024
2 parents 112b425 + 5239708 commit 64b1146
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions uxsim/uxsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,38 @@ def get_nodes_in_area(W, x, y, r):
if (node.x-x)**2 + (node.y-y)**2 < r**2:
nodes.append(node)
return nodes

def get_shortest_path_distance_between_all_nodes(W, return_matrix=False):
"""
Get the shortest distances (in meters) between all node pairs based on link lengths.
Parameters
----------
return_matrix : bool, optional
Whether to return the distance matrix as a numpy array. Default is False.
Returns
-------
dict or numpy array
Returns a dictionary of distances between nodes whose key is node if `return_matrix` is False.
Returns a numpy array of distances between nodes whose index is node.id if `return_matrix` is True.
"""
num_nodes = len(W.NODES)
distances = np.full((num_nodes, num_nodes), np.inf) # Initialize with infinity

# Fill in the distances based on the link lengths
for link in W.LINKS:
i = link.start_node.id
j = link.end_node.id
distances[i, j] = min(distances[i, j], link.length)

# Use Dijkstra algorithm to compute shortest distances
distances = *dijkstra(csr_matrix(distances), directed=True, return_predecessors=False),

if return_matrix == True:
return distances
else:
raise NotImplementedError("return_matrix=True is not implemented yet.")#TODO

def load_scenario_from_csv(W, fname_node, fname_link, fname_demand, tmax=None):
"""
Expand Down

0 comments on commit 64b1146

Please sign in to comment.