NetLogo Link切割与重连过程COUNT/ANY?错误排查求助
NetLogo链接切割与重连错误修复方案
问题描述
在NetLogo模型的链接切割与重连逻辑中,出现两类核心错误:
- 执行
go过程时,turtle 1运行let temporary-link-count count my-temporary-links报错:"COUNT expected input to be an agentset but got the number 0 instead" - 测试另一代码段时,turtle 0运行
if any? my-temporary-links [报错:"ANY? expected input to be an agentset but got the number 0 instead",涉事代码段:
if any? my-temporary-links [ let selected-link one-of my-temporary-links let other-turtle other [end1] of [selected-link] of selected-link ; Select the agent at the other end of the link let fikir-farki abs(Ai - get-my-link-weight other-turtle) ; External idea difference between agents
错误根源
- 变量类型不匹配:
my-temporary-links被定义为turtles-own变量,但代码中错误地将其赋值为数字(比如0)或未正确初始化为agentset。count、any?等命令仅接受agentset作为输入,传入数字会触发类型错误。 - 临时链接未正确登记:原
create-temporary-links-with过程中,注释掉了将新链接加入my-temporary-links的代码,导致该变量未被正确填充为链接集合。 - 链接操作逻辑错误:获取链接另一端海龟的语法错误,以及权重列表与链接的关联逻辑混乱(临时链接的权重不应存入全局
link-weights,该列表应为永久链接专属)。
修复方案与完整代码
修改要点
- 将
my-temporary-links定义为agentset类型,初始化时设置为no-links而非空列表。 - 修复
create-temporary-links-with过程,将新创建的临时链接添加到发起海龟的my-temporary-links集合中。 - 修正获取链接另一端海龟的语法,使用
other-end命令简化操作。 - 拆分权重存储:全局列表仅存储永久链接权重,临时链接权重改为链接自身的变量(新增
link-own定义)。 - 修正
go过程中对my-temporary-links的类型判断与操作逻辑。
修复后的完整代码
globals [ link-weight-threshold nd maximum-loop-count permanent-link-weights ; 仅存储永久链接权重 ] turtles-own [ my-permanent-links ; 存储永久链接(agentset) my-temporary-links ; 存储临时链接(agentset) agents-with-majority-external agents-with-minority-external Ai ; 将内部意见设为海龟变量,避免每次循环重新生成 ] links-own [ link-weight ; 链接自身存储权重,区分永久/临时 is-permanent? ; 标记是否为永久链接 ] to-report get-my-link-weight [other-turtle] ; 获取与指定海龟的永久链接权重 let target-link one-of (my-permanent-links with [other-end = other-turtle]) ifelse is-link? target-link [ report [link-weight] of target-link ] [ report 0 ; 无链接时返回0 ] end to create-permanent-links create-links-with n-of nd other turtles [ set is-permanent? true set link-weight random-float 0.5 + 0.5 ; 永久链接权重0.5-1 set color red ask myself [ set my-permanent-links lput self my-permanent-links ] ask other-end [ set my-permanent-links lput self my-permanent-links ] ] end to create-temporary-links-with [other-turtle] ; 确保未存在临时链接时创建 if not any? (my-temporary-links with [other-end = other-turtle]) [ create-link-with other-turtle [ set is-permanent? false set link-weight random-float 0.5 ; 临时链接权重0-0.5 set color blue set shape "temp" ] ; 将新链接加入双方的临时链接集合 set my-temporary-links lput (link-with other-turtle) my-temporary-links ask other-turtle [ set my-temporary-links lput (link-with myself) my-temporary-links ] ] end to remove-temporary-link-with [link-to-remove] if [is-permanent?] of link-to-remove = false [ ask link-to-remove [ ask end1 [ set my-temporary-links my-temporary-links with [self != myself] ] ask end2 [ set my-temporary-links my-temporary-links with [self != myself] ] die ] ] end to setup clear-all set nd 1 ; 每个海龟至少1个永久邻居 set link-weight-threshold 0.5 set maximum-loop-count 50 set permanent-link-weights [] create-turtles 3 [ setxy random-xcor random-ycor set shape "person" set my-permanent-links no-links ; 初始化为空agentset set my-temporary-links no-links set agents-with-majority-external [] set agents-with-minority-external [] set Ai random-float 2 - 1 ; 初始化内部意见 ] ask turtles [ create-permanent-links set color white ] reset-ticks end to go while [ticks < maximum-loop-count] [ ask turtles [ ; 阶段2:分配外部意见 let Bi random-float 2 - 1 ; 根据外部意见更新状态与颜色 ifelse Bi > 0 [ set agents-with-majority-external fput self agents-with-majority-external set color green ] [ set agents-with-minority-external fput self agents-with-minority-external set color orange ] ; 通过临时链接更新外部意见 let β 0.1 let y 0.3 if any? my-temporary-links [ ask my-temporary-links [ let neighbor-turtle other-end let weight [link-weight] of self ask neighbor-turtle [ set Bi Bi + y * abs(Bi - weight) + β * abs(weight - Bi) ] ] ] ; 链接切割与重连逻辑 if any? my-temporary-links [ let selected-link one-of my-temporary-links let neighbor-turtle [other-end] of selected-link let opinion-difference abs(Ai - get-my-link-weight neighbor-turtle) let threshold-value 0.2 if opinion-difference <= threshold-value [ ; 将临时链接转为永久链接 ask selected-link [ set is-permanent? true set color red set link-weight random-float 0.5 + 0.5 ] ; 更新双方的链接集合 set my-permanent-links lput selected-link my-permanent-links set my-temporary-links my-temporary-links with [self != selected-link] ask neighbor-turtle [ set my-permanent-links lput selected-link my-permanent-links set my-temporary-links my-temporary-links with [self != selected-link] ] ] ] ] tick ] end
额外说明
- 新增
links-own变量直接存储链接权重与类型,避免了全局列表与链接的关联混乱,逻辑更清晰。 - 初始化时将
my-permanent-links和my-temporary-links设为no-links(空agentset),确保count、any?命令能正常执行。 - 修正了临时链接创建时的双向登记逻辑,保证双方海龟都能正确追踪自己的临时链接。
内容的提问来源于stack exchange,提问作者Aleyna Aslan
相关产品推荐
相关产品推荐

