海龟的均匀分布
我试图在Max-Pxcor&的世界中创建乌龟的统一分布。 Max-Pycor = 49,在一个不包裹的世界中,左下角为0,0。 我已经使用了虚拟实验室中的代码( https://virtualbiologylab.org/population-ecolog/population-ecologe/ )标记模型,以创建统一的分布,但我遇到了我不完全理解的问题。 (我的NetLogo知识不足以尝试创建我自己的代码以统一分配点!) 该模型在运行时错误停止:
“ASK expected input to be an agent or agentset but got nobody instead”
relating to:
“ask patch x y” part of the code.
一些乌龟已经设置了,但不是全部,我无法弄清楚该错误是什么,就像我检查乌龟创建错误时一样,它具有反击& Counter2值属于世界尺寸,并且从该代码中,我希望与Counter& counter2作为x,y坐标也属于世界。因此,关于这件代码的某些东西我不完全理解。谁能帮忙?谢谢,艾恩
turtles-own [counter counter2 home_x home_y]
to setup
clear-all
reset-ticks
create-turtles N-turtles ;;create N turtles based on slider
end
to go
ask turtles
[
set counter max-pxcor / sqrt N-turtles / 2
set counter2 max-pycor / sqrt N-turtles / 2
repeat N-turtles
[
let x (counter + random 5 - random 5)
let y (counter2 + random 5 - random 5)
ask patch x y
[
sprout 1
[
set home_x x
set home_y y
]
]
set counter counter + max-pxcor / sqrt N-turtles
if counter >= max-pxcor
[
set counter max-pxcor / sqrt N-turtles / 2
set counter2 counter2 + max-pycor / sqrt N-turtles
]]]
end
I am attempting to create a uniform distribution of turtles in a world with max-pxcor & max-pycor = 49, with 0,0 at bottom left hand corner, in a world that does not wrap.
I have used code from the Virtual Lab (https://virtualbiologylab.org/population-ecology/) MarkRecpature model, to create the uniform distribution, but I am running into problems that I don’t fully understand. (My NetLogo knowledge would not be good enough to attempt to create my own code for uniform distribution of points!)
The model stops with a Runtime Error:
“ASK expected input to be an agent or agentset but got nobody instead”
relating to:
“ask patch x y” part of the code.
Some of the turtles have been setup, but not all and I cannot figure what the error is, as when I inspect the turtle creating the error, it has a counter & counter2 value that falls within the world dimensions, and from this code I would expect the patch with counter & counter2 as x,y coordinates also falls within the world. So there is something about this piece of code that I do not fully understand. Can anyone help? Thanks, Aine
turtles-own [counter counter2 home_x home_y]
to setup
clear-all
reset-ticks
create-turtles N-turtles ;;create N turtles based on slider
end
to go
ask turtles
[
set counter max-pxcor / sqrt N-turtles / 2
set counter2 max-pycor / sqrt N-turtles / 2
repeat N-turtles
[
let x (counter + random 5 - random 5)
let y (counter2 + random 5 - random 5)
ask patch x y
[
sprout 1
[
set home_x x
set home_y y
]
]
set counter counter + max-pxcor / sqrt N-turtles
if counter >= max-pxcor
[
set counter max-pxcor / sqrt N-turtles / 2
set counter2 counter2 + max-pycor / sqrt N-turtles
]]]
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够在两个不同的情况下重现崩溃。要了解错误的确切位置,它有助于在提供输出的代码中添加一些额外的命令:
这表明当您的Y超过49时,错误大部分发生,此时不再有一个相对应的补丁程序到坐标。您已经解决了X协调自己的问题。通过添加
添加counter2&gt; = max-pycor [set counter2 max-pycor / sqrt n-turtles / 2] < / code>在代码末尾解决此问题。
我崩溃的第二种情况是当x或y跌至0以下时。当您拥有高n-turtles时,这可能会发生,因此您的
max> max-pxcor / sqrt n-turtles / 2 < / code>是低于5。如果发生这种情况,
令X(Counter + Random 5 -Random 5)
有时可能会返回负值。我建议在此处具有随机数,并以您的世界规模和乌龟数量进行扩展。 (我对统一分布的知识还不够知识,无法在此处建议最好的选择)原始型号自从那里就不必担心任何一个,因此打开了世界包装。
最后,尽管您没有具体要求它,但您的代码会生成(n-turtles^2 + n-turtles)龟,而不是n-turtles,因为您首先生成n-turtles,然后让每个单个生成一个生成另一个n-turtles。我删除了您的第一代,并将柜台移至全球变量。
I was able to reproduce a crash in two distinct scenarios. To find out where exactly the error lies, it helps to add in some extra commands to your code that provide output:
This showed me that the error mostly occurs when your Y goes over 49, at which point there is no longer a patch that corresponds to the coordinates. You already fixed this problem for the X coordinate yourself. Repurposing your solution, by adding
if counter2 >= max-pycor [set counter2 max-pycor / sqrt N-turtles / 2]
near the end of your code this problem is solved.The second scenario where I got a crash was when either X or Y falls below 0. This can happen when you have a high N-turtles, so that your
max-pxcor / sqrt N-turtles / 2
is lower than 5. If this happens,let x (counter + random 5 - random 5)
might occasionally return a negative value. I suggest having the random number here scale with your world size and turtle count as well. (I am not knowledgeable enough about uniform distributions to advise the best option here)The original model didn't have to worry about any of these since there, world wrapping is turned on.
Finally, although you didn't specifically ask for it, your code would generate (N-turtles^2 + N-turtles) turtles , not N-turtles, since you first generate N-turtles and then let every single one of them generate another N-turtles. I removed your first turtle generation, and moved the counters to a be global variables.