如果他们的var1值相等,我有海龟链接(这很好)。我想将链接数限制为三个。我在代码的链接部分(If count my-links < 3
)之前添加了一个IF语句,但它不起作用;代理继续链接超过我设置的最大值。我读了另一个问题How to limit the number of links an agent can make in a model,但这似乎并不像我在这里尝试的那样。我做错了什么?
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
答案 0 :(得分:2)
通过查看完整的模块,正如@JenB所提到的那样,似乎没有条件限制目标乌龟用于建立链接的链接数量。
这将是第一步:
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语句。
我还认为您需要在代码中使用one-of
来在每个步骤中只创建一个链接。
您可以在communicate
子程序结束时使用此功能来终止额外链接。它有一个随机删除链接的缺点,也可以删除链接较少的链接而不是那些可能还有额外链接的链接。
ask turtles with [count my-links > LIMIT] [ if count my-links > LIMIT [ask n-of (count my-links - LIMIT) my-links [die]] ]