为什么 netlogo 对邻居的海龟计数不同?
Why does netlogo count turtles on neighbours differently?
当我 运行 以下两行时,我得到了不同的答案。有人知道为什么吗?第一个给出了我想要的答案:
ask turtles[
let tempcol [color] of self
show count (turtles-on neighbors4) with [color = tempcol]]
ask turtles[
set nextcolor [color] of self
let tempcol [color] of self
show count (turtles-on neighbors4) with [color = [color] of self]]
你是对的,问题在于使用 self
- 来自 dictionary entry 的原语:
"self" is simple; it means "me". "myself" means "the agent who asked
me to do what I'm doing right now.
简而言之,你想要myself
第二个例子。目前,你的第二个例子是说,"turtles, show the count of neighbor turtles whose color is the color of themselves" 你真正想说的是 "turtles, show the count of neighbor turtles whose color is the color of myself." 对于一个可能更清晰的例子,检查这个设置:
to setup
ca
crt 10 [
set color red
setxy random-xcor random-ycor
]
ask n-of 3 turtles [
set color blue
]
reset-ticks
end
这将创建 7 只红海龟和 3 只蓝海龟。现在,如果您要求其中一只蓝海龟显示与自己颜色相同的海龟的数量,我们应该期望它 return 值为 3。如果您 运行 该代码使用 self
,然而,值 returned 是 10- 因为 all 海龟的颜色等于它们自己的颜色:
to self-compare
ask one-of turtles with [ color = blue ] [
print "'[color] of self' example:"
show count turtles with [ color = [color] of self ]
]
end
如果您 运行 使用完全相同的代码但使用 myself
,它 return 就是我们期望的答案:
to myself-compare
ask one-of turtles with [ color = blue ] [
print "'[color] of myself' example:"
show count turtles with [ color = [color] of myself ]
]
end
我还要指出,几乎所有 of self
语句都是多余的——您应该能够将它们全部删除(除了 [color = [color] of self]]
,您将更改为 myself
语句)并让你的代码 运行 和以前一样。
当我 运行 以下两行时,我得到了不同的答案。有人知道为什么吗?第一个给出了我想要的答案:
ask turtles[
let tempcol [color] of self
show count (turtles-on neighbors4) with [color = tempcol]]
ask turtles[
set nextcolor [color] of self
let tempcol [color] of self
show count (turtles-on neighbors4) with [color = [color] of self]]
你是对的,问题在于使用 self
- 来自 dictionary entry 的原语:
"self" is simple; it means "me". "myself" means "the agent who asked me to do what I'm doing right now.
简而言之,你想要myself
第二个例子。目前,你的第二个例子是说,"turtles, show the count of neighbor turtles whose color is the color of themselves" 你真正想说的是 "turtles, show the count of neighbor turtles whose color is the color of myself." 对于一个可能更清晰的例子,检查这个设置:
to setup
ca
crt 10 [
set color red
setxy random-xcor random-ycor
]
ask n-of 3 turtles [
set color blue
]
reset-ticks
end
这将创建 7 只红海龟和 3 只蓝海龟。现在,如果您要求其中一只蓝海龟显示与自己颜色相同的海龟的数量,我们应该期望它 return 值为 3。如果您 运行 该代码使用 self
,然而,值 returned 是 10- 因为 all 海龟的颜色等于它们自己的颜色:
to self-compare
ask one-of turtles with [ color = blue ] [
print "'[color] of self' example:"
show count turtles with [ color = [color] of self ]
]
end
如果您 运行 使用完全相同的代码但使用 myself
,它 return 就是我们期望的答案:
to myself-compare
ask one-of turtles with [ color = blue ] [
print "'[color] of myself' example:"
show count turtles with [ color = [color] of myself ]
]
end
我还要指出,几乎所有 of self
语句都是多余的——您应该能够将它们全部删除(除了 [color = [color] of self]]
,您将更改为 myself
语句)并让你的代码 运行 和以前一样。