How to use agents as keys in table of table extension?

时光总嘲笑我的痴心妄想 提交于 2019-12-11 07:07:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!