Memory: storing patch variables in table for multiple patches

微笑、不失礼 提交于 2019-12-11 03:33:49

问题


I am creating turtle-specific tables in which to store patch coordinates, year of simulation (global variable set to 1, 2, or 3 depending on tick count), and a patch variable representing available resources.

I have managed to code for the creation of a table that has these values for the current patch but am stuck trying to scale this so that, at each time step, the turtle stores these three things for all its neighboring patches.

The code that seems to be working for the current patch is:

extensions [table]
globals [year]
turtles-own [memory-map]
patches-own [food]

to setup
 ca
 set year 1
 ask patches [set food random 10]
 crt 2 [set memory-map table:make]
 reset-ticks
end

to go
 if ticks = 100 [set year 2]
 if ticks = 200 [set year 3]
 ask turtles [fd 1 set-memory]
end

to set-memory
  let thispatch (list pxcor pycor year); key for table
  table:put memory-map thispatch food
end

Now, I tried to modify this code using foreach to loop through neighbors and get the set-memory procedure to work for each patch:

to set-memory
  foreach sort neighbors [ x ->
    ask x [
    let thispatch (list pxcor pycor year)
    table:put memory-map thispatch food 
  ]]
end

This last bit gives me an error that suggest table:put can only be used in turtle context.

I have also tried creating a list of coordinates/year for all neighbors first:

let thispatch [(list pxcor pycor year) ] of neighbors

which works but I can't figure out how to use each set of list inputs as a key for the table.

In the end, I need a table with keys containing (pxcor pycor year) of each neighbouring patch and the food value as the value of each key. Any help is much appreciated.


回答1:


Think I got it. I have created a standalone model that updates values in the table for neighbouring patches as well as the patch the turtle is standing on:

extensions
[
  table
]

patches-own
[
  id
  food
]

turtles-own
[
  memory-map
]

to setup
  ; Assign a specific ID to each patch to act as the key for the table 
  ;(that way you only deal with one value instead of a set of coordinates)
  (foreach (sort patches) (n-values (count patches) [i -> i + 1]) [[i _id] -> ask i [set id _id]]) 
  ask patches [set food 100]
  crt 1
  [
    set pcolor yellow ;for visualization purposes
    set plabel id ;for visualization purposes
    set memory-map table:make ; Create empty table
    table:put memory-map ([id] of patch-here) food ;Add key (id) and value (food) of current patch to the table
                                                   ; Loop asking each of the neighbors to update the table
    let patch-memmap memory-map ;so that patches can access the table, otherwise it's only accessible to turtles and it gives an error
    foreach sort neighbors [ x ->
      ask x [
        table:put patch-memmap id food
        set pcolor green ;for visualization purposes
        set plabel id ;for visualization purposes
    ]]
    set memory-map patch-memmap ;return the new table to the turtle 
    show memory-map ;check what table looks like
  ]
end

to go
  ask patches [set food 200]
  ask turtles 
  [ 
    move-to one-of neighbors 
    set food (food * 0.5) ;representing consumption in the current patch
    update-memory
  ]

end

to update-memory
  set pcolor red ;for visualization purposes
  set plabel id ;for visualization purposes
  table:put memory-map ([id] of patch-here) food
  let patch-memmap memory-map 
  foreach sort neighbors [ x ->
    ask x [
      table:put patch-memmap id food
      set pcolor pink 
      set plabel id 
  ]]
  set memory-map patch-memmap
  show memory-map
end


来源:https://stackoverflow.com/questions/57066138/memory-storing-patch-variables-in-table-for-multiple-patches

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