NetLogo 高效创建任意度分布的网络

Jen*_*enB 5 netlogo

这是NetLogo Efficient way to create fixed number of links的后续问题。在专注于避免嵌套的“询问”之后,我现在有了这段代码。它效率更高,但会创建太多链接。显然是逻辑错误,但我看不到。

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
Run Code Online (Sandbox Code Playgroud)

Bry*_*ead 5

不错的解决方案!请注意,other candidates实际上会遍历 agentset 中的每个代理,因此使用大量代理时它仍然会变慢,但比您的其他问题要慢,因为它没有让这些代理运行代码。它的运行速度给我留下了深刻的印象!

到错误上。在这部分:

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

我想你忘记了count前面的一个my-links

代码仍然可能会导致一些代理小于friends,因为当它到达时,世界可能已经没有候选人了。不知道你有多少关心这个。您可以清除链接并重试,直到您获得正确的号码。只要friends很小就应该没问题。

请注意,您可以通过将 放在set candidates other candidates开头来加快代码速度,如下所示:

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 ] ]
]
Run Code Online (Sandbox Code Playgroud)

这样,您就可以避免other candidates多次计算。