I have a csv called "Males.csv" with three columns "turning_angles" "speed" "step_length"
I have created a very simple model of agents in a blank environment
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
I want to write procedures called step
and turn
where agents move and draw their parameters from the columns in the csv. Is it possible to convert a csv column to a list and then randomly select from the distribution of the data?
for example - as pseudo code.
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
There is the CSV-extension. You also find an example model in the models library (see CSV Example.
The extension reads your CSV file line by line, so you need to loop and extract the values of your columns one by one into a list. You would make your life a little easier if you save your distribution not in columns but rows, but of course it also works with columns. You can use one-of
to draw a random item of that list.
I attached a code example of how you could solve your problem below.
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