Skip to content

Commit

Permalink
Merge pull request projectmesa#1001 from Casper-Smet/remove_agent
Browse files Browse the repository at this point in the history
Public remove_agent function for NetworkGrid
  • Loading branch information
Corvince authored Mar 15, 2021
2 parents 9292e22 + c681e6e commit 36c09e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,12 @@ def _remove_agent(self, agent: Agent, node_id: int) -> None:

self.G.nodes[node_id]["agent"].remove(agent)

def remove_agent(self, agent: Agent) -> None:
""" Remove the agent from the network and set its pos variable to None. """
pos = agent.pos
self._remove_agent(agent, pos)
agent.pos = None

def is_cell_empty(self, node_id: int) -> bool:
""" Returns a bool of the contents of a cell. """
return not self.G.nodes[node_id]["agent"]
Expand Down
9 changes: 9 additions & 0 deletions tests/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,15 @@ def test_move_agent(self):
assert _agent not in self.space.G.nodes[initial_pos]["agent"]
assert _agent in self.space.G.nodes[final_pos]["agent"]

def test_remove_agent(self):
for i, pos in enumerate(TEST_AGENTS_NETWORK_SINGLE):
a = self.agents[i]
assert a.pos == pos
assert a in self.space.G.nodes[pos]["agent"]
self.space.remove_agent(a)
assert a.pos is None
assert a not in self.space.G.nodes[pos]["agent"]

def test_is_cell_empty(self):
assert not self.space.is_cell_empty(0)
assert self.space.is_cell_empty(TestSingleNetworkGrid.GRAPH_SIZE - 1)
Expand Down

0 comments on commit 36c09e8

Please sign in to comment.