如何限制代理在模型中可以建立的链接数? [英] How to limit the number of links an agent can make in a model?
问题描述
我正在建立一个模型,其中包含许多通过链接进行连接的代理,如下所示:
I'm setting up a model with a number of agents who are connected via links as follows:
ask turtles [create-links-with turtles in-radius vision with [self != myself]]
但是我希望能够限制单个代理可以建立的连接数.我尝试了几件事,但无济于事.
But I want to be able to limit the number of connections that an individual agent can make. I've tried a few things but to no avail.
希望可以为您提供帮助.
Hope you can help.
推荐答案
您可以像这样使用n-of
原语获取随机选择的海龟子集进行链接:
You can get a randomly selected subset of turtles to link to using the n-of
primitive like this:
ask turtles [create-links-with n-of 3 turtles in-radius vision with [self != myself]]
但是,如果您想要一个固定的上限,您将需要做一些棘手的事情,因为这不会阻止其他乌龟创建指向同一只乌龟的链接.如果您想要固定数量的链接(在下面的示例中为5),则可以执行以下操作:
However, you will need to do something a bit trickier if you want a firm upper limit because this doesn't stop other turtles from creating links to the same turtle(s). If you want a fixed number of links (5 in example below), you can do this:
ask turtles
[ let new-links 5 - count my-links
let candidates other turtles with [ count my-links < 5 ]
create-links-with n-of min (list new-links count candidates) candidates
[ ... ]
]
如果您只想要一个上限,则可以要求具有my-links> limit的所有乌龟随机选择要删除的适当数量的链接.因此,创建链接后,类似以下内容(未经测试):
If you simply want an upper limit, you could ask any turtles with my-links > limit to randomly select the appropriate number of links to delete. So, after creating links, something like this (not tested):
ask turtles with [count my-links > LIMIT]
[ if count my-links > LIMIT [ask n-of (count my-links - LIMIT) my-links [die]] ]
这篇关于如何限制代理在模型中可以建立的链接数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!