首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >txt (csv)文件形式的Netlogo变量

txt (csv)文件形式的Netlogo变量
EN

Stack Overflow用户
提问于 2021-05-10 17:12:00
回答 2查看 34关注 0票数 0

我是Netlogo的新手。连接一个GIS文件,我想建立一个模型,模拟人们在食物上的支出,以应对空气污染,如PM (特殊问题)。当然,我还需要考虑其他变量,如气温、降雨量等。我有一个按社区收集的关于食物消费支出、空气污染和所有其他变量的面板数据。

我的问题是,我们是否可以创建一个随时间变化的变量,这意味着当滴答前进时,变量的值会发生变化。例如,智能体可能会对周围的空气污染做出反应,从而改变他们的食物消费行为。我想为邻居(补丁)设置一个“空气污染”的变量,这个变量会随着时间的推移而变化(根据我收集的数据)。因此,我想以某种方式链接每个变量的文本文件,并让net徽标在滴答前进时读取它。

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

EN

回答 2

Stack Overflow用户

发布于 2021-05-11 01:17:59

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

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

这两个都写起来很有趣。有没有人?

如果没有这些,你可以把你所有的数据读入NetLogo,然后填充一个列表(从概念上讲是由tick索引的)列表(由tick改变的4或5个变量),然后使用Netlogo语句来提取正确的由tick索引的列表(加或减1,注意0和1的索引)。)当您需要它时,然后解析该列表以从中提取变量。这肯定能行得通。

今天下午,我可能会写并发布这样一个列表解决方案列表。要看天气而定。同时,这是我曾经写过的一个相关的例子,读入一个CSV文件,并将变量写到一些补丁自己的变量中,这就是你所需要的。

我认为这段代码可以工作(当然,需要读取一个文件。)

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2021-05-11 02:06:21

下面的示例代码从csv文件中读取时间序列数据,并将其放入列表列表中,以便稍后提取。

代码语言:javascript
复制
extensions [csv ]
globals [ list-of-lists ]

to setup
  clear-all
  reset-ticks
 ;; let max-rows-to-read 3 ;; for testing purposes
  
  
  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 "Found 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 "================================"

;; skip lines if you need to 
;  let nextline file-read-line
;  print ("skipping the second row of header info")
;  print nextline
;  print " "

  let rowcount 0
 
  set list-of-lists [ ]
  
  while [ not file-at-end?  ]
  [
    set rowcount rowcount + 1
  ;  if rowcount > max-rows-to-read [ print "list of lists: " show list-of-lists file-close stop]
    
    let nextline file-read-line
    let mydata csv:from-row nextline
    print "=============================================="
    print (word "next incoming line to process is: " nextline )
    print mydata;

    
        let  onelist ( list 
               item 0 mydata
               item 1 mydata
               item 2 mydata )
      set list-of-lists lput onelist list-of-lists
     
      ]
 
  show list-of-lists
  file-close;
   
end


to go
  let indexer ticks ;; maybe you want this offset from ticks by some amount.
 
 
  ifelse ( indexer < length list-of-lists )
[
  let datalist item indexer list-of-lists
  let time item 0 datalist
  let temp item 1 datalist
  let smog item 2 datalist
  
  print (word "At tick " indexer " processing this list: " datalist )
  print "yielding: "
  print (word "At tick " indexer " time = " time " temp= " temp " and smog=" smog "\n")
]
  [
     print "All done. Have a nice day!"
  ]
 
 tick
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67467741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档