Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetLogo Efficiently create network with arbitrary degree distribution

Tags:

netlogo

This is a follow up question to NetLogo Efficient way to create fixed number of links. Having focussed on avoiding the nested `ask', I now have this code. It is much more efficient, but is creating too many links. Clearly a logic error but I can't see it.

globals
[ candidates
  friends
]

to setup
  clear-all
  set friends 2
  create-turtles 5000
  set candidates turtles
  make-network
end

to make-network
  ask turtles
  [ let new-links friends - count my-links
    if new-links > 0
    [ let chosen n-of min (list new-links count other candidates) other candidates
      create-links-with chosen [ hide-link ]
      set candidates other candidates
      ask chosen [ if my-links = friends [ set candidates other candidates ] ]
    ]
  ]
end
like image 281
JenB Avatar asked Oct 07 '15 19:10

JenB


1 Answers

Nice solution! Note that other candidates actually iterates through every agent in the agentset, so it will still get slow with lots of agents, but less so than in your other question since it's not having those agents run code. I'm really impressed by how quickly it runs!

Onto the bug. In this part:

if my-links = friends [ set candidates other candidates ]

I think you forgot a count in front of my-links.

The code can still end up with some agents with less than friends, since the world can be out of candidates by the time it gets to them. Not sure how much you care about this. You could just clear the links and retry until you have the right number. That should be okay as long as friends is pretty small.

Note that you can speed the code up a little bit by putting the set candidates other candidates at the beginning like so:

set candidates other candidates
if new-links > 0
[ let chosen n-of min (list new-links count candidates) candidates
  create-links-with chosen [ hide-link ]
  ask chosen [ if my-links = friends [ set candidates other candidates ] ]
]

That way, you avoid having to calculate other candidates several times.

like image 128
Bryan Head Avatar answered Sep 22 '22 14:09

Bryan Head