首页 > 解决方案 > Netlogo 变量作为 txt (csv) 文件

问题描述

我对 Netlogo 很陌生。链接一个 GIS 文件,我想制作一个模型来模拟人们对 PM (特别是物质)等空气污染的反应。当然,我还需要考虑其他变量,例如气温、降水等。我有一个按社区收集的关于食品消费支出、空气污染和所有其他变量的面板数据。

我的问题是我们是否可以制作一个随时间变化的变量,这意味着当刻度前进时,变量的值会发生变化。例如,代理人可能会对附近的空气污染做出反应,并改变他们的食物消费行为。我想为社区(补丁)制作​​一个“空气污染”变量,并且会随时间变化(根据我收集的数据)。所以不知何故,我想链接每个变量的文本文件,并让 net logo 在 tick 前进时读取它。

有没有办法做到这一点?您的建议将不胜感激。

标签: variablestextnetlogo

解决方案


我不知道如何链接到一个文件,只是拉一个项目。这听起来像是访问数据库,但我没有看到或知道(还只是我)从 netlogo 到数据库系统的直接接口。

比如说,有人可以使用 Python 编写这样的东西,并使用 Python 的接口来提取您需要的数据。或者,您可以再次使用 R 的接口并稍作调整,因为 R 确实有一些非常好的方法来读取 CSV 文件,并通过 R 中正确的聪明语句逐项访问它们。

这两个写起来都会很有趣。任何人?

缺少这一点,您可以将所有数据读入 NetLogo 并填充,例如,一个列表(概念上按刻度索引)列表(由刻度更改的 4 或 5 个变量),然后使用 Netlogo 语句拉右需要时按刻度索引的列表(加或减一,观察零与一索引。),然后解析该列表以从中提取变量。那肯定会奏效。

今天下午我可能会写并发布这样的列表解决方案列表。取决于天气。同时,这是我曾经写过的一个相关示例,它读取 CSV 文件并将变量写入一些补丁自己的变量,这是您最需要的。

我认为这段代码有效(当然还有要读取的文件。)

extensions [csv ]
globals [ XC YC ]
patches-own [
  country
  population
  emissions_mass
  vulnerability
  jetfuel_civilian
  climate_finance
]

to setup
  clear-all
  file-close
  ;; double the slashes so this works on a Windows computer
  ;; set-current-directory "C:\\Users\\wades\\Documents\\netlogo\\countries" ;
  ;; but simpler once just the let the user navigate to the file
  ;; file-open "countries.csv"
  let mypath "???"

  set-current-directory user-directory
  file-open user-file



  let headers file-read-line
  print (word "There are the columns: " headers);
  let headlist csv:from-row headers
  let colcount length headlist
  print (word "this many columns: " colcount)
  let i 0

  repeat colcount [
      let determ item i headlist
      print (word "column " i " is " determ)
      set i ( i + 1 )
    ]
  print "================================"

  let nextline file-read-line
  print ("skipping the second row of header info")
  print nextline
  print " "

  while [ not file-at-end?  ]
  [
    set nextline file-read-line
    let mydata csv:from-row nextline
    print "=============================================="
    print (word "next incoming line to process is: " nextline )
    print mydata;

    set XC item 1 mydata
    set YC item 2 mydata
    print (word " data for patch (" XC ", " YC " )" )

    let mytarget one-of patches with [ pxcor = XC and pycor = YC ]
    ask mytarget
     [
        set country           item 0 mydata
        set population        item 3 mydata
        set emissions_mass    item 4 mydata
        set vulnerability     item 5 mydata
        set jetfuel_civilian  item 6 mydata
        set climate_finance   item 7 mydata
        set pcolor blue
        set plabel country

     ]
    print " that patch should now be blue"


  ]

  file-close;
  reset-ticks
end


to go
  stop
  tick
end

推荐阅读