欢迎来到堆栈溢出!您可能想查看寻求帮助,以获取有关问题中包含内容的一些指南。包含最少的工作代码(例如,模型代码的非常基本/玩具版本)通常很有帮助,以提供有关如何构建模型的上下文。照原样,我不能 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
现在,您可以让您的海龟检查它们是否在“商店”。如果是,请检查商店是否已经具有相关的安全担忧级别 - 如果没有,请指定一个。然后,查询该表以获取当前商店的适当安全担忧级别(评论中的更多详细信息)。
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