[ifelse ( random-float 1 < 0.99 ) [
set security-apprehension security-apprehension + 1 ][
set successful-shoplifts successful-shoplifts + 1 ]
The above is from a section of coding related to shoplifter behaviour and shows two turtles-own variables (security-apprehension and successful-shoplifts), one of which will increase and the other remain the same. This can only occur when a turtle is on a certain patch (A store). I would like 'security-apprehension' to resort back to 0 when the turtle is not on that patch and for the score to be 'logged' i.e. go back to what it was previously when the turtle is back on the patch so I can set a criteria to determine whether a shoplifter should be considered a prolific offender to individual stores.
How can this be achieved?
I'm not 100% sure on the output you're after, but here's an attempt that makes use of the table
extension to build a turtle-specific dictionary to store patch coordinates as a key paired with the security apprehension level of the corresponding patch.
First, the setup:
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
Now, you can have your turtles check if they're at a 'store'. If they are, check if the store already has an associated security apprehension level- if it does not, assign one. Then, query that table to get the appropriate security apprehension level for the current store (more details in comments).
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
A modified version of the above as per clarification in comments:
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