diff --git a/uxsim/uxsim.py b/uxsim/uxsim.py index c480deb..24874c4 100644 --- a/uxsim/uxsim.py +++ b/uxsim/uxsim.py @@ -992,12 +992,17 @@ def route_search_all(s, infty=np.inf, noise=0): s.dist, s.pred = dijkstra(csgraph=csr_mat, directed=True, indices=None, return_predecessors=True) # Directly use the predecessors to update the next matrix for route choice - n_vertices = s.pred.shape[0] - s.next = np.full((n_vertices, n_vertices), -1, dtype=int) # Initialize with -1 - for i in range(n_vertices): - for j in range(n_vertices): - if i != j and s.pred[i, j] != -9999: - s.next[i, j] = s.pred[i, j] + # Vectorized update of self.next + n_vertices = self.pred.shape[0] + self.next = np.full((n_vertices, n_vertices), -1, dtype=int) # Initialize with -1 + + # Vectorized operation to copy `self.pred` into `self.next` where pred != -9999 + valid_pred = self.pred != -9999 + self.next[valid_pred] = self.pred[valid_pred] + + # Correcting diagonal elements if necessary + # This step ensures that diagonal elements, which represent paths from a node to itself, are correctly set to -1. + np.fill_diagonal(self.next, -1) def route_search_all_old(s, infty=9999999999999999999, noise=0): """