mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Automatic parallel stepping does nothing on Mesa 3.5+

Open 0xnithinmys opened this issue 1 month ago • 0 comments

enable_automatic_parallel_stepping() patches only AgentSet.shuffle_do (mesa_llm/parallel_stepping.py:78-105). Since Mesa 3.5.0, model.agents and model.agents_by_type[...] are _HardKeyAgentSet, which has its own shuffle_do and does not inherit from AgentSet. So the documented pattern, self.agents.shuffle_do("step") with parallel_stepping = True, never runs in parallel. The mesa>=3.0,<4 pin installs Mesa 3.5.1.

There are two effects:

  • Agents that implement only astep are never stepped. LLMAgent has no step of its own, so the call goes to Mesa's empty Agent.step. No error is raised.
  • Agents with a normal step run one after another. In threading mode, five 0.2 s steps took 1.0 s instead of about 0.2 s.

Expected behavior

With automatic mode on and parallel_stepping = True, model.agents.shuffle_do("step") steps the agents in parallel, as docs/apis/parallel_stepping.md describes.

To Reproduce

from mesa import Agent, Model
from mesa_llm.parallel_stepping import enable_automatic_parallel_stepping

class AsyncAgent(Agent):
    def __init__(self, model):
        super().__init__(model)
        self.steps_taken = 0

    async def astep(self):
        self.steps_taken += 1

class MyModel(Model):
    def __init__(self):
        super().__init__(rng=42)
        self.parallel_stepping = True
        enable_automatic_parallel_stepping(mode="asyncio")
        AsyncAgent.create_agents(self, 3)

    def step(self):
        self.agents.shuffle_do("step")

model = MyModel()
model.step()
print(type(model.agents).__name__, [a.steps_taken for a in model.agents])

Output with Mesa 3.5.1:

_HardKeyAgentSet [0, 0, 0]

Expected [1, 1, 1].

Additional context

  • test_automatic_parallel_shuffle_do passes because it builds an AgentSet([...]) by hand and never goes through model.agents.
  • Not covered by #162, #200, #220 or #264.
  • Proposed fix (tested locally): also patch and restore mesa.agentset._HardKeyAgentSet.shuffle_do when it exists, return self from the patched method, and add a test that steps through model.agents.

0xnithinmys avatar Oct 22 '26 16:10 0xnithinmys