为什么 NetLogo 创建一个 Blob 而不是一个清晰的形状?

Why does NetLogo create a Blob instead of a clear shape?

我是编码初学者。我很高兴得到建设性的批评,不仅是关于我的问题,还有关于我描述它们的方式。

我在 NetLogo 中的这段代码有问题:

patches-own[grass]
to setup
  clear-all
  ask one-of patches              ;;pic a random patch as center of the pasture
    [set grass 1]                 ;;and plant grass on it
  ask patches                     ;;search through all the patches to find the one (or several ones) 
    [if grass > 0                 ;;with grass on it
      [ask patches in-radius 3    ;;select the area arround the patch with the grass
        [set grass 1]]]             ;;and also plant grass here
   ask patches                     ;;search through all the patches to find the one (or several ones)
    [if grass > 0                 ;;with grass on it
      [set pcolor green]]         ;;and paint them green
  reset-ticks
  end

原来的代码比较大,但我把问题缩小到了这个片段。它是模型世界设置过程的一部分,这里的目的是在模型世界上随机创建一个定义大小的牧场。 (供奶牛觅食,但现在不是这个主题)

我预计代码会随机拍摄一块地块并在其上种草,然后将这块地块周围的植被面积增加到一定大小。所以我期望的结果是这样的:

expected outcome

但是我得到了一个大小和形状可变的绿色区域,有时覆盖整个世界。就像斑点一样。这里有一些不同外观的例子:

the blobs

可以绕过“Blob-creation”,例如,如果第一个有草的补丁在定义后立即被涂成绿色,然后在第二步中搜索绿色补丁而不是草 > 0 的补丁. 无论如何,我找到的每个解决方案都需要额外的步骤,我希望避免这些步骤。最重要的是,我想了解为什么会发生这种情况,这样我就可以避免它甚至在将来使用它。

代码非常简单明了。所以我想这更多是理解 Netlogos 对命令的解释的问题。

为什么 NeLogo 没有按照我的预期执行命令?

好问题!关键部分是这一点:

  ask patches                     ;;search through all the patches to find the one (or several ones) 
    [if grass > 0                 ;;with grass on it
      [ask patches in-radius 3    ;;select the area arround the patch with the grass
        [set grass 1]]]

ask 遍历每个补丁,依次让每个 运行 包含代码。 ask 以随机顺序执行(或者,更准确地说,代理集,例如 patches,是无序的)。作为一个例子,假设 patch 0 0 运行s 这个代码并给出了周围的 patches grass。下一个补丁 0 1 恰好 运行。因为它现在有草(由补丁 0 0 给它),它也给它的邻居草。现在,假设路径 0 2 恰好是 运行 下一个,依此类推。因此,blob 的形状将取决于补丁 运行 代码中的顺序。如果一个补丁已经被它的一个邻居给了草,它也会给它的邻居草。

幸运的是,修复很简单。您可以使用 with 只向 运行 请求带有草的补丁 运行,而不是在 运行 那个代码块时检查补丁是否有草。这看起来像这样:

  ask patches with [ grass > 0 ] ;;search through all the patches to find the one (or several ones) 
    [ask patches in-radius 3     ;;select the area arround the patch with the grass
      [set grass 1]]

patches with [ grass > 0 ] 指的只是那些有草的补丁(在任何补丁做任何事情之前),所以当请求 运行ning 时得到草的补丁不会结束 运行自己动手。