Skip to content

Commit

Permalink
Implement faster distance algorithm (#125)
Browse files Browse the repository at this point in the history
Implement `faster_current` distance algorithm
  • Loading branch information
N-Wouda authored Oct 14, 2024
1 parent 3c9e2f4 commit 04ce196
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions vrplib/parse/parse_distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,13 @@ def pairwise_euclidean(coords: np.ndarray) -> np.ndarray:
An n-by-n Euclidean distances matrix.
"""
diff = coords[:, np.newaxis, :] - coords
square_diff = diff**2
square_dist = np.sum(square_diff, axis=-1)
return np.sqrt(square_dist)
coords = np.atleast_2d(coords)

sq_sum = (coords**2).sum(axis=1)
sq_dist = np.add.outer(sq_sum, sq_sum) - 2 * (coords @ coords.T)
np.fill_diagonal(sq_dist, 0) # avoids minor numerical issues

return np.sqrt(sq_dist)


def from_lower_row(triangular: np.ndarray) -> np.ndarray:
Expand Down

0 comments on commit 04ce196

Please sign in to comment.