NetLogo -在同一品种类型的嵌套请求中调用品种时设置变量



我想设置"parent"变量,但不包含luck。

在下面的代码中,我使用了"outer"问指令"问乌龟"在这下面有一个嵌套的"问其他海龟"。在嵌套的请求中,我试图设置调用turtle的变量。

turtles-own [
a
b
]
to setup
clear-all
set-default-shape turtles "circle"
setup-turtles
reset-ticks
end
to go
ask turtle 0 
[
set a 2
set b 3 
show (word "(a, b) = (" a ", " b ")")
ask other turtles 
[
show (word "(a, b) = (" a ", " b ")")
set a a + 1
set b b + 1
show (word "(a, b) = (" a ", " b ")")
; Below isn't allowed. Message is: 
; This isn't something you can use "set" on. 
; set ([b] of myself) 14
]
]
tick
end

set ([b] of myself) 14"指令不起作用,信息是"这不是你可以使用的东西"设置"。干净。

编辑:在下面添加了澄清:让我详细说明一下。

运行一次go过程得到:

(turtle 0): "(a, b) = (2, 3)"
(turtle 1): "(a, b) = (0, 0)"
(turtle 1): "(a, b) = (1, 1)"
(turtle 2): "(a, b) = (0, 0)"
(turtle 2): "(a, b) = (1, 1)"
(turtle 3): "(a, b) = (0, 0)"
(turtle 3): "(a, b) = (1, 1)"

然而,我希望发生的是海龟1,2和3可以写"返回";要乌龟0,这样当"走"的时候过程下次运行时,我将设置如下:

(turtle 0): "(a, b) = (2, 14)"
(turtle 3): "(a, b) = (1, 1)"
(turtle 3): "(a, b) = (2, 2)"
(turtle 1): "(a, b) = (1, 1)"
(turtle 1): "(a, b) = (2, 2)"
(turtle 2): "(a, b) = (1, 1)"
(turtle 2): "(a, b) = (2, 2)"

一只海龟可以访问另一只海龟的变量,但它不能改变另一只海龟的变量。所以你必须回到原来的海龟,让它们使用ask myself [...]

改变自己的变量
ask turtle 0 
[
ask other turtles 
[
ask myself [set b 14]
]
]

出现此错误是因为您没有使用正确的方式在NetLogo中设置变量。相反,您应该使用:

ask other turtles 
[
show (word "(a, b) = (" a ", " b ")")
set a a + 1
set b b + 1
show (word "(a, b) = (" a ", " b ")") 
set b 14
]

这是因为你想成为set ([b] of myself) 14的东西在ask other turtles []里面。当其中的代码运行时,它将应用于指定集合中的每个海龟(在您的示例中,除了海龟0之外的所有海龟),直到它们都遵循了您给它们的指令。所以在这里写of myself有点多余。

最新更新