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 18, 2024
1 parent 5bf9471 commit 64bb4f7
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 @@ -12,6 +12,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 @@ -45,7 +47,7 @@ class Cell:

def __init__(
self,
coordinate: tuple[int, ...],
coordinate: Coordinate,
capacity: float | None = None,
random: Random | None = None,
) -> None:
Expand All @@ -59,23 +61,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 @@ -102,7 +102,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 @@ -113,7 +113,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 64bb4f7

Please sign in to comment.