Skip to content

Bug: Inconsistency Cell State Management #3068

@codebyNJ

Description

@codebyNJ

Describe the bug
The Cell class mesa/discrete_space/cell.py maintains redundant state using both cell.empty and cell.is_empty.
This leads to:

  • Possible state inconsistency cell.empty != cell.is_empty
  • Unnecessary O(n) list copying during remove_agent()
  • Use of a generic Exception when cell capacity is exceeded

Expected behavior

  • Cell emptiness should have a single source of truth
  • Checking whether a cell is empty should not copy the agent list
  • Capacity overflow should raise a specific exception type

To Reproduce

from mesa import Model
from mesa.discrete_space import OrthogonalMooreGrid
from mesa.discrete_space.cell_agent import CellAgent

model = Model()
grid = OrthogonalMooreGrid((3, 3), random=model.random)
cell = grid[(0, 0)]

cell._agents.append(CellAgent(model))  # bypass API

assert cell.is_empty 
assert cell.empty == True # inconsistent state

Additional context

  • remove_agent() triggers self.empty = self.is_empty
  • is_empty → agents → _agents.copy() causes O(n) overhead
  • Adding beyond capacity raises generic Exception

@EwoutH @quaquel I would like to know you thoughts on this particular issue. Can I raise a PR resolving the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions