From 515ebf36c4c05255d91a511ce66b412a3054f30a Mon Sep 17 00:00:00 2001 From: Toru Seo <34780089+toruseo@users.noreply.github.com> Date: Mon, 16 Sep 2024 19:18:24 +0900 Subject: [PATCH 1/2] add `get_shortest_path_distance_between_all_nodes` --- uxsim/uxsim.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/uxsim/uxsim.py b/uxsim/uxsim.py index 12051e1..37b6d15 100644 --- a/uxsim/uxsim.py +++ b/uxsim/uxsim.py @@ -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_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): """ From 5239708bda67a401a36a348a0df1a8a701122086 Mon Sep 17 00:00:00 2001 From: Toru Seo <34780089+toruseo@users.noreply.github.com> Date: Mon, 16 Sep 2024 19:21:15 +0900 Subject: [PATCH 2/2] correct name `get_shortest_path_distance_between_all_nodes` --- uxsim/uxsim.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uxsim/uxsim.py b/uxsim/uxsim.py index 37b6d15..b6025b6 100644 --- a/uxsim/uxsim.py +++ b/uxsim/uxsim.py @@ -2209,7 +2209,7 @@ def get_nodes_in_area(W, x, y, r): nodes.append(node) return nodes - def get_distance_between_all_nodes(W, return_matrix=False): + 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.