2

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.

adkane
  • 1,429
  • 14
  • 29

1 Answers1

3

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]] ]    
JenB
  • 17,620
  • 2
  • 17
  • 45
  • Thanks for your response. The last bit of code you suggested is what I want it to do i.e. set an upper limit. However when I implemented it I occasionally get an error message saying: First input to N-OF can't be negative. I don't really get that because surely the first line should get rid of turtles who could cause that error. – adkane Jun 24 '15 at 09:22
  • Okay, the reason you are getting that error is because NetLogo first creates the set of turtles and then does the deletion process on all of them in random order. At least some of the time, a deletion arising from an earlier turtle then makes the later turtle get under the limit because it is at the other end of the deleted link. I will update my answer. – JenB Jun 24 '15 at 09:28
  • Great, that works much better. So just to clarify that shouldn't set a lower limit too? In other words the agents can have fewer links than the limit? – adkane Jun 24 '15 at 09:59
  • yes, it will just find those that have too many after the creation process and then delete some links to bring them down. Good practice is to test each piece of code after you write it to make sure it does what you think it does. For this, I would create a histogram of the number of links (count my-links) for turtles and you will be able see that some have different numbers of links and that none have more than your limit. You can delete the histogram once you have tested the code. – JenB Jun 24 '15 at 11:07