diff --git a/uxsim/uxsim.py b/uxsim/uxsim.py index 12051e1..b6025b6 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_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): """