让海龟面对总价值最高的补丁

时间:2019-07-05 04:15:26

标签: netlogo

我正在NetLogo上研究动物运动模型。我创建了六个不同的代理集,其中包含位于圆锥体16 60中的补丁(在计算下一个代理集之前,请我的海龟将rt设为60)。然后,我要求每个代理集计算加权索引的总和(0-1之间的补丁变量)并将其值保存到全局变量中。因此,现在我给每只乌龟都指定了六个区域,并提供了一个值,该值代表乌龟整个代理集的资源值。我试图让我的海龟识别出哪个加权加权指数总和最高的agentset,然后移到其中的随机补丁。我遇到了问题,因为代表加权总和索引的全局变量未附加到代理集的位置,因此我不知道如何继续进行此步骤。我唯一的想法是制作一条复杂的ifelse链,在该链中,我让海龟比较总和并面对与该值相对应的agentset,但这似乎很长。任何有关如何解决此问题或使我的问题更清晰的想法或建议,将不胜感激!

我尝试制作一个列表并要求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

1 个答案:

答案 0 :(得分:1)

首先,不需要使用全局变量,只需使用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),然后将这些和以相同顺序放入新列表中。然后,它搜索该新列表的最大值,找到该最大值的位置,使用该位置从第一个列表中提取正确的代理集,然后面对该代理集内的随机补丁。

相关问题