NetLogo蝴蝶授粉模拟:农药与授粉能力负关联实现问题
NetLogo蝴蝶农药影响模拟问题修复
问题现状
我是一名小学教育与学前教育教师,正在用NetLogo开发模拟系统,展示化肥与农药对蝴蝶种群的影响——核心是呈现农药剂量越高,蝴蝶授粉能力越低、种群受影响越严重的逻辑。目前遇到的异常是:设定蝴蝶初始授粉能力为100%,但高农药剂量下,系统计算的授粉能力反而升高,完全违背预期。
现有代码(问题版本)
breed [butterflies butterfly] breed [flowers flower] globals [ butterfliesless-neighborhoods ;; 无蝴蝶邻域的斑块数量 pollinating-capacity ;; 原错误的授粉能力计算指标 ] patches-own [ butterflies-nearby ;; 邻域内的蝴蝶数量 ] flowers-own [ carried-butterflies ;; 花朵携带的蝴蝶 found-bivouac? ;; 是否找到投放蝴蝶的栖息地 ] to setup clear-all set-default-shape butterflies "butterflies" set-default-shape flowers "flower" ask patches [ set pcolor green + (random-float 0.8) - 0.4] ;; 渲染绿色草地 create-butterflies num-butterflies [ set color white set size 1.5 ;; 放大蝴蝶便于观察 setxy random-xcor random-ycor ] create-flowers num-flowers [ set color brown set size 1.5 ;; 放大花朵便于观察 set carried-butterflies nobody set found-bivouac? false setxy random-xcor random-ycor ] reset-ticks end to update-butterflies-counts ask patches [ set butterflies-nearby (sum [count butterflies-here] of neighbors) ] set butterfliesless-neighborhoods (count patches with [butterflies-nearby = 0]) end to calculate-pollinating-capacity set pollinating-capacity (butterfliesless-neighborhoods / (count patches with [not any? butterflies-here])) * 100 end to go ask flowers [ ifelse carried-butterflies = nobody [ search-for-butterflies ] ;; 寻找并携带蝴蝶 [ ifelse found-bivouac? [ find-empty-spot ] ;; 寻找空位置投放蝴蝶 [ find-new-bivouac ] ] ;; 寻找栖息地投放蝴蝶 wiggle fd 1 if carried-butterflies != nobody ;; 携带蝴蝶移动 [ ask carried-butterflies [ move-to myself ] ] ] ask butterflies with [not hidden?] [ wiggle fd pesticide-amount ] tick end to wiggle ;; 随机转向函数 rt random 50 - random 50 end to search-for-butterflies ;; 花朵寻找蝴蝶 set carried-butterflies one-of butterflies-here with [not hidden?] if (carried-butterflies != nobody) [ ask carried-butterflies [ hide-turtle ] ;; 隐藏蝴蝶避免被其他花朵重复携带 set color blue ;; 携带蝴蝶时花朵变蓝 fd 1 ] end to find-new-bivouac ;; 花朵寻找栖息地 if any? butterflies-here with [not hidden?] [ set found-bivouac? true ] end to find-empty-spot ;; 花朵寻找空位置投放蝴蝶 if all? butterflies-here [hidden?] [ ask carried-butterflies [ show-turtle ] ;; 重新显示蝴蝶 set color brown ;; 花朵恢复原色 set carried-butterflies nobody set found-bivouac? false rt random 360 fd 20 ] end
核心问题分析
- 授粉能力计算逻辑完全错误:原代码中
pollinating-capacity基于斑块的蝴蝶分布计算,和蝴蝶实际授粉能力、农药剂量没有任何关联,这是高剂量农药下数值异常的根本原因。 - 缺失农药与授粉能力的关联属性:没有给花朵设置农药剂量属性,也没有给蝴蝶设置个体授粉能力属性,无法实现“接触农药花朵→授粉能力下降”的逻辑。
- 蝴蝶移动逻辑不合理:直接用
pesticide-amount作为蝴蝶移动距离,混淆了“农药剂量”和“移动行为”的概念。
修正后的代码
breed [butterflies butterfly] breed [flowers flower] globals [ pollinating-capacity ;; 整体蝴蝶授粉能力平均值 ] butterflies-own [ pollination-ability ;; 单只蝴蝶的授粉能力,初始100% ] flowers-own [ pesticide-dose ;; 花朵携带的农药剂量(0=无农药) ] to setup clear-all set-default-shape butterflies "butterflies" set-default-shape flowers "flower" ask patches [ set pcolor green + (random-float 0.8) - 0.4 ] ;; 创建蝴蝶,初始授粉能力100% create-butterflies num-butterflies [ set color white set size 1.5 set pollination-ability 100 setxy random-xcor random-ycor ] ;; 创建花朵,分配农药剂量(需提前创建slider变量pesticide-max控制最大剂量) create-flowers num-flowers [ set color brown + (pesticide-dose / 100) ;; 颜色随农药剂量加深,直观展示 set size 1.5 set pesticide-dose random pesticide-max setxy random-xcor random-ycor ] calculate-pollinating-capacity reset-ticks end to calculate-pollinating-capacity ;; 计算所有蝴蝶授粉能力的平均值,反映整体授粉水平 if any? butterflies [ set pollinating-capacity mean [pollination-ability] of butterflies ] if no? butterflies [ set pollinating-capacity 0 ] end to go ;; 蝴蝶移动并与花朵交互 ask butterflies [ wiggle fd 1 ;; 接触花朵时,受农药影响降低授粉能力 if any? flowers-here [ let dose [pesticide-dose] of one-of flowers-here ;; 授粉能力下降:每次接触减少剂量*0.5%(系数可调整),最低为0 set pollination-ability max (list 0 (pollination-ability - (dose * 0.5))) ] ] ;; 花朵随机移动(保留原逻辑) ask flowers [ wiggle fd 0.5 ] calculate-pollinating-capacity tick end to wiggle rt random 50 - random 50 end
关键改动说明
- 添加核心属性:给蝴蝶加
pollination-ability(初始100),给花朵加pesticide-dose(通过滑块pesticide-max控制剂量范围) - 修正授粉能力计算:改为所有蝴蝶授粉能力的平均值,直接反映农药对整体授粉水平的影响
- 实现农药影响逻辑:蝴蝶接触带农药的花朵时,授粉能力随剂量增加而降低(最低限为0,避免负数)
- 简化无关逻辑:移除原代码中与授粉能力无关的斑块计数、花朵搬运蝴蝶的逻辑(若需保留可自行调整)
- 直观可视化:花朵颜色随农药剂量加深,方便学生观察农药分布
内容的提问来源于stack exchange,提问作者Carmen María Sevilla Izquierdo
相关产品推荐
相关产品推荐

