让海龟在 Netlogo 中穿过 5 个补丁后停下来?

问题描述 投票:0回答:1

我对 netlogo 建模还很陌生,并且陷入了代码的某些部分。这个想法本身很简单,但我不知道如何将其转化为编码。也许你能帮忙,非常感谢😊!

所以,有一定数量的海龟分布在网格的下部。有些海龟是“父母”,它们每进行一次程序就会孵化出 1 个后代。此外,海龟在每次执行过程中都会移动。有什么办法可以让海龟在穿过5个补丁后停下来,然后改变颜色,这样就很容易看出哪些海龟停止移动了?

breed [birds bird];my turtles

birds-own [
  parents?
]


to setup
  ca
  ask patches [set pcolor green]
  create-birds 100 [
    set color blue
    set size 2
    move-to one-of patches with [pycor < 50 ] ;100 x 100, wrapped,origin in the left bottom corner 
    set heading random 360
    set parents? false
  ]
  ask n-of 25 birds [set parents? true]
  reset-ticks
end

to go
 give-birth
 fly-around
 stop-flying 
  
  tick
end


  
to give-birth
  ask birds with [parents? = true][
    hatch 1 
  ]
end

to fly-around
  ask birds [
    fd 2 display
  ]
end

to stop-flying
  ask birds-on patches with [pycor > 70][  
;instead of pycor as a condition, I want them do stop and change color after crossing 5 patches.
    set color red
    stop
  ]
end
  

也许可以通过蜱虫来完成,但由于每个蜱虫都会孵化出新的海龟,因此海龟的蜱虫数量并不相同(有些比其他海龟更年轻)。 再次非常感谢!

netlogo agent-based-modeling
1个回答
0
投票

当然,我们的想法是让每只鸟使用一个计数器来检查它更改了补丁的次数。每当小鸟移动时,它都会检查它所在的补丁是否与之前的不同;如果这是真的,计数器将加 1;当计数器达到 5 时,小鸟就会停止。

鉴于您的小鸟移动的距离大于 1(在您的代码中当前为 2),为了使用上面的计数器,我建议让它们一次移动一步,并在每一步后检查补丁。您可以使用

repeat 2 [fd 1 ...]
而不是使用
fd 2
来完成此操作。

您可以按如下方式实现:

birds-own [
  counter
  last-patch
  stopped?
]

to setup
  ...
  create-birds 100 [
    ...
    set last-patch [patch-here]
    set stopped? FALSE
    ...
  ]
  ...
end

to fly-around
  repeat 2 [
    if (not stopped?) [
      fd 1
      if (patch-here != last-patch) [
        set counter counter + 1
        if (counter = 5) [
          set stopped? TRUE
          set color red
        ]
      ]
    ]
  ]
end

还有一些空间可以使此代码更高效(例如:到目前为止,每只鸟都会检查两次

if (not stopped?)
,因为它位于
repeat 2 [ ... ]
命令块内,即使它已经有
stopped? = TRUE
),但那就是逻辑差不多。

注意事项:

  • set heading random 360
    setup
    中是多余的,因为这是通过
    create-turtles
    创建海龟时的默认行为。但是,您可能想在
    hatch
    中使用它,因为孵化的海龟将继承父级的标题。这取决于你的意图。
© www.soinside.com 2019 - 2024. All rights reserved.