mesa
mesa copied to clipboard
Automatic parallel stepping does nothing on Mesa 3.5+
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
astepare never stepped.LLMAgenthas nostepof its own, so the call goes to Mesa's emptyAgent.step. No error is raised. - Agents with a normal
steprun 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_dopasses because it builds anAgentSet([...])by hand and never goes throughmodel.agents.- Not covered by #162, #200, #220 or #264.
- Proposed fix (tested locally): also patch and restore
mesa.agentset._HardKeyAgentSet.shuffle_dowhen it exists, returnselffrom the patched method, and add a test that steps throughmodel.agents.