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

为什么NetLogo会创建一个斑点而不是一个清晰的形状?,netlogo,Netlogo,我是一名编码初学者。我很高兴得到建设性的批评,不仅是对我的问题,而且对我描述问题的方式 我对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 pat

我是一名编码初学者。我很高兴得到建设性的批评,不仅是对我的问题,而且对我描述问题的方式

我对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
最初的代码更大,但我将问题缩小到了这个片段。这是模型世界设置过程的一部分,这里的目标是在模型世界上随机创建一个定义大小的牧场。(供奶牛搜索和食用,但这不是现在的主题)

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

  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]]

但我得到的是一个大小和形状各异的绿色区域,有时覆盖整个世界。就像那团。以下是其不同外观的一些示例:

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

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


为什么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
以随机顺序执行(或者更准确地说,代理集,例如
补丁
,是无序的)。例如,假设修补程序0运行此代码,并为周围的修补程序提供草。修补程序0 1将在下一步运行。因为它现在有草(补丁0给它),它也给它的邻居草。现在,假设路径02恰好运行在下一条路径上,以此类推。因此,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]]

带[grass>0]的补丁程序。
指的是那些带草的补丁程序(在任何补丁程序执行任何操作之前),这样在ask运行时得到草的补丁程序就不会自己运行了。

你好,布莱恩,太好了,非常感谢。我喜欢这个解决方案,因为它非常简单,不会破坏整个ccode。我试过了,现在效果很好。非常感谢你。向汉尼沙皮致以最诚挚的问候,希望他能帮助我们!如果您对答案感到满意,请确保将其标记为正确,以便其他人知道此问题已得到回答。你好,布莱恩,谢谢您的建议,我没有意识到这一点。我换了牌子,刷了绿色的方格马克。那是苏菲岑吗?谢谢汉斯!太好了。将来,只要绿色支票就足够了:)