Fix non-deterministic meta-agent membership assignment (#3184)#3433
Merged
Fix non-deterministic meta-agent membership assignment (#3184)#3433
Conversation
|
Performance benchmarks:
|
Member
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pre-PR Checklist
Summary
Fixes #3184 —
agent.meta_agentwas set non-deterministically when an agent belongs to multiple meta-agents.Bug / Issue
Three locations in
meta_agent.pyblindly setagent.meta_agent = selfwithout checking existing memberships:MetaAgent.__init__(line 315) — overwrites any existing assignmentadd_constituting_agents(line 391) — same issuecreate_meta_agentpath 1 (line 246) — iterates overa.meta_agents(set()) which has non-deterministic orderDifferent runs produce different
agent.meta_agentpointers for the same model, breaking reproducibility.Implementation
All three locations now pick the meta-agent with the lowest
unique_id:Consistent with the existing pattern in
remove_constituting_agents(lines 404-409).Testing
All 20 meta-agent tests pass. No new tests needed — fix makes existing behavior deterministic without changing semantics.
Additional Notes
None.
GSoC contributor checklist
Context & motivation
I found this bug while reading through #3184 and tracing how overlapping membership works in meta-agents.
agent.meta_agentwas just being set toselfeverywhere without checking if the agent already belonged to another meta-agent. The pointer ends up depending on creation order, which is not predictable. Reproducibility matters for any simulation framework — same inputs should give same outputs.What I learned
The interesting part was that
remove_constituting_agentsalready had the correct deterministic pattern (sorting byunique_id) but__init__andadd_constituting_agentsdidn't follow it. I also learned that iterating over a Pythonset()is non-deterministic across runs, so even thecreate_meta_agentpath needed sorting. The fix was about consistency — applying the same pattern that already existed in one place to the others that were missing it.Learning repo
My learning repo: https://github.com/codebyNJ/GSoC-learning-space
Readiness checks
pytest --cov=mesa tests/)ruff check . --fix)