-
Notifications
You must be signed in to change notification settings - Fork 923
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
Add create_agents
factory method to Agent
#2351
Conversation
Performance benchmarks:
|
Thanks for taking a shot as this, I’m intrigued why it increases code complexity in the examples instead of decreases it. Probably because of the lists that need to be created before create is called. Let me think a bit about this. Maybe some elegant AgentSet stuff can help here. Talking about that, would we like to (optionally) return an AgentSet with the just created Agents? Then you can directly start chaining. |
Lines of code and code complexity are not the same thing. But yes, since we are using python.random rather than numpy.random, we need list expressions. This adds a bit of overhead because we iterate seperately over n_agents for all arguments.
I have been wondering about this as well. On the one hand, it woudl be convenient to have it. On the other hand, it would add overhead and I can't immediately think of a use case were you want to operate on the agents immediately in |
I have updated the code a little bit. You can now pass either a sequence of length n (i.e., the number of agents to create) or any other object as argument/keyword argument. If a given argument is a sequence of length n the code assumes that each entry is for a different agent. Otherwise, the code assumes that the entire object needs to be passed to each Agent. |
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
I updated the benchmarks to use The current implementation needs further refinement. I check explicitly for each argument and keyword argument if it is a list and if its length equals the number of agents to be created. This is too restrictive. For example, a tuple or ndarray fails on this test. It also rules out generator objects (as in the boltzman position example where I have to wrap zip inside list). However, I also think this might become quite a convenient way to instantiate many agents. What do others think? I am mainly looking for feedback at the API level and input to develop this generator method one step further. Also, it might be convenient to have some additional methods on |
for more information, see https://pre-commit.ci
I have picked this up again, and it's ready for a review. It now supports Wolf.create_agents(
self,
self.initial_wolves,
self.rng.random(self.initial_wolves) * 2 * wolf_gain_from_food,
wolf_reproduce,
wolf_gain_from_food,
[
self.grid.all_cells.select_random_cell()
for _ in range(self.initial_wolves)
], # this is why I want to add draw_cells or something like that to CellCollection in a seperate PR
)
# in the simple case of width == height, you could reduce this to a one-liner
positions = list(
zip(
self.rng.integers(0, self.grid.width, n),
self.rng.integers(0, self.grid.height, n),
)
)
MoneyAgent.create_agents(self, self.num_agents, pos=positions) |
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
Could you rebase this PR and resolve conflicts? The benchmark models got removed, for example. Sorry for the trouble. |
done |
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.
LGTM
I was contemplating a bit about when this would be really useful (yeah yeah I know, I opened the original issue), especially because those newly created agents are now lost in the void of What might be really useful if it returns a convenient AgentSet with the just created Agents. Then, you can immediately let them do something, track them separate as a group, etc. |
I have been going back and forth on this. It's easy to add and might be useful in some cases, but I'm not sure it would be used a lot. Happy to add it in if you think it is useful. |
Can would personally like it. I can also add it tomorrow. |
just added it |
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.
Thanks!
Please update the type hints and docstring, after that go ahead and merge.
I updated the PR description based on the final implementation, could you check it? |
create_agent
factory method to Agentcreate_agents
factory method to Agent
Summary
Add a new
create_agents
factory method to theAgent
class that enables batch creation of agents with flexible parameter handling. This method simplifies the creation of multiple agents by supporting both uniform and per-agent parameter values.Motive
This addresses #2221 by providing a more efficient and flexible way to create multiple agents. Currently, users need to write explicit loops when creating multiple agents with varying parameters. This factory method streamlines the process and makes the code more readable and maintainable, especially when dealing with agents that have multiple initialization parameters.
Implementation
create_agents
as a class method toAgent
model
andn
parameters for the model instance and number of agentsn
(unique value for each agent)ListLike
class to make single values behave like sequencesAgentSet
containing all created agentsUsage Examples
Basic usage with uniform parameters:
Mixed uniform and per-agent parameters:
Complex initialization with multiple varying parameters:
Additional Notes