Skip to content

Commit

Permalink
Minor (bug) fixes to discrete_space (#2687)
Browse files Browse the repository at this point in the history
* minor (bug) fixes

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update __init__.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update cell.py

* more fixes

* updates

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
quaquel and pre-commit-ci[bot] authored Feb 13, 2025
1 parent 09cfde8 commit 22817e7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 2 additions & 0 deletions mesa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import datetime

import mesa.discrete_space as discrete_space
import mesa.experimental as experimental
import mesa.space as space
from mesa.agent import Agent
Expand All @@ -17,6 +18,7 @@
"DataCollector",
"Model",
"batch_run",
"discrete_space",
"experimental",
"space",
]
Expand Down
21 changes: 13 additions & 8 deletions mesa/discrete_space/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Cell:

__slots__ = [
"__dict__",
"agents",
"_agents",
"capacity",
"connections",
"coordinate",
Expand All @@ -65,8 +65,8 @@ def __init__(
super().__init__()
self.coordinate = coordinate
self.connections: dict[Coordinate, Cell] = {}
self.agents: list[
Agent
self._agents: list[
CellAgent
] = [] # TODO:: change to AgentSet or weakrefs? (neither is very performant, )
self.capacity: int | None = capacity
self.properties: dict[
Expand Down Expand Up @@ -104,15 +104,15 @@ def add_agent(self, agent: CellAgent) -> None:
agent (CellAgent): agent to add to this Cell
"""
n = len(self.agents)
n = len(self._agents)
self.empty = False

if self.capacity and n >= self.capacity:
raise Exception(
"ERROR: Cell is full"
) # FIXME we need MESA errors or a proper error

self.agents.append(agent)
self._agents.append(agent)

def remove_agent(self, agent: CellAgent) -> None:
"""Removes an agent from the cell.
Expand All @@ -121,7 +121,7 @@ def remove_agent(self, agent: CellAgent) -> None:
agent (CellAgent): agent to remove from this cell
"""
self.agents.remove(agent)
self._agents.remove(agent)
self.empty = self.is_empty

@property
Expand All @@ -134,6 +134,11 @@ def is_full(self) -> bool:
"""Returns a bool of the contents of a cell."""
return len(self.agents) == self.capacity

@property
def agents(self) -> list[CellAgent]:
"""Returns a list of the agents occupying the cell."""
return self._agents.copy()

def __repr__(self): # noqa
return f"Cell({self.coordinate}, {self.agents})"

Expand Down Expand Up @@ -180,12 +185,12 @@ def _neighborhood(
raise ValueError("radius must be larger than one")
if radius == 1:
neighborhood = {
neighbor: neighbor.agents for neighbor in self.connections.values()
neighbor: neighbor._agents for neighbor in self.connections.values()
}
if not include_center:
return neighborhood
else:
neighborhood[self] = self.agents
neighborhood[self] = self._agents
return neighborhood
else:
neighborhood: dict[Cell, list[Agent]] = {}
Expand Down
2 changes: 1 addition & 1 deletion mesa/discrete_space/cell_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(
if isinstance(cells, dict):
self._cells = cells
else:
self._cells = {cell: cell.agents for cell in cells}
self._cells = {cell: cell._agents for cell in cells}

# Get capacity from first cell if collection is not empty
self._capacity: int | None = (
Expand Down
2 changes: 1 addition & 1 deletion mesa/discrete_space/discrete_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _connect_single_cell(self, cell: T): ...
def all_cells(self):
"""Return all cells in space."""
return CellCollection(
{cell: cell.agents for cell in self._cells.values()}, random=self.random
{cell: cell._agents for cell in self._cells.values()}, random=self.random
)

def __iter__(self): # noqa
Expand Down
4 changes: 2 additions & 2 deletions mesa/examples/basic/boltzmann_wealth_model/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from mesa.examples.basic.boltzmann_wealth_model.model import BoltzmannWealth
from mesa.mesa_logging import DEBUG, log_to_stderr
from mesa.mesa_logging import INFO, log_to_stderr
from mesa.visualization import (
SolaraViz,
make_plot_component,
make_space_component,
)

log_to_stderr(DEBUG)
log_to_stderr(INFO)


def agent_portrayal(agent):
Expand Down

0 comments on commit 22817e7

Please sign in to comment.