使用 netlogo 获取全局变量的颜色
Get color of global variables with netlogo
我正在尝试获取全局变量的真实颜色。
这是我的代码:
breed [players player]
globals [
INITIAL-POSSESSION ;
]
to setup
clear-all
reset-ticks
set-initial-possession
end
to go
ticks
ask players [
decision
]
end
to-report initial-possession
report random 2
end
to set-initial-possession
ifelse initial-possession = 1
[
set INITIAL-POSSESSION black]
[
set INITIAL-POSSESSION white]
end
to decision
if ([color] of INITIAL-POSSESSION) = black
[] ;;do something
if ([color] of INITIAL-POSSESSION) = white
[];;do something
end
但是我得到这个错误:
OF expected input to be a turtle agentset or link agentset or turtle
or link but got the number 0 instead.
所以我将其更改为(并且有效):
to decision
if INITIAL-POSSESSION = 0
[]
if INITIAL-POSSESSION = 9.9
[]
end
但是还有其他方法吗? (我使用的是 netlogo 6.0)
我认为可能缺少一些代码,所以我无法确认,但看起来您可能没有将 BALL-OWNER
设置为海龟或补丁,而是直接为该变量赋值. of
从代理中查询变量(或从代理集中查询变量列表),因此如果 BALL-OWNER
设置为一个值,NetLogo 会感到困惑。但是,如果您确实将代理分配给 BALL-OWNER
,您的方法应该可以正常工作。例如,尝试 运行 下面的代码:
to setup
ca
crt 10 [
setxy random-xcor random-ycor
set color one-of [ red blue ]
]
reset-ticks
end
to go
let ball-owner one-of turtles
ifelse [color] of ball-owner = red [
print "red team has possession"
] [
print "blue team has possession"
]
end
编辑: 你绝对可以使用 global
来选择颜色,就像你在第二个代码块中所做的那样 - 我只是想指出 of
专门绑定到 agents
。如果你想把一个颜色存储在一个global
变量中用于比较,那是可以的,只是你的比较比使用of
:
更简单
globals [ initial-possession ]
to setup
ca
crt 3
set-initial-possession
reset-ticks
end
to go
ask turtles [
decision
]
end
to set-initial-possession
set initial-possession ifelse-value ( random 2 = 1 ) [black] [white]
end
to decision
ifelse initial-possession = black [
print "I see that black has possession"
] [
print "I see that white has possession"
]
end
我不确定这是否有帮助,这可能首先取决于您在 global
中存储颜色的目的!
我正在尝试获取全局变量的真实颜色。
这是我的代码:
breed [players player]
globals [
INITIAL-POSSESSION ;
]
to setup
clear-all
reset-ticks
set-initial-possession
end
to go
ticks
ask players [
decision
]
end
to-report initial-possession
report random 2
end
to set-initial-possession
ifelse initial-possession = 1
[
set INITIAL-POSSESSION black]
[
set INITIAL-POSSESSION white]
end
to decision
if ([color] of INITIAL-POSSESSION) = black
[] ;;do something
if ([color] of INITIAL-POSSESSION) = white
[];;do something
end
但是我得到这个错误:
OF expected input to be a turtle agentset or link agentset or turtle or link but got the number 0 instead.
所以我将其更改为(并且有效):
to decision
if INITIAL-POSSESSION = 0
[]
if INITIAL-POSSESSION = 9.9
[]
end
但是还有其他方法吗? (我使用的是 netlogo 6.0)
我认为可能缺少一些代码,所以我无法确认,但看起来您可能没有将 BALL-OWNER
设置为海龟或补丁,而是直接为该变量赋值. of
从代理中查询变量(或从代理集中查询变量列表),因此如果 BALL-OWNER
设置为一个值,NetLogo 会感到困惑。但是,如果您确实将代理分配给 BALL-OWNER
,您的方法应该可以正常工作。例如,尝试 运行 下面的代码:
to setup
ca
crt 10 [
setxy random-xcor random-ycor
set color one-of [ red blue ]
]
reset-ticks
end
to go
let ball-owner one-of turtles
ifelse [color] of ball-owner = red [
print "red team has possession"
] [
print "blue team has possession"
]
end
编辑: 你绝对可以使用 global
来选择颜色,就像你在第二个代码块中所做的那样 - 我只是想指出 of
专门绑定到 agents
。如果你想把一个颜色存储在一个global
变量中用于比较,那是可以的,只是你的比较比使用of
:
globals [ initial-possession ]
to setup
ca
crt 3
set-initial-possession
reset-ticks
end
to go
ask turtles [
decision
]
end
to set-initial-possession
set initial-possession ifelse-value ( random 2 = 1 ) [black] [white]
end
to decision
ifelse initial-possession = black [
print "I see that black has possession"
] [
print "I see that white has possession"
]
end
我不确定这是否有帮助,这可能首先取决于您在 global
中存储颜色的目的!