NetLogo:获取补丁集以排除乌龟内存中的补丁

时间:2017-03-28 19:49:29

标签: netlogo

我在NetLogo中建模区域选择。当乌龟构建一个区域时,它还会构建一个不需要的补丁(一个名为"黑名单"的代理集)的内存,然后需要在为该区域选择新补丁时避免这些补丁。在决定要声明的下一个补丁时,会创建一个名为" available-destinations,"的新补丁集。从哪个"最高价值"报告基于几个因素(见下文)。我希望可用目的地检查补丁是否是乌龟黑名单的一部分,并排除这些补丁。但是,在修补程序中,我无法弄清楚如何调用乌龟的黑名单。有什么建议?提前谢谢!

以下是我的主要代码:

patches-own
[
  owner  ;; turtle who claims patch for territory
  benefit  ;; i.e., food
  avoiding ;; turtle who is avoiding this patch
]

turtles-own
[
  start-patch  ;; my territory center
  destination  ;; my next patch to claim
  territory  ;; patches I own
  blacklist  ;; my agentset of patches to avoid
]

to pick-patch
    if patch-here = start-patch [ set destination highest-value ]
    if destination != nobody [ travel ]
end

to travel
     ;; there are a number of actions here, but the relevant one is:
     ;; check if owned, and avoid it:
     if patch-here != destination
          [ if owner != nobody  ;; if it's owned...
            [ if owner != self  ;; and not by me...
              [ avoid-obstacle
                move-to start-patch ]
            ]
          ]
end

to avoid-obstacle
     ask destination [ set avoiding myself ]
     set blacklist (patches with [avoiding = myself])
end

to-report highest-value            ;; <--- source of error since using "blacklist"
     let available-destinations edge-patches with [blacklist != myself]  
     report max-one-of available-destinations ([benefit-to-me / cost-to-me]) 
end

to-report benefit-to-me
     report mean [benefit] of patches in-radius 2 
end

to-report cost-to-me
     report distance [start-patch] of myself 
end

to-report edge-patches
     report (patch-set [neighbors4] of territory) with [owner = nobody] 
end

此代码导致此错误来自最高价值的报告者:此代码无法通过补丁运行,只有乌龟 - 错误,而乌龟0运行BLACKLIST 。我该如何解决这个问题?

我的另一个想法如下:使用补丁变量&#34;避免&#34;:

to-report highest-value
     let available-destinations edge-patches with [avoiding != myself]  
     report max-one-of available-destinations ([benefit-to-me / cost-to-me]) 
end

这样运行。麻烦的是,正如目前设计的&#34;避免障碍&#34;程序,补丁知道避免作为一只乌龟,如果多只乌龟决定避开补丁,这会被覆盖。

因此,如果我要使用它而不是龟的记忆,那么避免也应该是一个补丁集。但是,我还没能确定如何以这种方式编码。我试过这个:

to avoid-obstacle
      ask destination
        [ let now-avoiding myself
          set avoiding (turtle-set avoiding now-avoiding) ]   
      set blacklist (patch-set blacklist patches with [avoiding = myself])            
end

避免确实会成为龟套。乌龟的黑名单记忆永远无法正确完成 - 但它仍然是空的。此外,即使代理集避免包含乌龟,最高价值的报告者似乎也不会排除补丁。所以,我不知所措。

结论:如果有办法,可以选择使用原始方法调用海龟的黑名单。如果我决定选择其他路线,我也很想知道我在使用&#34;避免&#34;的想法中做错了什么。谢谢!

一个快速相关的问题:如何调用代理集来显示其中的代理列表?我想这样做是为了检查代码是否按预期工作。从命令中心,&#34;显示乌龟0&#34; [黑名单]只返回&#34;(agentset,50个补丁)&#34;而不是那50个补丁的列表,这是我真正想要看到的。

1 个答案:

答案 0 :(得分:2)

您可以使用member?做您想要排除补丁的内容吗?例如:

to-report highest-value           
     let available-destinations edge-patches with [ member? self blacklist = false ]  
     report max-one-of available-destinations ([benefit-to-me / cost-to-me]) 
end

我无法在没有您的设置和所有内容的情况下对其进行测试,但member?如果请求代理(在这种情况下,不是海龟,而是潜在的可用修补程序)则报告为真)属于agentset。有关简单的工作示例,请参阅:

to setup
  ca
  ask patches with [pxcor > 0 ] [
    set pcolor white
  ]
  crt 1 
end

to go
  ask turtles [
    let blacklist patches with [ pcolor = black ]
    let northpatches patches with [ pycor > 0 ]
    let northred ( northpatches with [ member? self blacklist = false ] )
    ask northred [ set pcolor red ]
    ask northred [
      print self
    ]
  ]
end

只要显示哪些补丁可用,请查看上述程序中的乌龟如何询问其代理人中的海龟&#34; northred&#34;将自己打印到控制台。这是一个简单的方法!