Skip to content

Commit 4eef2be

Browse files
committed
test: Remove place_agent duplicate warnings
1 parent 3563854 commit 4eef2be

File tree

3 files changed

+34
-38
lines changed

3 files changed

+34
-38
lines changed

benchmarks/Flocking/flocking.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def __init__(
2929
self,
3030
unique_id,
3131
model,
32-
pos,
3332
speed,
3433
direction,
3534
vision,
@@ -43,7 +42,6 @@ def __init__(
4342
4443
Args:
4544
unique_id: Unique agent identifier.
46-
pos: Starting position
4745
speed: Distance to move per step.
4846
direction: numpy vector for the Boid's direction of movement.
4947
vision: Radius to look around for nearby Boids.
@@ -54,7 +52,6 @@ def __init__(
5452
5553
"""
5654
super().__init__(unique_id, model)
57-
self.pos = np.array(pos)
5855
self.speed = speed
5956
self.direction = direction
6057
self.vision = vision
@@ -141,7 +138,6 @@ def make_agents(self):
141138
boid = Boid(
142139
unique_id=i,
143140
model=self,
144-
pos=pos,
145141
speed=self.speed,
146142
direction=direction,
147143
vision=self.vision,

tests/test_grid.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class MockAgent:
2424
Minimalistic agent for testing purposes.
2525
"""
2626

27-
def __init__(self, unique_id, pos):
27+
def __init__(self, unique_id):
2828
self.random = random.Random(0)
2929
self.unique_id = unique_id
30-
self.pos = pos
30+
self.pos = None
3131

3232

3333
class TestSingleGrid(unittest.TestCase):
@@ -53,7 +53,7 @@ def setUp(self):
5353
continue
5454
counter += 1
5555
# Create and place the mock agent
56-
a = MockAgent(counter, None)
56+
a = MockAgent(counter)
5757
self.agents.append(a)
5858
self.grid.place_agent(a, (x, y))
5959

@@ -258,7 +258,7 @@ def setUp(self):
258258
continue
259259
counter += 1
260260
# Create and place the mock agent
261-
a = MockAgent(counter, None)
261+
a = MockAgent(counter)
262262
self.agents.append(a)
263263
self.grid.place_agent(a, (x, y))
264264
self.num_agents = len(self.agents)
@@ -270,7 +270,7 @@ def test_enforcement(self, mock_model):
270270
"""
271271

272272
assert len(self.grid.empties) == 9
273-
a = MockAgent(100, None)
273+
a = MockAgent(100)
274274
with self.assertRaises(Exception):
275275
self.grid.place_agent(a, (0, 1))
276276

@@ -288,12 +288,12 @@ def test_enforcement(self, mock_model):
288288
# Place agents until the grid is full
289289
empty_cells = len(self.grid.empties)
290290
for i in range(empty_cells):
291-
a = MockAgent(101 + i, None)
291+
a = MockAgent(101 + i)
292292
self.grid.move_to_empty(a)
293293
self.num_agents += 1
294294
assert len(self.grid.empties) == 0
295295

296-
a = MockAgent(110, None)
296+
a = MockAgent(110)
297297
with self.assertRaises(Exception):
298298
self.grid.move_to_empty(a)
299299
with self.assertRaises(Exception):
@@ -334,7 +334,7 @@ def setUp(self):
334334
for _i in range(TEST_MULTIGRID[x][y]):
335335
counter += 1
336336
# Create and place the mock agent
337-
a = MockAgent(counter, None)
337+
a = MockAgent(counter)
338338
self.agents.append(a)
339339
self.grid.place_agent(a, (x, y))
340340

@@ -393,7 +393,7 @@ def setUp(self):
393393
continue
394394
counter += 1
395395
# Create and place the mock agent
396-
a = MockAgent(counter, None)
396+
a = MockAgent(counter)
397397
self.agents.append(a)
398398
self.grid.place_agent(a, (x, y))
399399

@@ -447,7 +447,7 @@ def setUp(self):
447447
continue
448448
counter += 1
449449
# Create and place the mock agent
450-
a = MockAgent(counter, None)
450+
a = MockAgent(counter)
451451
self.agents.append(a)
452452
self.grid.place_agent(a, (x, y))
453453

tests/test_space.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_agents_add_many(self):
4242
"""
4343
positions = np.random.rand(TEST_AGENTS_PERF, 2)
4444
for i in range(TEST_AGENTS_PERF):
45-
a = MockAgent(i, None)
45+
a = MockAgent(i)
4646
pos = [positions[i, 0], positions[i, 1]]
4747
self.space.place_agent(a, pos)
4848

@@ -59,7 +59,7 @@ def setUp(self):
5959
self.space = ContinuousSpace(70, 20, True, -30, -30)
6060
self.agents = []
6161
for i, pos in enumerate(TEST_AGENTS):
62-
a = MockAgent(i, None)
62+
a = MockAgent(i)
6363
self.agents.append(a)
6464
self.space.place_agent(a, pos)
6565

@@ -126,7 +126,7 @@ def test_bounds(self):
126126
"""
127127
boundary_agents = []
128128
for i, pos in enumerate(OUTSIDE_POSITIONS):
129-
a = MockAgent(len(self.agents) + i, None)
129+
a = MockAgent(len(self.agents) + i)
130130
boundary_agents.append(a)
131131
self.space.place_agent(a, pos)
132132

@@ -152,7 +152,7 @@ def setUp(self):
152152
self.space = ContinuousSpace(70, 20, False, -30, -30)
153153
self.agents = []
154154
for i, pos in enumerate(TEST_AGENTS):
155-
a = MockAgent(i, None)
155+
a = MockAgent(i)
156156
self.agents.append(a)
157157
self.space.place_agent(a, pos)
158158

@@ -208,7 +208,7 @@ def test_bounds(self):
208208
Test positions outside of boundary
209209
"""
210210
for i, pos in enumerate(OUTSIDE_POSITIONS):
211-
a = MockAgent(len(self.agents) + i, None)
211+
a = MockAgent(len(self.agents) + i)
212212
with self.assertRaises(Exception):
213213
self.space.place_agent(a, pos)
214214

@@ -231,7 +231,7 @@ def setUp(self):
231231
self.space = ContinuousSpace(70, 50, False, -30, -30)
232232
self.agents = []
233233
for i, pos in enumerate(REMOVAL_TEST_AGENTS):
234-
a = MockAgent(i, None)
234+
a = MockAgent(i)
235235
self.agents.append(a)
236236
self.space.place_agent(a, pos)
237237

@@ -442,7 +442,7 @@ def setUp(self):
442442
self.space = SingleGrid(50, 50, False)
443443
self.agents = []
444444
for i, pos in enumerate(TEST_AGENTS_GRID):
445-
a = MockAgent(i, None)
445+
a = MockAgent(i)
446446
self.agents.append(a)
447447
self.space.place_agent(a, pos)
448448

@@ -466,7 +466,7 @@ def test_remove_agent(self):
466466
def test_empty_cells(self):
467467
if self.space.exists_empty_cells():
468468
for i, pos in enumerate(list(self.space.empties)):
469-
a = MockAgent(-i, pos)
469+
a = MockAgent(-i)
470470
self.space.place_agent(a, pos)
471471
with self.assertRaises(Exception):
472472
self.space.move_to_empty(a)
@@ -551,7 +551,7 @@ def setUp(self):
551551
self.space = SingleGrid(50, 50, True) # Torus is True here
552552
self.agents = []
553553
for i, pos in enumerate(TEST_AGENTS_GRID):
554-
a = MockAgent(i, None)
554+
a = MockAgent(i)
555555
self.agents.append(a)
556556
self.space.place_agent(a, pos)
557557

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

645645
def test_get_empty_mask_with_agent(self):
646-
agent = MockAgent(0, self.grid)
646+
agent = MockAgent(0)
647647
self.grid.place_agent(agent, (4, 6))
648648

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

655655
def test_get_neighborhood_mask(self):
656-
agent = MockAgent(0, self.grid)
657-
agent2 = MockAgent(1, self.grid)
656+
agent = MockAgent(0)
657+
agent2 = MockAgent(1)
658658
self.grid.place_agent(agent, (5, 5))
659659
self.grid.place_agent(agent2, (5, 6))
660660
neighborhood_mask = self.grid.get_neighborhood_mask((5, 5), True, False, 1)
@@ -680,7 +680,7 @@ def condition(x):
680680
self.assertTrue(selected_mask.all())
681681

682682
def test_move_agent_to_cell_by_properties(self):
683-
agent = MockAgent(1, self.grid)
683+
agent = MockAgent(1)
684684
self.grid.place_agent(agent, (5, 5))
685685
conditions = {"layer1": lambda x: x == 0}
686686
target_cells = self.grid.select_cells(conditions)
@@ -689,7 +689,7 @@ def test_move_agent_to_cell_by_properties(self):
689689
self.assertNotEqual(agent.pos, (5, 5))
690690

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

713713
def test_move_agent_to_extreme_value_cell(self):
714-
agent = MockAgent(2, self.grid)
714+
agent = MockAgent(2)
715715
self.grid.place_agent(agent, (5, 5))
716716
self.grid.properties["layer2"].set_cell((3, 1), 1.1)
717717
target_cells = self.grid.select_cells(extreme_values={"layer2": "highest"})
@@ -721,7 +721,7 @@ def test_move_agent_to_extreme_value_cell(self):
721721
# Test using masks
722722
def test_select_cells_by_properties_with_empty_mask(self):
723723
self.grid.place_agent(
724-
MockAgent(0, self.grid), (5, 5)
724+
MockAgent(0), (5, 5)
725725
) # Placing an agent to ensure some cells are not empty
726726
empty_mask = self.grid.empty_mask
727727

@@ -755,10 +755,10 @@ def condition(x):
755755
self.assertCountEqual(selected_cells, expected_selection)
756756

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

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

790790
# Test if coordinates means the same between the grid and the property layer
791791
def test_property_layer_coordinates(self):
792-
agent = MockAgent(0, self.grid)
792+
agent = MockAgent(0)
793793
correct_pos = (1, 8)
794794
incorrect_pos = (8, 1)
795795
self.grid.place_agent(agent, correct_pos)
@@ -811,14 +811,14 @@ def test_property_layer_coordinates(self):
811811

812812
# Test selecting cells with only_empty parameter
813813
def test_select_cells_only_empty(self):
814-
self.grid.place_agent(MockAgent(0, self.grid), (5, 5)) # Occupying a cell
814+
self.grid.place_agent(MockAgent(0), (5, 5)) # Occupying a cell
815815
selected_cells = self.grid.select_cells(only_empty=True)
816816
self.assertNotIn(
817817
(5, 5), selected_cells
818818
) # The occupied cell should not be selected
819819

820820
def test_select_cells_only_empty_with_conditions(self):
821-
self.grid.place_agent(MockAgent(1, self.grid), (5, 5))
821+
self.grid.place_agent(MockAgent(1), (5, 5))
822822
self.grid.properties["layer1"].set_cell((5, 5), 2)
823823
self.grid.properties["layer1"].set_cell((6, 6), 2)
824824

@@ -855,7 +855,7 @@ def setUp(self):
855855
self.space = NetworkGrid(G)
856856
self.agents = []
857857
for i, pos in enumerate(TEST_AGENTS_NETWORK_SINGLE):
858-
a = MockAgent(i, None)
858+
a = MockAgent(i)
859859
self.agents.append(a)
860860
self.space.place_agent(a, pos)
861861

@@ -968,7 +968,7 @@ def setUp(self):
968968
self.space = NetworkGrid(G)
969969
self.agents = []
970970
for i, pos in enumerate(TEST_AGENTS_NETWORK_MULTIPLE):
971-
a = MockAgent(i, None)
971+
a = MockAgent(i)
972972
self.agents.append(a)
973973
self.space.place_agent(a, pos)
974974

0 commit comments

Comments
 (0)