Netlogo跨breed变量引用:将花朵毒性叠加到蜜蜂死亡率实现方案
报错原因
- 你在蜜蜂的执行上下文里直接调用了仅属于花朵品类的
toxicity变量,NetLogo无法在蜜蜂的属性列表中找到该变量,因此抛出错误 - 原有
intoxicarse过程存在嵌套ask bees的逻辑错误:你已经通过外层ask bees触发该过程,过程内部又重复查询所有蜜蜂,会导致逻辑混乱
修复后可直接运行的完整代码
breed [flowers flower] breed [bees bee] turtles-own [ mortality tipo_exposicion t_supervivencia ] flowers-own [toxicity] to setup clear-all create-flowers 5 [ set shape "flower" set color red set size 4 move-to one-of patches ] ask patches [set pcolor 63] create-bees 100 [ setxy random-xcor random-ycor set shape "person" set size 1 set color yellow set t_supervivencia 0 set mortality 0 ; 新增初始化死亡率属性 ] ask flowers [set color gray] reset-ticks end to go ask bees [ move-bees ] ask bees [intoxicarse] ask flowers [morir_bees_eating] tick end to move-bees right random 60 forward 1 set t_supervivencia t_supervivencia + 1 set mortality mortality + 1 end to intoxicarse ; 检索当前蜜蜂周围10半径内的灰色有毒花 let toxic-flowers flowers in-radius 10 with [color = gray] if any? toxic-flowers [ ; 提取所有有毒花的毒性值求和后叠加到自身死亡率 set mortality mortality + sum [toxicity] of toxic-flowers ] end to morir_bees_eating if color = gray [set toxicity 34] end
关键修改说明
- 蜜蜂创建时新增
mortality属性初始化,避免运行时未赋值报错 - 重写中毒逻辑,通过
of关键字提取花朵的toxicity属性后再叠加到蜜蜂的mortality,解决跨品类调用变量的错误 - 移除重复的嵌套
ask bees逻辑,避免计算结果异常
内容的提问来源于stack exchange,提问作者bksnlow
相关产品推荐
相关产品推荐

