NetLogo:如果变量值相同,则使代理链接 [英] Netlogo: getting agents to link if variables values are the same
问题描述
我试图获取两种代理(xagents和yagents)来检查变量的值在xagents半径之一内移动时是否相同.然后,他们应该链接.
I am trying to get two breeds of agents (xagents and yagents) to check to see if the value of a variable is the same when they move within one of xagents' radius. Then they should link.
当我尝试下面的代码时,它们会链接,但是当我检查链接的代理的值时,变量值不相等;他们不应该链接.问题的过程在代码的末尾.任何想法为什么会这样?
When I try the code below, they link but when I inspect the values of the linked agents, the variable values are not equal; they should not be linking. The problem procedure is at the end of the code. Any ideas why this is?
当我可以跳过这部分时,我希望代理更改另一个变量的值,但前提是它们的值必须与VAR1相同(因此链接).
When I can move past this part, I want the agents to change the value of another variable, but only if they have the same value as VAR1 (hence the link).
breed [xagents xagent]
breed [yagents yagent]
turtles-own [var1]
to setup
clear-all
resize-world -20 20 -20 20
setup-patches
setup-turtles
reset-ticks
end
to
setup-patches
ask patches [set pcolor gray ]
end
to
setup-turtles
set-default-shape xagents "circle 3"
create-xagents 10
[
set color white
set size 2
set var1 random-normal 5 1
setxy random-xcor random-ycor
]
set-default-shape yagents "circle 3"
create-yagents 20
[
set color blue
set size 2
set var1 random-normal 5 1
setxy random-xcor random-ycor
]
end
to go
move-xagents
move-yagents
ask xagents [communicate]
tick
end
to move-xagents
ask xagents [
rt random 50
lt random 50
forward 1
]
end
to move-yagents
ask yagents [
rt random 50
lt random 50
forward 1
]
end
;;THIS IS THE PROBLEM
to communicate
ask xagents in-radius 1 with [var1 = [var1] of myself]
[create-links-with other yagents-here
[
set color white
set thickness 0.1
]
]
end
推荐答案
此处存在一些可能会导致您遇到问题的问题.首先:
There are a few issues here that might be causing you problems. First:
set var1 random-normal 5 1
如果需要进行匹配,则两个代理共享random-normal
中的值的可能性极低-它会返回浮点值:
If you need have matches occur, the likelihood that two agents will share the value from random-normal
is extremely low- it returns a float value:
observer> show random-normal 5 1
observer: 4.051232264359846
选择另一种方法来选择var1
的值(例如random-poisson
或one-of [ 1 2 3 4 5 ]
),否则将无法获得匹配项.原始代码提供链接的唯一原因是other xagents
未包含在to communicate
代码块中(请参见下文).
Choose another way to select values for your var1
(eg random-poisson
, or one-of [ 1 2 3 4 5 ]
) or you will not get matches. The only reason your original code was giving links was because other xagents
was not included in your to communicate
code block (see below).
ask xagents in-radius 1 with [var1 = [var1] of myself]
在这里您应该使用other xagents
,否则您将包括询问代理,而不仅仅是其他in-radius 1
.
Here you should use other xagents
or you will include the asking agent, not just the other ones in-radius 1
.
[create-links-with other yagents-here
...
因此,在这里,您已经使用条件选择要构成链接的xagents
,但是相同的条件未应用于yagents
端.因此,您将获得xagents
与任何yagents-here
形成链接的功能.要解决此问题,只需确保yagents
还必须具有您要使用的var1
,例如:
So here, you have already used the conditional to select the xagents
that you would like to form a link, but the same conditional is not applied to the yagents
side. So, you were getting xagents
forming links with any yagents-here
. To fix that, just make sure that the yagents
must also have the var1
that you're after, something like:
to communicate
ask other xagents in-radius 3 with [var1 = [var1] of myself]
[create-links-with yagents in-radius 3 with [var1 = 2 ]
[
set color white
set thickness 0.1
]
]
end
这篇关于NetLogo:如果变量值相同,则使代理链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!