Reputation: 21
I want to create a (scale-free) network where each turtle is connected to a specific number, say four, of other turtles. To allow some turtles to have many connections while others have few (but not less than four), my thought was to let each turtle have four out-links and let in-links would necessarily vary. Using the code for building a Barabasi-Albert scale-free network available on pg. 131 in Scott and Koehler's "A Field Guide to NetLogo" as a starting point, my code is included below.
My question is how to make each turtle connnect to four other turtles.
breed [liberals liberal]
breed [conservatives conservative]
to setup
clear-all
set-default-shape turtles "circle"
create-liberals 5 [
set adopt? false
let n count turtles
set color blue
]
create-conservatives 20 [
set adopt? false
let n count turtles
set color red
]
let m 4
let p .05
set my-threshold my-threshold
ask turtles[
let me self
let degrees max-n-of m turtles [count link-neighbors]
foreach (sort degrees) [ [?] ->
let chance random-float 1.0
if ( ? != self) and (chance < p ) [
ask self [ create-link-to ? ]
]
]
]
ask turtles with [(count out-link-neighbors) = 0 ] [
let degrees max-n-of m turtles [ count link-neighbors ]
let t one-of degrees
foreach (sort degrees) [
ask self [ if ( t != self) [ create-link-to t] ]
]
]
reset-ticks
end
The code above is part of my attempt to recreate Paul Ormerod's model, available at https://onlinelibrary.wiley.com/doi/abs/10.1111/j.1468-0270.2006.00611.x
Upvotes: 2
Views: 1956
Reputation: 17678
Since you are doing a directed network, you won't have any issues with simply telling each turtle to select 4 other turtles and connect to them. The code for that would be:
ask turtles
[ create-links-to n-of 4 other turtles
]
Note that this is much more difficult in undirected networks because the links they 'receive' lead to too many links overall. Then you need to do something like NetLogo Efficient way to create fixed number of links
However, this will not get to your stated goal of a preferential attachment (scale-free, Barabasi-Albert or whatever you want to call it) degree distribution. The mechanism that generates that outcome is that the turtles select the turtles to make links with using weighted random selection, with the weight a normalised degree. You need the rnd
extension and that gets you the weighted-n-of
primitive.
I also noted some general issues with your code that are related to (I think) confusion about how the preferential attachment algorithm works and/or how NetLogo works. The weighted-n-of
primitive will get rid of your need to look at random numbers and should simplify your code a lot. However, I am not clear what let degrees max-n-of m turtles [count link-neighbors]
is supposed to do, but it appears to be creating a list of the four highest degree turtles. But the preferential attachment algorithm allows links to be created with even low degree nodes, just with lower probability.
Upvotes: 4