Skip to content

Agent interaction and maintaining lists of agents #31

Answered by jofmi
nataliedavis asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, sorry for the late answer!

I would try to create two seperate landscapes landscape_sheep and landscape_wolfs.
Having a grid with mixed agents is only useful if you want to query mixed lists.

Lists like current_sheep would then simply be landscape_sheep.agents.
In eat_sheep() you can then identify sheep on the same position as follows:

pos = landscape_wolfs.position[self]
poss_prey = landscape_sheep.agents[pos]
if len(poss_prey) > 0:
    prey, = poss_prey.random()
    landscape_sheep.remove_agents(prey)

(The comma in prey, = poss_prey.random() achieves the same as .to_list()[0])

If you are interested, we could add the finished model to the agentpy model library!

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@nataliedavis
Comment options

Answer selected by nataliedavis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants