Skip to content

Commit

Permalink
test: Remove place_agent duplicate warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rht committed Mar 24, 2024
1 parent 3563854 commit 4eef2be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
4 changes: 0 additions & 4 deletions benchmarks/Flocking/flocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def __init__(
self,
unique_id,
model,
pos,
speed,
direction,
vision,
Expand All @@ -43,7 +42,6 @@ def __init__(
Args:
unique_id: Unique agent identifier.
pos: Starting position
speed: Distance to move per step.
direction: numpy vector for the Boid's direction of movement.
vision: Radius to look around for nearby Boids.
Expand All @@ -54,7 +52,6 @@ def __init__(
"""
super().__init__(unique_id, model)
self.pos = np.array(pos)
self.speed = speed
self.direction = direction
self.vision = vision
Expand Down Expand Up @@ -141,7 +138,6 @@ def make_agents(self):
boid = Boid(
unique_id=i,
model=self,
pos=pos,
speed=self.speed,
direction=direction,
vision=self.vision,
Expand Down
20 changes: 10 additions & 10 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class MockAgent:
Minimalistic agent for testing purposes.
"""

def __init__(self, unique_id, pos):
def __init__(self, unique_id):
self.random = random.Random(0)
self.unique_id = unique_id
self.pos = pos
self.pos = None


class TestSingleGrid(unittest.TestCase):
Expand All @@ -53,7 +53,7 @@ def setUp(self):
continue
counter += 1
# Create and place the mock agent
a = MockAgent(counter, None)
a = MockAgent(counter)
self.agents.append(a)
self.grid.place_agent(a, (x, y))

Expand Down Expand Up @@ -258,7 +258,7 @@ def setUp(self):
continue
counter += 1
# Create and place the mock agent
a = MockAgent(counter, None)
a = MockAgent(counter)
self.agents.append(a)
self.grid.place_agent(a, (x, y))
self.num_agents = len(self.agents)
Expand All @@ -270,7 +270,7 @@ def test_enforcement(self, mock_model):
"""

assert len(self.grid.empties) == 9
a = MockAgent(100, None)
a = MockAgent(100)
with self.assertRaises(Exception):
self.grid.place_agent(a, (0, 1))

Expand All @@ -288,12 +288,12 @@ def test_enforcement(self, mock_model):
# Place agents until the grid is full
empty_cells = len(self.grid.empties)
for i in range(empty_cells):
a = MockAgent(101 + i, None)
a = MockAgent(101 + i)
self.grid.move_to_empty(a)
self.num_agents += 1
assert len(self.grid.empties) == 0

a = MockAgent(110, None)
a = MockAgent(110)
with self.assertRaises(Exception):
self.grid.move_to_empty(a)
with self.assertRaises(Exception):
Expand Down Expand Up @@ -334,7 +334,7 @@ def setUp(self):
for _i in range(TEST_MULTIGRID[x][y]):
counter += 1
# Create and place the mock agent
a = MockAgent(counter, None)
a = MockAgent(counter)
self.agents.append(a)
self.grid.place_agent(a, (x, y))

Expand Down Expand Up @@ -393,7 +393,7 @@ def setUp(self):
continue
counter += 1
# Create and place the mock agent
a = MockAgent(counter, None)
a = MockAgent(counter)
self.agents.append(a)
self.grid.place_agent(a, (x, y))

Expand Down Expand Up @@ -447,7 +447,7 @@ def setUp(self):
continue
counter += 1
# Create and place the mock agent
a = MockAgent(counter, None)
a = MockAgent(counter)
self.agents.append(a)
self.grid.place_agent(a, (x, y))

Expand Down
48 changes: 24 additions & 24 deletions tests/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_agents_add_many(self):
"""
positions = np.random.rand(TEST_AGENTS_PERF, 2)
for i in range(TEST_AGENTS_PERF):
a = MockAgent(i, None)
a = MockAgent(i)
pos = [positions[i, 0], positions[i, 1]]
self.space.place_agent(a, pos)

Expand All @@ -59,7 +59,7 @@ def setUp(self):
self.space = ContinuousSpace(70, 20, True, -30, -30)
self.agents = []
for i, pos in enumerate(TEST_AGENTS):
a = MockAgent(i, None)
a = MockAgent(i)
self.agents.append(a)
self.space.place_agent(a, pos)

Expand Down Expand Up @@ -126,7 +126,7 @@ def test_bounds(self):
"""
boundary_agents = []
for i, pos in enumerate(OUTSIDE_POSITIONS):
a = MockAgent(len(self.agents) + i, None)
a = MockAgent(len(self.agents) + i)
boundary_agents.append(a)
self.space.place_agent(a, pos)

Expand All @@ -152,7 +152,7 @@ def setUp(self):
self.space = ContinuousSpace(70, 20, False, -30, -30)
self.agents = []
for i, pos in enumerate(TEST_AGENTS):
a = MockAgent(i, None)
a = MockAgent(i)
self.agents.append(a)
self.space.place_agent(a, pos)

Expand Down Expand Up @@ -208,7 +208,7 @@ def test_bounds(self):
Test positions outside of boundary
"""
for i, pos in enumerate(OUTSIDE_POSITIONS):
a = MockAgent(len(self.agents) + i, None)
a = MockAgent(len(self.agents) + i)
with self.assertRaises(Exception):
self.space.place_agent(a, pos)

Expand All @@ -231,7 +231,7 @@ def setUp(self):
self.space = ContinuousSpace(70, 50, False, -30, -30)
self.agents = []
for i, pos in enumerate(REMOVAL_TEST_AGENTS):
a = MockAgent(i, None)
a = MockAgent(i)
self.agents.append(a)
self.space.place_agent(a, pos)

Expand Down Expand Up @@ -442,7 +442,7 @@ def setUp(self):
self.space = SingleGrid(50, 50, False)
self.agents = []
for i, pos in enumerate(TEST_AGENTS_GRID):
a = MockAgent(i, None)
a = MockAgent(i)
self.agents.append(a)
self.space.place_agent(a, pos)

Expand All @@ -466,7 +466,7 @@ def test_remove_agent(self):
def test_empty_cells(self):
if self.space.exists_empty_cells():
for i, pos in enumerate(list(self.space.empties)):
a = MockAgent(-i, pos)
a = MockAgent(-i)
self.space.place_agent(a, pos)
with self.assertRaises(Exception):
self.space.move_to_empty(a)
Expand Down Expand Up @@ -551,7 +551,7 @@ def setUp(self):
self.space = SingleGrid(50, 50, True) # Torus is True here
self.agents = []
for i, pos in enumerate(TEST_AGENTS_GRID):
a = MockAgent(i, None)
a = MockAgent(i)
self.agents.append(a)
self.space.place_agent(a, pos)

Expand Down Expand Up @@ -643,7 +643,7 @@ def test_get_empty_mask(self):
self.assertTrue(np.all(empty_mask == np.ones((10, 10), dtype=bool)))

def test_get_empty_mask_with_agent(self):
agent = MockAgent(0, self.grid)
agent = MockAgent(0)
self.grid.place_agent(agent, (4, 6))

empty_mask = self.grid.empty_mask
Expand All @@ -653,8 +653,8 @@ def test_get_empty_mask_with_agent(self):
self.assertTrue(np.all(empty_mask == expected_mask))

def test_get_neighborhood_mask(self):
agent = MockAgent(0, self.grid)
agent2 = MockAgent(1, self.grid)
agent = MockAgent(0)
agent2 = MockAgent(1)
self.grid.place_agent(agent, (5, 5))
self.grid.place_agent(agent2, (5, 6))
neighborhood_mask = self.grid.get_neighborhood_mask((5, 5), True, False, 1)
Expand All @@ -680,7 +680,7 @@ def condition(x):
self.assertTrue(selected_mask.all())

def test_move_agent_to_cell_by_properties(self):
agent = MockAgent(1, self.grid)
agent = MockAgent(1)
self.grid.place_agent(agent, (5, 5))
conditions = {"layer1": lambda x: x == 0}
target_cells = self.grid.select_cells(conditions)
Expand All @@ -689,7 +689,7 @@ def test_move_agent_to_cell_by_properties(self):
self.assertNotEqual(agent.pos, (5, 5))

def test_move_agent_no_eligible_cells(self):
agent = MockAgent(3, self.grid)
agent = MockAgent(3)
self.grid.place_agent(agent, (5, 5))
conditions = {"layer1": lambda x: x != 0}
target_cells = self.grid.select_cells(conditions)
Expand All @@ -711,7 +711,7 @@ def test_select_extreme_value_cells_return_mask(self):
self.assertTrue(target_mask[3, 1])

def test_move_agent_to_extreme_value_cell(self):
agent = MockAgent(2, self.grid)
agent = MockAgent(2)
self.grid.place_agent(agent, (5, 5))
self.grid.properties["layer2"].set_cell((3, 1), 1.1)
target_cells = self.grid.select_cells(extreme_values={"layer2": "highest"})
Expand All @@ -721,7 +721,7 @@ def test_move_agent_to_extreme_value_cell(self):
# Test using masks
def test_select_cells_by_properties_with_empty_mask(self):
self.grid.place_agent(
MockAgent(0, self.grid), (5, 5)
MockAgent(0), (5, 5)
) # Placing an agent to ensure some cells are not empty
empty_mask = self.grid.empty_mask

Expand Down Expand Up @@ -755,10 +755,10 @@ def condition(x):
self.assertCountEqual(selected_cells, expected_selection)

def test_move_agent_to_cell_by_properties_with_empty_mask(self):
agent = MockAgent(1, self.grid)
agent = MockAgent(1)
self.grid.place_agent(agent, (5, 5))
self.grid.place_agent(
MockAgent(2, self.grid), (4, 5)
MockAgent(2), (4, 5)
) # Placing another agent to create a non-empty cell
empty_mask = self.grid.empty_mask
conditions = {"layer1": lambda x: x == 0}
Expand All @@ -769,7 +769,7 @@ def test_move_agent_to_cell_by_properties_with_empty_mask(self):
) # Agent should not move to (4, 5) as it's not empty

def test_move_agent_to_cell_by_properties_with_neighborhood_mask(self):
agent = MockAgent(1, self.grid)
agent = MockAgent(1)
self.grid.place_agent(agent, (5, 5))
neighborhood_mask = self.grid.get_neighborhood_mask((5, 5), True, False, 1)
conditions = {"layer1": lambda x: x == 0}
Expand All @@ -789,7 +789,7 @@ def condition(x):

# Test if coordinates means the same between the grid and the property layer
def test_property_layer_coordinates(self):
agent = MockAgent(0, self.grid)
agent = MockAgent(0)
correct_pos = (1, 8)
incorrect_pos = (8, 1)
self.grid.place_agent(agent, correct_pos)
Expand All @@ -811,14 +811,14 @@ def test_property_layer_coordinates(self):

# Test selecting cells with only_empty parameter
def test_select_cells_only_empty(self):
self.grid.place_agent(MockAgent(0, self.grid), (5, 5)) # Occupying a cell
self.grid.place_agent(MockAgent(0), (5, 5)) # Occupying a cell
selected_cells = self.grid.select_cells(only_empty=True)
self.assertNotIn(
(5, 5), selected_cells
) # The occupied cell should not be selected

def test_select_cells_only_empty_with_conditions(self):
self.grid.place_agent(MockAgent(1, self.grid), (5, 5))
self.grid.place_agent(MockAgent(1), (5, 5))
self.grid.properties["layer1"].set_cell((5, 5), 2)
self.grid.properties["layer1"].set_cell((6, 6), 2)

Expand Down Expand Up @@ -855,7 +855,7 @@ def setUp(self):
self.space = NetworkGrid(G)
self.agents = []
for i, pos in enumerate(TEST_AGENTS_NETWORK_SINGLE):
a = MockAgent(i, None)
a = MockAgent(i)
self.agents.append(a)
self.space.place_agent(a, pos)

Expand Down Expand Up @@ -968,7 +968,7 @@ def setUp(self):
self.space = NetworkGrid(G)
self.agents = []
for i, pos in enumerate(TEST_AGENTS_NETWORK_MULTIPLE):
a = MockAgent(i, None)
a = MockAgent(i)
self.agents.append(a)
self.space.place_agent(a, pos)

Expand Down

0 comments on commit 4eef2be

Please sign in to comment.