问题
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?
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
回答1:
There are a few issues here that might be causing you problems. First:
set var1 random-normal 5 1
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
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]
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
...
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
来源:https://stackoverflow.com/questions/44122905/netlogo-getting-agents-to-link-if-variables-values-are-the-same