【问题标题】:Netlogo Error: "This <turtle> is already deadNetlogo 错误:“这个 <turtle> 已经死了
【发布时间】:2020-06-19 17:38:21
【问题描述】:

我正在 netlogo 中研究群聚行为,为了跟踪各种群,我使用了一个隐藏的“flock-holder”海龟,如果创建了新群或现有群用完,我可以孵化或让它死亡的成员。但是,我遇到了一个问题,有时当我尝试与群中的某些数据交互时,正如通过群中的单个成员所引用的那样,我收到一条消息说“那已经死了”,导致代码失败。

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

我的羊群评估函数的代码存在以下问题:

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-holder”乌龟。 flock-members: - 附着在隐藏的“flock-holder”海龟上的“猎物”海龟的代理集。

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

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

【问题讨论】:

  • 您能告诉我们错误发生在代码的哪一行吗?我怀疑你明确指的是猎物 2。
  • 我已经更新了我原来的帖子,用星号包围了这条线。
  • 突出显示“减去标题(average-schoolmate-heading([flock-members] of flock-reference))标题”中的“of”

标签: netlogo


【解决方案1】:

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

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

希望这会有所帮助, 查尔斯

【讨论】:

  • 这很有帮助,主要是因为我知道它在哪里设置为“nobody”。我认为错误源于它试图引用“nobody”,并给我一个稍微令人困惑的错误消息。我期待的错误消息更像是“没有人指向这个变量”而不是“那只乌龟死了”。但我会看看这是否有帮助,谢谢!
  • @AlpheausFeltham 收到解决问题的答案后,您应该接受它 - 这将归功于回答它的人,并从未回答列表中删除问题
猜你喜欢
  • 2023-03-27
  • 2013-07-04
  • 2021-06-29
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
相关资源
最近更新 更多