此代码与NetLogo沙堆模型的适应性有关。当每个补丁中的go颗粒数超过阈值时(在这种情况下为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 发布于 2015-03-15 16:35:03
不要问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 在这四个邻居中,只有真正的邻居在桌子上。
set selected-neighbors selected-neighbors with [
(abs (pxcor - [ pxcor ] of myself) < 2)
and
(abs (pycor - [ pycor ] of myself) < 2)
]
ask selected-neighbors [
....
]https://stackoverflow.com/questions/29062202
复制相似问题