0

I want to do a weighted random draw to get the attributes for my agent typologies in NetLogo as proposed here. Therefore, I want to use the rnd extension. I also want to use data stored in a table for the probabilities. Below is the approach I have so far which is working.

However, I have to store the data from the table manually in a list for further use. My question is if it is necessary to do so or if there is a more efficient (and faster) way to do it as I have way more attributes and age groups.

extensions[csv rnd]

globals[prob-age]

turtles-own [ age ]

breed [ AT1 AT1s ]
breed [ AT2 AT2s ]


to setup
  clear-all
  file-close-all ; Close any files open from last run
  
  ; uncomment here and comment below to run the code without the data table
  ;let age_written["65-69" "70-74" "75+"]
  ;let prob1[0.08 0.87 0.05]
  ;let prob2[0.09 0.1 0.81]
  
  load-age ;load the csv file
  
  ;store attributes as lists
  let age_written (list item 1 (item 0 prob-age) item 2 (item 0 prob-age) item 3 (item 0 prob-age) )
  let prob1 (list item 1 (item 1 prob-age) item 2 (item 1 prob-age) item 3 (item 1 prob-age) )
  let prob2 (list item 1 (item 2 prob-age) item 2 (item 2 prob-age) item 3 (item 2 prob-age) )
  
  ;map lists
  let pairs_AT1 (map list age_written prob1)
  let pairs_AT2 (map list age_written prob2)
  
  ;create turtles (agent typologies)
  create-AT1 100 [ ;create first agent-typology with a weighted random draw
   set age first rnd:weighted-one-of-list pairs_AT1 [ [p] -> last p ]
  ]
  
  create-AT2 100 [
   set age first rnd:weighted-one-of-list pairs_AT2 [ [p] -> last p ]
  ]
  
  reset-ticks
end

; procedure to load a csv file
to load-age
  file-close-all ; close all open files
  if not file-exists? "Age_prob_1.csv" [
    user-message "No file 'Age_prob_1.csv' exists!"
    stop
  ]
  file-open "Age_prob_1.csv" ; open the file with the turtle data
  set prob-age csv:from-file "Age_prob_1.csv" ; load data into variable
  file-close ; make sure to close the file
end

The data is available here. But I also stored it as lists in the code.

Hannah H.
  • 266
  • 3
  • 14
  • do you only need to do assignments once (eg assigning an age during setup)? Or will there be frequent random draws (eg getting a mortality probability each tick)? If once, it's probably ok to use a csv import. If multiple times, then it would be much more efficient to have the information stored in a global variable (a list or table) so it can be accessed without opening/reading/closing a file. – JenB Aug 11 '20 at 08:10
  • Thanks for your answer. I have both: attributes that are loaded once (age, gender, etc) and attributes that are accessed in every time step (mortality rate). – Hannah H. Aug 11 '20 at 09:17

0 Answers0