From 5ceb0b91455c6da42917badea2944ce94d5603a1 Mon Sep 17 00:00:00 2001 From: corvince <13568919+Corvince@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:54:20 +0200 Subject: [PATCH] Change connection name to be coordinate-like (tuple) --- mesa/experimental/cell_space/cell.py | 12 +++++++----- mesa/experimental/cell_space/grid.py | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/mesa/experimental/cell_space/cell.py b/mesa/experimental/cell_space/cell.py index 97c34abb48a..da0b4521314 100644 --- a/mesa/experimental/cell_space/cell.py +++ b/mesa/experimental/cell_space/cell.py @@ -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. @@ -43,7 +45,7 @@ class Cell: def __init__( self, - coordinate: tuple[int, ...], + coordinate: Coordinate, capacity: float | None = None, random: Random | None = None, ) -> None: @@ -57,15 +59,15 @@ 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: @@ -73,7 +75,7 @@ def connect(self, other: Cell, name: str | None = None) -> None: """ if name is None: - name = str(other.coordinate) + name = other.coordinate self.connections[name] = other def disconnect(self, other: Cell) -> None: diff --git a/mesa/experimental/cell_space/grid.py b/mesa/experimental/cell_space/grid.py index bf5c127f987..aa7d069fa74 100644 --- a/mesa/experimental/cell_space/grid.py +++ b/mesa/experimental/cell_space/grid.py @@ -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 @@ -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]):