如何停止孵化。 Link与妈妈

How to stop hatching. Link with the mother

我正在尝试制作一个简单的模型,其中海龟在世界各地移动并且它们只繁殖一次。所以,如果模型从 20 只海龟开始,它必须在达到 40 只时停止(没有在代码中告诉它)。孵化后,新海龟将 link 与母亲同在。这是代码。

breed [sons son]
sons-own [...]

to setup
  ca
  crt 20
  [while [any? other turtles-here]
    [setxy random-xcor random-ycor]]
  reset-ticks 
end

to go
  move
  reproduce
  tick
end

to move
  ask turtles [
  lt random 45
  rt random 45
  fd 0.5
  ]
end

to reproduce
  ask turtles [
    if count my-links = 0 [
      hatch-sons 1 [create-link-with myself]
      ]
    ]
end

此处的代码运行完美,但我想知道是否有像 stop 这样的命令可以在 1 个儿子后停止孵化。我尝试使用 stop,但我尝试的方法似乎没有用。

让你的海龟只孵化一个后代的一种方法是简单地创建一个 turtles-own 变量,你可以用它来跟踪海龟是否已经繁殖。这是一个非常简单的例子:

turtles-own [ have-reproduced? ]

to setup
  ca
  reset-ticks
  crt 20 [
    set have-reproduced? false
    setxy random 30 - 15 random 30 - 15 
  ]
end

to go
  ask turtles [
    if have-reproduced? = false [
      hatch 1       
      set have-reproduced? true
    ]
    fd 1
  ]
end

在上面的代码中,have-reproduced? 变量初始为所有海龟的 false。然而,一旦乌龟孵化出后代,它们的 have-reproduced? 将设置为 true,因此下次调用该过程时它们不会孵化任何后代(尽管它们的后代会)。尝试 运行 该代码并在您的界面上包含 count turtles 的监视器,您将看到每次调用 go 时海龟的数量增加 20。

原始的 stop 是退出循环,但这似乎不必要地复杂,因为您要做的就是取出没有儿子的子集并让他们重现。您现有的代码稍加修改就可以使它们指向 links(否则 children 将不会重现,因为它们的 parent 有一个 link),但这可能更清楚。

to reproduce
  ask turtles with [ count my-out-links = 0 ] [
    hatch-sons 1 [create-link-from myself]
  ]
end

请注意,我还更改了 link 创作以进行定向。