Reputation: 21
I have a simulation where turtles walk onto red patches and die, which works, but everything that has n-of
in it reports an error as soon as most of/all turtles are dead. I do understand the error, since the simulation tries to get n-of
while there is no turtle left, but how do i fix that? Is there a way to use n-of when at the end of the simulation all the turtles are dead?
If there is, how do I do it?,
and if not, is there an alternative way to making the turtles die on red patches?
My simulation needs every turtle to be gone as soon as it walks on a red patch, but they can't walk on/over each other, which would make it hard to let them gather on one red patch (there are about 500 turtles).
Thank you! edit: I edited my code so that I don't need n-of anymore. Now, the part of my code where I want one turtle to set pen-mode to "down" is
to go
....
ask one-of turtles [set pen-mode "down"]
....
end
and the error message is now:
ASK expected input to be an agent or agentset but got NOBODY instead. error while observer running ASK called by procedure GO called by Button 'go'
as soon as the simulation ends.
The one-of turtles
was suggested, but for now every turtle sets its pen-mode to "down", but I only want one turtle to do that.
Upvotes: 0
Views: 910
Reputation: 14972
You are not telling us what you are using n-of
for, so it's hard to suggest an alternative approach. But in general the way to prevent n-of
from crashing when there are not enough turtles to select, is to use something like:
n-of (min list n count turtles) turtles
where n
is the number of turtles that you would like to select if possible.
Upvotes: 2
Reputation: 12580
Basically, you want to use at most n
turtles. That is, if there at least n
turtles, you should use n
of them, otherwise you should just use all the turtles. We can turn this into reporter pretty easily:
to-report at-most-n-of [ n agentset ]
ifelse count agentset > n [
report n-of n agentset
] [
report agentset
]
end
You use this exactly like n-of
but it won't error if there aren't enough turtles.
Upvotes: 2