Search code examples
netlogodie

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


In NetLogo: suppose the model has

  1. a turtle (0) of breed A with undirected links with 3 turtles (1, 2 and 3) of breed B;
  2. the turtle 0 has an attribute named "number-of-links" that equals 3.

Now, let one of the 3 neighbors of 0 dies..

How can I program turtle 0 to change its number-of-links automatically to 2?


Solution

  • If all you want is a way of keeping track of the number links, use count my-links instead of a custom variable.

    In general, the least bug prone way of having a value update when the number of links changes is to compute that value when you need it. For number of links, this is simply count my-links. For more complicated things, wrap them in a reporter:

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

    If this doesn't work for whatever reason (agents need to react to a link disappearing or you're seeing a serious, measurable performance hit from calculating on the fly), you'll have to make the updates yourself when the number of links change. The best way to do this is to encapsulate the behavior in a command:

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

    and then encapsulate the things that can cause the number of links to change (such as turtle death) in commands as well:

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