海龟变量 "logged" 可以用于不同的补丁吗?

Can a turtle variable "logged" for different patches?

    [ifelse ( random-float 1 < 0.99 ) [                              
      set security-apprehension security-apprehension + 1 ][
      set successful-shoplifts successful-shoplifts + 1 ]

您好。

以上来自与入店行窃行为相关的一段代码,显示了两个海龟自身的变量(安全逮捕和成功入店行窃),其中一个会增加,另一个保持不变。这只有在乌龟在某个补丁(A 商店)上时才会发生。我希望 'security-apprehension' 在乌龟不在那个补丁上时返回到 0,并且得分为 'logged' 即当乌龟回到补丁上时回到以前的状态,所以我可以设置一个标准来确定商店扒手是否应该被视为个别商店的多产罪犯。

有人可以建议如何实现吗?

欢迎使用计算器!您可能想查看 asking help 以了解有关问题中应包含哪些内容的一些指南。包含最少的工作代码(例如,模型代码的非常基本/玩具版本)通常有助于提供有关如何构建模型的上下文。照原样,我不是 100% 确定你之后的输出,但这里尝试使用 table 扩展来构建一个特定于海龟的字典来存储补丁坐标作为与配对的键相应补丁的安全忧虑级别。

首先,设置:

extensions [ table ]
turtles-own [ security-table current-security-apprehension]

to setup 
  ca
  ask n-of 20 patches [ 
    set pcolor green
  ]
  crt 1 [
    setxy random-xcor random-ycor
    set security-table table:make
    set current-security-apprehension "NA"

  ]
  reset-ticks
end

现在,您可以让您的海龟检查它们是否在 'store'。如果是,请检查商店是否已经具有相关的安全忧虑级别 - 如果没有,则指定一个。然后,查询 table 以获得当前商店的适当安全忧虑级别(评论中有更多详细信息)。

to go 
  ask turtles [
    set current-security-apprehension "NA"
    rt random 91 - 45 
    fd 1
    ; If you're at a 'store'
    if [ pcolor ] of patch-here = green [
      ; Use the pxcor-pycor pair in a list as a dictionary
      ; key to store/check security apprehension in a table
      let this-key list pxcor pycor
      ; Check if the store has been visited
      if not table:has-key? security-table this-key [
        ; If not, store the security value for this store
        ; (randomly chosen, in this example- how you set this
        ; depends on your model)
        table:put security-table this-key one-of [ "high" "medium" "low" ]
      ]
      ; Get the security apprehension level for the current patch
      set current-security-apprehension table:get security-table this-key
    ]
    if current-security-apprehension != "NA" [
      show ( word "Security apprehension on " patch-here " is " current-security-apprehension )
    ]
  ]
  tick
end

如果这与您的想法不符,您可能需要在问题中提供更多细节 - 包括更多代码或对您已经尝试过的内容的描述可能会有所帮助。

编辑:

根据评论中的说明对上述内容进行了修改:

extensions [ table ]
turtles-own [ security-table shoplift-table current-security-apprehension]

to setup
  ca
  ask n-of 20 patches [
    set pcolor green
  ]
  crt 1 [
    setxy random-xcor random-ycor
    set security-table table:make
    set shoplift-table table:make
    set current-security-apprehension 0
  ]
  reset-ticks
end

to go
  ask turtles [
    set current-security-apprehension "NA"
    rt random 91 - 45
    fd 1
    ; If you're at a 'store'
    if [ pcolor ] of patch-here = green [

      ; Use the pxcor-pycor pair in a list as a dictionary
      ; key to store/check security apprehension in a table
      let this-key list pxcor pycor

      ; Check if the store has been visited, add shoplift
      ; and security values as needed
      if not table:has-key? security-table this-key [
        table:put security-table this-key 0
        table:put shoplift-table this-key 0
      ]

      ; Try to shoplift- if random value 0-1 is less than
      ; 0.98, count as a success. Otherwise, add one to 
      ; security score
      ifelse random-float 1 < 0.98 [
        ; Increase the current shoplifting value by one
        let cur-shop table:get shoplift-table this-key
        table:put shoplift-table this-key cur-shop + 1
      ] [
        ; Otherwise, increase the security apprehension by 1
        let cur-sec table:get security-table this-key
        table:put security-table this-key cur-sec + 1
        print "Caught you red-handed."
        ; Check if they have been caught here 2 or more times:
        if table:get security-table this-key >= 2 [
          print "CALL THE POLICE!"
        ]
      ]
    ]
  ]
  tick
end