Skip to content

Make create_meta_agent deterministic#3183

Merged
quaquel merged 6 commits intomesa:mainfrom
quaquel:create_meta_agent
Jan 20, 2026
Merged

Make create_meta_agent deterministic#3183
quaquel merged 6 commits intomesa:mainfrom
quaquel:create_meta_agent

Conversation

@quaquel
Copy link
Copy Markdown
Member

@quaquel quaquel commented Jan 20, 2026

This is a simple bug fix to ensure deterministic behavior of create_meta_agent. Instead of a set, we use a dict which is ordered, to ensure we have only unique agents. We do the same inside path_1 to also make this deterministic. Finally, we remove the call to model.register_agent in add_constituting_agents.

I am not sure about remove_constituting_agents, this does a deregister call, but I am fairly sure this should be agent.remove((). @tpike3, can you clarify?

Closes #3182 and #3184

@quaquel quaquel added the bug Release notes label label Jan 20, 2026
@github-actions

This comment was marked as off-topic.

@EwoutH EwoutH requested a review from tpike3 January 20, 2026 07:34
@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Jan 20, 2026

Since dicts are ordered in modern Python, can we just use dict keys? I don’t know if there are any performance implications here.

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Jan 20, 2026

Since dicts are ordered in modern Python, can we just use dict keys? I don’t know if there are any performance implications here.

Which is exactly what I did :)

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Jan 20, 2026

@tpike3, @EwoutH, For the memory leak bugfix to pass, this PR needs to be merged first.

@quaquel quaquel mentioned this pull request Jan 20, 2026
@edgeofchaos42
Copy link
Copy Markdown

@quaquel Thanks for crushing on this to fix the issue.

Generally speaking, the difference between remove_constituting_agents and agent.remove is remove consituting agents removes the agent from the meta agent (e.g., they leave the alliance) but the agent is still a part of the model, agent.remove would remove them from the model entirely if I am not mistaken.

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Jan 20, 2026

No, model.deregister_agent is what actually removes an agent from the model. Agent.remove() calls this. And, if you have a CellAgent or ContinuousSpaceAgent, agent.remove() also removes the agent from the space.

@quaquel quaquel merged commit 9bec397 into mesa:main Jan 20, 2026
14 checks passed
@edgeofchaos42
Copy link
Copy Markdown

edgeofchaos42 commented Jan 20, 2026

No, model.deregister_agent is what actually removes an agent from the model. Agent.remove() calls this. And, if you have a CellAgent or ContinuousSpaceAgent, agent.remove() also removes the agent from the space.

Hmmm I still thing there is still a bug in meta-agents/ the logic needs to be more transparent. This is currently not how it is written and was not touched by this PR. My goal with this is to write down my understanding until i/someone else has time to address. Document for the group.

User goal - remove agent from meta-agent but keep it in the model:
remove_constituting_agents should only remove form the meta-agent not the model

  • line 384 in function remove_constituting_agents deregisters agent which will remove from the model not remove it from the meta-agent.
  • Needs to be updated to just remove it from the meta-agent
  • Needs to validate that when agent removed from model, it is also removed from meta-agent

@quaquel

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create_meta_agent is non-deterministic

3 participants