Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 1 addition & 65 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,16 +469,8 @@ def is_cell_empty(self, pos: Coordinate) -> bool:
x, y = pos
return self._grid[x][y] == self.default_val()

def move_to_empty(self, agent: Agent, num_agents: int | None = None) -> None:
def move_to_empty(self, agent: Agent) -> None:
"""Moves agent to a random empty cell, vacating agent's old cell."""
if num_agents is not None:
warn(
(
"`num_agents` is being deprecated since it's no longer used "
"inside `move_to_empty`. It shouldn't be passed as a parameter."
),
DeprecationWarning,
)
num_empty_cells = len(self.empties)
if num_empty_cells == 0:
raise Exception("ERROR: No empty cells")
Expand All @@ -501,26 +493,6 @@ def move_to_empty(self, agent: Agent, num_agents: int | None = None) -> None:
self.remove_agent(agent)
self.place_agent(agent, new_pos)

def find_empty(self) -> Coordinate | None:
"""Pick a random empty cell."""
import random

warn(
(
"`find_empty` is being phased out since it uses the global "
"`random` instead of the model-level random-number generator. "
"Consider replacing it with having a model or agent object "
"explicitly pick one of the grid's list of empty cells."
),
DeprecationWarning,
)

if self.exists_empty_cells():
pos = random.choice(sorted(self.empties))
return pos
else:
return None

def exists_empty_cells(self) -> bool:
"""Return True if any cells empty else False."""
return len(self.empties) > 0
Expand All @@ -538,42 +510,6 @@ class SingleGrid(_Grid):
torus: Boolean which determines whether to treat the grid as a torus.
"""

def position_agent(
self, agent: Agent, x: int | str = "random", y: int | str = "random"
) -> None:
"""Position an agent on the grid.
This is used when first placing agents! Setting either x or y to "random"
gives the same behavior as 'move_to_empty()' to get a random position.
If x or y are positive, they are used.
Use 'swap_pos()' to swap agents positions.
"""
warn(
(
"`position_agent` is being deprecated; use instead "
"`place_agent` to place an agent at a specified "
"location or `move_to_empty` to place an agent "
"at a random empty cell."
),
DeprecationWarning,
)

if not (isinstance(x, int) or x == "random"):
raise Exception(
"x must be an integer or a string 'random'."
f" Actual type: {type(x)}. Actual value: {x}."
)
if not (isinstance(y, int) or y == "random"):
raise Exception(
"y must be an integer or a string 'random'."
f" Actual type: {type(y)}. Actual value: {y}."
)

if x == "random" or y == "random":
self.move_to_empty(agent)
else:
coords = (x, y)
self.place_agent(agent, coords)

def place_agent(self, agent: Agent, pos: Coordinate) -> None:
"""Place the agent at the specified location, and set its pos variable."""
if self.is_cell_empty(pos):
Expand Down
29 changes: 5 additions & 24 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,25 +265,6 @@ def setUp(self):
self.grid.place_agent(a, (x, y))
self.num_agents = len(self.agents)

@patch.object(MockAgent, "model", create=True)
def test_position_agent(self, mock_model):
a = MockAgent(100, None)
with self.assertRaises(Exception) as exc_info:
self.grid.position_agent(a, (1, 1))
expected = (
"x must be an integer or a string 'random'."
" Actual type: <class 'tuple'>. Actual value: (1, 1)."
)
assert str(exc_info.exception) == expected
with self.assertRaises(Exception) as exc_info:
self.grid.position_agent(a, "(1, 1)")
expected = (
"x must be an integer or a string 'random'."
" Actual type: <class 'str'>. Actual value: (1, 1)."
)
assert str(exc_info.exception) == expected
self.grid.position_agent(a, "random")

@patch.object(MockAgent, "model", create=True)
def test_enforcement(self, mock_model):
"""
Expand All @@ -297,28 +278,28 @@ def test_enforcement(self, mock_model):

# Place the agent in an empty cell
mock_model.schedule.get_agent_count = Mock(side_effect=lambda: len(self.agents))
self.grid.position_agent(a)
self.grid.move_to_empty(a)
self.num_agents += 1
# Test whether after placing, the empty cells are reduced by 1
assert a.pos not in self.grid.empties
assert len(self.grid.empties) == 8
for _i in range(10):
self.grid.move_to_empty(a, num_agents=self.num_agents)
self.grid.move_to_empty(a)
assert len(self.grid.empties) == 8

# Place agents until the grid is full
empty_cells = len(self.grid.empties)
for i in range(empty_cells):
a = MockAgent(101 + i, None)
self.grid.position_agent(a)
self.grid.move_to_empty(a)
self.num_agents += 1
assert len(self.grid.empties) == 0

a = MockAgent(110, None)
with self.assertRaises(Exception):
self.grid.position_agent(a)
self.grid.move_to_empty(a)
with self.assertRaises(Exception):
self.move_to_empty(self.agents[0], num_agents=self.num_agents)
self.move_to_empty(self.agents[0])


# Number of agents at each position for testing
Expand Down
4 changes: 1 addition & 3 deletions tests/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,9 @@ def test_remove_agent(self):

def test_empty_cells(self):
if self.space.exists_empty_cells():
pytest.deprecated_call(self.space.find_empty)
for i, pos in enumerate(list(self.space.empties)):
a = MockAgent(-i, pos)
self.space.position_agent(a, x=pos[0], y=pos[1])
assert self.space.find_empty() is None
self.space.place_agent(a, pos)
with self.assertRaises(Exception):
self.space.move_to_empty(a)

Expand Down