NetLogo 高效创建具有任意度分布的网络
NetLogo Efficiently create network with arbitrary degree distribution
这是 的后续问题。在专注于避免嵌套的“询问”之后,我现在有了这段代码。它的效率要高得多,但会创建太多链接。明明是逻辑错误,可是我看不出来
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
不错的解决方案!请注意 other candidates
实际上 iterates through every agent in the agentset, so it will still get slow with lots of agents, but less so than in 因为它没有那些代理 运行 代码。 运行s!
的速度之快让我印象深刻
关于错误。这部分:
if my-links = friends [ set candidates other candidates ]
我想你忘记了 my-links
前面的 count
。
代码仍然可能以一些小于 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 ] ]
]
这样,您就不必多次计算 other candidates
。
这是
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
不错的解决方案!请注意 other candidates
实际上 iterates through every agent in the agentset, so it will still get slow with lots of agents, but less so than in
关于错误。这部分:
if my-links = friends [ set candidates other candidates ]
我想你忘记了 my-links
前面的 count
。
代码仍然可能以一些小于 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 ] ]
]
这样,您就不必多次计算 other candidates
。