0

I have a population of turtles where I would like them to create a random number of links to other turtles based on a 'bias' (a value from 1 to 5) I've given them. Now, if there aren't enough turtles with the same 'bias' then I would like them to first create links with the turtles with the same bias and then create links to random other turtles.

So far I've figured out this much

    ask people [ 
      set my_friends 1 + random (average-node-degree * 2)
      create-links-to n-of my_friends other people with [bias = [bias] of myself]
     ]

But then I run into the problem that there might not be enough turtles with the same bias as I would like them to create links to. How do make the turtles create links to random turtles with the 'remaining' number of my_friends?

1 Answers1

0

EDIT: The answer has been improved incorporating JenB's suggestion in the comment.

I would do

ask people [ 
  set my_friends 1 + random (average-node-degree * 2)
  let target other people with [bias = [bias] of myself] ; This is just for readability.

  ifelse (my_friends <= count target)
    [create-links-to n-of my_friends target]
    [create-links-to target
     let surplus (my_friends - count target)
     let backup-target people with [bias != [bias] of myself]
     create-links-to up-to-n-of surplus backup-target]
]

So just ask turtles to keep track of the spare friends-space they have, and use it to target people that have a different bias than their preferred one.

Matteo
  • 2,774
  • 1
  • 6
  • 22
  • 1
    This is good but you don't need the second `ifelse` since you can use `up-to-n-of` instead of `n-of` in the second allocation – JenB Jun 16 '21 at 19:24
  • @JenB Indeed! I edited my answer including that, thanks. – Matteo Jun 18 '21 at 22:04