error when removing agents
See original GitHub issueAssume we have an agent that removes other agents from the simulation e.g. for simulating predator-prey I think I might have encountered a bug. When removing an agent from the model/environment in step 1, the method is called on the agent again in the next step which leads to an
AgentpyError
Agent 17 has no environment with topology '['grid', 'space', 'network']'
Here is a minimal example to reproduce the error:
import agentpy as ap
class SomeAgent(ap.Agent):
def do_things(self):
for n in self.neighbors():
print(f"removing: {n}")
n.delete()
break
class SomeModel(ap.Model):
def setup(self, **kwargs):
self.grid = self.add_grid([self.p.size] * 2)
self.grid.add_agents(self.p.agent_count, SomeAgent, random=True)
def step(self):
self.agents.do_things()
if __name__ == "__main__":
params ={
"agent_count": 100,
"size": 20,
"steps": 100
}
model = SomeModel(params)
results = model.run()
I also tried to call the remove_agents function on the grid and model, but no luck.
Is this a bug, or am I doing it wrong?
Cheers
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
You cannot delete a server that is being used by Release ...
There are two possible causes for this error: 1. The agent in question currently belongs to an agent group. 2. A problem with...
Read more >Best practices for removing agents - Zendesk help
This article describes best pratices for removing agents from your Zendesk account. Follow these steps when removing an agent from your team ...
Read more >Problem to delete an agent | AT&T Cybersecurity
I've trouble deleting an agent. I've restaged the asset and I deleted it but the agent remained and I get an error by...
Read more >Encountering Error When Trying To Remove CCG Agent ...
When attempting to remove the CCG Agent component, why do we get the following message? "ERROR Removing target failed with error. Additional ...
Read more >Error in deleteSelf(): Agent should belong to some population
Agents flowing through a process must exist inside a population. You are removing the boxes from their population (inside the pallet agent) ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I added three features to address this issue in https://github.com/JoelForamitti/agentpy/pull/6:
First, there is a new method
AgentList.call
with an argumentcheck_alive
that can be used to prevent looping over deleted agents.Second,
Agent.neighbors
can now also be called for agents without an environment, and will return an empty list instead of an error.Third, there is a new property
Agent.alive
that can be used to check manually if an agent has been deleted.The example from the beginning now works with the following adaption:
I tried to avoid to add a default check_alive to commands like
self.agents.do_things()
as it might slow models down that don’t do a lot of deletion. An issue for another time is that removing from lists is very inefficient, I’ve been thinking about using sets in the future.That is a good idea! It would have to detect wether the element is still in the list
agents
. One way to do that could be to change theAttrList
class from a list to an iterator over its parent list. I will try that out