-
Notifications
You must be signed in to change notification settings - Fork 928
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
Allow AgentSet.do() to take Callable function #2219
Conversation
picks up on an idea from projectmesa#1944, see projectmesa#1944 (comment)
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
creating agentsets is expensive, so this makes it possible to avoid creating them
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
Performance benchmarks:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Code wise and docstring wise it looks really good to me. Only a few tests are needed.
I added tests and updated the Readme with a note about the breaking change and some usage examples. @quaquel please check the tests and examples and @projectmesa/maintainers I would love one additional review. |
Performance benchmarks:
|
This PR enhances `AgentSet.do` to take a callable or str. Currently, AgentSet.do takes a `str` which maps to a method on the agents in the set. This PR makes it possible to also use a `Callable` instead. This callable will be called with the `agent` as the first argument.⚠️ Breaking change⚠️ A small breaking change is introduced here: the `method_name` parameter is renamed to `method`. For models that use this as a keyword argument this is a breaking change, and need to replace `do(method_name="something")` with `do(method="something")`.
This PR enhances `AgentSet.do` to take a callable or str. Currently, AgentSet.do takes a `str` which maps to a method on the agents in the set. This PR makes it possible to also use a `Callable` instead. This callable will be called with the `agent` as the first argument.⚠️ Breaking change⚠️ A small breaking change is introduced here: the `method_name` parameter is renamed to `method`. For models that use this as a keyword argument this is a breaking change, and need to replace `do(method_name="something")` with `do(method="something")`.
This PR enhances `AgentSet.do` to take a callable or str. Currently, AgentSet.do takes a `str` which maps to a method on the agents in the set. This PR makes it possible to also use a `Callable` instead. This callable will be called with the `agent` as the first argument.⚠️ Breaking change⚠️ A small breaking change is introduced here: the `method_name` parameter is renamed to `method`. For models that use this as a keyword argument this is a breaking change, and need to replace `do(method_name="something")` with `do(method="something")`.
This PR enhances
AgentSet.do
to take a callable or str. Currently, AgentSet.do takes astr
which maps to a method on the agents in the set. This PR makes it possible to also use aCallable
instead.This callable will be called with the
agent
as the first argument.A small breaking change is introduced here: the
method_name
parameter is renamed tomethod
. For models that use this as a keyword argument this is a breaking change, and need to replacedo(method_name="something")
withdo(method="something")
.Usage examples
Here are a few examples of how to use
do()
withCallable
functions.Suppose we want to move each agent by a fixed offset:
You might want to increase the energy level of all agents by a fixed amount:
You can apply a function conditionally to agents that meet a certain criterion:
You may want to gather results from applying a function to each agent:
If your callable needs additional arguments, you can pass them directly via
do()
:Suppose you want to update each agent's position based on their velocity:
And of course the current behavior of passing a string as method is still supported: