NetLogo技术问询:统计海龟改造的受保护地块数量方法
NetLogo海龟感知改造地块及统计受保护地块改造数方案
一、优化地块感知与改造的便捷写法
你的现有代码重复筛选了两次相同地块,既冗余又影响运行效率。可以用let提前存储目标地块集合,简化逻辑:
ask u-pequeños [ ; 先筛选出半径20内符合条件的地块,存到变量里复用 let target-patches patches in-radius 20 with [pcolor = yellow and IUA > 5] if any? target-patches [ ask target-patches [ set pcolor red ; 改造地块 ] ] ]
这种写法既减少了重复计算,也让逻辑更清晰。如果需要感知的条件更复杂,直接修改with里的判断式即可,比如加入protected相关的筛选(如果需要针对受保护地块单独处理)。
二、统计改造的受保护地块数量
1. 全局统计总改造数
如果需要统计所有农民改造的受保护地块总数,先在代码开头的globals里定义统计变量:
globals [ transformed-protected ] ; 存储改造的受保护地块总数
然后在setup过程里初始化:
to setup ; 其他初始化代码... set transformed-protected 0 end
之后在农民改造地块的代码里,先筛选出目标地块中的受保护地块,再累加数量:
ask u-pequeños [ let target-patches patches in-radius 20 with [pcolor = yellow and IUA > 5] if any? target-patches [ ; 筛选出目标地块里的受保护地块 let protected-transformed target-patches with [protected] ; 累加总数到全局变量 set transformed-protected transformed-protected + count protected-transformed ; 执行改造 ask target-patches [ set pcolor red ] ] ]
2. 单只农民统计自身改造数
如果需要统计每只农民自己改造的受保护地块数,先给农民海龟添加变量:
turtles-own [ my-transformed-protected ] ; 单只农民的改造数
在创建农民时初始化:
create-u-pequeños 10 [ ; 其他属性设置... set my-transformed-protected 0 ]
然后改造时累加自身的统计数:
ask u-pequeños [ let target-patches patches in-radius 20 with [pcolor = yellow and IUA > 5] if any? target-patches [ let protected-transformed target-patches with [protected] set my-transformed-protected my-transformed-protected + count protected-transformed ask target-patches [ set pcolor red ] ] ]
之后可以通过ask u-pequeños [ show my-transformed-protected ]查看每只农民的改造数据。
内容的提问来源于stack exchange,提问作者bksnlow
相关产品推荐
相关产品推荐

