内存:在表中存储多个补丁的补丁变量 [英] Memory: storing patch variables in table for multiple patches

查看:84
本文介绍了内存:在表中存储多个补丁的补丁变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建特定于海龟的表,用于存储补丁程序坐标,模拟年份(根据滴答计数将全局变量设置为1、2或3)以及代表可用资源的补丁程序变量.

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

现在,我尝试使用foreach修改此代码以遍历邻居,并使set-memory过程适用于每个补丁:

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

最后一点给我一个错误,提示table:put仅可在乌龟环境中使用.

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.

最后,我需要一个表,其中包含每个相邻补丁的(pxcor pycor年)和食物值作为每个键的值的键.任何帮助都将不胜感激.

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.

推荐答案

认为我明白了.我创建了一个独立的模型,该模型将更新表中相邻补丁程序以及乌龟所站在的补丁程序中的值:

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

这篇关于内存:在表中存储多个补丁的补丁变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆