【问题标题】:Netlogo: Code is asking patches to move, but the instructions are on ask turtleNetlogo:代码要求补丁移动,但说明在问海龟
【发布时间】:2021-08-13 14:57:47
【问题描述】:

因此,我之前曾询问过补丁不存储某个邻居值的问题,多亏了 Matteo、JenB 和 Lena,我才得以进一步发展。 Sicen 那么代码已经进步了一点,因此我遇到了一个新错误。

首先,如果复制和粘贴,代码可以工作,但有两件事是必要的,我没有底片,所以有必要将中心更改为角落,我不知道如何不创建设置按钮,所以需要它.

现在,代码生成了 3 种颜色的 3 个区域,每个边框根据随机数移动。这样,靠近边界的海龟应该同时改变补丁的颜色和它自己的颜色,同时随机转动和移动。

问题是我不知道我错过了什么,但补丁确实改变了颜色,但乌龟没有改变或移动,说指令正在被补丁和补丁不移动,这是有道理的。但为什么?我设置了条件,如果海龟有一定的能量,那么他们应该做我要求的事情。

问题再次在于,将分号中的部分移动到 move-potrero。

turtles-own [energia]
 patches-own
[bordear
abandono
reforestado
potrerizado
temperatura
humedad
dosel
]
breed [ potreros potrero ]  
breed [ bordes borde ] 
breed [ bosques bosque ]

to setup
  clear-all

  ask patches with [
    pxcor <= 30 and
    pxcor >= min-pxcor and
    pycor <= 60 and
    pycor >= min-pycor ] [
    set pcolor 35
    set temperatura 26.5
    set dosel 1
   set humedad 90.4
  ]
 ;Potrero
  ask patches with [
    pxcor <= 60 and
    pxcor >= 30 and
    pycor <= 60 and
    pycor >= min-pycor ] [
    set pcolor 44
    set temperatura 29
    set dosel 84.3
   set humedad 79.3;
  ]
  ;Borde
  ask patches with [
    pxcor <= 90 and
    pxcor >= 60 and
    pycor <= 60 and
    pycor >= min-pycor ] [
    set pcolor 66
   set temperatura 26.3
   set dosel 85.2
   set humedad 94 ;
  ]
  ;Bosque

 ;se establece la forma de la rana
 
 ; Se establecen las condiciones de las ranas, tamaño color y lugar de aparicion. La energia sera igual en todas las ranas en el set up.

   create-potreros 50

  [ set size 3        ;; easier to see
    set color yellow
setxy random xcor random ycor
move-to one-of patches with [pcolor = 35]
    set heading random 45 + 45
    set energia 50
 ]   ;; red = not carrying food

 ;Potrero


 
    reset-ticks
end

to go
  ask potreros [
    if energia < 50 [descansar-potrero]
    if energia >= 50 and energia <= 80 [move-potrero]
    if energia > 80 [ if ticks mod 50 = 0 [reproducirse]]
    set heading random 270
      fd 1
 set energia energia - 2
  morir]

  ask patches [ reforestacion ]
  ask patches [ deforestacion ]
  ask patches [abandonacion]
  ask patches [borderizacion]


if ticks >= 500 [stop]
tick
end

to reforestacion
  if pcolor = 44 [
     set reforestado count neighbors with [pcolor = 66] ]
  if reforestado >= 3 [if random 100 <= 50 [
    set pcolor 66
    set temperatura 27 + random 3
    set humedad 90 + random 5
    set dosel 70 + random 10 ]
  ]


end


to deforestacion
  if pcolor = 44 [
     set potrerizado count neighbors with [pcolor = 35] ]
  if potrerizado >= 3 [if random 100 <= 50 [
    set pcolor 35
    set temperatura 26.5 + random 3
    set humedad 80 + random 10
    set dosel 40 + random 10]
  ]


end

to abandonacion
  if pcolor = 35 [
     set abandono count neighbors with [pcolor = 44] ]
  if abandono >= 3 [if random 100 <= 50 [
    set pcolor 44
    set temperatura 26.5 + random 3
    set humedad 80 + random 10
    set dosel 40 + random 10]
  ]

end

to borderizacion
  if pcolor = 66 [
     set bordear count neighbors with [pcolor = 44] ]
  if bordear >= 3 [if random 100 <= 50 [
    set pcolor 44
    set temperatura 27 + random 3
    set humedad 90 + random 5
    set dosel 70 + random 10]
  ]
end

to move-potrero
  ask neighbors in-radius 2
     [if temperatura >= 28.6 or temperatura <= 22.6 or dosel >= 5 or humedad <= 84
      [ set pcolor red
       ;facexy random 30 random 60
      ;fd 5
      ;set energia energia - 10]]
    ;[ask potreros [descansar-potrero]]
  ]]



end

to descansar-potrero
  ifelse pcolor = 35 [
    set energia energia + 6]
  [set energia energia + 1]

end

to reproducirse
  if energia > 80 [ if random 100 > 60 [set energia energia - 70
      hatch 1 [ rt random-float 360 fd 2
      set energia energia / 3]]]
end
  to morir
  if energia <= 1 [die]

end

【问题讨论】:

    标签: netlogo patch


    【解决方案1】:

    1

    问题是您要求补丁执行海龟命令(例如facexyfd),因为neighbors 报告补丁。来自NetLogo dictionary

    报告包含 8 个周围补丁(邻居)或 4 个周围补丁(邻居 4)的代理集。

    因此,诀窍是将neighbors 替换为turtles

    to move-potrero
      ask turtles in-radius 2 [
       if temperatura >= 28.6 or temperatura <= 22.6 or dosel >= 5 or humedad <= 84 [
         set pcolor red
         facexy random 30 random 30
         fd 5
         set energia energia - 10
        ] 
      ]
      
      ask potreros [descansar-potrero]
    end
    

    允许此代码运行的原因是海龟可以对它们所在的补丁的变量进行操作(请参阅User Manual > 编程指南 > 变量)。 因此,例如,要求海龟检查temperatura 不会产生错误,即使temperaturapatches-own

    另一种选择可能是要求补丁(即您使用neighbors 调用的补丁)要求“他们的”海龟做事。您可以使用turtles-here

    to move-potrero
      ask neighbors in-radius 2 [
       if temperatura >= 28.6 or temperatura <= 22.6 or dosel >= 5 or humedad <= 84 [
         ask turtles-here [
           set pcolor red
           facexy random 30 random 30
           fd 5
           set energia energia - 10
          ] 
        ]
      ]
      
      ask potreros [descansar-potrero]
    end
    

    使用此设置,每个相邻的补丁都会检查这些补丁相关的条件,如果其中一个为真,则要求那里的每只海龟做某事。 结果相同,尽管我认为前一个选项需要更少的计算时间:由您选择您喜欢的样式。

    为什么我说“相同”?看下一点。

    2

    neighbors in-radius 2 表达式在 NetLogo 中没有多大意义:neighbors 将报告 8 个相邻的补丁,这些补丁不会随着某个半径而改变。我也可以写neighbors in-radius 1000,但我总是会收到相同的 8 个补丁,因为只有这 8 个是某个补丁的邻居。

    知道这一点特别重要,因为 NetLogo 不会告诉你,即如果你写 neighbors in-radius 2,它不会给出错误消息(实际上这不是语法错误,因为 in-radius 出现在代理集之后在数字之前,就像requires):NetLogo 将继续仅报告相邻的补丁。

    因此,您应该从上面的第 1 点中选择的选项是第一个选项,即带有ask turtles in-radius 2 的选项。事实上,这是让你玩 in-radius 的唯一选项。

    3

    请注意,存在resize-world 命令(请参阅here)。通过将 0 包含为 min-pxcormin-pycor,您将能够在您的代码中自动实现您的世界不包含负坐标的事实。

    【讨论】:

    • Matteo 非常感谢。首先不知道我无法修改邻居这么大的修正!有了这个,你对海龟的纠正非常有用,我认为正确的答案就像你的第二个建议一样,我没有考虑过问海龟自己。我真的很感谢你抽出时间。希望这是最后一个,至少对于这个项目而言。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 2015-10-14
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多