首页 > 解决方案 > 设置新的目标补丁,但不包括 Netlogo 中的当前补丁

问题描述

我正在创建一个复制商店扒手行为的模拟。海龟分为“专业”和“新手”扒手,如果“专业”被商店保安逮捕,他们可能(1/2)想要选择一家新商店来定位“需要新商店”。

“专业”扒手瞄准特定半径内“安全”值最低的商店,所有值都是在创建时设置的。

我正在尝试设置一个新的“目标商店”,半径为 10 的“商店”,具有第二低的“安全性”,即不包括当前的“目标商店”,但我遇到了困难。

到目前为止,我已尝试对以下代码进行几处添加以排除当前的目标商店,这包括“会员?我的补丁”的变体,因为已将扒手被捕的“商店”添加到此“补丁集”这将通知稍后的命令。我还列出了一个递增的“安全”值列表,告诉“扒手”以“安全”(确定商店漏洞的值)作为目标的“商店”,与列表中的第 1 项相同,但我担心这可能不起作用,因为它们的原始目标商店可能不一定是项目 0,因为它们以 10 单位半径内“安全性”最低的商店为目标。

这些是我目前正在使用的代码行,任何建议将不胜感激。

***编辑:理想情况下,我可以喜欢使用“mypatches”的代码,因此每次在商店逮捕专业扒手时,可以将商店添加到 mypatches 中,随后的目标商店可以排除所有属于我的补丁。

to new-target-store
ask turtles [ if
new-store-required = 1 and professional = 1 and    (random-float 1 < 0.5) [  
set target-store min-one-of store in-radius 10 [security]
]
]

end

编辑2:我已经解决了问题。

标签: netlogotarget

解决方案


您可能希望包含您的setup代码,或者如果它真的很长,则可以包含它的精简版本,以确保答案符合您使用的结构。我会通过有一个turtles-own变量来存储他们当前的目标来解决这个问题,如果它是空的,他们可以尝试填充它(而不是为此目的使用额外的布尔值)。此外,您可能希望将 1/0 选项转换为true/false以获得更清晰的代码。查看此示例设置:

globals [ stores ]

patches-own [ security ]

turtles-own [ 
  current-target 
  professional?
  mypatches 
]

to setup
  ca
  ask n-of 20 patches [
    set pcolor green
    set security 1 + random 10
  ]
  set stores patches with [ pcolor = green ]

  crt 5 [
    setxy random-xcor random-ycor
    pd
    set professional? one-of [ true false ]
    set current-target nobody
    set mypatches ( patch-set )
  ]
  reset-ticks
end

这设置了一个包含一些绿色补丁的世界,这些补丁被分组到被patch-set调用的stores. 此外,您有一些将布尔值professional?设置为 true 或 false 的海龟。它们在没有current-target存储和空mypatches patch-set变量的情况下进行初始化。

现在,您可以让海龟检查它们是否current-target存在。如果没有,他们可以possible-targets从不等于patch-here请求海龟的存储集(这里)中为该变量分配一个存储。专业窃贼可以通过排除属于其变量成员的任何商店来进一步改进possible-targets以排除他们被逮捕的任何商店。mypatches patch-set评论中的更多细节:

to go
  ask turtles [
    ; if you have no target currently
    if current-target = nobody [
      ; Create a temporary patch set made up of all stores except for
      ; the one that I'm currently in
      let possible-targets stores with [ self != [patch-here] of myself ]
      ifelse professional? [
        ; Have professional thieves revise their possible targets to exclude
        ; any in the patchset mypatches, then choose a possible target
        set possible-targets possible-targets with [ not member? self [mypatches] of myself ]
        set current-target min-one-of possible-targets in-radius 10 [ security ]
      ] [
        ; Have amateur thieves choose randomly from the possible targets
        set current-target one-of possible-targets 
      ]
    ]
    ; move closer to your current target, or
    ; move to it exactly if you're near enough
    ifelse current-target != nobody [
      face current-target 
      ifelse distance current-target > 1 [
        fd 1 
      ] [ 
        move-to current-target
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ; *** do your shoplifting attempt/check here ***
        ; For this example, just have professional thieves sometimes
        ; add this patch to their mypatches patchset
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        if professional? and random-float 1 < 0.5 [
          set mypatches ( patch-set mypatches patch-here )
        ]        
        ; Reset current-target to nobody
        set current-target nobody
      ]
    ] [
      ; if no suitable nearby targets, wander randomly
      rt random 61 - 30
      fd 1
    ]
  ]
  tick        
end

如果您运行的时间足够长,最终您的专业窃贼将无法找到目标商店,因为他们已将所有商店添加到他们的mypatches变量中。


推荐阅读