Skip to content

Commit

Permalink
Added torus adjustment to Grid's move_agent. Updated torus_adj in ite…
Browse files Browse the repository at this point in the history
…r_neighborhood.
  • Loading branch information
JamesArruda committed Nov 16, 2017
1 parent 275674d commit 8745111
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ def iter_neighborhood(self, pos, moore,
not (0 <= dy + y < self.height)):
continue

px = self.torus_adj(x + dx, self.width)
py = self.torus_adj(y + dy, self.height)
px, py = self.torus_adj((x + dx, y + dy))

# Skip if new coords out of bounds.
if(self.out_of_bounds((px, py))):
Expand Down Expand Up @@ -246,11 +245,15 @@ def get_neighbors(self, pos, moore,
return list(self.iter_neighbors(
pos, moore, include_center, radius))

def torus_adj(self, coord, dim_len):
def torus_adj(self, pos):
""" Convert coordinate, handling torus looping. """
if self.torus:
coord %= dim_len
return coord
if not self.out_of_bounds(pos):
return pos
elif not self.torus:
raise Exception("Point out of bounds, and space non-toroidal.")
else:
x, y = pos[0] % self.width, pos[1] % self.height
return x, y

def out_of_bounds(self, pos):
"""
Expand Down Expand Up @@ -295,6 +298,7 @@ def move_agent(self, agent, pos):
pos: Tuple of new position to move the agent to.
"""
pos = self.torus_adj(pos)
self._remove_agent(agent.pos, agent)
self._place_agent(pos, agent)
agent.pos = pos
Expand Down

0 comments on commit 8745111

Please sign in to comment.