Repository navigation
meta-agents refactor from id to agent object - #3875
Conversation
|
Performance benchmarks:
|
There was a problem hiding this comment.
This all looks good. Great speed up.
I have two questions --
-
How much to support backwards compatibility? I am under the impression that with 4.0 causing breaking changes at this point we shouldn't. But I could be talked back.
-
(Separate, but related to this PR) Should meta-agents live where it is living or like the behavioral framework - should we create a mix-in for this and then create a general mix-ins folder that holds all these mix-ins? (In the future when we have 100 - maybe these are broken off into their own libraries for whatever work one is doing.)
Regarding 1 - Yes the backwards capability was overkill on my part, particularly because it hasn't been part of a release; removed Regarding 2 - Good point, and this is a big decision, maybe I was avoiding to meet the GSOC deadline and went overly aggressive moving it out. However, do we want to go to the monorepo like polars; huggingface or put them in a directory like .ext (sqlalchemy) or .contrib (django). @EwoutH. @wang-boyu and @quaquel -- What are your thoughts, do you already have a plan? |
|
Sorry for the late reply. Thanks for working on this. In time, meta-agents can be a core Mesa feature, if it's up to me. Otherwise I'm not against any of the other ideas, they all make sense in some way. We could do some experimentation with But for meta-agents, I think it's cool, useful to a large enough audience, and sets mesa apart. My 2 cents: let's aim to make it core feature. |
EwoutH
left a comment
There was a problem hiding this comment.
A few higher-level questions, mostly on design and API rather than implementation:
1. What can be stored?
The backend accepts anything hashable (strings, ints, objects). The API is meant for agents. Can we make the API accept only agents in the model, and raise an error otherwise? Right now add_member(group, agent.unique_id) silently stores an int instead of failing.
2. Can we use the public signal?
Meta-agents uses a private hook (_agent_removed_hooks) to learn when an agent is removed. The model already emits ModelSignals.AGENT_REMOVED for this. Can meta-agents use that instead? If not, the signal system is missing something, and we should fix that.
CC @quaquel, is signals ready enough?
3. Are removed agents fully cleaned up?
The backend now holds direct references to agents, and queries no longer filter out removed ones. Could you add a test that a removed agent (and a removed group) is garbage-collected and no longer appears in members_of / groups_of?
4. Is the order reproducible?
Results are sorted by str(agent). If users override __str__, ties can occur, and the order then depends on memory addresses. That breaks runs with a fixed seed. Can we sort by unique_id instead?
5. What does each removal method do?
There's remove_member, deactivate, dissolve, and agent.remove(). What exactly happens after each one? If that's hard to explain in a few lines, we may have one too many.
6. Can the API be smaller?
- Do we need lookup by group name? Nothing else in Mesa does that.
- Why do add/remove return a
MembershipView, while queries return anAgentSet? find_combinationsandevaluate_combinationare about finding coalitions, not managing memberships. Could they move to the example?relationcan be any value ("member","leader", ...). Is that intended? Or do we want a set of special types with certain, defined meanings? Worth discussing a bit I think.
7. PR housekeeping
- The description still mentions
_resolve_to_agent(), which was removed. - Could you keep only tests of the public API? Tests of private helpers or specific line numbers break easily.
- Some benchmarks show 7–12% slowdowns on models that don't use meta-agents. Probably noise, but could you rerun them?
|
Thanks @EwoutH! - Responses below --- 1. What can be stored? -- Good catch I missed then when I did the transition, I will fix it 2. Can we use the public signal? -- This is an artifact of moving it in and out of experimental and I forgot to make this change. I will switch it, but then meta-agents should stay in experimental until signals comes out, which was the original reason not to use it. Also speaks to 6. 3. Are removed agents fully cleaned up? - Yes, and I will add the test 4. Is the order reproducible? - Makes sense, I can do that 5. What does each removal method do? (hmmm I will relook the doc string to put them in context of meta-agents) 6. Can the API be smaller? This gets back to Mesa organization. The idea/plan was make an extensible API so as meta-agents is employed new and more complex features can be added with the core abstraction of meta-agents as adaptive networks being the same. For me its critical to store these ideas from open source and an extensible API becomes the way to do that. To your point, if I am understanding correctly, what is the organization and how do you keep it clean? I am open to ideas as this is directly related to the larger issue of Mesa's organization. 7. PR housekeeping Good catch on |
…h Signal remove all outdataed code and tests related to remove hooks
|
I addressed many of @EwoutH points, some of the like size of API etc I did not address pending further discussion of Mesa organization. Can I please get an approval to merge |
jackiekazil
left a comment
There was a problem hiding this comment.
LGTM. If there are additional changes, let's open up a new PR.
#3875 moved meta-agents back to mesa.experimental.meta_agents, but the migration guide and the API page still import from mesa.meta_agents, which no longer exists. Copying the examples fails with ModuleNotFoundError, and the API page's automodule can't import the module, so the reference section is empty on the docs site. Update the imports and the automodule target, and fix the migration guide heading, which said meta-agents moved out of experimental. Co-authored-by: Tom Pike <[email protected]>
The Meta-Agents Rewrite (mesa/mesa#3811, mesa/mesa#3875) moved the package from mesa.meta_agents to mesa.experimental.meta_agents, and mesa no longer exports meta_agents at the top level. The warehouse example still imported the old path: from mesa.meta_agents import MetaAgents Since test_examples.py imports every model module, that ModuleNotFoundError aborts collection, so the whole examples suite fails before a single test runs. That is the examples job currently red on mesa main, and therefore on every open mesa PR. mesa's own migration guide gives the new path: "Meta-agents still live in mesa.experimental.meta_agents". Nothing else in the example needs changing, since the rest already uses the rewritten API through model.meta_agents. Against mesa main, test_examples.py goes from 1 collection error, 0 tests run to 24 passed / 1 failed. The remaining failure is VirusAntibodyModel, a separate pre-existing bug fixed by #496.
Summary
Migrate the meta-agent
MembershipBackendfrom storingunique_idintegers to storing agent objects directly, aligning it with howAgentSet,Agent, andModelhandle agents throughout the rest of Mesa.Motive
The backend was introduced using
unique_idintegers as the canonical identity for membership edges. This was inconsistent with the rest of Mesa's core, where agent objects are passed and stored directly. The mismatch forced the API layer (MetaAgents) to maintain an expensive O(N) reverse-lookup (_live_entity_lookup) on every call to resolve IDs back to live agent objects. Switching to objects removes this indirection, simplifies the code, and brings meta-agents in line with the established patterns.Implementation
Backend (
backend.py):_to_id()which extractedunique_idfrom agent objects. The backend now stores whatever hashable entity is passed in — agent objects for Mesa agents, plain strings/ints for standalone usage.API layer (
meta_agents_api.py):_entity_id(),_live_entity_lookup(), and_resolve_entity()— the entire ID-to-object resolution layer._resolve_group(),_resolve_view(),members_of(),groups_of(),query_memberships(),at_level(), anddissolve()to work with agent objects directly from the backend.Tests:
unique_id-based to object-based acrosstest_meta_agents_backend.py,test_meta_agents_api.py, andtest_alliance_formation_model.py. Reduce number of test to focus only Public APIUsage Examples
N/A — The public API (
create,add_member,remove_member,members_of,groups_of, etc.) is unchanged. This is a backend storage change. The only difference is thatbackend.as_triplets(),backend.groups_of(), andbackend.agents_of()now return agent objects instead ofunique_idintegers.