gpt4 book ai didi

netlogo - 海龟开始时的正态分布

转载 作者:行者123 更新时间:2023-12-02 13:38:14 28 4
gpt4 key购买 nike

我想根据高斯分布将海龟随机放置在某些 x 和 y 坐标范围内,并且同一 block 上没有两只海龟。

我尝试过的事情:1.存在http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-normal但我该如何避免海龟位于同一 block 地方。

我以前使用的代码(仅随机分布):

ask n-of population patches-in-box 
[
sprout-inboxturtles 1
]

;population-海龟数量;patches-in-box - 我想放置海龟的地方

最佳答案

我刚刚意识到您可以通过使用rnd:weighted-n-of来完全解决速度问题。 NetLogo 的原语 Rnd Extension !这是一些修改后的代码:

extensions [ rnd ]

to distribute-turtles [ pop box ]
if pop > count box [ error "Box can't hold all turtles!" ]
let ys [ pycor ] of box
let xs [ pxcor ] of box
let min-x min xs
let min-y min ys
let max-x max xs
let max-y max ys
let mid-x mean list min-x max-x
let mid-y mean list min-y max-y
let w max-x - min-x
let h max-y - min-y
ask rnd:weighted-n-of pop box [
[ (p (pxcor - mid-x) (w / 6)) * (p (pycor - mid-y) (h / 6)) ] of ?
] [ sprout 1 ]
end

to-report p [ x std-dev ]
report (1 / (std-dev * sqrt (2 * pi))) * e ^ (0 - ((x ^ 2) / (2 * (std-dev ^ 2))))
end

什么rnd:weighted-n-of所做的是它需要一个主体集(或一个列表)和一个报告器任务,该任务应该为每个元素返回一个“权重”。权重较大的元素被选中的机会更大。在我们的例子中,我们使用 probability density function 将这些权重分配给框中的补丁。的normal distribution (即代码中的 p 报告器)。

您可以按照与我的其他答案相同的方式使用distribute-turtles:

to setup
ca
let patches-in-box patches with [ abs pxcor < 10 and abs pycor < 10 ]
let population (count patches-in-box - 10)
ask patches-in-box [ set pcolor black + 2 ]
distribute-turtles population patches-in-box
end

...但在这种情况下,即使 population 几乎与 count patch-in-box 一样大,代码也运行得非常快。为什么?因为rnd:weighted-n-of足够“聪明”,有时会丢弃已经被选取的元素,并继续只在尚未选取的元素中进行选取。 (如果您对细节感兴趣,您可以查看the underlying Scala code。)在我们的例子中,这意味着盒子中心附近的补丁不会被一遍又一遍地失败挑选:只有空闲的位置才会继续发挥作用。接近尾声。

关于netlogo - 海龟开始时的正态分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25136161/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com