Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add get_shortest_path_distance_between_all_nodes #132

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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