NetLogo - 计算相邻代理变量的差异
NetLogo - calculate the difference of a variable of neighbouring agents
我想让每个代理向他们的邻居询问他们的海龟拥有变量的值,并根据拥有的差异设置它们。
我知道如何计算距离:
if (any? other turtles-here)
[
ask neighbors [ ;; ask 8 neighbors / neighbors4 for 4
;if (max-one-of turtles [distance myself]) <= 3
;[set opinion opinion - .1] ; no change in opinion
;if (distancexy point1-pxcor point1-pycor) > 20 and (distancexy point1-pxcor point1-pycor) <= 50
;[set point1-location "middle"]
;if (distancexy point1-pxcor point1-pycor) > 50
;[set point1-location "far"]
]
然而,为了交换价值,我很难实施它。我该如何实现?
这是我的 MWE。
请注意,相关代码部分是伪代码。
breed [ turtles ]
turtles-own [ variable ]
to setup
clear-all
create-turtles 100
[
set variable random-float 10
]
reset-ticks
end
to communicate
if (any? other turtles-here)
[
ask neighbors [
pseudo-code: if difference of your variable and my variable is bigger then 3, than do nothing
if differences less then 3, calculate the higher variable minus 0.1 and the lower variable plus 0.1
if difference less then 2, calculate the higher variable minus 0.3 and the lower plus 0.3
if difference less then 1, calculate the arithmetical mean
]
]
end
to go
ask turtles [
rt random 360
fd 1
communicate
]
tick
end
您的伪代码留下了很大的解释空间,因此这可能不是您想要的,但我认为它可以帮助您入门:
to communicate
ask turtles-on neighbors [
let both-turtles (turtle-set self myself)
let difference abs (variable - [ variable ] of myself)
if difference < 1 [ ask both-turtles [ set variable mean [ variable ] of both-turtles ] ]
if difference < 2 [ bring-closer both-turtles 0.3 ]
if difference < 3 [ bring-closer both-turtles 0.1 ]
]
end
to bring-closer [ both-turtles delta ]
ask min-one-of both-turtles [ variable ] [ set variable variable + delta ]
ask max-one-of both-turtles [ variable ] [ set variable variable - delta ]
end
这里发生了很多事情,但没有什么太复杂的。我认为您在这里必须理解的主要概念是:self
和 myself
,将代理集存储在局部变量中的想法,以及编写带参数的过程的想法。您可以在 programming guide and look up the relevant primitives in the dictionnary.
中阅读所有这些内容
我想让每个代理向他们的邻居询问他们的海龟拥有变量的值,并根据拥有的差异设置它们。
我知道如何计算距离:
if (any? other turtles-here)
[
ask neighbors [ ;; ask 8 neighbors / neighbors4 for 4
;if (max-one-of turtles [distance myself]) <= 3
;[set opinion opinion - .1] ; no change in opinion
;if (distancexy point1-pxcor point1-pycor) > 20 and (distancexy point1-pxcor point1-pycor) <= 50
;[set point1-location "middle"]
;if (distancexy point1-pxcor point1-pycor) > 50
;[set point1-location "far"]
]
然而,为了交换价值,我很难实施它。我该如何实现?
这是我的 MWE。
请注意,相关代码部分是伪代码。
breed [ turtles ]
turtles-own [ variable ]
to setup
clear-all
create-turtles 100
[
set variable random-float 10
]
reset-ticks
end
to communicate
if (any? other turtles-here)
[
ask neighbors [
pseudo-code: if difference of your variable and my variable is bigger then 3, than do nothing
if differences less then 3, calculate the higher variable minus 0.1 and the lower variable plus 0.1
if difference less then 2, calculate the higher variable minus 0.3 and the lower plus 0.3
if difference less then 1, calculate the arithmetical mean
]
]
end
to go
ask turtles [
rt random 360
fd 1
communicate
]
tick
end
您的伪代码留下了很大的解释空间,因此这可能不是您想要的,但我认为它可以帮助您入门:
to communicate
ask turtles-on neighbors [
let both-turtles (turtle-set self myself)
let difference abs (variable - [ variable ] of myself)
if difference < 1 [ ask both-turtles [ set variable mean [ variable ] of both-turtles ] ]
if difference < 2 [ bring-closer both-turtles 0.3 ]
if difference < 3 [ bring-closer both-turtles 0.1 ]
]
end
to bring-closer [ both-turtles delta ]
ask min-one-of both-turtles [ variable ] [ set variable variable + delta ]
ask max-one-of both-turtles [ variable ] [ set variable variable - delta ]
end
这里发生了很多事情,但没有什么太复杂的。我认为您在这里必须理解的主要概念是:self
和 myself
,将代理集存储在局部变量中的想法,以及编写带参数的过程的想法。您可以在 programming guide and look up the relevant primitives in the dictionnary.