问题描述
这是对> NetLogo高效方法的后续问题创建固定数量的链接.专注于避免嵌套的询问"之后,我现在有了这段代码.它效率更高,但是创建了太多链接.显然是逻辑错误,但我看不到.
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
推荐答案
很好的解决方案!请注意,other candidates
实际上遍历代理集中的每个代理,因此在许多代理中它仍然会变慢,但是要比,因为它没有这些代理运行代码.它的运行速度让我印象深刻!
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!
该错误.在这一部分:
if my-links = friends [ set candidates other candidates ]
我认为您忘记了my-links
前面的count
.
I think you forgot a count
in front of my-links
.
代码仍然可以以小于friends
的某些代理结束,因为在到达代理之前,世界可能已经不存在了.不知道您对此有多关心.您只需清除链接,然后重试,直到获得正确的号码为止.只要friends
很小,就可以了.
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.
请注意,您可以通过将set candidates other candidates
放在开头这样来加快代码的速度:
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 ] ]
]
那样,您不必多次计算other candidates
.
That way, you avoid having to calculate other candidates
several times.
这篇关于NetLogo高效地创建具有任意程度分布的网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!