netlogo - 让海龟面对总和值最高的一组补丁
问题描述
我正在 NetLogo 研究动物运动模型。我创建了六个不同的代理集,其中包含锥内 16 60 的补丁(要求我的海龟在计算下一个代理集之前将 rt 转为 60)。然后我要求每个代理集计算加权指数的总和(0-1 之间的补丁变量)并将值保存到全局变量中。所以现在我给了每个海龟周围的六个区域,以及一个代表整个代理集对海龟的资源价值的值。我试图让我的海龟识别哪个代理集的加权指数总和最高,并移动到其中的随机补丁。我遇到了问题,因为代表总加权指数的全局变量未附加到代理集的位置,所以我不知道如何从这一步继续前进。我唯一的想法是做一个复杂的 ifelse 链,我让海龟比较总和值并面对与该值对应的代理集,但这似乎很长。非常感谢任何有关如何解决此问题或使我的问题更清晰的想法或建议!
我试图列出一个列表并调用 with-max 但同样,这会将最大总和报告为一个数字,而不是它所属的代理集。
; This code gets called in my go procedure as ask bears []
;I am first creating the agentsets
set heading 0
set n-patches patches in-cone 16 60
rt 60
set ne-patches patches in-cone 16 60
rt 60
set se-patches patches in-cone 16 60
rt 60
set s-patches patches in-cone 16 60
rt 60
set sw-patches patches in-cone 16 60
rt 60
set nw-patches patches in-cone 16 60
; Now I'm adding the index value for all patches within each agentset
set n-sum sum [weighted-index] of n-patches
set ne-sum sum [weighted-index] of ne-patches
set se-sum sum [weighted-index] of se-patches
set s-sum sum [weighted-index] of s-patches
set sw-sum sum [weighted-index] of sw-patches
set nw-sum sum [weighted-index] of nw-patches
; Lost after this
解决方案
首先,不需要使用全局变量,只需使用let
创建一个临时局部变量即可。
这是一个非常棘手的问题,因为您不能使用任何在代理集中找到最大值的内置原语,而列表原语没有这种能力。
我已经编写了一个独立模型来演示您想要什么。它pcolor
用作值而不是加权指数,我选择了随着数字增加而变暗的颜色。
to testme
clear-all
ask patches [ set pcolor one-of [ 28 54 110 ] ]
create-turtles 3
[ setxy random-xcor random-ycor
set color white
]
ask turtles
[ set heading 0
let n-patches patches in-cone 16 60
rt 60
let ne-patches patches in-cone 16 60
rt 60
let se-patches patches in-cone 16 60
rt 60
let s-patches patches in-cone 16 60
rt 60
let sw-patches patches in-cone 16 60
rt 60
let nw-patches patches in-cone 16 60
let directions-list shuffle (list n-patches ne-patches se-patches s-patches sw-patches nw-patches)
print directions-list
let sums-list map [ thisDir -> sum [pcolor] of thisDir ] directions-list
print sums-list
let max-sums max sums-list
print max-sums
let chosen position max-sums sums-list
print chosen
face one-of item chosen directions-list
]
end
您可以使用to-report
过程来简化六个代理集的计算,但我没有这样做,因为我想使用您的代码来帮助保持它的可读性
这会将事情打印出来,这样您就可以看到它在做什么。请注意,shuffle
如果 2 个或更多恰好具有相同的总数,则确保随机选择。请记住,NetLogo 列表是 0 索引的,因此position
将返回 0 到 5,而不是 1 到 6。
它所做的是将代理集放入一个列表中,计算列表map
中每个项目的总和值(使用 ),并将这些总和以相同的顺序放入一个新列表中。然后它搜索该新列表的最大值,找到该最大值的位置,使用该位置从第一个列表中提取正确的代理集,然后面对该代理集中的随机补丁。
推荐阅读
- android - FAILURE:构建失败,从 git 克隆应用程序时出现异常
- r - 多列缺失值的新变量
- python - 在numpy中的两个频率之间进行插值
- python-3.x - 根据 Pandas DataFrame 中的标量条件替换值
- javascript - 组合从 json 文件中挑选出来的多个对象并作为 Observable 返回
- php - 使用 php exec() 调用该批处理文件时,批处理文件重定向到一个空文件
- http - HTTP2.0普遍使用吗?
- javascript - document.getElementById - 未捕获的 ReferenceError:$ 未定义
- laravel - 如何使用 vue 和 laravel 自动填充表单字段
- python - 从字符串中提取某些整数,然后对其进行规范化