Skip to content

Commit

Permalink
Update continuous_space.py
Browse files Browse the repository at this point in the history
  • Loading branch information
quaquel committed Jan 2, 2025
1 parent 357bc75 commit cc4a095
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions mesa/experimental/continuous_space/continuous_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,15 @@ def calculate_difference_vector(
if indices is not None
else self.agent_positions
)
delta = point[np.newaxis, :] - positions
delta = positions - point[np.newaxis, :]

if self.torus:
delta = np.abs(delta, out=delta)
delta = np.minimum(delta, self.size - delta, out=delta)
inverse_delta = delta - np.sign(delta) * self.size
logical = np.abs(delta) < np.abs(inverse_delta)
out = np.zeros(delta.shape)
out[logical] = delta[logical]
out[~logical] = inverse_delta[~logical]
delta = out

return delta

Expand All @@ -138,7 +142,13 @@ def calculate_distances(self, point, indices=None) -> tuple[np.ndarray, np.ndarr
)

if self.torus:
delta = self.calculate_difference_vector(point)
delta = point[np.newaxis, :] - positions

delta = np.abs(delta, out=delta)
delta = np.minimum(
delta, self.size - delta, out=delta
)

dists = np.linalg.norm(delta, axis=1)
else:
dists = cdist(point[np.newaxis, :], positions)[:, 0]
Expand Down

0 comments on commit cc4a095

Please sign in to comment.