NET-代理从.csv中提取随机值

fdbelqdn  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(113)

我有一个名为“Males.csv”的CSV文件,其中有三列“turning_angles”“speed”“step_length”
我在空白环境中创建了一个非常简单的代理模型

to set-up 
  clear-all
  reset-ticks
  ask patches [set pcolor green]
  ask n-of home patches [set pcolor brown]
  ask patches with [pcolor = brown] [
  sprout n-turtles [set breed turtle set shape "arrow" set sex one-of ["male" "female"]]]
end

字符串
我想写一个名为stepturn的程序,代理从csv中的列中移动和提取参数。是否可以将csv列转换为列表,然后从数据的分布中随机选择?
例如作为伪代码。

turtles-own [step-length turn]

to step 
ask turtle fd random step-length from_column "step_length" "Males.csv"
end 

to turn 
ask turtle fd random turn from_column "turning_angles" "Males.csv"
end

to go 
ask turtles [step turn]
end

bgibtngc

bgibtngc1#

CSV-extension。你也可以在模型库中找到一个示例模型(参见CSV Example)。
扩展程序逐行读取CSV文件,因此您需要循环并将列的值逐个提取到列表中。如果您保存您的分布而不是按列而是按行保存,您的生活会更轻松一些,但当然它也适用于列。您可以使用one-of绘制该列表的随机项。
我附上了一个代码示例,说明如何解决下面的问题。

extensions [ csv ]
globals [
  step_length_ls
  turning_angles_ls
]

to read_csv
  ; close file connections
  file-close-all
  ; open your csv file
  file-open "males.csv"
  ; read first line of csv file (headers), alternatively just hardcode the index of columns
  let header csv:from-row file-read-line
  ; determine position of columns according to header
  let idx_step_col position "step_length" header
  let idx_turning_col position "turning_angles" header
  ; initialize global variables as lists
  set step_length_ls []
  set turning_angles_ls []  
  
  ; read data in via a while loop
  while [ not file-at-end? ] [
    ; here the CSV extension grabs a single line and puts the data of that in the data variable (list)
    let data csv:from-row file-read-line
    ; append the values to the global list
    set step_length_ls lput (item idx_step_col data) step_length_ls
    set turning_angles_ls lput (item idx_turning_col data) turning_angles_ls
  ]
end

to step 
  fd one-of step_length_ls
end 

to turn_turtle 
  ; not sure if fd is what you want here, but according to your pseudocode it is
  fd one-of turning_angles_ls 
end

to setup
  read_csv
  create-turtles 1
end

to go 
  ask turtles [step turn_turtle]
end

字符串

相关问题