5

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
Community
  • 1
  • 1
JenB
  • 17,620
  • 2
  • 17
  • 45

1 Answers1

5

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.

Community
  • 1
  • 1
Bryan Head
  • 12,360
  • 5
  • 32
  • 50
  • Strange, I'm seeing your new algorithm (with the `count` fix and moving `set candidates other candidates` up) as being way faster. ~6.5 seconds for the new one vs ~30 seconds for the old one. I did not include the fix for some nodes ending up with not enough friends. Both algorithms would need it though. – Bryan Head Oct 08 '15 at 15:05
  • 1
    you are right. Don't know what I did the first time (probably introduced a new bug) but I got 31s for original approach and 9s for new approach with 10000 turtles and 2 friends. Have added answer to original question and will try and get this deleted since the question posed is just a bug and not useful to others. – JenB Oct 08 '15 at 20:13
  • 1
    Those are about the times I was seeing as well. I would recommend keeping the question. The rest of the discussion outside of the bug might be useful to people. – Bryan Head Oct 08 '15 at 20:50