我如何收集“身份证"? NetLogo行为空间使用定向链接的方式? [英] How can I collect the "ids" of directed links using the NetLogo Behavior Space?
问题描述
我们在NetLogo模型中存储了很多数据作为链接的属性.当我使用行为空间设计实验并指导数据收集时,我指定了要提取的链接属性".但是,在csv文件中,我看不到链接的ID,这对于理解数据是必不可少的.如何收集搜索结果中链接的ID?据我了解,没有启用该命令的原语.
We have stored a lot of data as attributes of links in our NetLogo model. When I use the Behavior Space to design experiments and to direct data collection, I specify the "[attribute] of link" to be extracted. However, in the csv file I cannot see the id of the link, which is a essential to understand the data. How can I collect the ids of links in my results? From what I understand there is no primitive to enable this command.
推荐答案
每当您要使用BehaviorSpace从单个代理(包括链接)中提取信息时,一种不错的方法是使用csv
扩展名,如这个答案:
Whenever you want to extract information from individual agents (including links) using BehaviorSpace, a nice way to do it is to use the csv
extension as explained in this answer:
https://stackoverflow.com/a/52406247/487946
通常的想法是,我们可以将csv嵌入到csv中,然后在R(或Python或Julia或其他工具)中使用类似read_csv
的函数从BehaviorSpace结果中提取内部csv".
The general idea is that we can embed csv into our csv, and then use a read_csv
-like function in R (or Python or Julia or whatever) to extract the "inner csv" from our BehaviorSpace results.
对于链接,包括链接的每个末端的who
号以唯一地标识它会很有用. (这是我主张在任何情况下都使用who
数字的少数情况之一.)
In the case of links, it would be useful to include the who
number of each end of the link to uniquely identify it. (This is one of the very few cases where I'll advocate using a who
number for anything.)
让我们以这个愚蠢的示例模型为例:
Let's take this silly example model:
extensions [ csv ]
links-own [ attribute ]
to setup
clear-all
create-turtles 3 [
create-links-with other turtles [
set attribute random-float 1
]
]
reset-ticks
end
to go
ask links [ set attribute attribute * 0.5 ]
tick
end
它只会创建三只乌龟,它们之间具有链接,将链接的attribute
设置为随机数,并在模型滴答时反复将该数字减半.
It just creates three turtles with links between them, sets the attribute
of the link to a random number and repeatedly halves that number as the model ticks.
要生成将嵌入到BehaviorSpace结果中的csv,我们编写以下报告程序:
To generate the csv that we will embed into our BehaviorSpace results, we write the following reporter:
to-report link-attributes-csv
report csv:to-string
fput ["who1" "who2" "attribute" ]
[ (list [ who ] of end1 [ who ] of end2 attribute) ] of links
end
如果您在运行setup
之后在命令中心尝试一下,它将输出以下内容:
If you try it out in the command center after running setup
, it will output something like this:
observer> setup
observer> print link-attributes-csv
who1,who2,attribute
0,1,0.9409784968740699
1,2,0.9079884204004846
0,2,0.9070292656950991
如您所见,我们有一个简洁的csv表,其中的每一行代表一个特定的链接,由它所连接的海龟的who
编号标识.
As you can see, we have a neat little csv table, where each row represents a particular link, identified by the who
number of the turtles it connects.
由于该报告者报告了一个字符串(并且该字符串包含换行符是可以的),因此我们可以直接在BehaviorSpace实验中使用它:
Since this reporter reports a string (and it's ok for this string to contain line breaks), we can use it directly in a BehaviorSpace experiment:
运行此实验(带有表输出")将提供以下输出文件:
Running this experiment (with "table output") gives the following output file:
"BehaviorSpace results (NetLogo 6.1.1)"
"link-attributes-example.nlogo"
"experiment"
"10/16/2019 11:00:12:495 +0100"
"min-pxcor","max-pxcor","min-pycor","max-pycor"
"-16","16","-16","16"
"[run number]","[step]","link-attributes"
"1","0","who1,who2,attribute
1,2,0.15670083797389645
0,2,0.40055350697928993
0,1,0.34892645306446335"
"2","0","who1,who2,attribute
0,1,0.2831244347856665
1,2,0.27721328746715357
0,2,0.5221352362751627"
"2","1","who1,who2,attribute
0,1,0.14156221739283326
0,2,0.26106761813758134
1,2,0.13860664373357678"
"1","1","who1,who2,attribute
0,2,0.20027675348964497
1,2,0.07835041898694822
0,1,0.17446322653223167"
"1","2","who1,who2,attribute
1,2,0.03917520949347411
0,2,0.10013837674482248
0,1,0.08723161326611584"
"2","2","who1,who2,attribute
1,2,0.06930332186678839
0,1,0.07078110869641663
0,2,0.13053380906879067"
所有换行符看上去都有些奇怪,但是您的数据分析工具应该能够解决这个问题.这是使用R和Tidyverse处理此问题的方法:
It looks at bit weird with all the line breaks, but your data analysis tools should be able to deal with that. Here is how to handle this using R and the Tidyverse:
library(tidyverse)
df <-
read_csv("experiment-table.csv", skip = 6) %>%
mutate(`link-attributes` = map(`link-attributes`, read_csv)) %>%
unnest()
The purrr::map
and tidyr::unnest
functions are the key ones. I won't explain them here, but its worth looking them up and familiarizing yourself with them.
我们的最终结果如下:
# A tibble: 18 x 5
`[run number]` `[step]` who1 who2 attribute
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1 0 1 2 0.157
2 1 0 0 2 0.401
3 1 0 0 1 0.349
4 2 0 0 1 0.283
5 2 0 1 2 0.277
6 2 0 0 2 0.522
7 2 1 0 1 0.142
8 2 1 0 2 0.261
9 2 1 1 2 0.139
10 1 1 0 2 0.200
11 1 1 1 2 0.0784
12 1 1 0 1 0.174
13 1 2 1 2 0.0392
14 1 2 0 2 0.100
15 1 2 0 1 0.0872
16 2 2 1 2 0.0693
17 2 2 0 1 0.0708
18 2 2 0 2 0.131
我希望这会有所帮助.
这篇关于我如何收集“身份证"? NetLogo行为空间使用定向链接的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!