-
Notifications
You must be signed in to change notification settings - Fork 927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Meta agents #2575
base: main
Are you sure you want to change the base?
Meta agents #2575
Conversation
- Add create meta-agents to experimental - Add tests of meta-agents - Add example with an alliance formation model in basic examples
Performance benchmarks:
|
Did you consider incorporating the |
I did, however just to get meta-agents started and integrated I avoided it since it will add a layer of complications and I was worried about collisions and MRO issues -- Ways forward in no particular order are add AgentSet so each meta-agent has the AgentSet functionality, integrate threading so meta-agents can be on their own thread in 3.13 forward, allow for greater combinatorics (e.g. agents can be in multiple meta-agents) |
Cool, I will try to dive in and do a proper review tomorrow or Monday. |
I am playing catch-up on this. This is interesting. I do not have strong opinions on the setup, and I feel like @EwoutH and @quaquel have it covered. Question for everyone regarding adding an example in the core examples folder: If memory serves me correctly -- we were adamant about only having five examples with ones that build off of those for maintenance purposes. In what cases do we justify an additional example to the core folder as opposed to the examples folder? (I ask because our original examples folder grew to the maintenance issue that it is because we were trying to demonstrate all the functionality we had in at least one location). |
Well clearly I am going to have a bias on this, however, I did consider the challenge of an excess of core examples and the reason I built in basic examples as an exception is because as far I know this would be a unique capability of Mesa compared to NetLogo, MASON etc. Putting it in the basic examples would make it more prominent and easier for users to see, ideally further increasing Mesa's competitiveness as the ABM library of choice. A second option would be putting it in mesa-examples and then adding some documentation in getting started. Or I could just put it in Mesa-examples. I am good with whatever the group decides, just let me know. |
Let's focus on the functionality first and then work out the details on the examples. |
For various experimental features, I added some examples into the folder with the new experimental code. Once the API started to stabilize, I removed those examples. That might be a good middle ground here as well. The example helps clarify the experimental feature. |
- add alliance formation model to example_tests
for more information, see https://pre-commit.ci
I have some initial thoughts, mainly on the conceptual and API design. Our Here's how we could redesign this: class MetaAgent(mesa.Agent):
"""An agent that is composed of other agents and can act as a unit."""
def __init__(self, model, components=None):
super().__init__(model)
# Use AgentSet to manage components - inheriting all its powerful features
self._components = AgentSet(components or [], random=model.random)
@property
def components(self):
"""Read-only access to components as an AgentSet."""
return self._components
# Component management
def add_component(self, agent):
self._components.add(agent)
def remove_component(self, agent):
self._components.discard(agent) This gives us several benefits:
For example, here's how your alliance formation could look using this approach: class Alliance(MetaAgent):
def __init__(self, model, components=None):
super().__init__(model, components)
# Compute alliance properties from components using AgentSet methods
self.power = self.components.agg("power", sum) * 1.1 # With synergy bonus
self.position = self.components.agg("position", np.mean)
@classmethod
def evaluate_potential(cls, agents: AgentSet) -> float:
"""Evaluate if these agents would make a good alliance using AgentSet methods."""
total_power = agents.agg("power", sum)
positions = agents.get("position")
position_spread = max(positions) - min(positions)
return total_power * (1 - position_spread) We could even add some convenience methods to AgentSet to support meta-agent operations: class AgentSet:
def to_meta_agent(self, meta_agent_class, **kwargs):
"""Convert this AgentSet into a MetaAgent of the specified class."""
return meta_agent_class(self.model, components=self, **kwargs)
def find_combinations(self, size_range=(2,5), evaluation_func=None, min_value=None):
"""Find valuable combinations of agents in this set."""
combinations = []
for size in range(*size_range):
for candidate_group in itertools.combinations(self, size):
group_set = AgentSet(candidate_group, random=self.random)
if evaluation_func:
value = evaluation_func(group_set)
if min_value is None or value >= min_value:
combinations.append((group_set, value))
return combinations Then alliance formation becomes very natural: def step(self):
# Find potential alliances among base agents
base_agents = self.agents.select(agent_type=BaseAgent)
potential_alliances = base_agents.find_combinations(
evaluation_func=Alliance.evaluate_potential,
min_value=10
)
# Form some alliances
for agents, value in potential_alliances:
if self.random.random() < 0.3: # Some formation probability
alliance = agents.to_meta_agent(Alliance) Some open questions to consider:
I think this approach would make the feature more intuitive and maintainable while preserving all the functionality of the current implementation. It would also make it easier to extend with new capabilities in the future. What do you think about moving in this direction? Happy to elaborate on any of these points. Edit: I can also help with the implementation if you'd like. |
Just some quick thoughts:
|
Thanks @EwoutH and @quaquel I appreciate the time you are spending on this, which came out unexpectedly. So for expanding the MetaAgent to have AgentSet, that works for me. On the attribute name maybe For practical way forward: There is some more nuance to the bilateral shapley value where you do not want to do mass aggregation and strictly speaking I am not doing it exactly right, but that is off topic. However, it leads to the larger conceptualization of meta-agents, which you can read or not at your leisure. For less practical off the top of my head considerations
Let me know you thoughts. |
@tpike3 I created an initial implementation on the |
Thanks @EwoutH! -- merging the code right now and yaaa I should have totally integrated AgentSet from the beginning |
Status Update So I realized we are looking at the problem in different not necessarily mutually exclusive ways. So what I am working on is this--- From @EwoutH a base MetaAgent optimizes use of AgentSet From me the ability to dynamically create multiple agent types Things to do --
Let me more if you have more thoughts |
I finally had time to take another look at this. Based on #2538, my understanding is that meta agents are composed of agents and have their own behavior and state which might be based on the behavior and state of its constituting agents. I think the ideas here are very interesting, but also hard to get right (which is why I don't know of any other ABM library that has something similar). I am, however, a bit confused, about First, if retain_subagent_attributes:
for agent in agents:
for name, value in agent.__dict__.items():
if not callable(value):
meta_attributes[name] = value
for key, value in meta_attributes.items():
setattr(meta_agent_instance, key, value) Second, |
@quaquel Sorry for the delay, I was visiting some family over the holiday However, I made a bunch of updates to ideally allow more user options. Below I walk through each one, and I think that address your points you outline excepted for function to method, which I will change probably tomorrow since we are getting weather. First - Based on @EwoutH comments I created Example use case I am thinking is an autonomous systems in a warehouse. So it has parts like wheels, FLIR sensors, computer etc, now it can have a meta agent that calls the unique attributes and methods of each of its subagents. Second - I added multi-level agents as a stand alone and separate function. This is like the alliance formation and dynamically adds new agent classes MetaAgents at increasing hierarchies. I also added the use of Example use case is the provided example which is a more pure instantiation of the bilateral shapely value, but to your point an individual agent may now join a level 3 agent as it position changed. This, however, would be dependent on the example model but the multi_level agent code does allow for it by changing the logical of the multi-level alliance formation model. Finally multi-level agents does allow for adding the sub agents attributes and methods. The thought behind this is unlike the first example that is explicit, this can allow for recombination and emergent agents. Example use case and just to keep with with alliance: I once did a tribal affiliation model of the Libyan civil war. In this case some tribes may be coming with control of road networks and certain attributes and methods for dealing with that, while another tribe may come with control of oil fields and likewise have unique functions and attributes, this would allow for a singular agent who has leaders managing both assets as well as subgroups managing their respective territory. I concede this is an arguable feature. The hard question to me, which ideally by dabbling in this unique and difficult landscape will help inspire the community to explore, is how one can assess how well the groups have coalesced together and what is their chances for descending into infighting and how do you manage that computationally. In one sense this expands experimental to not only do great Mesa improvements (like agentset, propertylayer, continuous space etc) but also to try some novel and questionable features to ideally get a larger community to explore the space and push the whole ABM field forward. Let me know what you think. Thanks again for the review! In the end I really only did an example 2 but in future pull requests could add examples for 1 and 3. Let me know what you think. |
- allow for deliberate meta-agents creation - allow for combinatorics - allow for dynamic agent creation fix methods-functions; add tests
Let’s stop apologizing for this stuff, we’re all doing this in our free time. Thanks for working on it! I will try to review the ContiniousSpace soon, and then the PR hopefully later this week. Exciting to have so much serious work going on! |
Summary
This PR is useful for creating meta-agents that represent groups of agents with interdependent characteristics.
New meta-agent classes are created dynamically using the provided name, attributes and functions of sub agents, and unique attributes and functions.
supersedes #2561
Motive
This method is for dynamically creating new agents (meta-agents).
Meta-agents are defined as agents composed of existing agents.
Meta-agents are created dynamically with a pointer to the model, name of the meta-agent,
iterable of agents to belong to the new meta-agents, any new functions for the meta-agent,
any new attributes for the meta-agent, whether to retain sub-agent functions,
whether to retain sub-agent attributes.
Examples of meta-agents:
battery, computer etc. and the meta-agent is the car itself.
Currently meta-agents are restricted to one parent agent for each subagent/
one meta-agent per subagent.
Goal is to assess usage and expand functionality.
Implementation
Method has three paths of execution:
Added
meta_agents.py
in experimentalAdded tests in test-agent.py
Added alliance formation model in basic examples
Usage Examples
I added a basic example of alliance formation using the bilateral shapley value
Step 0- 50 Agents:
Step 8 - 17 Agents of increasing hierarchy added dynamically during code execution:
Additional Notes
Currently restricted to one parent agent and one meta-agent per agent. Goal is to assess usage and expand functionality.