我使用总共1000只海龟创建了一个矩形海龟网格。
let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape [ sprout 1 ]
现在我需要创建一种渐变,这将给我的龟群提供距离感。给定值为“种子”的值为0时,它会在某个talk_radius 中发出值为0的消息。
这个 talk_radius 里面的海龟计算它们与种子的距离。如果距离小于固定值(称为 gradient_distance ),则乌龟将假定为 gradient_value ,并发出一条带有其值的消息。<登记/>
其他乌龟也这样做。所以每个人都会取值x + 1,其中x是 gradient_distance 中乌龟的最低值,如picture
这是伪代码中的相对算法:
loop
if gradient seed = TRUE then // check if robot is designated as gradient source
gradient value(self) = 0
else
gradient value(self) = GRADIENT MAX
for all neighbors n do
if measured distance(n) < G then // G represents the gradient-distance and GRADIENT MAX is infinity
if gradient value(n) < gradient value(self) then
gradient value(self) = gradient value(n)
gradient value(self) = gradient value(self) + 1
transmit gradient value(self)
这是我在netlogo中的实现:
globals [talk_radius gradient_max gradient_distance]
turtles-own [gradient_seed gradient_value]
to setup
ca
resize-world -60 60 -20 20
crt 1000
let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape [ sprout 1 ]
set talk_radius 4
set gradient_max 100000
set gradient_distance 1
ask turtles
[set shape "circle"]
ask turtles-on patch -50 5
[set gradient_seed true]
end
to gradient-formation
while [true]
[
ask turtles
[
ifelse (gradient_seed = true)
[
set gradient_value 0
]
[
set gradient_value gradient_max
set color scale-color green gradient_value 0 120
ask (other turtles) in-radius talk_radius with [distance myself <= gradient_distance] ;; i consider all the turtle in talk_radius having the right gradient_distance
[
let a ([gradient_value] of self) ;; "of self" is not necessary but helped me for a better comprehension
if (a < ([gradient_value] of myself))
[
ask myself [set gradient_value a]
]
]
set gradient_value (gradient_value + 1)
]
set color scale-color green gradient_value 0 120
]
]
end
我使用了比例颜色来反馈我所做的事情,正如您在image中看到的那样。
现在问题是:我尝试 let a ([gradient_value] of self)
而不是 set a ([gradient_value] of self)
将 a 添加到turtle变量中(我添加了一个在乌龟自己的名单上面)。
我认为结果会是一样的,但是我会在image中看到每个海龟的不断增加的gradient_value (白色表示非常高 gradient_value )。
为何如此区别?
先谢谢你,对不起这个长期问题感到抱歉。
答案 0 :(得分:2)
在回应有关改进问题的讨论时非常谨慎地编辑
首先,我想从更简单的代码版本开始。我相信这与没有while[true]
的你完全相同。为了清楚起见,我删除了您正在创建的额外1000只海龟,并将ifelse
将种子分成两个单独的ask
语句。我也移动着色,直到值计算完成。
globals [talk_radius gradient_max gradient_distance]
turtles-own [gradient_seed? gradient_value]
to setup
clear-all
resize-world -60 60 -20 20
let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape
[ sprout 1
[ set shape "circle"
set gradient_seed? false
]
]
set talk_radius 4
set gradient_max 100000
set gradient_distance 1
repeat 10 [ gradient-formation ]
end
to gradient-formation
ask turtles-on patch -50 5
[ set gradient_seed? true
set gradient_value 0
]
ask turtles with [not gradient_seed?]
[ set gradient_value gradient_max
ask (other turtles) in-radius talk_radius with [distance myself <= gradient_distance]
[ let my-gradval ([gradient_value] of self)
if my-gradval < [gradient_value] of myself
[ ask myself [set gradient_value my-gradval]
]
]
set gradient_value (gradient_value + 1)
]
ask turtles [set color scale-color green gradient_value 0 120 ]
end
这里有一个概念问题。在乌龟计算出gradient_value之前,它是0.这意味着大量的海龟会在附近有一只乌龟,然后将自己的gradient_value设为1.它不会产生颜色渐变。要解决这个问题,您需要多次运行渐变形成。代码while [true]
中的方法引入了无限循环。相反,您可以repeat
任意次数(上面的代码中为10)。
let
与set
+ turtles-won
的问题在于,拥有海龟的set
会创建1000份gradient_value - 每只乌龟一份。 let
版本创建一个所有海龟都可以访问的(临时)全局变量。因此,当您使用set
时,您要为该乌龟设置它,而不是一般的访问号码。我认为正在发生的事情是,set gradient_value my-gradval
行正在访问错误的龟的my-gradval副本。
但是,从讨论中,导致问题的代码的目的是找到局部最小值。有更直接的方法可以做到这一点。
to gradient-formation
ask turtles-on patch -50 5
[ set gradient_seed? true
set gradient_value 0
]
ask turtles with [not gradient_seed?]
[ set gradient_value 1 + min [gradient_value] of
other turtles in-radius min (list talk_radius gradient_distance)
]
ask turtles [set color scale-color green gradient_value 0 120 ]
end
添加最低工作示例以显示差异。
这是let
(全局变量)版本
turtles-own [testval]
to testme
clear-all
create-turtles 500
[ setxy random-xcor random-ycor
set color blue
set testval 1 + random 10
]
ask one-of turtles
[ set color red
inspect self
type "testval of asking turtle is " print testval
ask turtles-on neighbors
[ set color yellow
let my-testval [testval] of self ;; creates a temp global variable
type "my-testval is " print my-testval
if my-testval < [testval] of myself
[ ask myself
[ set testval my-testval ;; copies the global variable value
]
]
]
]
end
这是set
(海龟属性)版本
turtles-own [testval my-testval]
to testme
clear-all
create-turtles 500
[ setxy random-xcor random-ycor
set color blue
set testval 1 + random 10
]
ask one-of turtles
[ set color red
inspect self
type "testval of asking turtle is " print testval
ask turtles-on neighbors
[ set color yellow
set my-testval [testval] of self
type "my-testval is " print my-testval
if my-testval < [testval] of myself
[ ask myself
[ set testval my-testval ;; copies value from one attribute to other
]
]
]
]
end