NetLogo执行face指令报错'期望agent却得到NOBODY'问题咨询
NetLogo FACE指令报NOBODY错误修复方案
错误根因
报错核心原因有2个,和你怀疑的!= nobody判断无关:
- 括号优先级错误导致代理集合为空:你定义
highlyRelatedPersons和relatedPersons时,没有给连接筛选逻辑加括号,导致with [trust = 0.6]作用在[other-end]返回的海龟集合上,而非连接集合上。海龟本身没有trust属性,最终筛选出的relatedPersons可能为空,one-of空集会返回nobody,传入face就触发报错。 - 兜底分支缺少非空校验:最后一层
face one-of relatedPersons没有提前判断集合是否非空,边界场景下(比如连接创建失败、单智能体场景)会直接传入nobody。
另外你的连接初始化逻辑存在冗余:无向连接不需要每个智能体都主动向其他所有智能体创建,重复调用create-connections-with会生成冗余重复连接,干扰后续筛选。
修复要点
- 调整连接筛选的括号位置,确保
trust判断作用在连接对象上,避免筛选出空集合 - 优化连接初始化逻辑,判断连接是否存在后再创建,避免重复生成无向连接
- 所有调用
one-of取代理传入face前,先通过any?校验对应代理集合非空,从根源避免取到nobody - 保留原有移动优先级逻辑:视野内高信任智能体 > 视野内非高信任邻居 > 全局有信任连接的智能体
修复后完整代码
breed [ persons a-person ] undirected-link-breed [ connections connection ] connections-own [ trust ] persons-own [ neighbours ] to setup clear-all create-persons 10 [ setxy random-xcor random-ycor ] setup-connections reset-ticks end to setup-connections ; 避免重复创建无向连接 ask persons [ ask other persons [ if not connection? myself [ create-connection-with myself [ set trust 0.6 ] ] ] ] end to go setNeighbours movePersons tick end to setNeighbours ask persons [ set neighbours other persons in-cone 4 360 ] end to movePersons ask persons [ ; 修正括号,先筛选信任值0.6的连接,再取另一端海龟,最后筛选视野范围内的 let highTrustConnections my-out-connections with [trust = 0.6] let highlyRelatedPersons turtle-set [other-end] of highTrustConnections in-cone 4 360 let relatedPersons turtle-set [other-end] of highTrustConnections ifelse any? highlyRelatedPersons [ face one-of highlyRelatedPersons ] [ let weaklyRelatedNeighbours neighbours with [not member? self highlyRelatedPersons] ifelse any? weaklyRelatedNeighbours [ face one-of weaklyRelatedNeighbours ] [ ; 兜底分支增加非空判断,避免传入nobody if any? relatedPersons [ face one-of relatedPersons ] ] ] ; 朝向确定后执行移动,原代码漏了前进步长,这里补示例值可按需调整 fd 0.1 ] end
注:原代码仅实现了朝向判断逻辑,没有添加移动前进步长,修复版中补了
fd 0.1作为示例,你可以根据业务需求调整移动速度参数。
内容的提问来源于stack exchange,提问作者kay21
相关产品推荐
相关产品推荐

