如何让NetLogo中的智能体固定在互不靠近的特定斑块?
NetLogo智能体(国家)分块静止且间距较远的实现方案
修改后的完整代码
turtles-own [ military-strength economic-strength ] to setup clear-all ;; 挑选20个彼此间距至少5个斑块的随机斑块(可调整5来改变间距) let target-patches [] while [length target-patches < 20] [ let candidate one-of patches if all? target-patches [distance candidate > 5] [ set target-patches lput candidate target-patches ] ] ;; 在每个选中的斑块上创建代表国家的智能体 foreach target-patches [ patch -> create-turtles 1 [ move-to patch ; 移动到斑块中心 set size 1 + random-float 6 set shape "flag" set color one-of [blue green red yellow orange pink cyan magenta brown black gray violet] set military-strength random-float 0.2 + 0.1 set economic-strength one-of [1 2 3] ; 随机选取经济实力数值 ] ] reset-ticks end to go ;; 空过程,确保智能体保持静止 end
关键改动说明
- 确保每个智能体独占斑块:通过预先筛选斑块列表,再在每个斑块上创建一个智能体,彻底避免多个智能体出现在同一斑块的情况。
- 控制智能体间距:用
distance candidate > 5判断候选斑块与已选斑块的距离,数值5可根据需求调整——数值越大,智能体间距越远。 - 强制智能体静止:
go过程留空,移除原代码中无意义的fd 0,确保智能体不会产生任何移动。 - 修正经济实力赋值:原代码直接给
economic-strength赋值列表,改为用one-of随机选取列表中的一个数值,符合“国家经济实力为单一数值”的逻辑。
内容的提问来源于stack exchange,提问作者Hager Lotfy
相关产品推荐
相关产品推荐

