Conversation
|
Performance benchmarks:
|
| agent_1 = agents[1] | ||
| agent_list = agents.to_list() | ||
| agent_0 = agent_list[0] | ||
| agent_1 = agent_list[1] |
There was a problem hiding this comment.
I suggest fixing this find_combinations instead.
|
Thanks for the quick review @quaquel. I double-checked |
|
That is not entirely true. Look at the typing of |
- Update find_combinations to operate on iterables/tuples - Remove intermediate AgentSet creation in evaluate_combination - Update evaluation_func typing across meta_agents - Adjust alliance_formation example to convert tuple to AgentSet internally
|
Thanks for the clarification — I’ve updated the implementation accordingly. Changes made:
I’ve also noticed a couple of now-redundant lines in All relevant tests are passing:
I'm happy to adjust further based on your preference. |
| def find_combinations( | ||
| model, | ||
| group: AgentSet, | ||
| group: AgentSet | list | Iterable, |
There was a problem hiding this comment.
| group: AgentSet | list | Iterable, | |
| group: Iterable, |
iterable is enough here. AgentSet and list are already iterable so no need to add them here.
| Args: | ||
| model: The model instance. | ||
| group (AgentSet): The set of agents to find combinations in. | ||
| group (AgentSet | list | Iterable): The set of agents to find combinations in. |
There was a problem hiding this comment.
please remove all typing from docstring. Should not be there if typing is used.
| if isinstance(group, AgentSet): | ||
| agent_list = group.to_list() | ||
| else: | ||
| agent_list = list(group) if not isinstance(group, list) else group | ||
|
|
| group_set = AgentSet(group, random=self.random) | ||
| agent_ids = sorted(group_set.get("unique_id")) # by default is bilateral |
There was a problem hiding this comment.
you don't need an agentset here. You can just use operator.itemgetter instead as key in sorted.
Please do. |
- Remove unnecessary AgentSet conversions - Accept Iterable directly in find_combinations - Simplify calculate_shapley_value agent handling - Clean up redundant logic per review feedback
9de6e4c to
95d97c7
Compare
| agent_0 = agents[0] | ||
| agent_1 = agents[1] | ||
| agent_0, agent_1 = agents | ||
| agent_set = AgentSet(agents, random=self.random) |
There was a problem hiding this comment.
this should not be needed at all.
| positions = agent_set.get("position") | ||
| new_position = 1 - (max(positions) - min(positions)) | ||
| potential_utility = agents.agg("power", sum) * 1.2 * new_position | ||
| potential_utility = agent_set.agg("power", sum) * 1.2 * new_position |
There was a problem hiding this comment.
You only have 2 agents, so why use an aggregation here? Just write it up explicitly for 2 agents.
|
Thanks for the feedback, it makes sense I'll simplify it to use the attributes directly since it's just 2 agents. |
…/github.com/souro26/mesa into fix-alliance-formation-agentset-deprecation :wq:wq# the commit.
Summary
Updates the
alliance_formationexample model to use the newAgentSet.to_list()method instead of deprecated direct indexing.Bug / Issue
The alliance_formation model uses deprecated
AgentSet.__getitem__(e.g.,agents[0]), which was deprecated in #3208 and will be removed in Mesa 4.0.This triggers PendingDeprecationWarning when running the example, and since this is a reference example that users often copy, it should demonstrate current best practices.
Implementation
Replaced direct indexing:
With the recommended pattern from the migration guide (#3218):
Testing
Ran the alliance_formation example test:
Additional Notes
Follow-up to #3208 and #3218.
This change eliminates deprecation warnings in the alliance_formation example model.