如果其中一个链接消失,如何更改海龟的属性?

How to change a turtle's attribute if one of its links disappear?

在 NetLogo 中:假设模型有

  1. 一只品种 A 的海龟 (0) 与品种 B 的 3 只海龟 (1、2 和 3) 有无向链接;
  2. 乌龟 0 有一个名为 "number-of-links" 的属性,它等于 3。

现在,让 0 的 3 个邻居之一死亡..

我如何编程让海龟 0 自动将其链接数更改为 2?

如果您只需要一种跟踪数字 link 的方法,请使用 count my-links 而不是自定义变量。

一般来说,在 link 的数量发生变化时更新值的最不容易出错的方法是在需要时计算该值。对于 link 的数量,这只是 count my-links。更复杂的东西,用reporter包起来:

to-report energy-of-neighbors
  report sum [ energy ] of link-neighbors
end

如果出于某种原因这不起作用(代理需要对 link 消失做出反应,或者您看到即时计算对性能造成严重的、可衡量的影响),您将不得不当 link 的数量发生变化时,您自己进行更新。最好的方法是将行为封装在命令中:

to update-on-link-change [ link-being-removed ] ;; turtle procedure
  ; update stuff
end

然后把能引起link数量变化的东西(比如海龟死亡)也封装在命令里:

to linked-agent-death ;; turtle procedure
  ask links [
    ask other-end [ update-on-link-change myself ]
  ]
  die
end