Trying to replace items in a nested list every tick
I am creating a model that simulates the spread of a virus around the world. I currently have 16 regions, each containing 28 variables affecting the spread of the virus. Due to the large number of variables / regions I know it is most efficient / better to use lists.
This is my current code for setting up regions:
Set Name 0
Set Living-Pop 1
Set Infected-Pop 2
Set Dead-Pop 3
Set Vaccinated-Pop 4
Set Count-Patches 5
Set Density 6
Set Infected-Density 7
Set Development 8
Set Healthcare 9
Set Research-Constant 10
Set Research-Rate 11
Set Water-Constant 12
Set Water-Rate 13
Set Airport-Security 14
Set Border-Security 15
Set Vaccine-Constant 16
Set Vaccine-Rate 17
Set Region-Number 18
Set Airports? 19
Set Hospitals? 20
Set ER? 21
Set Research? 22
Set Water? 23
Set Infected-water? 24
Set Infected? 25
Set Vaccine? 26
Set Borders? 27
set region-data [
[ "Hinterlands" 36138177 0 0 0 2702 13374.60289 0 10 0.975 7.5E-11 0.027103633 0.5 5 0.25 0.03 50000000000 0.007227635 1 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "USA" 318433268 0 0 0 1272 250340.6195 0 10 1 7.5E-11 0.238824951 0.5 5 0.25 0.075 50000000000 0.063686654 2 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "Central America" 204157962 0 0 0 383 533049.5091 0 6 0.45 2.5E-11 0.030623694 0.6 3.6 0.4 0.01 50000000000 0.024498955 3 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "Andes" 97253901 0 0 0 558 174290.1452 0 5 0.45 2.5E-11 0.012156738 0.2 1 0.3 0.05 50000000000 0.00972539 4 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "Amazon" 186508253 0 0 0 1407 132557.3937 0 6 0.45 1.5E-11 0.016785743 0.35 2.1 0.33 0.03 50000000000 0.02238099 5 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "Patagonia" 94307819 0 0 0 481 196066.1518 0 5 0.65 1.5E-11 0.007073086 0.4 2 0.3 0.05 50000000000 0.009430782 6 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "Europe" 742538246 0 0 0 1175 631947.4434 0 10 0.85 2.5E-11 0.185634562 0.5 5 0.1 0.01 50000000000 0.148507649 7 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "Middle East" 209455634 0 0 0 1197 174983.8212 0 5 0.3 1.5E-11 0.015709173 0.1 0.5 0.5 0.5 50000000000 0.020945563 8 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "North Africa" 459321750 0 0 0 2661 172612.4577 0 4 0.2 2.5E-11 0.045932175 0.1 0.4 0.3 0.2 50000000000 0.03674574 9 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "South Africa" 695349014 0 0 0 1535 452996.1003 0 4 0.1 2.5E-11 0.069534901 0.2 0.8 0.3 0.03 50000000000 0.055627921 10 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "Russia" 143538971 0 0 0 4071 35258.89732 0 8 0.5 7.5E-11 0.086123383 0.5 4 0.25 0.03 50000000000 0.022966235 11 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "East Asia" 1510450616 0 0 0 1602 942853.0687 0 7 0.35 6.5E-11 0.68725503 0.7 4.9 0.25 0.075 50000000000 0.211463086 12 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "South Asia" 1674152344 0 0 0 1346 1243798.175 0 5 0.25 3.5E-11 0.29297666 0.8 4 0.3 0.01 50000000000 0.167415234 13 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "Austalia" 23132574 0 0 0 1156 20010.87716 0 9 0.85 5E-11 0.010409658 0.3 2.7 0.25 0 50000000000 0.004163863 14 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "Oceania" 43984482 0 0 0 121 363508.1157 0 2 0.3 5E-12 0.000439845 0.01 0.02 0.25 0 50000000000 0.001759379 15 TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE]
[ "Ocean" 0 0 0 0 56542 0 0 0 0 0 0 0 0 0 0 0 0 16 FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE]
In the install routine, I want to write a routine that will select 1 region from the region data list (using a selection) and replace Infected? with true and Infected-Pop with a random integer from 1 to 10. In my procedure, I then plan to write procedures that will set new values ββfor the infected-pop, live pops and dead pops of each tick, based on the percentage of infection, the infection rate tbd , health, development, total density, and infected density.
Before converting to storing data in lists from storing data in patches, I had this code:
to-infect
ask patches with [infected?][
if infected-pop < living-pop [
set infected-pop ((infected-pop + (((((infected-pop / living-pop) * (infection-rate / healthcare)) / development) * ((density - infected-density))) * 10))) ]
if infected-pop > living-pop [ set infected-pop living-pop ]
end
And you will need to find a way to rewrite this code to work with lists.
Can anyone help me write a customization routine to fulfill my purpose / help me change the transition routine for lists rather than patches?
source to share
There are probably smarter solutions, but you can do something like this:
to replace [region var-position new-value]
let i 0
foreach region-data
[
if item 0 ? = region
[
set region-data replace-item i region-data replace-item var-position ? new-value
]
set i (i + 1)
]
end
If the if condition checks if the first element of the current subscriptions is region-data
equal region
. The command inside an if-condition is a nested function replace-item
. Internal takes the current sublist region-data
and replaces the specified var-position
subscription with the specified one new-value
. External than replaces the old subscriptions in region-data
this modified list. Suppose you have a selection on your interface named "region-chooser" that you could call the replace function that does your customization like this:
replace region-chooser Infected? TRUE
replace region-chooser Infected-Pop random 10
You can also use this function for any region, for example:
replace "USA" Water-rate 50
source to share