Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Hexgrid._connect_cells_2d to use x,y coordinates #2632

Merged
merged 4 commits into from
Jan 20, 2025
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
16 changes: 8 additions & 8 deletions mesa/experimental/cell_space/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,20 +273,20 @@ class HexGrid(Grid[T]):
def _connect_cells_2d(self) -> None:
# fmt: off
even_offsets = [
(-1, -1), (-1, 0),
( 0, -1), ( 0, 1),
( 1, -1), ( 1, 0),
(-1, -1), (0, -1),
( -1, 0), ( 1, 0),
( -1, 1), (0, 1),
]
odd_offsets = [
(-1, 0), (-1, 1),
( 0, -1), ( 0, 1),
( 1, 0), ( 1, 1),
(0, -1), (1, -1),
( -1, 0), ( 1, 0),
( 0, 1), ( 1, 1),
]
# fmt: on

for cell in self.all_cells:
i = cell.coordinate[0]
offsets = even_offsets if i % 2 == 0 else odd_offsets
i = cell.coordinate[1]
offsets = even_offsets if i % 2 else odd_offsets
self._connect_single_cell_2d(cell, offsets=offsets)

def _connect_cells_nd(self) -> None:
Expand Down
34 changes: 17 additions & 17 deletions tests/test_cell_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def test_cell_neighborhood():
grid = HexGrid(
(width, height), torus=False, capacity=None, random=random.Random(42)
)
for radius, n in zip(range(1, 4), [2, 6, 11]):
for radius, n in zip(range(1, 4), [3, 7, 13]):
if radius == 1:
neighborhood = grid._cells[(0, 0)].neighborhood
else:
Expand All @@ -366,7 +366,7 @@ def test_cell_neighborhood():
grid = HexGrid(
(width, height), torus=False, capacity=None, random=random.Random(42)
)
for radius, n in zip(range(1, 4), [5, 10, 17]):
for radius, n in zip(range(1, 4), [4, 10, 17]):
if radius == 1:
neighborhood = grid._cells[(1, 0)].neighborhood
else:
Expand All @@ -385,35 +385,35 @@ def test_hexgrid():
assert len(grid._cells) == width * height

# first row
assert len(grid._cells[(0, 0)].connections.values()) == 2
assert len(grid._cells[(0, 0)].connections.values()) == 3
for connection in grid._cells[(0, 0)].connections.values():
assert connection.coordinate in {(0, 1), (1, 0)}
assert connection.coordinate in {(0, 1), (1, 0), (1, 1)}

# second row
assert len(grid._cells[(1, 0)].connections.values()) == 5
assert len(grid._cells[(1, 0)].connections.values()) == 4
for connection in grid._cells[(1, 0)].connections.values():
# fmt: off
assert connection.coordinate in {(0, 0), (0, 1),
(1, 1),
(2, 0), (2, 1)}
assert connection.coordinate in { (1, 1), (2, 1),
(0, 0), (2, 0),}
# fmt: on

# middle odd row
assert len(grid._cells[(5, 5)].connections.values()) == 6
for connection in grid._cells[(5, 5)].connections.values():
# fmt: off
assert connection.coordinate in {(4, 5), (4, 6),
(5, 4), (5, 6),
(6, 5), (6, 6)}
assert connection.coordinate in { (4, 4), (5, 4),
(4, 5), (6, 5),
(4, 6), (5, 6)}

# fmt: on

# middle even row
assert len(grid._cells[(4, 4)].connections.values()) == 6
for connection in grid._cells[(4, 4)].connections.values():
# fmt: off
assert connection.coordinate in {(3, 3), (3, 4),
(4, 3), (4, 5),
(5, 3), (5, 4)}
assert connection.coordinate in {(4, 3), (5, 3),
(3, 4), (5, 4),
(4, 5), (5, 5)}

# fmt: on

Expand All @@ -424,9 +424,9 @@ def test_hexgrid():
assert len(grid._cells[(0, 0)].connections.values()) == 6
for connection in grid._cells[(0, 0)].connections.values():
# fmt: off
assert connection.coordinate in {(9, 9), (9, 0),
(0, 9), (0, 1),
(1, 9), (1, 0)}
assert connection.coordinate in {(0, 9), (1, 9),
(9, 0), (1, 0),
(0, 1), (1, 1)}

# fmt: on

Expand Down
Loading