question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error when removing agents

See original GitHub issue

Assume 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:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JoelForamitticommented, Mar 26, 2021

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 argument check_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:

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.call('do_things', check_alive=True)  # This is new

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.

0reactions
JoelForamitticommented, Mar 25, 2021

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 the AttrList class from a list to an iterator over its parent list. I will try that out

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found