diff --git a/docs/best-practices.rst b/docs/best-practices.rst index 4519d6881ff..a73c125f899 100644 --- a/docs/best-practices.rst +++ b/docs/best-practices.rst @@ -27,7 +27,7 @@ organize them. For example, if the visualization uses image files, put those in an ``images`` directory. The `Schelling -`_ model is +`_ model is a good example of a small well-packaged model. It's easy to create a cookiecutter mesa model by running ``mesa startproject`` diff --git a/mesa/space.py b/mesa/space.py index abdb1869f58..96e1a4da317 100644 --- a/mesa/space.py +++ b/mesa/space.py @@ -454,16 +454,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") @@ -486,26 +478,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 @@ -523,42 +495,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): diff --git a/tests/test_grid.py b/tests/test_grid.py index a24db60a594..16d70730b3b 100644 --- a/tests/test_grid.py +++ b/tests/test_grid.py @@ -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: . 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: . 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): """ @@ -297,26 +278,26 @@ 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) diff --git a/tests/test_space.py b/tests/test_space.py index c445135c0aa..8691dc45f0a 100644 --- a/tests/test_space.py +++ b/tests/test_space.py @@ -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)