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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With