NetLogo:从行数可变的输入文件中读取数据

发布于 2024-08-21 12:23:36 字数 2332 浏览 10 评论 0原文

我一直在 NetLogo 中运行博弈论模拟,现在有很多数据文件,其中包含交叉表数据 - 每列存储不同变量的值,并且有 c.包含数据的 1000 行。我正在尝试编写一个程序来获取这些文件并计算每列的平均值。

我有一个程序,只要每个文件中有恒定数量的数据行,它就可以工作。该程序使用文件读取命令循环来计算运行总计,然后在读取所有行后除以读取的行数。

但是,我的真实数据文件的行数是可变的。我一直在尝试使用 file-at-end 修改我的代码?让它在最后一行之后退出正在运行的总循环,但我一直无法找到任何有效的使用它的方法 - 我只是收到一条错误消息,指出文件已结束。

请问有人可以建议一种处理这个问题的方法吗?我已粘贴下面的工作代码。

--

globals [target-file-name  
current-tally-file  
lines-read  
coops-fixed-run cheats-fixed-run either-fixed-run coop-freq-min-run  
coop-freq-max-run coop-freq-mean-run ticks-run  
num-lines  
]  

to setup  
set target-file-name user-input "Type a name for the target file"  
file-open target-file-name  
file-print("TallyFile Reps pFixCoop pFixCheat pFixEither MeanMinCoop MeanMaxCoop MeanMeanCoop")  
file-close  
set num-lines read-from-string user-input "How many lines in the file to be processed?" 
end 

to go  
set current-tally-file user-file  
file-open current-tally-file  
set lines-read 0  

while [lines-read < num-lines][  
let in1 file-read set coops-fixed-run (coops-fixed-run + in1)  
let in2 file-read set cheats-fixed-run (cheats-fixed-run + in2)  
let in3 file-read set either-fixed-run (either-fixed-run + in3)  
let in4 file-read set coop-freq-min-run (coop-freq-min-run + in4)  
let in5 file-read set coop-freq-max-run (coop-freq-max-run + in5)  
let in6 file-read set coop-freq-mean-run (coop-freq-mean-run + in6)  
let in7 file-read set ticks-run (ticks-run + in7)    
set lines-read (lines-read + 1)   
]  

stop-and-clear  
end  


to stop-and-clear  

let pfixcoop (coops-fixed-run / lines-read)  
let pfixcheat (cheats-fixed-run / lines-read)  
let pfixeither (either-fixed-run / lines-read)  
let mean-of-mins (coop-freq-min-run / lines-read)  
let mean-of-maxs (coop-freq-max-run / lines-read)  
let mean-of-means (coop-freq-mean-run / lines-read)  
let mean-of-ticks (ticks-run / lines-read)  

file-open target-file-name  
file-print (word current-tally-file " " lines-read " " pfixcoop " " pfixcheat "
" pfixeither " " mean-of-mins " " mean-of-maxs " " mean-of-means " "
mean-of-ticks)  
file-close  


set coops-fixed-run 0  
set cheats-fixed-run 0  
set either-fixed-run 0  
set coop-freq-min-run 0  
set coop-freq-max-run 0  
set coop-freq-mean-run 0  
set ticks-run 0  
set lines-read 0  

stop  

end  

I've been running game theory simulations in NetLogo and I now have lots data files, containing cross-tabulated data - each column store a value of a different variable, and there are c. 1000 rows containing the data. I'm trying to write a programme that will take these files and calculate the mean value for each column.

I have a programme that works as long as there is a constant number of data rows in each file. The programme uses a loop of file-read commands to calculate running totals, which are then divided by the nuber of rows read once all of the rows have been read.

However, my real data files have variable numbers of rows. I have been trying to modify my code using file-at-end? to get it to exit the running total loop after the last line, but I haven't been able to find any way of employing it that works - I just get an error saying that the file is at an end.

Please could someone suggest a way of dealing with this? I've pasted the working code below.

--

globals [target-file-name  
current-tally-file  
lines-read  
coops-fixed-run cheats-fixed-run either-fixed-run coop-freq-min-run  
coop-freq-max-run coop-freq-mean-run ticks-run  
num-lines  
]  

to setup  
set target-file-name user-input "Type a name for the target file"  
file-open target-file-name  
file-print("TallyFile Reps pFixCoop pFixCheat pFixEither MeanMinCoop MeanMaxCoop MeanMeanCoop")  
file-close  
set num-lines read-from-string user-input "How many lines in the file to be processed?" 
end 

to go  
set current-tally-file user-file  
file-open current-tally-file  
set lines-read 0  

while [lines-read < num-lines][  
let in1 file-read set coops-fixed-run (coops-fixed-run + in1)  
let in2 file-read set cheats-fixed-run (cheats-fixed-run + in2)  
let in3 file-read set either-fixed-run (either-fixed-run + in3)  
let in4 file-read set coop-freq-min-run (coop-freq-min-run + in4)  
let in5 file-read set coop-freq-max-run (coop-freq-max-run + in5)  
let in6 file-read set coop-freq-mean-run (coop-freq-mean-run + in6)  
let in7 file-read set ticks-run (ticks-run + in7)    
set lines-read (lines-read + 1)   
]  

stop-and-clear  
end  


to stop-and-clear  

let pfixcoop (coops-fixed-run / lines-read)  
let pfixcheat (cheats-fixed-run / lines-read)  
let pfixeither (either-fixed-run / lines-read)  
let mean-of-mins (coop-freq-min-run / lines-read)  
let mean-of-maxs (coop-freq-max-run / lines-read)  
let mean-of-means (coop-freq-mean-run / lines-read)  
let mean-of-ticks (ticks-run / lines-read)  

file-open target-file-name  
file-print (word current-tally-file " " lines-read " " pfixcoop " " pfixcheat "
" pfixeither " " mean-of-mins " " mean-of-maxs " " mean-of-means " "
mean-of-ticks)  
file-close  


set coops-fixed-run 0  
set cheats-fixed-run 0  
set either-fixed-run 0  
set coop-freq-min-run 0  
set coop-freq-max-run 0  
set coop-freq-mean-run 0  
set ticks-run 0  
set lines-read 0  

stop  

end  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2024-08-28 12:23:36

从文件中读取所有行的方法如下所示:

to read-file [filename]
  file-open filename
  while [not file-at-end?][
    ;read one line
    let in1 file-read
    let in2 file-read
    ;and so one, at the end you will probably want to put these into some global variable
    set global-in1 fput in1 global-in1
  ]
  file-close filename
end

这假设所有行都具有确切的数据项名称编号,并且您知道该编号是多少。否则只需使用 file-read-line 而不是 file-read

A method to read all lines from a file looks like:

to read-file [filename]
  file-open filename
  while [not file-at-end?][
    ;read one line
    let in1 file-read
    let in2 file-read
    ;and so one, at the end you will probably want to put these into some global variable
    set global-in1 fput in1 global-in1
  ]
  file-close filename
end

This assumes that all rows have exactly the name number of data items, and you know what that number is. Otherwise just use file-read-line instead of file-read

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文