如何让两个品种的海龟在同一个巢穴中交替出现

How to make two breeds of turtles alternate when coming out of the same nest

在这个蚂蚁模型中,我有两个品种的海龟:未成年人和觅食者。现在所有的觅食者都会排成一列,然后是所有的未成年人(这是通过延迟初始离开来完成的。我希望他们轮流出来,直到我 运行 离开觅食者或 运行 未成年人。

我试过将 ask turtles [if who >= ticks [ stop ]] 作为 GO 程序的第一步,但这使得所有的海龟都从巢中爆炸出来,而不是留下一个文件。

to go 
  ask foragers
  [ if who >= ticks [ stop ]  ;; delay initial departure
    wiggle
    fd 1 ]
  ask minors
  [ if who >= ticks [ stop ]  ;; delay initial departure
    ifelse color = white
    [ look-for-transporter ]
    [ hitchhike ]]
  tick
end

我希望觅食者和未成年者在离巢时交替。现在所有的觅食者都先于未成年人离开。

who 编号分配给每个 turtle,因为它是创建的并且独立于品种。因此,如果您创建 10 个觅食者,然后创建 10 个未成年人,则您的觅食者将具有从 0 到 9 的 who 值,而您的未成年人将具有从 10 到 19 的 who 值。因此,无论哪个品种您首先创建(因此具有最低范围的 who 数字)将根据您的 if who >= ticks... 代码开始移动。要让基于 who 的代码执行您需要的操作,您必须交替创建 foragers 和 minors。

但是,通常使用 who 数字会有一些限制 - 您可能会发现创建自己的变量或以其他方式控制它更容易。例如,下面的设置在世界的最左侧创建了一个 nest-patch 并将一些觅食者和未成年人移动到该补丁。海龟有一个名为 at-nest? 的布尔变量,您可以使用它来控制哪些海龟可以移动:

breed [ foragers forager ]
breed [ minors minor ]

globals [ last-left nest-patch ]
turtles-own [ at-nest? ]

to setup
  ca
  create-foragers 10 [ set color red ]
  create-minors 20 [ set color blue ]
  set nest-patch patch min-pxcor 0
  ask nest-patch [ set pcolor yellow ]
  ask turtles [
    move-to nest-patch
    set heading 90
    set at-nest? true
    pd
  ]
  set last-left minors
  reset-ticks
end

最初,所有海龟的 at-nest? 都设置为 true。然后,您可以在您要求将 at-nest? 设置为 true 的每个品种的个体之间切换。看看下面的例子,评论中有更多细节:

to go
  ; If there are any turtles on the nest patch with at-nest? set to true
  if any? ( turtles-on nest-patch ) with [ at-nest? ] [
    ; If both breeds are present on the nest patch, alternate back and forth
    ; between breeds to set at-nest? to false
    ifelse ( length remove-duplicates [breed] of turtles-on nest-patch = 2 ) [
      set last-left ifelse-value ( last-left = minors ) [ foragers ] [ minors ]
      ask one-of last-left with [ at-nest? ] [
        set at-nest? false
      ]
    ] [
      ; Otherwise, just ask one-of whichever breed is left to set at-nest? to false
      ask one-of turtles-on nest-patch [
        set at-nest? false
      ]   
    ]
  ]

  ; Ask any turtles who have at-nest? set to false to move
  ask turtles with [ not at-nest? ] [
    if heading != 90 [
      rt ifelse-value ( breed = minors ) [ 12 ] [ -12 ]
    ]
    if xcor > 0 and heading = 90 [
      rt ifelse-value ( breed = minors ) [ 12 ] [ -12 ]
    ]
    fd 1
  ]      
  tick
end

该代码输出如下内容: