NetLogo: n-error when all turtles die

I have a simulation where the turtles walk on red spots and die and this works, but whatever it has n-of

reports an error once most / all of the turtles are dead. I understand the error as the simulation is trying to get n-of

while the turtle is not present, but how do I fix it? Is there a way to use n-of when at the end of the simulation all the turtles are dead? If so, how can I do it? and if not, is there an alternative way to make the turtles die on red spots? My simulation requires each turtle to leave as soon as it has walked on a red spot, but they cannot walk on top of each other making them difficult to collect on one red patch (about 500 turtles).

Thank! edit: I edited my code so I don't need n-of anymore. Now the part of my code where I want one turtle to set the pen-mode to the "down" position,

to go .... ask one-of turtles [set pen-mode "down"] .... end

and the error message:

Expects ASK to be agent or agent, but got NOBODY instead. error while running the observer ASK called by the GO procedure called by the "go" button

as soon as the simulation ends.

It was suggested one-of turtles

, but for now each turtle sets its pen mode to down, but I only need one turtle.

0


source to share


2 answers


Basically, you want to use no more n

turtles. That is, if there are at least n

turtles, you should use one n

of them, otherwise you should just use all turtles. We can easily turn this into a reporter:

to-report at-most-n-of [ n agentset ]
  ifelse count agentset > n [
    report n-of n agentset
  ] [
    report agentset
  ]
end

      



You use it exactly how n-of

, but it won't be an error if the turtles aren't enough.

+2


source


You don't tell us what you're using n-of

, so it's hard to come up with an alternative approach. But in general, a way to prevent n-of

crashes when turtles are missing is to use something like:

n-of (min list n count turtles) turtles

      



where n

is the number of turtles you would like to select, if possible.

+2


source







All Articles