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
2 changes: 1 addition & 1 deletion examples/schelling/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(self, width=20, height=20, density=0.8, minority_pc=0.2, homophily=
agent_type = 0

agent = SchellingAgent((x, y), self, agent_type)
self.grid.position_agent(agent, (x, y))
self.grid.position_agent(agent, x, y)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bug where in the Schelling example, the number of agents in the scheduler does not match the number of agents in the space.

self.schedule.add(agent)

self.running = True
Expand Down
11 changes: 11 additions & 0 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,17 @@ def position_agent(
If x or y are positive, they are used.
Use 'swap_pos()' to swap agents positions.
"""
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":
if len(self.empties) == 0:
raise Exception("ERROR: Grid full")
Expand Down
19 changes: 19 additions & 0 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,25 @@ 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 Down