从同一个巢中出来时,如何使两种海龟交替出现

时间:2019-05-09 15:12:14

标签: netlogo

在这种蚂蚁模型中,我有两种乌龟:未成年人和觅食者。现在,所有觅食者都会出来一个文件,然后是所有未成年人(这是通过延迟初始离开来完成的。我希望他们出门时轮流到我用完觅食者或未成年人用完为止。 >

我曾尝试将问龟[如果>>谁滴答[停止]]作为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

我希望觅食者和未成年人在离开巢穴时轮流交替。现在所有的觅食者都比未成年人要早。

1 个答案:

答案 0 :(得分:0)

who编号是在每个乌龟创建时分配的,并且与品种无关。因此,如果您创建10个觅食者,然后创建10个未成年人,则您的觅食者的who值将从0到9,而您的未成年人的who值则从10到19。因此,无论哪个品种您首先创建(因此具有最小范围的who数字)将根据您的if who >= ticks...代码开始移动。要使基于who的代码完成您需要的工作,您必须交替创建觅食者和未成年人。

但是,通常使用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

该代码输出如下内容:

enter image description here