此代码与NetLogo sandpile模型的适应性有关。当每个贴片中的颗粒数超过阈值时,在这种情况下为3,颗粒重新分布到周围的邻居。我正在尝试将一个谷物重新分配到4个随机邻居补丁。
代码返回运行时错误,因为边缘处的补丁不会包含所有8个邻居,所以当要求4个随机邻居时,它会返回错误,说明不能从3等请求4个赎金代理。
我正在尝试找到一段可以解决此问题的代码。
****代码低于*****
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
答案 0 :(得分:1)
不要问4个邻居,而是问min list 4 count neighbors
:
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 [
....
]