Netlogo、Neighbors4 和按代理颜色选择
Netlogo, Neighbors4 and selecting by agent-color
我是 NetLogo 的新手,所以如果我的问题读起来像个新手...这就是原因。
我正在使用 neigbhors4 命令来识别攻击者代理的四个邻居。然后我想 select 根据他们的颜色和优先级排名(黑色,棕色和白色)从四个邻居中选择。如果邻居的颜色是黑色(优先级#1),则下一组指令将应用于该代理。如果 none 个邻居是黑色,则优先级排名中的下一个颜色(棕色)将收到指令。
是否最好使用某种类型的列表来实现?
以下答案强调新手的简单性。所以它只处理提出的非常具体的问题。
to-report choose-neighbor
let _candidates neighbors4 with [pcolor = black]
if any? _candidates [report one-of _candidates]
set _candidates neighbors4 with [pcolor = brown]
if any? _candidates [report one-of _candidates]
set _candidates neighbors4 with [pcolor = white]
if any? _candidates [report one-of _candidates]
report nobody
end
您会注意到这段代码有很多重复。如果将这种重复捆绑到一个子程序中可能是个好主意,例如
to-report choose-nbr [#color]
let _candidates neighbors4 with [pcolor = #color]
report ifelse-value (any? _candidates) [one-of _candidates] [nobody]
end
我是 NetLogo 的新手,所以如果我的问题读起来像个新手...这就是原因。
我正在使用 neigbhors4 命令来识别攻击者代理的四个邻居。然后我想 select 根据他们的颜色和优先级排名(黑色,棕色和白色)从四个邻居中选择。如果邻居的颜色是黑色(优先级#1),则下一组指令将应用于该代理。如果 none 个邻居是黑色,则优先级排名中的下一个颜色(棕色)将收到指令。
是否最好使用某种类型的列表来实现?
以下答案强调新手的简单性。所以它只处理提出的非常具体的问题。
to-report choose-neighbor
let _candidates neighbors4 with [pcolor = black]
if any? _candidates [report one-of _candidates]
set _candidates neighbors4 with [pcolor = brown]
if any? _candidates [report one-of _candidates]
set _candidates neighbors4 with [pcolor = white]
if any? _candidates [report one-of _candidates]
report nobody
end
您会注意到这段代码有很多重复。如果将这种重复捆绑到一个子程序中可能是个好主意,例如
to-report choose-nbr [#color]
let _candidates neighbors4 with [pcolor = #color]
report ifelse-value (any? _candidates) [one-of _candidates] [nobody]
end