NetLogo创建固定数量链接的有效方法 [英] NetLogo Efficient way to create fixed number of links

查看:117
本文介绍了NetLogo创建固定数量链接的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型中大约有5000名代理商(人员).我想给他们任意数量的朋友,并且有互惠但随机的配对.因此,如果人A选择人B,那么人B也将选择人A.我的代码可以正常工作,但是速度很慢.将来我可能会希望增加朋友的数量和人数.有更快的建议吗?

I have about 5000 agents (people) in my model. I want to give them an arbitrary number of friends and have reciprocal but random pairing. So if person A chooses person B then person B also chooses person A. My code works fine, but is fairly slow. I will likely want to increase both the number of friends and the number of people in the future. Any quicker suggestions?

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

请注意,在上面的代码中,friends是一个全局变量,但是我最终的代码可能会泛化为将wanted-number-of-friends作为个人的属性.

Note that friends is a global variable in the above code, but my eventual code will probably generalise to have wanted-number-of-friends as an attribute of people.

已编辑,添加了if new-links > 0条件,以便在不需要找到任何候选者时避免嵌套的ask.虽然速度有所提高,但仍无法真正扩展.

EDITED Added if new-links > 0 condition so that the nested ask is avoided when no candidates need to be found. This improved speed but still not really scaleable.

推荐答案

好问题.优化实际上是非常具有挑战性的.有问题的行是:

Great question. This is actually quite challenging to optimize. The problematic line is:

let candidates other people with [ count my-links < friends ]

这很慢,因为它需要每个座席与每个其他座席进行检查.拥有5000个代理商,那就是2500万张支票!不幸的是,如果没有一些奇特的数据结构,实际上没有一种优化此特定行的好方法.

This is slow because it has every agent checking with every other agent. With 5000 agents, that's 25,000,000 checks! Unfortunately, there isn't really a good way to optimize this particular line without some fancy data structures.

幸运的是,有一种解决方案可以很好地概括为在网络中生成任何度数分布(听起来这就是您最终想要的).不幸的是,该解决方案无法很好地转换为NetLogo.不过这里是:

Fortunately, there is a solution that generalizes really well to generating any degree distribution in the network (which it sounds like that's what you ultimately want). Unfortunately, the solution doesn't translate super well to NetLogo. Here it is though:

  let pairs [] ;; pairs will hold a pairs of turtles to be linked
  while [ pairs = [] ] [ ;; we might mess up creating these pairs (by making self loops), so we might need to try a couple of times
    let half-pairs reduce sentence [ n-values friends [ self ] ] of turtles ;; create a big list where each turtle appears once for each friend it wants to have
    set pairs (map list half-pairs shuffle half-pairs) ;; pair off the items of half-pairs with a randomized version of half-pairs, so we end up with a list like: [[ turtle 0 turtle 5 ] [ turtle 0 turtle 376 ] ... [ turtle 1 turtle 18 ]]
    ;; make sure that no turtle is paired with itself
    if not empty? filter [ first ? = last ? ] pairs [
      set pairs []
    ]
  ]
  ;; now that we have pairs that we know work, create the links
  foreach pairs [
    ask first ? [
      create-link-with last ?
    ]
  ]

这里friends是全局变量还是乌龟变量都没有关系.花费的时间取决于尝试配对的次数,这是随机的.经过实验,我发现使用5000个代理,每个代理的学位通常为5秒,大约是3秒.相比之下,使用您原来的方式在我的计算机上大约是60秒(这是我所要的)建议使用较少的代理).

It doesn't matter if friends here is a global or a turtle variable. The amount of time this takes depends on the number of times that it needs to try making pairs, which is random. Experimenting, I found that it was usually about 3 seconds with 5000 agents, each with degree 5. This is compared to about 60 seconds on my machine with your original way of doing this (which, for what it's worth, is the way I would recommend when using fewer agents).

这篇关于NetLogo创建固定数量链接的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆