Netlogo Error: "This <turtle> is already dead

Netlogo Error: "This <turtle> is already dead

我正在研究 netlogo 中的集群行为,为了跟踪各种集群,我使用了一只隐藏的 "flock-holder" 乌龟,如果创建了新集群或现有集群,我可以孵化或让它死亡成员用完。然而,我遇到了一个问题,有时当我尝试与群中的某些数据交互时,通过群中的单个成员引用时,我收到一条消息说 "That is dead",导致代码失败。

根据我对 "die" 命令的理解,如果任何种类的海龟死亡,它应该将自己从引用它的任何代理集或变量中删除,因此这种错误不应该是问题?我该如何修复或至少调试这个奇怪的问题?

我的 flock-evaluation 函数的代码存在以下问题:

to evaluate-flock
  if is-in-flock = True ; checking to see if a flock has died out
  [
    if get-flock-size flock-reference < 2 ; is the turtle the only one in the flock?
    [
      if verbose = True [ print "Flock has dwindled to nothing" ]
      ask flock-reference ; has no more members, so is removed.
      [
        ask flock-members
        [
          set flock-reference nobody ; clear any remaining flock members of association with this flock
        ]
        die
      ]
      set is-in-flock False ; no longer in a flock
    ]
  ]
  ifelse is-in-flock = True ; is turtle in a flock?
  [
    if verbose = True [ type "This turtle is in flock " print [ who ] of [ flock-reference ] of self ]
    if any? other preys in-radius vision with [ is-in-flock = True ] with [ flock-reference != [ flock-reference ] of myself ]; check for nearby turtles that are in different flocks
    [
      if verbose = True [ print "There are other nearby flocks" ]
      let current-school-size ( get-flock-size [ flock-reference ] of self )
      if verbose = True [ type "I am part of a school of " print current-school-size ]
      let temp-list turtle-set other preys in-radius vision with [ is-in-flock = True ] with [ flock-reference != [ flock-reference ] of myself ] with [ ( get-flock-size flock-reference ) > current-school-size ] with [ subtract-headings ( average-schoolmate-heading [ flock-members ] of flock-reference ) heading < 60]; are any nearby turtles in different, larger flocks that I am alligned with? if so, add them to a list
      if count temp-list > 0 ; does the list have any members?
      [
        if verbose = True [ print "Found a bigger flock" ]
        ask flock-reference
        [
          remove-from-flock myself ; remove myself from my old flock
        ]
        set flock-reference [ flock-reference ] of ( max-one-of temp-list [ get-flock-size flock-reference ] ); join the biggest flock on this list
        set is-in-flock True ; sets it to true in case it wasn't for some reason.
      ]
    ]
  ]
  [
    if verbose = True [ type "Turtle " type [ who ] of self print " is not in a flock" ]
    ifelse any? other preys in-radius vision with [ is-in-flock = True ] ; are there any pre-existing flocks the turtle can join?
    [
      if verbose = True [ print "There are nearby flocks" ]
      let potential-flock turtle-set other preys in-radius vision with [ is-in-flock = True ] ; grab any nearby turtles that are already in a flock
      ***set potential-flock potential-flock with [ subtract-headings ( average-schoolmate-heading ( [ flock-members ] of flock-reference ) ) heading < 60]; remove any that are not aligned with this turtle***
      if count potential-flock > 0
      [
        if verbose = True [ print "There are nearby flocks that I am aligned with" ]
        set flock-reference [ flock-reference ] of ( max-one-of potential-flock [ get-flock-size flock-reference ] ); join the biggest flock on this list
        set is-in-flock True ; turtle is now in a flock
      ]
    ]
    [ ; if there are no pre-existing flocks, turtle starts its own
      let potential-flock turtle-set other preys in-radius vision with [ is-in-flock = False ] ; Grab any nearby turtles not already in a flock
      set potential-flock potential-flock with [ subtract-headings ( average-schoolmate-heading potential-flock ) heading < 60]; remove any that that are not aligned with this turtle
      if count potential-flock > 0
      [
        if visualize-flock-creation = True
        [
          set color green
          ask potential-flock [ set color green ]
          wait 0.25
          set color blue
          ask potential-flock [ set color blue ]
        ]
        if verbose = True [ type "Number of nearby potential flockmates " print count potential-flock ]
        hatch-flock-holders 1 ; create a new flock-holder
        [
          set size 0
          set color black ; sets the new flock's placeholder color to the background
          set flock-members potential-flock ; adds the list of members to the new flock
          ask flock-members
          [
            set flock-reference myself ; asks the new flock members to add the new flock as their flock-reference 
            set is-in-flock True ; all these turtles are now in a flock
          ]
        ]
      ]
    ]
  ]
end

以上代码的变量名引用可能不清楚: flock-reference: - 每个成群的 "prey" 乌龟持有的变量,它只指向隐藏的 "flock-holder" 乌龟。 flock-members:- "prey" 海龟的代理集附加到隐藏的 "flock-holder" 海龟。

我在下面添加了完整错误消息的图片。

如果对这里发生的事情有任何困惑,或者有什么我可以澄清的,请告诉我。谢谢!

我无法对此进行测试,但我认为错误源于代理 prey 2 引用了已死亡的 flock-holder。当 flock-holder 死亡时,它(如您所知)从它所属的任何代理集中删除,并且保存指向它的指针的变量被重置为指向 nobody。然而,NetLogo 足够聪明,知道这个 nobody 是死的 flock-holder 并给你你遇到的错误信息。如果在错误发生后您检查 prey 2,或在命令行输入 show [flock-reference] of prey 2,我希望您会发现 flock-reference 确实设置为 nobody

我的猜测是,在您的代码中的某处,并非(现已死亡)羊群中的所有猎物都被重新分配给另一个羊群,而是保留了它们的旧值 flock-reference,现在 [=13] =].当你要求一群人死去时,你可以添加行 show preys with [flock-reference = nobody]。如果有的话,你可以追查原因。

希望这对您有所帮助, 查尔斯