Conversation
This commit introduces a new feature to the Mesa Agent-Based Modeling framework: a select_agents method. This enhancement is inspired by NetLogo's n-of functions and a smart idea from @rht as discussed in mesa#1894. It aims to provide a flexible and efficient way to select and filter agents in a model, based on a wide range of criteria. #### Features The `select_agents` method offers several key functionalities: 1. **Selective Quantity (`n`):** Specify the number of agents to select. If `n` is omitted, all matching agents are chosen. 2. **Criteria-Based Sorting (`sort` and `direction`):** Sort agents based on one or more attributes. The sorting order can be set individually for each attribute. 3. **Functional Filtering (`filter`):** Use a lambda function or a callable to apply complex filter conditions. 4. **Type-Based Filtering (`agent_type`):** Filter agents by their class type, allowing for selection among specific subclasses. 5. **Flexible Size Handling (`up_to`):** When `True`, the method returns up to `n` agents, which is useful when the available agent count is less than `n`.
2b791de to
3ce1afe
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1911 +/- ##
==========================================
+ Coverage 77.53% 77.99% +0.45%
==========================================
Files 15 15
Lines 1015 1036 +21
Branches 221 231 +10
==========================================
+ Hits 787 808 +21
Misses 197 197
Partials 31 31 ☔ View full report in Codecov by Sentry. |
|
Note to self: Check correct randomization of selecting Edit: Implemented in c8c7a58. |
When n is defined, it should randomly select n agents. Therefore a shuffle step is added.
|
Two things I want to explicitly link to this proposal:
|
|
I'm leaning stronger and stronger in the direction of going with Let's see how the AgentSet discussion in #1912 progresses before continuing with this PR. |
|
Closing this one, the |
This PR introduces a new
select_agentsmethod to the Model class. It's inspired by NetLogo'sn-offunctions and an earlier concept from @rht as discussed in #1894. It aims to provide a flexible and efficient way to select and filter agents in a model, based on a wide range of criteria.Features
The
select_agentsmethod offers several key functionalities:n): Specify the number of agents to select. Ifnis omitted, all matching agents are chosen.sortanddirection): Sort agents based on one or more attributes. The sorting order can be set individually for each attribute.filter_func): Use a lambda function or a callable to apply complex filter conditions.agent_type): Filter agents by their class type, allowing for selection among specific subclasses.up_to): WhenTrue, the method returns up tonagents, which is useful when the available agent count is less thann.Usage examples
Random Selection of a Specific Number of Agents:
Sorting and Selecting Agents Based on Multiple Criteria:
Using a Custom Filter Function:
Selecting Agents of a Specific Type:
Selecting Agents with a Complex Conditional Filter:
Combining Type Filtering with Attribute Sorting:
Selecting a Maximum Number of Agents when Available Agents are Fewer:
Inverse Functional Filtering for Excluding Certain Agents:
Implementation Details
This method is implemented in the
Modelclass and tested intest_model.py. The tests cover various scenarios and edge cases, and therefore are examples in themselves.Review
When reviewing, please:
Closes #1905.