【问题标题】:How can I collect the "ids" of directed links using the NetLogo Behavior Space?如何使用 NetLogo 行为空间收集定向链接的“ID”?
【发布时间】:2019-10-15 15:55:28
【问题描述】:

我们在 NetLogo 模型中存储了大量数据作为链接的属性。当我使用行为空间设计实验和指导数据收集时,我指定要提取的“链接的[属性]”。但是,在 CSV 文件中,我看不到链接的 ID,这对于理解数据至关重要。如何在我的结果中收集链接的 ID?据我了解,没有启用此命令的原语。

【问题讨论】:

    标签: netlogo data-collection behaviorspace


    【解决方案1】:

    每当您想使用 BehaviorSpace 从单个代理(包括链接)中提取信息时,一个不错的方法是使用csv 扩展,如本答案所述:

    https://stackoverflow.com/a/52406247/487946

    一般的想法是我们可以将 csv 嵌入到我们的 csv 中,然后在 R(或 Python 或 Julia 或其他)中使用类似 read_csv 的函数从我们的 BehaviorSpace 结果中提取“内部 csv”。

    在链接的情况下,包含链接每一端的who 编号以唯一标识它会很有用。 (这是我提倡使用who 号码的极少数情况之一。)

    让我们以这个愚蠢的示例模型为例:

    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 设置为一个随机数,并随着模型的滴答声不断地将该数字减半。

    为了生成我们将嵌入到 BehaviorSpace 结果中的 csv,我们编写了以下报告器:

    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后在命令中心试一试,会输出如下内容:

    observer> setup
    observer> print link-attributes-csv
    who1,who2,attribute
    0,1,0.9409784968740699
    1,2,0.9079884204004846
    0,2,0.9070292656950991
    

    如您所见,我们有一个简洁的小 csv 表,其中每一行代表一个特定的链接,由它连接的海龟的 who 数量标识。

    由于这个记者报告了一个字符串(而且这个字符串可以包含换行符),我们可以直接在BehaviorSpace实验中使用:

    运行这个实验(使用“表格输出”)会得到以下输出文件:

    "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 来处理这个问题:

    library(tidyverse)
    df <-
      read_csv("experiment-table.csv", skip = 6) %>%
      mutate(`link-attributes` = map(`link-attributes`, read_csv)) %>%
      unnest()
    

    purrr::maptidyr::unnest 函数是关键函数。我不会在这里解释它们,但值得查找并熟悉它们。

    我们的最终结果如下所示:

    # 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 
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      和补丁一样,链接由两个数字标识,这两个数字是两端的 who 数字。您可以保存链接的字符串表示形式(例如,(链接 0 1))或将数字提取为列表(或单独)。例如,

      to test
        ca
        crt 2
        ask turtle 0 [create-link-with turtle 1]
        print link 0 1
        ask link 0 1 [let id sort [who] of both-ends print id] ; a list
        ask link 0 1 [let id sort [who] of both-ends
          print (word item 0 id "-" item 1 id)
        ] ; a string
      end
      

      【讨论】:

        猜你喜欢
        • 2014-04-02
        • 1970-01-01
        • 2019-06-20
        • 1970-01-01
        • 2022-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多