如何在NetLogo中为新冠疫情ABM添加真实地理位置与区域数据?
NetLogo新冠ABM模型地理适配修改方案
一、核心需求落地方向
- 导入目标州地图作为模型运行的地理边界
- 按该州各地区实际人口数分配模拟居民(turtle)
- 按各地区初始阳性病例数设置初始感染者
- 保留原有感染、康复、免疫衰减逻辑,适配地理约束
二、分步修改实现
1. 导入地图并设置地理边界
NetLogo支持导入图片作为地图背景,需先将地图处理为栅格格式(比如用GIS导出为黑白图,黑色标记不可通行区域):
- 点击NetLogo菜单栏
File > Import > Import Drawing选择地图图片,调整min-pxcor/max-pxcor/min-pycor/max-pycor参数匹配地图尺寸 - 添加地图初始化代码,标记不可通行区域:
to setup-map ;; 假设地图中黑色(rgb 0 0 0)为不可通行区域,标记为棕色区分 ask patches with [pcolor = black] [ set pcolor brown ] end
2. 按地区分配人口与初始感染者
先整理各地区的地理边界(转成NetLogo坐标)、人口数、初始感染数为列表,在setup阶段按规则生成turtle:
;; 新增turtle变量记录所属地区,全局变量统计各类人群数量 globals [healthy-count infected-count recovered-count] turtles-own [illness region] to setup clear-all reset-ticks setup-map ;; 先加载地图 ;; 地区人口数据格式:[地区名 人口数 x最小值 x最大值 y最小值 y最大值] let region-data [ ["A区" 50 -20 -10 10 20] ["B区" 80 0 15 -5 15] ["C区" 70 -15 5 -20 -5] ] foreach region-data [ let region-info ? let region-name item 0 region-info let pop item 1 region-info let x-min item 2 region-info let x-max item 3 region-info let y-min item 4 region-info let y-max item 5 region-info create-turtles pop [ ;; 仅在当前地区的可通行patch上生成居民 let target-patch one-of patches with [pxcor > x-min and pxcor < x-max and pycor > y-min and pycor < y-max and pcolor != brown] setxy [pxcor] of target-patch [pycor] of target-patch set shape "person" set color pink set size 1.0 set illness 0 set region region-name ] ] ;; 按地区设置初始感染者 let initial-infections [ ["A区" 3] ["B区" 5] ["C区" 2] ] foreach initial-infections [ let inf-info ? let region-name item 0 inf-info let inf-count item 1 inf-info ask n-of inf-count turtles with [region = region-name and color = pink] [ set color yellow set shape "X" set size 1.0 ] ] update-stats ;; 初始化统计数据 end
3. 优化移动逻辑(适配地理约束)
修改原有随机移动代码,避免turtle穿过不可通行区域:
to move ask turtles [ rt random 360 let target-patch patch-ahead movement ;; 目标patch可通行则移动,否则尝试转向 if target-patch != nobody and [pcolor] of target-patch != brown [ fd movement ] else [ repeat 10 [ rt random 90 set target-patch patch-ahead movement if target-patch != nobody and [pcolor] of target-patch != brown [ fd movement stop ] ] ] ] end
4. 实时统计人群数据
添加统计过程,可在NetLogo界面添加监控器绑定对应全局变量:
to update-stats set healthy-count count turtles with [color = pink] set infected-count count turtles with [color = yellow] set recovered-count count turtles with [color = white] end
记得在go过程末尾调用update-stats,实现实时统计。
三、完整修改后代码
globals [healthy-count infected-count recovered-count] turtles-own [illness region] to setup clear-all reset-ticks setup-map ;; 地区人口数据:[地区名 人口数 x最小值 x最大值 y最小值 y最大值] let region-data [ ["A区" 50 -20 -10 10 20] ["B区" 80 0 15 -5 15] ["C区" 70 -15 5 -20 -5] ] foreach region-data [ let region-info ? let region-name item 0 region-info let pop item 1 region-info let x-min item 2 region-info let x-max item 3 region-info let y-min item 4 region-info let y-max item 5 region-info create-turtles pop [ let target-patch one-of patches with [pxcor > x-min and pxcor < x-max and pycor > y-min and pycor < y-max and pcolor != brown] setxy [pxcor] of target-patch [pycor] of target-patch set shape "person" set color pink set size 1.0 set illness 0 set region region-name ] ] ;; 各地区初始感染数 let initial-infections [ ["A区" 3] ["B区" 5] ["C区" 2] ] foreach initial-infections [ let inf-info ? let region-name item 0 inf-info let inf-count item 1 inf-info ask n-of inf-count turtles with [region = region-name and color = pink] [ set color yellow set shape "X" set size 1.0 ] ] update-stats end to setup-map ;; 导入地图后,标记不可通行区域(示例为黑色patch) ask patches with [pcolor = black] [ set pcolor brown ] end to go tick move infect recover lose-immunity update-stats end to move ask turtles [ rt random 360 let target-patch patch-ahead movement if target-patch != nobody and [pcolor] of target-patch != brown [ fd movement ] else [ repeat 10 [ rt random 90 set target-patch patch-ahead movement if target-patch != nobody and [pcolor] of target-patch != brown [ fd movement stop ] ] ] ] end to infect ask turtles [ if color = yellow [ ask turtles in-radius infect-distance [ if random-float 200 < infect-chance [ if color = pink [ set color yellow set shape "X" ] ] ] ] ] end to recover ask turtles [ if color = yellow [ set illness illness + 1 if illness > infectious-period [ set color white set shape "circle" set size 1.0 set illness 0 ] ] ] end to lose-immunity ask turtles [ if color = white and waning-immunity < random-float 100 [ set color pink set shape "person" ] ] end to update-stats set healthy-count count turtles with [color = pink] set infected-count count turtles with [color = yellow] set recovered-count count turtles with [color = white] end
内容的提问来源于stack exchange,提问作者Sarah Isnan
相关产品推荐
相关产品推荐

