NetLogo-将沙堆模型中的沙粒分布到随机邻居
此代码与NetLogo沙堆模型的自适应相关。当每个斑块中的go晶粒数超过阈值(在本例中为3)时,晶粒将重新分布到周围的相邻区域。我正试图将一粒粮食重新分配给4个随机相邻的补丁 代码返回一个运行时错误,因为边缘的补丁程序不会包含所有8个邻居,因此当请求4个随机邻居时,它返回一个错误,说明无法从3个代理程序请求4个赎金代理,以此类推 我试图找到一段代码来解决这个问题 ****代码如下*****NetLogo-将沙堆模型中的沙粒分布到随机邻居,netlogo,Netlogo,此代码与NetLogo沙堆模型的自适应相关。当每个斑块中的go晶粒数超过阈值(在本例中为3)时,晶粒将重新分布到周围的相邻区域。我正试图将一粒粮食重新分配给4个随机相邻的补丁 代码返回一个运行时错误,因为边缘的补丁程序不会包含所有8个邻居,因此当请求4个随机邻居时,它返回一个错误,说明无法从3个代理程序请求4个赎金代理,以此类推 我试图找到一段代码来解决这个问题 ****代码如下***** to-report stabilize [animate?] let active-patches p
to-report stabilize [animate?]
let active-patches patches with [ n > threshold ]
;; The number iterations the avalanche has gone for. Use to calculate lifetimes.
let iters 0
;; we want to count how many patches became overloaded at some point
;; during the avalanche, and also flash those patches. so as we go, we'll
;; keep adding more patches to to this initially empty set.
let avalanche-patches no-patches
while [ any? active-patches ] [
let overloaded-patches active-patches with [ n > threshold ]
if any? overloaded-patches [
set iters iters + 1
]
ask overloaded-patches [
set base-color fired-color
;; subtract 'threshold' amount from this patch
update-n -4
if animate? [ recolor ]
;; edge patches have less than four neighbors, so some sand may fall off the edge
ask n-of 4 neighbors [
update-n 1
if animate? [ recolor ]
]
]
if animate? [ display ]
;; add the current round of overloaded patches to our record of the avalanche
;; the patch-set primitive combines agentsets, removing duplicates
set avalanche-patches (patch-set avalanche-patches overloaded-patches)
;; find the set of patches which *might* be overloaded, so we will check
;; them the next time through the loop
set active-patches patch-set [ neighbors ] of overloaded-patches
]
report (list avalanche-patches iters)
end
不要询问4个邻居,而是询问
min list 4 count neights
:
ask n-of (min list 4 count neighbors) neighbors [
...
]
但是从模型的角度来看,在表的边缘会有不同的再分配,所以这不是一个好主意。也许更好的做法是先创建一个包装好的世界,然后手动“推”掉桌子上的谷物:选择4个邻居,只调用那些具有相近pxcor和pycor的邻居。剩下的就不在桌上了
如果世界被包裹(水平和垂直),我们总是可以选择4个邻居:
let selected-neighbors n-of 4 neighbors
在这4个邻居中,只有真正的邻居在桌子上
set selected-neighbors selected-neighbors with [
(abs (pxcor - [ pxcor ] of myself) < 2)
and
(abs (pycor - [ pycor ] of myself) < 2)
]
ask selected-neighbors [
....
]
用[
(我的abs(pxcor-[pxcor])<2)
和
(abs(pycor-[pycor]我自己)<2)
]
询问选定的邻居[
....
]