-
Notifications
You must be signed in to change notification settings - Fork 939
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
remove_all_agents method added to model #2394
Conversation
for more information, see https://pre-commit.ci
Performance benchmarks:
|
Let's make it explicit what it does and call it
|
I made the suggested name change. |
Thanks! Do we need to handle Agents being present in Spaces or other structures? (luckily they don't have schedulers anymore.) Or document something on how to handle this? And maybe test? |
This starts to feel like a round trip.... Tests were already included. I deliberately use agent.remove internally so it works for e.g., CellAgents. This is also stated in #2393. I will not bother with the existing spaces because they are a massive memory leak already and outside scope. The best solution for those is to subclass Agent.remove explicitly in your own custom agent class. |
I understand the feeling. I think there's a balance here. From my perspective, I can do a full, proper review end of day when I can take the time for it, and bundle anything in that single review. Or I can jot down a quick thought immediately with the risk that I might have another one a bit later. In this case it were two thoughts within half an hour of the PR opening. That's still a lot faster than end-of-day (which I would also find a very acceptable timeline for a volunteer based project). But let me know what you prefer. We also do have to acknowledge that PR authors and PR reviewers have different roles and thus different responsibilities, but both have an essential role to play. I understand it can feel a bit asymmetrical because you have done most feature work last week, but I know exactly what it feels like. Part of it is avoidable, and we should strive to remove as much of that part as possible, but part of it is an essential pain of pushing things through at high velocity in a library committed to a long-term stable API. Adding something to a standardized, user facing API just come with additional effort / costs compared to doing it yourself within the model. I'm convinced if we keep appreciating both sides of this coin, whichever we occupy in a certain PR, we'll keep improving. I can't promise it will never be without any pain (for example, I do feel #2394), because sometimes a little pain a huge long term benefits. We can't prevent everything, but we can prevent a lot and should strive for that. So do call things out, but always assume good intentions. If we need to discuss this further (in a call / face to face) let me know.
A bit of robustness would be nice here. Could you add some Agents to a custom AgentSet and Grid, and see if they are properly removed?
That's really nice, didn't know that!
In my view, PR descriptions should be readable on their own. We have templates for these, let me check if I can add them to a menu, never got to that. They don't have to be epistels, sometimes a single line per header could be enough. Personally, I have largely automated this process using LLMs. For example, I find if you add the issue description, the code diff (add .diff to the PR URL (https://github.com/projectmesa/mesa/pull/2394.diff)) and optionally a few other important points/thought, LLMs are amazing in formatting and generating usage examples. PromptWrite a concise PR description using the issue and diff, following the PR template.Removing all agents in the model
PR template
ResponseHere's a concise PR description following the provided template:SummaryAdd a MotiveThis feature addresses the need to efficiently remove all agents from a model, particularly useful in scenarios like evolutionary simulations where the entire population needs to be replaced each tick. Currently, there's no convenient way to perform this operation, especially considering the weakref nature of Implementation
Usage Examplesmodel = MyModel()
# ... add agents and run simulation ...
model.remove_all_agents() # Removes all agents from the model
assert len(model.agents) == 0 Additional NotesThis method ensures that agents are properly removed using their Good PR descriptions are helpful for reviewers, but also historical documentation and especially useful in a public-facing library, where users might want to know why a feature is designed like the way it is.
Okay, in this specific case I can get behind this, but maybe add a line of documentation that Agents are removed from the cell space automatically, but not from the current spaces.
I was also thinking in that direction. Would a simple test case that shows this be possible? |
I don't understand what you are proposing. This adds a method to the model class so why test other non-Model class related things? That is more of a form of integration testing. Moreover, mesa.space.Grid and its subclasses won't work because its not covered in Agent.remove (and cannot be covered there because agents don't know about mesa.space classes.
No. What would make more sense is improving the documentation of |
From my view the difference is when removing an Agent in a loop or one by one, you often have code around it to handle other removals, like the space. With In my opinion there should be something somewhere for people to find to resolve their error when inevitably they think remove all agents from a model that has a Space. It doesn't need to handle it perfectly all the time be default, but a piece of documentation or testing should be there. That's the cost of doing business in a public API. Once we have deprecated the spaces formally, this will be a different story. And Of course any maintainers might have different opinions about any of this, and I'm happy to follow a majority. |
Fair enough. I have added something on this in |
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.
This covers it well, thanks!
Could you make the PR description readable on it's own?
Closes #2393
At the moment, there is no convenience method for removing all agents from the model. Since model.agents returns a weakref agentset, doing operations on this won't work. It seems we need a model level method like model.remove_all_agents(), which would call agent.remove on each agent. It needs to run through agent.remove rather than just rebuild the model._agents datastructures to ensure that agents are also removed from the experimental cell spaces.