gpt4 book ai didi

csv - 使用 Netlogo 模型库中的 CSV 示例创建代理

转载 作者:行者123 更新时间:2023-12-01 06:12:37 25 4
gpt4 key购买 nike

我对这段代码创建的海龟数量有疑问:

to read-turtles-from-csv
file-close-all ; close all open files
if not file-exists? "turtles.csv" [
user-message "No file 'turtles.csv' exists! Try pressing WRITE-TURTLES-TO-CSV."
stop
]
file-open "turtles.csv" ; open the file with the turtle data

; We'll read all the data in a single loop
while [ not file-at-end? ] [

let data csv:from-row file-read-line

create-turtles 1 [
print "item column 4"
show item 4 data







]
]

file-close ; make sure to close the file
end

我的 turtles.csv 文件只有两行,所以我在这里期望的是 create-turtles 1 重复了行数,我有两个代理,第 4 列中的 2 个数字被打印出来。令人惊讶的是,创建了 4 只海龟!为什么?

谢谢

最佳答案

我想知道您的 turtles.csv 是否被读取为多行?尝试做类似的事情:

to read-file
file-close-all
file-open "turtles.csv"
while [not file-at-end?] [
print csv:from-row file-read-line
]
file-close-all
end

查看 Netlogo 是如何读取文件的。看来您走在正确的轨道上,否则-我只是按照您的示例测试了一个类似的文件,并且按预期得到了我的两只乌龟。使用名为“turtle_details.csv”的 .csv 文件,如下所示:

  color size heading
1 red 2 90
2 blue 4 180

我用这段代码用 .csv 中的变量生成了两只海龟:

extensions [ csv ]

to setup
ca
reset-ticks
file-close-all
file-open "turtle_details.csv"

;; To skip the header row in the while loop,
; read the header row here to move the cursor
; down to the next line.
let headings csv:from-row file-read-line

while [ not file-at-end? ] [
let data csv:from-row file-read-line
print data
create-turtles 1 [
set color read-from-string item 0 data
set size item 1 data
set heading item 2 data
]
]
file-close-all
end

关于csv - 使用 Netlogo 模型库中的 CSV 示例创建代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44771781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com