Skip to content

Commit

Permalink
Change connection name to be coordinate-like (tuple)
Browse files Browse the repository at this point in the history
  • Loading branch information
Corvince committed Sep 16, 2024
1 parent 384a7ce commit 5ceb0b9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions mesa/experimental/cell_space/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from mesa.agent import Agent
from mesa.experimental.cell_space.cell_agent import CellAgent

Coordinate = tuple[int, ...]


class Cell:
"""The cell represents a position in a discrete space.
Expand Down Expand Up @@ -43,7 +45,7 @@ class Cell:

def __init__(
self,
coordinate: tuple[int, ...],
coordinate: Coordinate,
capacity: float | None = None,
random: Random | None = None,
) -> None:
Expand All @@ -57,23 +59,23 @@ def __init__(
"""
super().__init__()
self.coordinate = coordinate
self.connections: dict[str, Cell] = {}
self.connections: dict[Coordinate, Cell] = {}
self.agents: list[
Agent
] = [] # TODO:: change to AgentSet or weakrefs? (neither is very performant, )
self.capacity = capacity
self.properties: dict[str, object] = {}
self.properties: dict[Coordinate, object] = {}
self.random = random

def connect(self, other: Cell, name: str | None = None) -> None:
def connect(self, other: Cell, name: Coordinate | None = None) -> None:
"""Connects this cell to another cell.
Args:
other (Cell): other cell to connect to
"""
if name is None:
name = str(other.coordinate)
name = other.coordinate
self.connections[name] = other

def disconnect(self, other: Cell) -> None:
Expand Down
4 changes: 2 additions & 2 deletions mesa/experimental/cell_space/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _connect_single_cell_nd(self, cell: T, offsets: list[tuple[int, ...]]) -> No
if self.torus:
n_coord = tuple(nc % d for nc, d in zip(n_coord, self.dimensions))
if all(0 <= nc < d for nc, d in zip(n_coord, self.dimensions)):
cell.connect(self._cells[n_coord], f"{d_coord}")
cell.connect(self._cells[n_coord], d_coord)

def _connect_single_cell_2d(self, cell: T, offsets: list[tuple[int, int]]) -> None:
i, j = cell.coordinate
Expand All @@ -100,7 +100,7 @@ def _connect_single_cell_2d(self, cell: T, offsets: list[tuple[int, int]]) -> No
if self.torus:
ni, nj = ni % height, nj % width
if 0 <= ni < height and 0 <= nj < width:
cell.connect(self._cells[ni, nj], f"{(di, dj)}")
cell.connect(self._cells[ni, nj], (di, dj))


class OrthogonalMooreGrid(Grid[T]):
Expand Down

0 comments on commit 5ceb0b9

Please sign in to comment.