Skip to content

[BUG] FixedAgent keeps a reference to a cell that may reject it #3411

@muzman123

Description

@muzman123

Describe the bug

When a FixedAgent is placed into a cell that is already at capacity, the operation partially completes and leaves the agent in a broken state. The agent's internal reference gets set to the cell before the cell has actually accepted it. If the cell rejects the agent (because it's full), the reference is never cleaned up so agent permanently believes it occupies a cell that has no record of it.

Expected behavior

If placing an agent into a cell fails, nothing should change. The agent's cell reference should remain None, exactly as it was before the attempt.

To Reproduce

from mesa import Model
from mesa.discrete_space import OrthogonalMooreGrid, FixedAgent

model = Model()
grid = OrthogonalMooreGrid((2, 2), random=model.random)
cell = list(grid.all_cells)[0]
cell.capacity = 1

a1 = FixedAgent(model)
a1.cell = cell  # works fine, cell is now full

a2 = FixedAgent(model)
try:
    a2.cell = cell  # raises — cell is full
except Exception:
    pass

print(a2.cell is cell)    # True  — agent thinks it was placed
print(a2 in cell.agents)  # False — cell has no record of it

The agent and cell are now permanently out of sync. Any logic that relies on agent.cell will silently misbehave for this agent.

Additional context

I can work on a PR to fix this if required

Discovered while building a simulation using FixedAgent and OrthogonalMooreGrid with per-cell capacity limits.

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