Reputation: 507
I have turtles with a memory that stores the patches the turtles travel:
set memory (patch-set memory patch-here)
How can I see the coordinates of each of the patches of its memory?
Upvotes: 0
Views: 225
Reputation: 2926
Your memory
is a patch-set, so you can ask
its members just as you would ask patches
. For example:
to inspect-memory
ask one-of turtles [
print word "This is the memory of " self
ask memory [
print list pxcor pycor
]
]
end
Alternatively, consider that the reporter above (i.e. list pxcor pycor
) is just a reporter that you can apply to any patch or group of patches by using of
. In that case:
to inspect-memory
ask one-of turtles [
print word "This is the memory of " self
print [list pxcor pycor] of memory
]
end
The first example will report separate lists, and to me it seems mostly useful for printing. The second example will report a list of lists, so it seems more useful for further manipulation.
Anyway I don't know if you already considered this but note that, being memory
a patch-set, its patches will always come in random order. If you want memory
to mirror the journey of your turtles, then you have to make it a list:
turtles-own [
memory
]
to setup
clear-all
create-turtles 1 [
set memory (list)
]
end
to go
ask turtles [
right random 360
move-to patch-ahead 1
set memory (lput patch-here memory)
]
end
Considering that we now have a list of patches, you can still achieve the same type of output. The code below uses foreach
to give you the same result as the first example above (i.e. the one returning separate lists):
to inspect-memory
ask one-of turtles [
print word "This is the memory of " self
foreach memory [
p ->
ask p [
print list pxcor pycor
]
]
]
end
While the code below uses map
to give you the same result as the second example above (i.e. the one returning a list of lists):
to inspect-memory
ask one-of turtles [
print word "This is the memory of " self
print map [p -> [list pxcor pycor] of p] memory
]
end
Of course, instead of printing your results to the Command Center, you can also just turn these command-procedures into reporter-procedures by using to-report
and report
; so that the results are readily available in other places of your code for further use if needed
Anyway, if this is the only way in which you are using memory
, it leads to consider whether you can just use memory
directly as a list of coordinates rather than an agentset/list of patches from which you have to extract that information:
to go
ask turtles [
right random 360
move-to patch-ahead 1
set memory (lput ([list pxcor pycor] of patch-here) memory)
]
end
Upvotes: 3