I'm fairly new to NetLogo and its programming capabilities. I am trying to create a grouping mechanism for my code so that NLCD classifications can be mirrored in my NetLogo world.
For example, the NLCD veg-type 11 and 12 are for areas classified as water. Instead of calling the individual veg-types each time, I want to assign groups (e.g. "water-patches) to optimize my code. At the moment, my code is very tedious by having an if
statement correspond to each veg-type.
ask patches [
ifelse veg-type > 0
[
if veg-type = 41 ;;NLCD forest gridcodes
[set pcolor 55]
if veg-type = 42
[set pcolor 55]
if veg-type = 43
[set pcolor 55]
if veg-type = 52
[set pcolor 55]
if veg-type = 71 ;;NLCD grass gridcodes
[set pcolor 43]
if veg-type = 90
[set pcolor 43]
if veg-type = 95
[set pcolor 43]
if veg-type = 81 ;;NLCD agriculture gridcodes
[set pcolor orange]
if veg-type = 82
[set pcolor orange]
if veg-type = 21 ;;NLCD urban gridcodes
[set pcolor red]
if veg-type = 22
[set pcolor red]
if veg-type = 23
[set pcolor red]
if veg-type = 24
[set pcolor red]
if veg-type = 11 ;;NLCD water gridcodes
[set pcolor blue]
if veg-type = 12
[set pcolor blue]
if veg-type = 31 ;;NLCD barren gridcode
[set pcolor brown]
]
[set pcolor white]
]
How can I create groups for the different veg-types so that one FOREST group can correspond to patches with veg-type 41, 42, 43, 52? Do I need to create a multiple agentsets (e.g. forest-patches, water-patches, etc.) or should I table:put
the veg-type information into a table and use the table:group-items
command?
I appreciate any and all input!
You don't need to use tables for this and whether you want to create permanent agentsets or simply create as required depends on how often you would need to construct them. I would also recommend using an actual variable other than colour to define your groups. For example, later on you might want to have dirty water having a different colour visually but still have those patches be considered water in the procedures. And you don't want to have to rewrite stuff to unlink colour used for display from colour used as an indicator of patch type. So, add a variable (I am calling it NLCDtype) like this. You presumably already have this for veg-type, but you want to combine them into groups.
patches-own
[ NLCDtype
]
Then you can revise the code block you presented along these lines:
ask patches [
ifelse veg-type > 0
[ if member? veg-type [41 42 43 52] ;;NLCD forest gridcodes
[ set pcolor 55
set NLCDtype "forest"
]
if member? veg-type [71 90 95 ] ;;NLCD grass gridcodes
[ set pcolor 43
set NLCDtype "grass"
]
; ... other codes I didn't bother with
[ set pcolor white ]
]
Note that I am doing two things here. (1) Putting all the different values for the same type into a single line of code. I used the member?
form as it is short, but you could also have used or
to combine them (something like if veg-type=41 or veg-type=42 or veg-type=43 [ set...]
). (2) Assign a value for the variable NLCDtype.
You can then use the group variable to restrict the application of code, something like
ask patches with [NLCDtype = "grass"]
[ set pcolor green
]
The advantage of this approach is that the set of patches is created as it is used. For example, if you have land use changes in your model (so grass becomes agriculture), then this construction will always find the patches that are currently of the correct type.
However, if you don't expect the patches to change types, you can create some global variables and set permanent patch-sets. This will make your code faster because the patch-sets are created only once. To do this:
globals
[ grass-patches
forest-patches
; other names
]
ask patches
[ if veg-type > 0
; all the assigning stuff already discussed
]
; now they all have a group identifier, set the global variable patchsets
set grass-patches patches with [NLCDtype = "grass"]
set ...