首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >沙堆模型中砂粒的NetLogo分布与随机邻域

沙堆模型中砂粒的NetLogo分布与随机邻域
EN

Stack Overflow用户
提问于 2015-03-15 15:12:20
回答 1查看 84关注 0票数 1

此代码与NetLogo沙堆模型的适应性有关。当每个补丁中的go颗粒数超过阈值时(在这种情况下为3 ),颗粒将被重新分配到周围的邻居。我试图重新分配一个粮食到4个随机邻居补丁。

代码返回一个运行时错误,因为边缘的补丁不会有所有的8个邻居,所以当请求4个随机邻居时,它返回一个错误,说明不能从3个请求4个勒索代理等。

我正在试图找到一段代码来解决这个问题。

*以下代码*

代码语言:javascript
复制
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  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-15 16:35:03

不要问4个邻居,而要问min list 4 count neighbors

代码语言:javascript
复制
ask n-of (min list 4 count neighbors) neighbors [ 
  ... 
]

但是从模型的角度来看,在表格的边缘会有不同的再分配,所以这不是一个好主意。也许最好有一个包装好的世界,然后手动“推开”桌子上的谷物:选择4个邻居,只调用那些与pxcor和pycor相近的。其余的都放在桌子上了。

如果世界被包裹(水平和垂直),我们总是可以选择4个邻居:

代码语言:javascript
复制
let selected-neighbors n-of 4 neighbors 

在这四个邻居中,只有真正的邻居在桌子上。

代码语言:javascript
复制
set selected-neighbors selected-neighbors with [ 
  (abs (pxcor - [ pxcor ]  of myself) < 2) 
  and
  (abs (pycor - [ pycor ]  of myself) < 2) 
  ] 

ask selected-neighbors [
 ....
 ]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29062202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档