基于Yadov2021模型修改:资源使用量可视化的色块扩展需求
解决方案:基于现有模型实现能源使用量增长的色块扩展效果
核心思路:无需重构原有消费者选择逻辑,仅通过修改patch变量的更新规则和颜色渲染逻辑,就能把"资源消耗减少"的逻辑转为"使用量增长扩展"的效果。
1. 先定义能源类型与颜色映射
先在全局变量里维护能源和对应色块的映射,方便后续修改和维护:
globals [ energy-color-map ; 键为能源类型字符串,值为对应显示颜色 ] to setup ; 初始化能源-颜色映射,可根据需求新增/修改 set energy-color-map (list (list "natural-gas" yellow) (list "coal" black) (list "nuclear" blue) ) ; 初始化patch的resource-area:给每种能源设置核心区域(比如标记为"core-xxx"),其余为"unused" ask patches with [pxcor = 0 and pycor = 0] [ set resource-area "core-natural-gas" ] ask patches with [pxcor = 10 and pycor = 0] [ set resource-area "core-coal" ] ask patches with [pxcor = -10 and pycor = 0] [ set resource-area "core-nuclear" ] ask other patches [ set resource-area "unused" ] ; 保留原有setup的其他逻辑... end
2. 修改resource-area的更新规则(从消耗转扩展)
把蚂蚁模型中"资源减少"的逻辑,改成"消费者选择能源后,向外扩展对应能源的色块范围":
; 消费者选择能源后触发的扩展逻辑 to expand-energy-area [chosen-energy] ; 找到对应能源的核心区域或已扩展区域 let target-patches patches with [resource-area = (word "core-" chosen-energy) or resource-area = chosen-energy] if not empty? target-patches [ ; 从已有区域向外扩展1格,只覆盖未被占用的patch ask patches in-radius 1 around one-of target-patches [ if resource-area = "unused" [ set resource-area chosen-energy ] ] ] end
3. 替换patch颜色渲染逻辑
把原来"资源耗尽变黑"的渲染逻辑,改成根据resource-area的值显示对应能源色块:
to update-patch-colors ask patches [ ; 默认底色为白色 let display-color white ; 匹配当前patch的能源类型,设置对应颜色 foreach energy-color-map [ if (resource-area = first ?) or (resource-area = (word "core-" first ?)) [ set display-color last ? ] ] set pcolor display-color ] end
4. 适配原有消费者选择逻辑
不需要改动消费者选择的核心逻辑(比如偏好、决策算法),只需要把"选择门店"的动作替换为"选择能源并触发扩展":
; 原有消费者选择门店的过程,修改为选择能源 to consumer-choose-energy ; 保留你原有Yadov 2021模型中的选择逻辑(比如基于价格、偏好),这里用示例逻辑替代 let chosen-energy one-of ["natural-gas" "coal" "nuclear"] ; 触发能源区域扩展 expand-energy-area chosen-energy end
5. 整合到go循环
在原有模型的go过程中,加入颜色更新的调用即可:
to go ; 原有模型的消费者行为逻辑... ask consumers [ consumer-choose-energy ] ; 每步更新色块显示 update-patch-colors ; 原有模型的其他循环逻辑... end
可选优化:基于使用量的色块深浅变化
如果需要体现使用量多少而非单纯区域扩展,可以把resource-area改为存储[能源类型 使用量]的列表,颜色随使用量深浅变化:
; 初始化时改为列表 ask patches with [pxcor = 0 and pycor = 0] [ set resource-area (list "natural-gas" 0) ] ; 扩展时累加使用量 to expand-energy-area [chosen-energy] let target-patch one-of patches with [first resource-area = chosen-energy] if target-patch != nobody [ ask target-patch [ set resource-area (list chosen-energy (last resource-area + 1)) ; 同时向外扩展新patch ask patches in-radius 1 around self [ if resource-area = (list "unused" 0) [ set resource-area (list chosen-energy 1) ] ] ] ] end ; 颜色渲染改为随使用量缩放 to update-patch-colors ask patches [ let energy-type first resource-area let usage last resource-area let display-color white foreach energy-color-map [ if first ? = energy-type [ ; 根据使用量缩放颜色深浅,max-usage可自定义 set display-color scale-color last ? usage 0 50 ] ] set pcolor display-color ] end
内容的提问来源于stack exchange,提问作者Jaco
相关产品推荐
相关产品推荐

