I have a problem with accelerating a code in NetLogo. I tried a few things and they didn't work. The problem is in the "setup-patches" procedure that refers to the creation of turtles (please, see the code).
I have a map (I use the GIS extension, but to be a replicable example I didn't use it here) with 7 classes (which in the code is called habitatcover). 6 habitatcover have vegetation and therefore is used by turtles (mobile agent) and 1 class has no vegetation and therefore is not used by turtles (habitatcover 7). Turtles can born in the 6 types of habitatcover that have vegetation and their combinations result in 63 items (please, see the variable in the code called ValidHabs). Also, I have 3 types of metabolism and 3 types of reproduction, which results in 9 combinations, that is, 9 types of turtles.
For each ValidHabs item (which has a total of 63 items) 50 individuals of each of the 9 types of turtles are born. Therefore, for item 0 of ValidHabs which is [1] 450 individuals will be born (= 9 * 50) it executes all the procedures and goes to the next item in the ValidHabs list until it goes through all the items in this list.
The problem is that as the world is 1000x1000 and 450 turtles are born per list item (which has 63 items in total), using GIS, as well, I have several maps to perform these procedures, in addition to having minimum 15 repetitions per map (using BehaviourSpace) takes a lot of time do execute one map (almost 5 days per map). I've already tried to make modifications to speed it up and haven't had much success. I know the problem is in the "setup-patches" procedure, but I'm not quite sure how to solve it. Can anybody help me?
Thanks in advance!
The code below:
globals
[
RandomSeeds
HotPatchSet
ValidHabs
ValidHabsItem
CurrentHotPatchSet
]
turtles-own
[
turtle-profiles-habitat
energy-reproduction
turtle-metabolism
turtle-profiles-code
turtle-profiles-reproduction-code
turtle-profiles-metabolism-code
turtle-profiles-habitat-code
]
patches-own
[
habitatcover
]
to setup
clear-all
set RandomSeeds 1
random-seed RandomSeeds
resize-world 999 * 0 ( 999 * 1 ) ( 999 * -1 ) 999 * 0 ;; defines the edge size of the world and location of origin: corner top left. The world is 1000x1000
read-legal-habs ;; create list of valid habitats for each turtle profile
set ValidHabsItem 0
setup-layers
setup-patches
reset-ticks
end
to read-legal-habs ;; create list of valid habitats for each turtle profile
set ValidHabs [[1] [2] [3] [4] [5] [6] [1 2] [1 3] [1 4] [1 5] [1 6] [2 3] [2 4] [2 5] [2 6] [3 4] [3 5] [3 6] [4 5] [4 6] [5 6] [1 2 3] [1 2 4] [1 2 5] [1 2 6] [1 3 4] [1 3 5] [1 3 6] [1 4 5] [1 4 6] [1 5 6] [2 3 4] [2 3 5] [2 3 6] [2 4 5] [2 4 6] [2 5 6] [3 4 5] [3 4 6] [3 5 6] [4 5 6] [1 2 3 4] [1 2 3 5] [1 2 3 6] [1 2 4 5] [1 2 4 6] [1 2 5 6] [1 3 4 5] [1 3 4 6] [1 3 5 6] [1 4 5 6] [2 3 4 5] [2 3 4 6] [2 3 5 6] [2 4 5 6] [3 4 5 6] [1 2 3 4 5] [1 2 3 4 6] [1 2 3 5 6] [1 2 4 5 6] [1 3 4 5 6] [2 3 4 5 6] [1 2 3 4 5 6]]
end
to setup-layers ;; the original code I use GIS. And depending on how the class distributions (habitatcover) are on the map, it takes longer to create the turtles
let pcolors [ ]
set pcolors [ 25 65 23 53 105 10 2 ]
ask patches
[
set pcolor item ( random 7 ) pcolors
]
ask patches
[
if pcolor = 25 [ set habitatcover 1 ]
if pcolor = 65 [ set habitatcover 2 ]
if pcolor = 23 [ set habitatcover 3 ]
if pcolor = 53 [ set habitatcover 4 ]
if pcolor = 105 [ set habitatcover 5 ]
if pcolor = 10 [ set habitatcover 6 ]
if pcolor = 2 [ set habitatcover 7 ]
]
end
to setup-patches
let list1 ( list 2 4 8 ) ;; metabolism types
let list2 ( list 10 15 20 ) ;; reproduction types
let n0 count turtles
set HotPatchSet patches with [ ( habitatcover != 7 ) ] ;; referring to all 6 habitatcovers that have some type of vegetation removing habitatcover 7 that has no vegetation
set CurrentHotPatchSet HotPatchSet with [ habitatcover = one-of item ValidHabsItem ValidHabs ] ;; CurrentHotPatchSet will be the HotPatchSet with the current habitatcover ;; ValidHabsItem is the item that goes from 0 to 62 since there are 63 habitacover combinations (see the list ValidHabs has 63 items)
let s ( length list1 ) * ( length list2 ) * 50 ;; 50 individuals per profile = 3 * 3 * 50 = 450 turtles
while [ n0 < s ]
[
(
foreach list1
[
this-metabolism ->
foreach list2
[
this-reproduction ->
let c count CurrentHotPatchSet
if c = 0 [ stop ]
ask one-of CurrentHotPatchSet
[
sprout 1
[
set turtle-profiles-habitat item ValidHabsItem ValidHabs ;; I need the ValidHabsItem and turtle-profiles-habitat, because in the "go" procedure I'm calling each item (ValidHabsItem) from the ValidHabs list to execute all the procedures
set turtle-metabolism this-metabolism
set energy-reproduction this-reproduction
setup-turtles
]
set CurrentHotPatchSet CurrentHotPatchSet with [ not any? turtles in-radius 3 ] ;; each turtle must be a minimum distance of 3 patches and can't have more than one turtle in each patch
]
]
]
)
set n0 count turtles
]
end
to setup-turtles
set turtle-profiles-habitat-code reduce_list turtle-profiles-habitat
(
ifelse
turtle-metabolism = 2 [ set turtle-profiles-metabolism-code "_M1" ]
turtle-metabolism = 4 [ set turtle-profiles-metabolism-code "_M2" ]
turtle-metabolism = 6 [ set turtle-profiles-metabolism-code "_M3" ]
)
(
ifelse
energy-reproduction = 5 [ set turtle-profiles-reproduction-code "_R1" ]
energy-reproduction = 10 [ set turtle-profiles-reproduction-code "_R2" ]
energy-reproduction = 15 [ set turtle-profiles-reproduction-code "_R3" ]
)
set turtle-profiles-code ( word turtle-profiles-habitat-code turtle-profiles-reproduction-code turtle-profiles-metabolism-code )
end
to-report reduce_list [ a_list ]
report read-from-string word reduce word a_list ""
end