限制代理可以建立的链接数量 [英] Limit the number of links an agent can make
问题描述
如果海龟的 var1 值相等,我会链接它们(这很好用).我想将链接数量限制为三个.我在代码的链接部分之前添加了一个 IF 语句(If count my-links <3
),但它不起作用;代理继续链接超过我设置的最大值.我读了另一个问题 如何限制代理可以在模型中创建的链接数量 但这似乎并不能完全满足我在这里的尝试.我做错了什么?
I have turtles linking if they have an equal value for var1 (this works fine). I want to limit the number of links to just three. I added an IF statement before the linking part of the code (If count my-links < 3
), but it does not work; the agents continue to link past the max value I set. I read the other question How to limit the number of links an agent can make in a model but that doesn't seem to quite do what I am attempting here. What am I doing wrong?
to communicate
If count my-links < 3
[
ask other xagents in-radius 5 with [var1 = [var1] of myself]
[create-links-with yagents in-radius 5 with [var1 = [var1] of myself]
[
set color white
set thickness 0.1
]
]
]
end
推荐答案
在让海龟创建新链接之前限制它们的链接数:
通过查看您的完整模块,正如@JenB 提到的那样,似乎没有条件限制目标海龟用于建立链接的链接数量.
Limit number of links for the turtles before letting them create new ones:
By looking at your complete module, as @JenB mentioned that, it seems that there's no condition to limit number of links that the targeted turtle has for making a link.
这是第一步:
to communicate
If count my-links < 3
[
ask other xagents in-radius 5 with [(var1 = [var1] of myself) and (count my-links < 3)]
[create-links-with yagents in-radius 5 with [(var1 = [var1] of myself) and (count my-links < 3)]
[
set color white
set thickness 0.1
]
]
]
end
但是如果没有这样的代理怎么办?(半径为 5,具有相同的 val1 和小于 3 的链接)可能需要一个 if 语句.
But what if there's no agent like that? (in radius of 5, with the same val1 and links less than 3) Probably an if-statement is needed.
我还认为您需要在代码中使用 one-of
以在每个步骤中只创建一个链接.
I also think you need to use one-of
in your code to make only one link in each step.
您可以在 communicate
子过程的末尾使用它来终止额外的链接.它有一个缺点是随机删除链接,也可能从链接较少的海龟中删除链接,而不是那些也可能有额外链接的海龟.
You can have this at the end of your communicate
sub-procedure to kill the extra links. It has a down-side of random removing links and also may remove the link from turtles with fewer link instead of the ones that may also have extra links.
ask turtles with [count my-links > LIMIT] [ if count my-links > LIMIT [ask n-of (count my-links - LIMIT) my-links [die]] ]
这篇关于限制代理可以建立的链接数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!