Skip to content

Agent assignment to a full cell leaves inconsistent state #3373

@souro26

Description

@souro26

Summary

When assigning a CellAgent to a cell that is already at capacity, an exception is raised, but the system is left in an
inconsistent state. After the failed assignment, agent.cell references the cell and the agent is not present in cell.agents. This violates the expected relationship between agents and cells.

Reproduction

from mesa.discrete_space.grid import OrthogonalVonNeumannGrid
from mesa.discrete_space.cell_agent import CellAgent
from mesa import Model


class DummyModel(Model):
    pass


model = DummyModel()
grid = OrthogonalVonNeumannGrid((1, 1), capacity=1)

cell = grid[(0, 0)]

a1 = CellAgent(model)
a2 = CellAgent(model)

# Fill the cell
a1.cell = cell

# Attempt to overfill
try:
    a2.cell = cell
except Exception as e:
    print("Placement failed as expected:", e)

print("a2.cell:", a2.cell)
print("Is a2 in cell.agents?:", a2 in cell.agents)

Current Behavior

The output is,

Placement failed as expected: ERROR: Cell is full
a2.cell: Cell((0, 0), [...])
Is a2 in cell.agents?: False
The agent believes it belongs to the cell, but the cell does not contain the agent.

Expected Behavior

If assignment fails due to capacity, the agent’s state should remain unchanged and consistent. The setter should not partially mutate state before raising.

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