Skip to content

Commit

Permalink
Make swap_pos part of Grid instead of SingleGrid (projectmesa#1507)
Browse files Browse the repository at this point in the history
* Make swap_pos part of Grid instead of SingleGrid

* Update test_grid.py

The second commit changes the agents sampling to
`agent_a, agent_b = list(filter(None, self.grid))[:2]`
because when the test is moved to `TestBaseGrid`, the `self.grid` in `TestBaseGrid` is not full.
  • Loading branch information
Tortar authored Nov 5, 2022
1 parent c7ec7b4 commit c201bcb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
40 changes: 20 additions & 20 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,26 @@ def remove_agent(self, agent: Agent) -> None:
self.empties.add(pos)
agent.pos = None

def swap_pos(self, agent_a: Agent, agent_b: Agent) -> None:
"""Swap agents positions"""
agents_no_pos = []
if (pos_a := agent_a.pos) is None:
agents_no_pos.append(agent_a)
if (pos_b := agent_b.pos) is None:
agents_no_pos.append(agent_b)
if agents_no_pos:
agents_no_pos = [f"<Agent id: {a.unique_id}>" for a in agents_no_pos]
raise Exception(f"{', '.join(agents_no_pos)} - not on the grid")

if pos_a == pos_b:
return

self.remove_agent(agent_a)
self.remove_agent(agent_b)

self.place_agent(agent_a, pos_b)
self.place_agent(agent_b, pos_a)

def is_cell_empty(self, pos: Coordinate) -> bool:
"""Returns a bool of the contents of a cell."""
x, y = pos
Expand Down Expand Up @@ -492,26 +512,6 @@ def position_agent(
coords = (x, y)
self.place_agent(agent, coords)

def swap_pos(self, agent_a: Agent, agent_b: Agent) -> None:
"""Swap agents positions"""
agents_no_pos = []
if (pos_a := agent_a.pos) is None:
agents_no_pos.append(agent_a)
if (pos_b := agent_b.pos) is None:
agents_no_pos.append(agent_b)
if agents_no_pos:
agents_no_pos = [f"<Agent id: {a.unique_id}>" for a in agents_no_pos]
raise Exception(f"{', '.join(agents_no_pos)} - not on the grid")

if pos_a == pos_b:
return

self.remove_agent(agent_a)
self.remove_agent(agent_b)

self.place_agent(agent_a, pos_b)
self.place_agent(agent_b, pos_a)

def place_agent(self, agent: Agent, pos: Coordinate) -> None:
if self.is_cell_empty(pos):
super().place_agent(agent, pos)
Expand Down
58 changes: 30 additions & 28 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,36 @@ def test_agent_remove(self):
assert agent.pos is None
assert self.grid.grid[x][y] is None

def test_swap_pos(self):

# Swap agents positions
agent_a, agent_b = list(filter(None, self.grid))[:2]
pos_a = agent_a.pos
pos_b = agent_b.pos

self.grid.swap_pos(agent_a, agent_b)

assert agent_a.pos == pos_b
assert agent_b.pos == pos_a
assert self.grid[pos_a] == agent_b
assert self.grid[pos_b] == agent_a

# Swap the same agents
self.grid.swap_pos(agent_a, agent_a)

assert agent_a.pos == pos_b
assert self.grid[pos_b] == agent_a

# Raise for agents not on the grid
self.grid.remove_agent(agent_a)
self.grid.remove_agent(agent_b)

id_a = agent_a.unique_id
id_b = agent_b.unique_id
e_message = f"<Agent id: {id_a}>, <Agent id: {id_b}> - not on the grid"
with self.assertRaisesRegex(Exception, e_message):
self.grid.swap_pos(agent_a, agent_b)


class TestBaseGridTorus(TestBaseGrid):
"""
Expand Down Expand Up @@ -268,34 +298,6 @@ def test_enforcement(self, mock_model):
with self.assertRaises(Exception):
self.move_to_empty(self.agents[0], num_agents=self.num_agents)

# Swap agents positions
agent_a, agent_b = random.sample(list(self.grid), k=2)
pos_a = agent_a.pos
pos_b = agent_b.pos

self.grid.swap_pos(agent_a, agent_b)

assert agent_a.pos == pos_b
assert agent_b.pos == pos_a
assert self.grid[pos_a] == agent_b
assert self.grid[pos_b] == agent_a

# Swap the same agents
self.grid.swap_pos(agent_a, agent_a)

assert agent_a.pos == pos_b
assert self.grid[pos_b] == agent_a

# Raise for agents not on the grid
self.grid.remove_agent(agent_a)
self.grid.remove_agent(agent_b)

id_a = agent_a.unique_id
id_b = agent_b.unique_id
e_message = f"<Agent id: {id_a}>, <Agent id: {id_b}> - not on the grid"
with self.assertRaisesRegex(Exception, e_message):
self.grid.swap_pos(agent_a, agent_b)


# Number of agents at each position for testing
# Initial agent positions for testing
Expand Down

0 comments on commit c201bcb

Please sign in to comment.