NetLogo-使用BehaviorSpace获取每次重复的所有海龟位置 [英] NetLogo - using BehaviorSpace get all turtles locations as the result of each repetition
问题描述
我正在使用BehaviorSpace使用不同的参数运行该模型数百次.但是我需要知道所有海龟的位置,而不是仅仅知道海龟的数量.如何使用BehaviorSpace实现它?
I am using BehaviorSpace to run the model hundreds of times with different parameters. But I need to know the locations of all turtles as a result instead of only the number of turtles. How can I achieve it with BehaviorSpace?
当前,我通过以下代码将结果输出到csv文件中:
Currently, I output the results in a csv file by this code:
to-report get-locations
report (list xcor ycor)
end
to generate-output
file-open "model_r_1.0_locations.csv"
file-print csv:to-row get-locations
file-close
end
但是所有结果都被弹出到同一个csv文件中,因此我无法确定每次运行的情况.
but all results are popped into same csv file, so I can't tell the condition of each running.
推荐答案
Seth建议合并csv输出的文件名中的rel ="nofollow noreferrer"> behaviorspace-run-number
.它将使您可以将该文件与主BehaviorSpace输出文件中的摘要数据相关联.
Seth's suggestion of incorporating behaviorspace-run-number
in the filename of your csv
output is one alternative. It would allow you to associate that file with the summary data in your main BehaviorSpace output file.
另一种选择是在行为空间实验定义中将列表报告者作为度量".例如,在您的情况下:
Another option is to include list reporters as "measures" in your behavior space experiment definition. For example, in your case:
map [ t -> [ xcor ] of t ] sort turtles
map [ t -> [ ycor ] of t ] sort turtles
然后,您可以使用自己喜欢的数据分析语言手动"解析结果列表.我以前在Julia中使用了以下功能:
You can then parse the resulting list "manually" in your favourite data analysis language. I've used the following function for this before, in Julia:
parselist(strlist, T = Float64) = parse.(T, split(strlist[2:end-1]))
我确信您可以轻松地用Python或R或您使用的任何语言编写一些等效的代码.
I'm sure you can easily write some equivalent code in Python or R or whatever language you're using.
在上面的示例中,我为xcor
和ycor
的乌龟输出了单独的列表.您也可以输出一个列表列表",但解析起来会比较棘手.
In the example above, I've outputted separate lists for the xcor
and the ycor
of turtles. You could also output a single "list of lists", but the parsing would be trickier.
巧合的是,今天我必须为另一个项目做类似的事情,并且我意识到 csv
扩展名和R可以使此操作非常容易.
Coincidentally, I had to do something similar today for a different project, and I realized that a combination of the csv
extension and R can make this very easy.
总体思路如下:
-
在NetLogo中,使用
csv:to-string
将列表数据编码为字符串,然后将该字符串直接写入BehaviorSpace输出中.
In NetLogo, use
csv:to-string
to encode list data into a string and then write that string directly in the BehaviorSpace output.
在R中,使用 purrr::map
和 readr::read_csv
,后跟
In R, use purrr::map
and readr::read_csv
, followed by tidyr::unnest
, to unpack everything in a neat "one observation per row" dataframe.
换句话说:我们喜欢CSV,因此我们将CSV放入CSV中,以便我们可以在解析的同时进行解析.
In other words: we like CSV, so we put CSV in our CSV so we can parse while we parse.
这是一个完整的例子.假设我们有以下NetLogo模型:
Here is a full-fledged example. Let's say we have the following NetLogo model:
extensions [ csv ]
to setup
clear-all
create-turtles 2 [ move-to one-of patches ]
reset-ticks
end
to go
ask turtles [ forward 1 ]
tick
end
to-report positions
let coords [ (list who xcor ycor) ] of turtles
report csv:to-string fput ["who" "x" "y"] coords
end
然后,使用我们的positions
报告器作为输出,定义以下微小的BehaviorSpace实验,该实验只有两个重复,并且时间限制为两个:
We then define the following tiny BehaviorSpace experiment, with only two repetitions and a time limit of two, using our positions
reporter as an output:
用于处理此问题的R代码非常简单明了:
The R code to process this is pleasantly straightforward:
library(tidyverse)
df <- read_csv("experiment-table.csv", skip = 6) %>%
mutate(positions = map(positions, read_csv)) %>%
unnest()
这将导致以下数据帧的整洁:
Which results in the following dataframe, all neat and tidy:
> df
# A tibble: 12 x 5
`[run number]` `[step]` who x y
<int> <int> <int> <dbl> <dbl>
1 1 0 0 16 10
2 1 0 1 10 -2
3 1 1 1 9.03 -2.24
4 1 1 0 -16.0 10.1
5 1 2 1 8.06 -2.48
6 1 2 0 -15.0 10.3
7 2 0 1 -14 1
8 2 0 0 13 15
9 2 1 0 14.0 15.1
10 2 1 1 -13.7 0.0489
11 2 2 0 15.0 15.1
12 2 2 1 -13.4 -0.902
朱莉娅的同一件事:
using CSV, DataFrames
df = CSV.read("experiment-table.csv", header = 7)
cols = filter(col -> col != :positions, names(df))
df = by(df -> CSV.read(IOBuffer(df[:positions][1])), df, cols)
这篇关于NetLogo-使用BehaviorSpace获取每次重复的所有海龟位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!