Search code examples
social-networkingnetlogo

How to limit the number of links an agent can make in a model?


I'm setting up a model with a number of agents who are connected via links as follows:

ask turtles [create-links-with turtles in-radius vision with [self != myself]]

But I want to be able to limit the number of connections that an individual agent can make. I've tried a few things but to no avail.

Hope you can help.


Solution

  • You can get a randomly selected subset of turtles to link to using the n-of primitive like this:

    ask turtles [create-links-with n-of 3 turtles in-radius vision with [self != myself]]
    

    However, you will need to do something a bit trickier if you want a firm upper limit because this doesn't stop other turtles from creating links to the same turtle(s). If you want a fixed number of links (5 in example below), you can do this:

      ask turtles
      [ let new-links 5 - count my-links
        let candidates other turtles with [ count my-links < 5 ]
        create-links-with n-of min (list new-links count candidates) candidates
        [ ... ]
      ]
    

    If you simply want an upper limit, you could ask any turtles with my-links > limit to randomly select the appropriate number of links to delete. So, after creating links, something like this (not tested):

    ask turtles with [count my-links > LIMIT]
    [ if count my-links > LIMIT [ask n-of (count my-links - LIMIT) my-links [die]] ]