读取csv表Netlogo时内存问题



我正在编写一个代表数百只海龟的Netlogo代码。每只乌龟都有一组参数和状态变量,这些参数和变量存储在csv表中(在运行Netlogo代码之前创建(。表格是特定于阶段的,即如果乌龟达到一定年龄并改变了生命阶段(例如,幼年期->成年期(,它必须阅读另一个特定于新生命阶段的表格。此外,每个表都用一个数字命名(例如,"juvenile_file_1.csv"、"成人文件_1.csv"、…、"成人文档_8.csv"等(,并且每个乌龟在这些表中随机选择。这里有一个例子:

set ran.num 50 ; there are 50 tables for juveniles 
ask turtles[
let num.file random (ran.num) + 1
set unique.num.file num.file ; I added this line so that turtles keep the same table number among stages. 
; I tried this to gain on speed and memory, but it did not change so much
let file.name (word "juvenile_file_" num.file ".csv")
if( file-exists? file.name = FALSE )
[ user-message "Warning: input file  with num does not exist in the current directory!" ]
set juvenile.input csv:from-file file.name
set index 1 ; to read the firs row of the table. index in increased at each time step.
[other code lines]
set sex item 16 item index juvenile.input ; read column 17, first numerical row of the table (very first row has headings)
set stage item 17 item index juvenile.input
set exposure.surv item 18 item index juvenile.input
set reproduction-period item 19 item index juvenile.input
]

【其他代码行】

if Length >= 0.95 * L-max and stage = "juvenile"
[
set stage "adult" 
set juvenile.input 0
let file.name (word "adult_" choice.GUTS "_file_" unique.num.file ".csv")
if( file-exists? file.name = FALSE )
[ user-message "Warning: input file  with num does not exist in the current directory!" ]
set adult.input csv:from-file file.name
]

因为要求海龟在改变生命阶段时阅读表格要求太高了,我认为最好在设置过程中阅读所有表格,然后在需要时选择其中一个。但是,如何在设置过程中以自动方式读取所有表格?对我来说,仅仅写十几个名字(例如,成人.input.1、成人.input.2、青少年.input.1和青少年.input.2等(似乎相当耗时。我希望这是清楚的。谢谢你的帮助!

我会考虑表扩展。将所有成人参数放在一个文件中,行以参数集编号(1-8等(开头,然后将所有参数值作为列包含。

然后,您可以将所有参数读取到一个表中(使用csv:from文件和table:from列表。

然后,您的代理可以选择一个随机参数集编号,并使用table:get从表中获取参数值列表。

最新更新