mesa / mesa/mesa

cell attribute in CellAgent and FixedAgent

Open
#3,687 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
3.9k
Forks
1.3k
Avg merge
2d 12h
Merged PRs (30d)
20

Description

The`cell` property defined in `HasCell` is not available in the signature of the subclass of `CellAgent` or `FixedAgent`

**Reason -** This is because `CellAgent`, `FixedAgent` or `HasCell` doesn't initialise the property

**Effect -** This causes `AttributeError` or `TypeError` when the subclass of those classes doesn't define the `cell` attribute in their signature or consume it.

This feels counterintuitive as both `CellAgent` and `FixedAgent` says they have `cell` attribute and when a subclass tries to access it, it throws error.

My doubt is, was this intentional? If it's not, I can make a PR for the solution (only after the issue is aligned).

**To reproduce the error:**

```python

from mesa import Model
from mesa.discrete_space import Cell, CellAgent, Network
import networkx as nx

class MockAgent(CellAgent):
"""Mock Agent with no custom step"""
def __init__(self, model, **kwargs):

# Causes TypeError when the call hits the python object as cell was never consumed downstream
# MRO: CellAgent -> Agent -> HasAgent -> BaseMovement -> object
super().__init__(model, **kwargs)

# This works as the user consumes `cell` but counterintuitive
"""
def __init__(self, model, cell):
super().__init__(model)
self.cell = cell # @property in HasCell
"""

class MockModel(Model):
"""Mock model with no custom step"""

N = 2

M = MockModel()
main_network = nx.complete_graph(N)
main_layout = nx.random_layout(main_network)

grid = Network(
G = main_network,
capacity = N,
layout = main_layout,
)

agents = list(MockAgent.create_agents(
model = M,
n = N,
cell = list(grid.all_cells)
))

```

I caught this behaviour while developing a model in my [repo](https://github.com/IlamaranMagesh/mesa-learning-space/tree/contagion-model)

Contributor guide

Open the contributing guide

Research direction

Start by tracing initialization through HasCell, CellAgent, FixedAgent, and their parent classes, using the reproduction in the issue to observe the failing subclass construction. Check how cell is handled when a subclass neither declares nor consumes it. Done means the shown subclass can be created and its cell property accessed without AttributeError or TypeError, with regression coverage for the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.