mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Should Agent.step() be enforced as abstract, or warn if not overridden?

Open dietcoke-17 opened this issue 5 months ago • 2 comments

Problem Agent.step() and Agent.advance() are empty stubs meant to be overridden by subclasses, but Mesa doesn't enforce this. A user can write:

model = Model()
agent = Agent(model)
agent.step()  # Silent no output — user doesn't know they forgot to override

The agent does nothing. No error. No warning. The user has no idea. This is a common Python OOP bug — empty base methods that should signal "you must implement this."

Option A: Make Agent abstract with @abstractmethod (breaking change) Pros:

  • Enforced at class definition time — can't instantiate bare Agent() at all
  • Type checkers (mypy, pyright) will catch missing implementations
  • Follows Python's standard OOP pattern

Cons:

  • Breaking change — any code doing Agent(model) will fail
  • Would require Mesa 4.0+ release
  • Subclasses that don't define step() would also break

Option B: Add warnings.warn (non-breaking) Pros:

  • Non-breaking — existing code continues to work
  • Users who forgot to override get alerted
  • Easy to implement and test

Cons:

  • Warning can be silenced by user (even accidentally)
  • Doesn't prevent the bug, only warns about it
  • Adds runtime overhead to every call

Option C: Both (warn in Mesa 3.x, enforce in Mesa 4.0) Add warnings now, then switch to ABC in the next major version when breaking changes are expected.

Which direction should we go? I can submit a PR for whichever approach you prefer.

dietcoke-17 avatar Jun 24 '26 05:06 dietcoke-17

I see where you are coming from, but I have to disagree with the premise that this is a bug that needs fixing in Mesa. In Mesa, Agent.step() and Agent.advance() are intentionally left as empty stubs for several reasons:

  1. Passive Agents: Not every agent in a simulation is an active decision-maker. Models frequently utilize inanimate or passive agents (e.g., obstacles, environmental markers, or static resources). If we enforce step() via @abstractmethod or warnings, we force developers to write boilerplate def step(self): pass for every single passive entity, which severely degrades the developer experience.

  2. The advance() method comes under SimultaneousActivation. If a user is building a standard model with RandomActivation or SequentialActivation(refer https://github.com/mesa/mesa/blob/main/docs/tutorials/2_agent_activation.ipynb), throwing a warning that they forgot to implement advance() would be inaccurate and confusing.

  3. The framework is actively shifting toward Event scheduling and time-based actions. In this new architecture, agents can react to scheduled events or execute specific Actions without ever relying on a global, synchronous step() loop. Forcing the implementation of step() would actually conflict with where the framework's internal architecture is heading

The silent "no output" behavior is exactly what a passive agent should do. While making Agent an ABC makes sense in pure OOP theory, it adds unnecessary rigidity to an ABM framework.

codebreaker32 avatar Jun 24 '26 06:06 codebreaker32

And

  1. step() and andvace() are conventions but they are not actively used inside mesa. Typically, a user will do something like self.agents.shuffle_do("my_custom_method").

quaquel avatar Jun 24 '26 07:06 quaquel