首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取csv表时的内存问题

读取csv表时的内存问题
EN

Stack Overflow用户
提问于 2022-03-09 14:37:18
回答 1查看 35关注 0票数 1

我正在开发一个代表数百只海龟的网络标识代码。每个海龟的特征是一组参数和状态变量,这些参数和状态变量存储在csv表中(在运行Net徽标代码之前创建)。表格是特定阶段的,也就是说,如果一只海龟达到了一定的年龄,并且改变了它的生命阶段(例如,少年->成人),它必须阅读另一张特定于新生命阶段的表格。此外,每个表都用一个数字命名(例如,"juvenile_file_1.csv“、"adult_file_1.csv”、.、"adult_file_8.csv“等),每只海龟在这些表中随机选择。下面是一个示例:

代码语言:javascript
复制
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
  ]

其他代码行

代码语言:javascript
复制
  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
]

因为让海龟在改变生命阶段的时候读表格太苛刻了,所以我认为最好在安装过程中阅读所有的表格,然后在需要的时候选择其中的一张。但是,如何在安装过程中以自动化的方式读取所有表?只写十几个名字(例如成人输入1,成人输入2,少年输入1,少年输入2等等)我觉得挺费时的。我希望这是清楚的。谢谢你的帮忙!

EN

回答 1

Stack Overflow用户

发布于 2022-03-10 15:15:29

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

然后,可以将所有参数读入表中(使用csv:from和table :from)。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71411064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档