如何在指定区域选取对应数量turtle修改颜色实现传染病模型初始感染设置
问题原因
现有代码中ask n-of initially_infected humans会从全局所有已创建的人类个体中随机选择,无法限定在对应区域:第一次执行时会从所有绿区人类中选对应数量的感染者,但第二次执行时会从绿区+蓝区所有人类中选,大概率选到已创建的绿区人类,既无法保证蓝区的初始感染数量,也做不到两区分别独立感染。
修正代码
to setup_agents create-humans green_population [ move-to one-of patches with [pcolor = green and not any? humans-here] set shape "green person" set color yellow set size 1 set green_antibodies 0 ] ; 仅筛选绿区的人类设置初始感染 ask n-of initially_infected humans with [ [pcolor] of patch-here = green ] [set color red] create-humans blue_population [ move-to one-of patches with [pcolor = blue and not any? humans-here] set shape "blue person" set color yellow set size 1 set blue_antibodies 0 ] ; 仅筛选蓝区的人类设置初始感染 ask n-of initially_infected humans with [ [pcolor] of patch-here = blue ] [set color red] end
优化方案(可选)
如果需要提升执行效率,可在创建对应区域人群时直接存储临时变量引用,避免每次全局筛选:
to setup_agents let green-group nobody create-humans green_population [ move-to one-of patches with [pcolor = green and not any? humans-here] set shape "green person" set color yellow set size 1 set green_antibodies 0 set green-group (turtle-set green-group self) ] ask n-of initially_infected green-group [set color red] let blue-group nobody create-humans blue_population [ move-to one-of patches with [pcolor = blue and not any? humans-here] set shape "blue person" set color yellow set size 1 set blue_antibodies 0 set blue-group (turtle-set blue-group self) ] ask n-of initially_infected blue-group [set color red] end
内容的提问来源于stack exchange,提问作者Aitezaz Ahsan
相关产品推荐
相关产品推荐

