NetLogo中如何提取agentset的who编号并添加到patch变量列表?
嵌套列表产生原因
[who] of turtles-here 本身返回的就是包含当前patch上所有海龟who编号的列表,你直接用lput操作会把这个子列表作为单个元素追加到visitantBees中,最终就会生成嵌套结构。
解决方法
方法1:使用sentence拼接列表(最简实现)
sentence是NetLogo内置的列表拼接操作符,会自动将两个列表的元素合并为一维列表,直接替换原代码中的lput逻辑即可:
to computingFrequency ask patches [ if any? turtles-here [ set visitantBees sentence visitantBees [who] of turtles-here ] ] end
方法2:遍历turtles-here逐个插入(适合需自定义判断的场景)
如果你后续需要对来访海龟做额外校验(比如去重、只记录特定品类海龟),可以逐个遍历当前格子上的海龟,再插入who编号:
to computingFrequency ask patches [ if any? turtles-here [ ask turtles-here [ set visitantBees lput (who of myself) visitantBees ] ] ] end
可选优化:避免重复记录同一只海龟
如果不需要重复记录同一只海龟的多次到访,可以增加成员判断逻辑:
ask turtles-here [ let current-who who if not member? current-who visitantBees [ set visitantBees lput current-who visitantBees ] ]
内容的提问来源于stack exchange,提问作者Raíssa Fernandes
相关产品推荐
相关产品推荐

