Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow selecting a fraction of agents in the AgentSet (projectmesa#2253)
This PR updates the `select` method in the `AgentSet` class by replacing the `n` parameter with a more versatile `at_most` parameter. The `at_most` parameter allows for selecting either a specific number of agents or a fraction of the total agents when provided as an integer or a float, respectively. Additionally, backward compatibility is maintained by supporting the deprecated `n` parameter, which will trigger a warning when used. ### Motive Previously, the `select` method only allowed users to specify a fixed number of agents (`n`) to be selected. The new `at_most` parameter extends this functionality by enabling the selection of agents based on a proportion of the total set, which is particularly useful in scenarios where relative selection is desired over absolute selection. ### Implementation - **`at_most` Parameter:** - Accepts either an integer (to select a fixed number of agents) or a float between 0.0 and 1.0 (to select a fraction of the total agents). - `at_most=1` selects one agent, while `at_most=1.0` selects all agents. - If a float is provided, it determines the maximum fraction of agents to be selected from the total set. It rounds down to the nearest number of whole agents. - **Backward Compatibility:** - The deprecated `n` parameter is still supported, but it now serves as a fallback for `at_most` and triggers a deprecation warning. - **Behavior Notes:** - `at_most` serves as an upper limit on the number of selected agents. If additional filtering criteria are provided, the final selection may include fewer agents. - For random sampling, users should shuffle the `AgentSet` before applying `at_most`.
- Loading branch information