NetLogo: n-of error when all turtles die

[亡魂溺海] 提交于 2019-12-11 20:51:24

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/26961082/netlogo-n-of-error-when-all-turtles-die

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!