我正在创建特定于海龟的表,用于存储补丁坐标,模拟年份(全局变量设置为1、2或3,具体取决于滴答计数)以及代表可用资源的补丁变量。
我已经设法编写了一个表,该表具有当前补丁的这些值,但是一直试图扩展该表,以便在每个时间步中,乌龟都会为其所有相邻补丁存储这三样东西。
似乎适用于当前补丁的代码是:
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过程适用于每个补丁:
to set-memory
foreach sort neighbors [ x ->
ask x [
let thispatch (list pxcor pycor year)
table:put memory-map thispatch food
]]
end
最后一点给我一个错误,提示table:put仅可在乌龟环境中使用。
我还尝试过首先为所有邻居创建一个坐标/年列表:
let thispatch [(list pxcor pycor year) ] of neighbors
可以,但是我不知道如何使用每组列表输入作为表的键。
最后,我需要一个表,其中包含每个相邻补丁的(pxcor pycor年)键和食物值作为每个键的值的键。非常感谢您的帮助。
答案 0 :(得分:0)
认为我明白了。我创建了一个独立的模型,该模型将更新表中相邻补丁以及乌龟站立的补丁的值:
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