问题
I am using the table extension as variable owned by a breed of agents.
The content of this table contains values referred to agents of another agent-set (events
).
As table keys, I use e-ids
a list with the who
numbers of each agent in events
breed.
The following procedure initializes the tables:
to setup-tables
ask walkers [
set we-tfound table:make
set we-interest table:make
foreach e-ids [ [?] -> table:put we-tfound ? 0
table:put we-interest ? 1 ] ]
ask links [
set popularity table:make
foreach e-ids [ [?] -> table:put popularity ? 0 ] ]
end
The answer to others posts recommends not to use who
numbers and iterate over agents by using, for example, construct ask agents [...]
.
However, I do not know how could be the best way to iterate over table:keys
using agents or someway better than whos ids.
Thank you very much for your help
回答1:
Since agents can't be used as keys in tables, using who
numbers is perfectly reasonable. The danger here is that if a turtle dies, you end up with an entry that doesn't actually correspond to an existing turtle. (This is also the reason agents are not allowed as keys in tables.) You can do is-turtle? turtle key
to see if key
corresponds to an existing turtle.
To convert table:keys
back to an agentset, you can do:
turtle-set map turtle table:keys my-table
Thus, to ask
all the turtles that are keys in a table, you do:
ask turtle-set map turtle table:keys my-table [ do-stuff ]
map turtle table:keys
converts the list of who
numbers to a list of turtles. turtle-set
then converts that to an agentset.
来源:https://stackoverflow.com/questions/44991405/how-to-use-agents-as-keys-in-table-of-table-extension